-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace pid with flock for runtime config loading
Use lock file and flock(2) to ensure there is only a single instance of k0s running. This is more reliable than storing the pid in the runtime config. This solves false positives with k0s runtime config leftovers. Fixes: #5399 Signed-off-by: Natanael Copa <[email protected]>
- Loading branch information
Showing
6 changed files
with
161 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
Copyright 2025 k0s 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 flock | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
// Flock represents a file-based lock | ||
// The lock is tied to the lifetime of the associated process. | ||
type Flock struct { | ||
path string | ||
file *os.File | ||
} | ||
|
||
// New creates a new Flock instance. | ||
func New(path string) *Flock { | ||
return &Flock{ | ||
path: path, | ||
} | ||
} | ||
|
||
// Unlock releases the lock. | ||
func (f *Flock) Unlock() error { | ||
if err := f.file.Close(); err != nil { | ||
return err | ||
} | ||
|
||
f.file = nil | ||
return nil | ||
} | ||
|
||
// Close is an alias for Unlock, ensuring compatibility with the cleanup pattern in the provided code. | ||
func (f *Flock) Close() error { | ||
return f.Unlock() | ||
} | ||
|
||
// Path returns the file path associated with the lock. | ||
func (f *Flock) Path() string { | ||
return f.path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Copyright 2025 k0s 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 flock | ||
|
||
import ( | ||
"golang.org/x/sys/windows" | ||
"os" | ||
) | ||
|
||
// TryLock attempts to acquire the lock. Returns true if successful, false otherwise. | ||
func (f *Flock) TryLock() (bool, error) { | ||
file, err := os.OpenFile(f.path, os.O_CREATE|os.O_RDWR, 0600) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
handle := windows.Handle(file.Fd()) | ||
overlapped := new(windows.Overlapped) | ||
err = windows.LockFileEx( | ||
handle, | ||
windows.LOCKFILE_EXCLUSIVE_LOCK|windows.LOCKFILE_FAIL_IMMEDIATELY, | ||
0, | ||
1, | ||
0, | ||
overlapped, | ||
) | ||
if err != nil { | ||
file.Close() | ||
if err == windows.ERROR_LOCK_VIOLATION { | ||
return false, nil // Lock is already held by another process | ||
} | ||
return false, err | ||
} | ||
|
||
f.file = file | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.