Skip to content

Commit 409601a

Browse files
committed
Clean..
Signed-off-by: Kimmo Lehto <[email protected]>
1 parent 0f569f3 commit 409601a

File tree

3 files changed

+62
-94
lines changed

3 files changed

+62
-94
lines changed

configurer/linux.go

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
"al.essio.dev/pkg/shellescape"
1313
"github.com/k0sproject/rig/exec"
1414
"github.com/k0sproject/rig/os"
15-
"github.com/k0sproject/version"
1615
)
1716

1817
// Linux is a base module for various linux OS support packages
1918
type Linux struct {
20-
paths map[string]string
21-
pathMu sync.Mutex
19+
paths map[string]string
20+
pathMu sync.RWMutex
21+
pathOnce sync.Once
2222
}
2323

2424
// OSKind returns the identifier for Linux hosts
@@ -35,59 +35,48 @@ func (l *Linux) OSKind() string {
3535
// path as a parameter.
3636

3737
func (l *Linux) initPaths() {
38-
if l.paths != nil {
39-
return
40-
}
41-
l.paths = map[string]string{
42-
"K0sBinaryPath": "/usr/local/bin/k0s",
43-
"K0sConfigPath": "/etc/k0s/k0s.yaml",
44-
"K0sJoinTokenPath": "/etc/k0s/k0stoken",
45-
"DataDirDefaultPath": "/var/lib/k0s",
46-
}
38+
l.pathOnce.Do(func() {
39+
l.paths = map[string]string{
40+
"K0sBinaryPath": "/usr/local/bin/k0s",
41+
"K0sConfigPath": "/etc/k0s/k0s.yaml",
42+
"K0sJoinTokenPath": "/etc/k0s/k0stoken",
43+
"DataDirDefaultPath": "/var/lib/k0s",
44+
}
45+
})
46+
}
47+
48+
func (l *Linux) path(key string) string {
49+
l.initPaths()
50+
l.pathMu.RLock()
51+
defer l.pathMu.RUnlock()
52+
return l.paths[key]
4753
}
4854

4955
// K0sBinaryPath returns the path to the k0s binary on the host
5056
func (l *Linux) K0sBinaryPath() string {
51-
l.pathMu.Lock()
52-
defer l.pathMu.Unlock()
53-
54-
l.initPaths()
55-
return l.paths["K0sBinaryPath"]
57+
return l.path("K0sBinaryPath")
5658
}
5759

5860
// K0sConfigPath returns the path to the k0s config file on the host
5961
func (l *Linux) K0sConfigPath() string {
60-
l.pathMu.Lock()
61-
defer l.pathMu.Unlock()
62-
63-
l.initPaths()
64-
return l.paths["K0sConfigPath"]
62+
return l.path("K0sConfigPath")
6563
}
6664

6765
// K0sJoinTokenPath returns the path to the k0s join token file on the host
6866
func (l *Linux) K0sJoinTokenPath() string {
69-
l.pathMu.Lock()
70-
defer l.pathMu.Unlock()
71-
72-
l.initPaths()
73-
return l.paths["K0sJoinTokenPath"]
67+
return l.path("K0sJoinTokenPath")
7468
}
7569

7670
// DataDirDefaultPath returns the path to the k0s data dir on the host
7771
func (l *Linux) DataDirDefaultPath() string {
78-
l.pathMu.Lock()
79-
defer l.pathMu.Unlock()
80-
81-
l.initPaths()
82-
return l.paths["DataDirDefaultPath"]
72+
return l.path("DataDirDefaultPath")
8373
}
8474

8575
// SetPath sets a path for a key
8676
func (l *Linux) SetPath(key, value string) {
77+
l.initPaths()
8778
l.pathMu.Lock()
8879
defer l.pathMu.Unlock()
89-
90-
l.initPaths()
9180
l.paths[key] = value
9281
}
9382

@@ -114,21 +103,6 @@ func (l *Linux) K0sCmdf(template string, args ...interface{}) string {
114103
return fmt.Sprintf("%s %s", l.K0sBinaryPath(), fmt.Sprintf(template, args...))
115104
}
116105

117-
func (l *Linux) K0sBinaryVersion(h os.Host) (*version.Version, error) {
118-
k0sVersionCmd := l.K0sCmdf("version")
119-
output, err := h.ExecOutput(k0sVersionCmd, exec.Sudo(h))
120-
if err != nil {
121-
return nil, err
122-
}
123-
124-
version, err := version.NewVersion(output)
125-
if err != nil {
126-
return nil, err
127-
}
128-
129-
return version, nil
130-
}
131-
132106
// K0sctlLockFilePath returns a path to a lock file
133107
func (l *Linux) K0sctlLockFilePath(h os.Host) string {
134108
if h.Exec("test -d /run/lock", exec.Sudo(h)) == nil {

configurer/windows.go

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import (
1212
"github.com/k0sproject/rig/exec"
1313
"github.com/k0sproject/rig/os"
1414
ps "github.com/k0sproject/rig/pkg/powershell"
15-
"github.com/k0sproject/version"
1615
)
1716

1817
// Windows provides helpers and defaults for Windows hosts
1918
type BaseWindows struct {
20-
paths map[string]string
21-
pathMu sync.Mutex
19+
paths map[string]string
20+
pathMu sync.RWMutex
21+
pathOnce sync.Once
2222
}
2323

2424
// OSKind returns the identifier for Windows hosts
@@ -27,54 +27,48 @@ func (w *BaseWindows) OSKind() string {
2727
}
2828

2929
func (w *BaseWindows) initPaths() {
30-
if w.paths != nil {
31-
return
32-
}
33-
w.paths = map[string]string{
34-
"K0sBinaryPath": `C:\\Program Files\\k0s\\k0s.exe`,
35-
"K0sConfigPath": `C:\\ProgramData\\k0s\\k0s.yaml`,
36-
"K0sJoinTokenPath": `C:\\ProgramData\\k0s\\k0stoken`,
37-
"DataDirDefaultPath": `C:\\ProgramData\\k0s`,
38-
}
30+
w.pathOnce.Do(func() {
31+
w.paths = map[string]string{
32+
"K0sBinaryPath": `C:\\Program Files\\k0s\\k0s.exe`,
33+
"K0sConfigPath": `C:\\ProgramData\\k0s\\k0s.yaml`,
34+
"K0sJoinTokenPath": `C:\\ProgramData\\k0s\\k0stoken`,
35+
"DataDirDefaultPath": `C:\\ProgramData\\k0s`,
36+
}
37+
})
38+
}
39+
40+
func (w *BaseWindows) path(key string) string {
41+
w.initPaths()
42+
w.pathMu.RLock()
43+
defer w.pathMu.RUnlock()
44+
return w.paths[key]
3945
}
4046

4147
// K0sBinaryPath returns the path to the k0s binary on the host
4248
func (w *BaseWindows) K0sBinaryPath() string {
43-
w.pathMu.Lock()
44-
defer w.pathMu.Unlock()
45-
w.initPaths()
46-
return w.paths["K0sBinaryPath"]
49+
return w.path("K0sBinaryPath")
4750
}
4851

4952
// K0sConfigPath returns the path to the k0s config file on the host
5053
func (w *BaseWindows) K0sConfigPath() string {
51-
w.pathMu.Lock()
52-
defer w.pathMu.Unlock()
53-
w.initPaths()
54-
return w.paths["K0sConfigPath"]
54+
return w.path("K0sConfigPath")
5555
}
5656

5757
// K0sJoinTokenPath returns the path to the k0s join token file on the host
5858
func (w *BaseWindows) K0sJoinTokenPath() string {
59-
w.pathMu.Lock()
60-
defer w.pathMu.Unlock()
61-
w.initPaths()
62-
return w.paths["K0sJoinTokenPath"]
59+
return w.path("K0sJoinTokenPath")
6360
}
6461

6562
// DataDirDefaultPath returns the path to the k0s data dir on the host
6663
func (w *BaseWindows) DataDirDefaultPath() string {
67-
w.pathMu.Lock()
68-
defer w.pathMu.Unlock()
69-
w.initPaths()
70-
return w.paths["DataDirDefaultPath"]
64+
return w.path("DataDirDefaultPath")
7165
}
7266

7367
// SetPath sets a path for a key
7468
func (w *BaseWindows) SetPath(key, value string) {
69+
w.initPaths()
7570
w.pathMu.Lock()
7671
defer w.pathMu.Unlock()
77-
w.initPaths()
7872
w.paths[key] = value
7973
}
8074

@@ -102,20 +96,6 @@ func (w *BaseWindows) K0sCmdf(template string, args ...interface{}) string {
10296
return fmt.Sprintf(`& %s %s`, ps.DoubleQuotePath(w.K0sBinaryPath()), fmt.Sprintf(template, args...))
10397
}
10498

105-
func (w *BaseWindows) K0sBinaryVersion(h os.Host) (*version.Version, error) {
106-
k0sVersionCmd := w.K0sCmdf("version")
107-
output, err := h.ExecOutput(k0sVersionCmd)
108-
if err != nil {
109-
return nil, err
110-
}
111-
112-
ver, err := version.NewVersion(strings.TrimSpace(output))
113-
if err != nil {
114-
return nil, err
115-
}
116-
return ver, nil
117-
}
118-
11999
// K0sctlLockFilePath returns a path to a lock file
120100
func (w *BaseWindows) K0sctlLockFilePath(h os.Host) string {
121101
// Use a system-wide temp location

pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ type configurer interface {
144144
Arch(os.Host) (string, error)
145145
K0sCmdf(string, ...interface{}) string
146146
K0sBinaryPath() string
147-
K0sBinaryVersion(os.Host) (*version.Version, error)
148147
K0sConfigPath() string
149148
DataDirDefaultPath() string
150149
K0sJoinTokenPath() string
@@ -490,6 +489,21 @@ func (h *Host) InstallK0sBinary(path string) error {
490489
return nil
491490
}
492491

492+
func (h *Host) K0sBinaryVersion() (*version.Version, error) {
493+
cmd := h.Configurer.K0sCmdf("version")
494+
output, err := h.ExecOutput(cmd, exec.Sudo(h))
495+
if err != nil {
496+
return nil, err
497+
}
498+
499+
ver, err := version.NewVersion(strings.TrimSpace(output))
500+
if err != nil {
501+
return nil, err
502+
}
503+
504+
return ver, nil
505+
}
506+
493507
func (h *Host) setFileMode(path string, mode fs.FileMode) error {
494508
perm := fmt.Sprintf("%04o", uint32(mode)&0o7777)
495509
return h.Configurer.Chmod(h, path, perm, exec.Sudo(h))
@@ -501,7 +515,7 @@ func (h *Host) UpdateK0sBinary(path string, version *version.Version) error {
501515
return fmt.Errorf("update k0s binary: %w", err)
502516
}
503517

504-
updatedVersion, err := h.Configurer.K0sBinaryVersion(h)
518+
updatedVersion, err := h.K0sBinaryVersion()
505519
if err != nil {
506520
return fmt.Errorf("failed to get updated k0s binary version: %w", err)
507521
}

0 commit comments

Comments
 (0)