Skip to content

Commit 0f569f3

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

File tree

11 files changed

+137
-98
lines changed

11 files changed

+137
-98
lines changed

configurer/linux.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ type Linux struct {
2121
pathMu sync.Mutex
2222
}
2323

24+
// OSKind returns the identifier for Linux hosts
25+
func (l *Linux) OSKind() string {
26+
return "linux"
27+
}
28+
2429
// NOTE The Linux struct does not embed rig/os.Linux because it will confuse
2530
// go as the distro-configurers' parents embed it too. This means you can't
2631
// add functions to base Linux package that call functions in the rig/os.Linux package,
@@ -168,12 +173,6 @@ func (l *Linux) DownloadURL(h os.Host, url, destination string, opts ...exec.Opt
168173
return nil
169174
}
170175

171-
// K0sDownloadURL returns the release URL for the requested k0s binary
172-
func (l *Linux) K0sDownloadURL(_ os.Host, version *version.Version, arch string) (string, error) {
173-
v := strings.ReplaceAll(strings.TrimPrefix(version.String(), "v"), "+", "%2B")
174-
return fmt.Sprintf("https://github.com/k0sproject/k0s/releases/download/v%[1]s/k0s-v%[1]s-%[2]s", v, arch), nil
175-
}
176-
177176
// ReplaceK0sTokenPath replaces the config path in the service stub
178177
func (l *Linux) ReplaceK0sTokenPath(h os.Host, spath string) error {
179178
return h.Exec(fmt.Sprintf("sed -i 's^REPLACEME^%s^g' %s", l.K0sJoinTokenPath(), spath))
@@ -198,17 +197,6 @@ func (l *Linux) Chown(h os.Host, path, owner string, opts ...exec.Option) error
198197
return h.Exec(cmd, opts...)
199198
}
200199

201-
func (l *Linux) Chmod(h os.Host, path, perm string, opts ...exec.Option) error {
202-
if _, err := strconv.ParseUint(perm, 8, 32); err != nil {
203-
return fmt.Errorf("invalid file mode %q: %w", perm, err)
204-
}
205-
if len(opts) == 0 {
206-
opts = []exec.Option{exec.Sudo(h)}
207-
}
208-
cmd := fmt.Sprintf(`chmod %s %s`, perm, shellescape.Quote(path))
209-
return h.Exec(cmd, opts...)
210-
}
211-
212200
// KubeconfigPath returns the path to a kubeconfig on the host
213201
func (l *Linux) KubeconfigPath(h os.Host, dataDir string) string {
214202
linux := &os.Linux{}

configurer/windows.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ type BaseWindows struct {
2121
pathMu sync.Mutex
2222
}
2323

24+
// OSKind returns the identifier for Windows hosts
25+
func (w *BaseWindows) OSKind() string {
26+
return "windows"
27+
}
28+
2429
func (w *BaseWindows) initPaths() {
2530
if w.paths != nil {
2631
return
@@ -139,12 +144,6 @@ func (w *BaseWindows) DownloadURL(h os.Host, url, destination string, opts ...ex
139144
return nil
140145
}
141146

142-
// K0sDownloadURL returns the release URL for the requested k0s binary
143-
func (w *BaseWindows) K0sDownloadURL(_ os.Host, versionV *version.Version, arch string) (string, error) {
144-
v := strings.ReplaceAll(strings.TrimPrefix(versionV.String(), "v"), "+", "%2B")
145-
return fmt.Sprintf("https://github.com/k0sproject/k0s/releases/download/v%[1]s/k0s-v%[1]s-%[2]s.exe", v, arch), nil
146-
}
147-
148147
// ReplaceK0sTokenPath replaces the config path in the service stub
149148
func (w *BaseWindows) ReplaceK0sTokenPath(h os.Host, spath string) error {
150149
// Replace literal REPLACEME with actual token path
@@ -168,11 +167,6 @@ func (w *BaseWindows) Chown(h os.Host, path, owner string, _ ...exec.Option) err
168167
return nil
169168
}
170169

171-
// Chmod is a no-op on Windows
172-
func (w *BaseWindows) Chmod(h os.Host, path, perm string, _ ...exec.Option) error {
173-
return nil
174-
}
175-
176170
// KubeconfigPath returns the path to a kubeconfig on the host
177171
func (w *BaseWindows) KubeconfigPath(h os.Host, dataDir string) string {
178172
win := &os.Windows{}

phase/arm_prepare.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ func (p *PrepareArm) Prepare(config *v1beta1.Cluster) error {
3838
return false
3939
}
4040

41-
arch := h.Metadata.Arch
41+
arch, err := h.Arch()
42+
if err != nil {
43+
log.Warnf("%s: failed to detect architecture: %v", h, err)
44+
return false
45+
}
4246

4347
if !strings.HasPrefix(arch, "arm") && !strings.HasPrefix(arch, "aarch") {
4448
return false
@@ -68,8 +72,12 @@ func (p *PrepareArm) Run(ctx context.Context) error {
6872
}
6973

7074
func (p *PrepareArm) etcdUnsupportedArch(_ context.Context, h *cluster.Host) error {
71-
log.Warnf("%s: enabling ETCD_UNSUPPORTED_ARCH=%s override - you may encounter problems with etcd", h, h.Metadata.Arch)
72-
h.Environment["ETCD_UNSUPPORTED_ARCH"] = h.Metadata.Arch
75+
arch, err := h.Arch()
76+
if err != nil {
77+
return err
78+
}
79+
log.Warnf("%s: enabling ETCD_UNSUPPORTED_ARCH=%s override - you may encounter problems with etcd", h, arch)
80+
h.Environment["ETCD_UNSUPPORTED_ARCH"] = arch
7381

7482
return nil
7583
}

phase/download_binaries.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,24 @@ func (p *DownloadBinaries) Run(_ context.Context) error {
4646
var bins binaries
4747

4848
for _, h := range p.hosts {
49-
if bin := bins.find(h.Configurer.Kind(), h.Metadata.Arch); bin != nil {
49+
arch, err := h.Arch()
50+
if err != nil {
51+
return err
52+
}
53+
osKind := h.Configurer.Kind()
54+
if bin := bins.find(osKind, arch); bin != nil {
5055
continue
5156
}
5257

53-
bin := &binary{arch: h.Metadata.Arch, os: h.Configurer.Kind(), version: p.Config.Spec.K0s.Version}
58+
bin := &binary{arch: arch, os: osKind, version: p.Config.Spec.K0s.Version}
5459

5560
// find configuration defined binpaths and use instead of downloading a new one
5661
for _, v := range p.hosts {
57-
if v.Metadata.Arch == bin.arch && v.Configurer.Kind() == bin.os && v.K0sBinaryPath != "" {
62+
vArch, err := v.Arch()
63+
if err != nil {
64+
return err
65+
}
66+
if vArch == bin.arch && v.Configurer.Kind() == bin.os && v.K0sBinaryPath != "" {
5867
bin.path = h.K0sBinaryPath
5968
}
6069
}
@@ -72,8 +81,12 @@ func (p *DownloadBinaries) Run(_ context.Context) error {
7281
}
7382

7483
for _, h := range p.hosts {
84+
arch, err := h.Arch()
85+
if err != nil {
86+
return err
87+
}
7588
if h.K0sBinaryPath == "" {
76-
if bin := bins.find(h.Configurer.Kind(), h.Metadata.Arch); bin != nil {
89+
if bin := bins.find(h.Configurer.Kind(), arch); bin != nil {
7790
h.UploadBinaryPath = bin.path
7891
}
7992
} else {

phase/download_k0s.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -73,29 +73,23 @@ func (p *DownloadK0s) downloadK0s(_ context.Context, h *cluster.Host) error {
7373
}
7474

7575
log.Infof("%s: downloading k0s %s", h, p.Config.Spec.K0s.Version)
76-
if h.K0sDownloadURL != "" {
77-
expandedURL := h.ExpandTokens(h.K0sDownloadURL, p.Config.Spec.K0s.Version)
78-
log.Infof("%s: downloading k0s binary from %s", h, expandedURL)
79-
if err := h.Configurer.DownloadURL(h, expandedURL, tmp, exec.Sudo(h)); err != nil {
80-
return fmt.Errorf("failed to download k0s binary: %w", err)
81-
}
82-
} else {
83-
url, err := h.Configurer.K0sDownloadURL(h, p.Config.Spec.K0s.Version, h.Metadata.Arch)
84-
if err != nil {
85-
return fmt.Errorf("failed to determine k0s download url: %w", err)
86-
}
87-
if err := h.Configurer.DownloadURL(h, url, tmp, exec.Sudo(h)); err != nil {
88-
return fmt.Errorf("failed to download k0s binary: %w", err)
89-
}
76+
url, err := h.K0sDownloadURL(p.Config.Spec.K0s.Version)
77+
if err != nil {
78+
return fmt.Errorf("failed to determine k0s download url: %w", err)
79+
}
80+
if err := h.Configurer.DownloadURL(h, url, tmp, exec.Sudo(h)); err != nil {
81+
return fmt.Errorf("failed to download k0s binary: %w", err)
82+
}
83+
h.Metadata.K0sBinaryTempFile = tmp
84+
85+
if h.IsWindows() {
86+
return nil
9087
}
9188

92-
// Make executable on POSIX; no-op on Windows
9389
if err := chmodWithMode(h, tmp, fs.FileMode(0o755)); err != nil {
9490
log.Debugf("%s: chmod %s failed: %v", h, tmp, err)
9591
}
9692

97-
h.Metadata.K0sBinaryTempFile = tmp
98-
9993
return nil
10094
}
10195

phase/gather_facts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ func (p *GatherFacts) Run(ctx context.Context) error {
3535
}
3636

3737
func (p *GatherFacts) investigateHost(_ context.Context, h *cluster.Host) error {
38-
output, err := h.Configurer.Arch(h)
38+
arch, err := h.Arch()
3939
if err != nil {
4040
return err
4141
}
42-
h.Metadata.Arch = output
42+
log.Infof("%s: detected %s architecture", h, arch)
4343

4444
if !p.SkipMachineIDs && p.Config.Spec.K0s.Version.LessThan(uniqueMachineIDSince) {
4545
id, err := h.Configurer.MachineID(h)

phase/lock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (p *Lock) startTicker(ctx context.Context, h *cluster.Host) error {
8181
for {
8282
select {
8383
case <-ticker.C:
84-
if err := touchWithTime(h, lfp, time.Now()); err != nil {
84+
if err := h.Configurer.Touch(h, lfp, time.Now()); err != nil {
8585
log.Debugf("%s: failed to touch lock file: %s", h, err)
8686
}
8787
case <-ctx.Done():

phase/upload_k0s.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (p *UploadK0s) uploadBinary(_ context.Context, h *cluster.Host) error {
8282
return fmt.Errorf("stat %s: %w", h.UploadBinaryPath, err)
8383
}
8484

85-
if err := touchWithTime(h, tmp, stat.ModTime()); err != nil {
85+
if err := h.Configurer.Touch(h, tmp, stat.ModTime()); err != nil {
8686
return fmt.Errorf("failed to touch %s: %w", tmp, err)
8787
}
8888
if err := chmodWithMode(h, tmp, fs.FileMode(0o755)); err != nil {

phase/uploadfiles.go

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func (p *UploadFiles) applyFileMetadata(h *cluster.Host, dest, owner, perm strin
256256
if timestamp != nil {
257257
err := p.Wet(h, fmt.Sprintf("set timestamp for %s to %s", dest, timestamp.String()), func() error {
258258
log.Debugf("%s: touching %s", h, dest)
259-
return touchWithTime(h, dest, *timestamp)
259+
return h.Configurer.Touch(h, dest, *timestamp, exec.Sudo(h))
260260
})
261261
if err != nil {
262262
return fmt.Errorf("failed to touch %s: %w", dest, err)
@@ -277,23 +277,3 @@ func chmodWithMode(h *cluster.Host, path string, mode fs.FileMode) error {
277277
perm := fmt.Sprintf("%04o", uint32(mode)&0o7777)
278278
return h.Configurer.Chmod(h, path, perm, exec.Sudo(h))
279279
}
280-
281-
func touchWithTime(h *cluster.Host, path string, ts time.Time) error {
282-
if fsys := h.SudoFsys(); fsys != nil {
283-
if t, ok := fsys.(interface{ TouchT(string, time.Time) error }); ok {
284-
return t.TouchT(path, ts)
285-
}
286-
if t, ok := fsys.(interface{ Touch(string) error }); ok {
287-
return t.Touch(path)
288-
}
289-
}
290-
if fsys := h.Fsys(); fsys != nil {
291-
if t, ok := fsys.(interface{ TouchT(string, time.Time) error }); ok {
292-
return t.TouchT(path, ts)
293-
}
294-
if t, ok := fsys.(interface{ Touch(string) error }); ok {
295-
return t.Touch(path)
296-
}
297-
}
298-
return nil
299-
}

phase/validate_hosts.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
13+
"github.com/k0sproject/version"
1314
log "github.com/sirupsen/logrus"
1415
)
1516

@@ -110,9 +111,19 @@ func (p *ValidateHosts) validateSudo(_ context.Context, h *cluster.Host) error {
110111
return nil
111112
}
112113

114+
var k0sWindowsWorkerSupportSince = version.MustConstraint(">= 1.34.0-0")
115+
113116
func (p *ValidateHosts) validateOS(_ context.Context, h *cluster.Host) error {
114-
if h.IsController() && h.IsWindows() {
115-
return fmt.Errorf("windows is not supported for controller nodes")
117+
if !h.IsWindows() || p.Config.Spec.K0s.Version == nil {
118+
return nil
119+
}
120+
121+
if h.IsController() {
122+
return fmt.Errorf("windows is not supported on k0s controller nodes")
123+
}
124+
125+
if !k0sWindowsWorkerSupportSince.Check(p.Config.Spec.K0s.Version) {
126+
return fmt.Errorf("windows workers require k0s version %s", k0sWindowsWorkerSupportSince)
116127
}
117128
return nil
118129
}

0 commit comments

Comments
 (0)