container/pod id file: truncate instead of throwing an error

Truncate the container and pod ID files instead of throwing an error.
The main motivation is to prevent redundant work when starting systemd
units.  Throwing an error when the file already exists is not preventing
races or file corruptions, so let's leave that to the user which in
almost all cases are generated (and tested) systemd units.

Signed-off-by: Valentin Rothberg <vrothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2022-10-24 13:57:37 +02:00
parent 99dfb70950
commit 221cfc6872
11 changed files with 20 additions and 99 deletions

View File

@ -682,18 +682,15 @@ func DefaultContainerConfig() *config.Config {
return containerConfig
}
func CreateCidFile(cidfile string, id string) error {
cidFile, err := OpenExclusiveFile(cidfile)
func CreateIDFile(path string, id string) error {
idFile, err := os.Create(path)
if err != nil {
if os.IsExist(err) {
return fmt.Errorf("container id file exists. Ensure another container is not using it or delete %s", cidfile)
}
return fmt.Errorf("opening cidfile %s", cidfile)
return fmt.Errorf("creating idfile: %w", err)
}
if _, err = cidFile.WriteString(id); err != nil {
logrus.Error(err)
defer idFile.Close()
if _, err = idFile.WriteString(id); err != nil {
return fmt.Errorf("writing idfile: %w", err)
}
cidFile.Close()
return nil
}