Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding lock files for kubeconfig updating #28034

Merged
merged 1 commit into from
Jun 24, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions pkg/client/unversioned/clientcmd/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,38 @@ func WriteToFile(config clientcmdapi.Config, filename string) error {
return err
}
}

err = lockFile(filename)
if err != nil {
return err
}
defer unlockFile(filename)

if err := ioutil.WriteFile(filename, content, 0600); err != nil {
return err
}
return nil
}

func lockFile(filename string) error {
// TODO: find a way to do this with actual file locks. Will
// probably need seperate solution for windows and linux.
f, err := os.OpenFile(lockName(filename), os.O_CREATE|os.O_EXCL, 0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thanks. Looks like other folks are using windows API directly - https://github.com/pinterest/knox/blob/master/client/flock_windows.go

@krousey Thanks for the explanation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dims I would definitely consider that for a later release, but I didn't want to try to rush something that hairy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@krousey yes of course.

if err != nil {
return err
}
f.Close()
return nil
}

func unlockFile(filename string) error {
return os.Remove(lockName(filename))
}

func lockName(filename string) string {
return filename + ".lock"
}

// Write serializes the config to yaml.
// Encapsulates serialization without assuming the destination is a file.
func Write(config clientcmdapi.Config) ([]byte, error) {
Expand Down
16 changes: 16 additions & 0 deletions pkg/client/unversioned/clientcmd/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,22 @@ func TestMigratingFileSourceMissingSkip(t *testing.T) {
}
}

func TestFileLocking(t *testing.T) {
f, _ := ioutil.TempFile("", "")
defer os.Remove(f.Name())

err := lockFile(f.Name())
if err != nil {
t.Errorf("unexpected error while locking file: %v", err)
}
defer unlockFile(f.Name())

err = lockFile(f.Name())
if err == nil {
t.Error("expected error while locking file.")
}
}

func Example_noMergingOnExplicitPaths() {
commandLineFile, _ := ioutil.TempFile("", "")
defer os.Remove(commandLineFile.Name())
Expand Down