Skip to content

Commit

Permalink
nodeup file: Set owner & group when we write the file.
Browse files Browse the repository at this point in the history
Otherwise we had an issue where the file existed with the correct
owner/group; when we rewrote it we set the owner/group to root/root;
but we then didn't set the owner/group if they were previously
correct.

Was visible in the flatcar test results.
  • Loading branch information
justinsb committed Feb 7, 2021
1 parent dbd1925 commit 6494073
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
8 changes: 7 additions & 1 deletion upup/pkg/fi/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import (
"k8s.io/kops/util/pkg/hashing"
)

func WriteFile(destPath string, contents Resource, fileMode os.FileMode, dirMode os.FileMode) error {
// WriteFile writes a file to the specified path, setting the mode, owner & group.
func WriteFile(destPath string, contents Resource, fileMode os.FileMode, dirMode os.FileMode, owner string, group string) error {
err := os.MkdirAll(path.Dir(destPath), dirMode)
if err != nil {
return fmt.Errorf("error creating directories for destination file %q: %v", destPath, err)
Expand All @@ -45,6 +46,11 @@ func WriteFile(destPath string, contents Resource, fileMode os.FileMode, dirMode
return err
}

_, err = EnsureFileOwner(destPath, owner, group)
if err != nil {
return err
}

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions upup/pkg/fi/files_owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"k8s.io/klog/v2"
)

// EnsureFileOwner will set the owner & group for a file.
// Empty values for owner/group will leave the owner/group unchanged.
func EnsureFileOwner(destPath string, owner string, groupName string) (bool, error) {
changed := false
stat, err := os.Lstat(destPath)
Expand Down
2 changes: 1 addition & 1 deletion upup/pkg/fi/nodeup/nodetasks/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func (_ *File) RenderLocal(t *local.LocalTarget, a, e, changes *File) error {
}
} else if e.Type == FileType_File {
if changes.Contents != nil {
err = fi.WriteFile(e.Path, e.Contents, fileMode, dirMode)
err = fi.WriteFile(e.Path, e.Contents, fileMode, dirMode, fi.StringValue(e.Owner), fi.StringValue(e.Group))
if err != nil {
return fmt.Errorf("error copying file %q: %v", e.Path, err)
}
Expand Down
2 changes: 1 addition & 1 deletion upup/pkg/fi/nodeup/nodetasks/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (_ *Service) RenderLocal(t *local.LocalTarget, a, e, changes *Service) erro

if changes.Definition != nil {
servicePath := path.Join(systemdSystemPath, serviceName)
err := fi.WriteFile(servicePath, fi.NewStringResource(*e.Definition), 0644, 0755)
err := fi.WriteFile(servicePath, fi.NewStringResource(*e.Definition), 0644, 0755, "", "")
if err != nil {
return fmt.Errorf("error writing systemd service file: %v", err)
}
Expand Down

0 comments on commit 6494073

Please sign in to comment.