Skip to content

Commit

Permalink
Merge pull request #10760 from hakman/automated-cherry-pick-of-#10757…
Browse files Browse the repository at this point in the history
…-upstream-release-1.19

Automated cherry pick of #10757: nodeup file: Set owner & group when we write the file.
  • Loading branch information
k8s-ci-robot committed Feb 8, 2021
2 parents a51130e + 5a2d5c8 commit 850cc30
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions upup/pkg/fi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ go_library(
"executor.go",
"files.go",
"files_owner.go",
"files_owner_windows.go",
"has_address.go",
"http.go",
"lifecycle.go",
Expand Down
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
24 changes: 24 additions & 0 deletions upup/pkg/fi/files_owner_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// +build windows

/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fi

// EnsureFileOwner dummy version for Windows
func EnsureFileOwner(destPath string, owner string, groupName string) (bool, error) {
return false, nil
}
2 changes: 1 addition & 1 deletion upup/pkg/fi/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestWriteFile(t *testing.T) {
},
}
for _, test := range tests {
err := WriteFile(test.path, NewBytesResource(test.data), test.fileMode, test.dirMode)
err := WriteFile(test.path, NewBytesResource(test.data), test.fileMode, test.dirMode, "", "")
if err != nil {
t.Errorf("Error writing file {%s}, error: {%v}", test.path, err)
continue
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 850cc30

Please sign in to comment.