Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: k0sproject/k0s
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9a20f31c80dec7fe4d164e9dc514fae10ddafded
Choose a base ref
..
head repository: k0sproject/k0s
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: d5e22545a0cc222b31067370c4510b5dc2650425
Choose a head ref
Showing with 23 additions and 37 deletions.
  1. +14 −25 pkg/config/flock_unix.go
  2. +3 −8 pkg/config/flock_windows.go
  3. +6 −4 pkg/config/runtime.go
39 changes: 14 additions & 25 deletions pkg/config/flock_unix.go
Original file line number Diff line number Diff line change
@@ -19,35 +19,18 @@ limitations under the License.
package config

import (
"errors"
"golang.org/x/sys/unix"
"os"
"path/filepath"
)

// tryLock attempts to acquire the lock. Returns *os.File if successful, nil otherwise.
func tryLock(path string, exclusive bool) (*os.File, error) {
fullPath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
mode := os.O_RDWR
if exclusive {
mode |= os.O_CREATE
}
file, err := os.OpenFile(fullPath, mode, 0600)
func tryLock(path string) (*os.File, error) {
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, err
}

flockMode := unix.LOCK_NB
if exclusive {
flockMode |= unix.LOCK_EX
} else {
flockMode |= unix.LOCK_SH
}

if err := unix.Flock(int(file.Fd()), flockMode); err != nil {
if err := unix.Flock(int(file.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil {
_ = file.Close()
if err == unix.EWOULDBLOCK {
return nil, ErrK0sAlreadyRunning // Lock is already held by another process
@@ -57,12 +40,18 @@ func tryLock(path string, exclusive bool) (*os.File, error) {
return file, nil
}

// locked checks if the lock is currently held by another process.
func locked(path string) bool {
f, err := tryLock(path, false)
// isLocked checks if the lock is currently held by another process.
func isLocked(path string) bool {
file, err := os.OpenFile(path, os.O_RDWR, 0600)
if err != nil {
return errors.Is(err, ErrK0sAlreadyRunning)
return false
}
defer file.Close()

// Attempt a non-blocking shared lock to test the lock state
if err := unix.Flock(int(file.Fd()), unix.LOCK_SH|unix.LOCK_NB); err != nil {
return err == unix.EWOULDBLOCK
}
f.Close()

return false
}
11 changes: 3 additions & 8 deletions pkg/config/flock_windows.go
Original file line number Diff line number Diff line change
@@ -21,16 +21,11 @@ package config
import (
"golang.org/x/sys/windows"
"os"
"path/filepath"
)

// tryLock attempts to acquire the lock. Returns true if successful, false otherwise.
func tryLock(path string) (*os.File, error) {
fullPath, err := filepath.Abs(path)
if err != nil {
return nil, err
}
file, err := os.OpenFile(fullPath, os.O_CREATE|os.O_RDWR, 0600)
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return nil, err
}
@@ -59,8 +54,8 @@ func tryLock(path string) (*os.File, error) {
return file, nil
}

// locked checks if the lock is currently held by another process.
func locked(path string) bool {
// isLocked checks if the lock is currently held by another process.
func isLocked(path string) bool {
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0600)
if err != nil {
return false
10 changes: 6 additions & 4 deletions pkg/config/runtime.go
Original file line number Diff line number Diff line change
@@ -56,12 +56,11 @@ type RuntimeConfig struct {
type RuntimeConfigSpec struct {
NodeConfig *v1beta1.ClusterConfig `json:"nodeConfig"`
K0sVars *CfgVars `json:"k0sVars"`
Pid int `json:"pid"` // unused. Delete
lockFile *os.File
}

func LoadRuntimeConfig(path string) (*RuntimeConfigSpec, error) {
if !locked(path + ".lock") {
if !isLocked(path + ".lock") {
return nil, ErrK0sNotRunning
}

@@ -116,7 +115,11 @@ func NewRuntimeConfig(k0sVars *CfgVars) (*RuntimeConfig, error) {
//
// It works similar on Windows, but with LockFileEx

lockFile, err := tryLock(k0sVars.RuntimeConfigPath+".lock", true)
path, err := filepath.Abs(k0sVars.RuntimeConfigPath + ".lock")
if err != nil {
return nil, err
}
lockFile, err := tryLock(path)
if err != nil {
return nil, err
}
@@ -137,7 +140,6 @@ func NewRuntimeConfig(k0sVars *CfgVars) (*RuntimeConfig, error) {
Spec: &RuntimeConfigSpec{
NodeConfig: nodeConfig,
K0sVars: k0sVars,
Pid: os.Getpid(),
lockFile: lockFile,
},
}