Skip to content

Commit 14e44b4

Browse files
authored
Add K0sBinaryVersion(...) as a Configurer method (#361)
Introduce K0sBinaryVersion and use it in places where the k0s version was retrieved manually. Also switch from string to *version.Version for versions, so that we cannot forget to add or strip the "v" prefix when processing version numbers. Signed-off-by: Tom Wieczorek <[email protected]>
1 parent 499bdea commit 14e44b4

File tree

6 files changed

+69
-27
lines changed

6 files changed

+69
-27
lines changed

configurer/linux.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/alessio/shellescape"
1111
"github.com/k0sproject/rig/exec"
1212
"github.com/k0sproject/rig/os"
13+
"github.com/k0sproject/version"
1314
)
1415

1516
// Static Constants Interface for overriding by distro-specific structs
@@ -61,6 +62,21 @@ func (l Linux) K0sBinaryPath() string {
6162
return "/usr/local/bin/k0s"
6263
}
6364

65+
func (l Linux) K0sBinaryVersion(h os.Host) (*version.Version, error) {
66+
k0sVersionCmd := l.K0sCmdf("version")
67+
output, err := h.ExecOutput(k0sVersionCmd, exec.Sudo(h))
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
version, err := version.NewVersion(output)
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
return version, nil
78+
}
79+
6480
// K0sConfigPath returns the location of k0s configuration file
6581
func (l Linux) K0sConfigPath() string {
6682
return "/etc/k0s/k0s.yaml"
@@ -87,14 +103,14 @@ func (l Linux) DownloadURL(h os.Host, url, destination string, opts ...exec.Opti
87103
}
88104

89105
// DownloadK0s performs k0s binary download from github on the host
90-
func (l Linux) DownloadK0s(h os.Host, version, arch string) error {
106+
func (l Linux) DownloadK0s(h os.Host, version *version.Version, arch string) error {
91107
tmp, err := l.TempFile(h)
92108
if err != nil {
93109
return err
94110
}
95111
defer func() { _ = h.Execf(`rm -f "%s"`, tmp) }()
96112

97-
url := fmt.Sprintf("https://github.com/k0sproject/k0s/releases/download/v%s/k0s-v%s-%s", version, version, arch)
113+
url := fmt.Sprintf("https://github.com/k0sproject/k0s/releases/download/%s/k0s-%s-%s", version, version, arch)
98114
if err := l.DownloadURL(h, url, tmp); err != nil {
99115
return err
100116
}

phase/download_k0s.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package phase
22

33
import (
44
"fmt"
5-
"strings"
65

76
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
87
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
9-
"github.com/k0sproject/rig/exec"
8+
"github.com/k0sproject/version"
109
log "github.com/sirupsen/logrus"
1110
)
1211

@@ -41,25 +40,28 @@ func (p *DownloadK0s) Run() error {
4140
}
4241

4342
func (p *DownloadK0s) downloadK0s(h *cluster.Host) error {
44-
target := p.Config.Spec.K0s.Version
45-
log.Infof("%s: downloading k0s %s", h, target)
46-
if err := h.Configurer.DownloadK0s(h, target, h.Metadata.Arch); err != nil {
43+
targetVersion, err := version.NewVersion(p.Config.Spec.K0s.Version)
44+
if err != nil {
45+
return err
46+
}
47+
48+
log.Infof("%s: downloading k0s %s", h, targetVersion)
49+
if err := h.Configurer.DownloadK0s(h, targetVersion, h.Metadata.Arch); err != nil {
4750
return err
4851
}
4952

50-
output, err := h.ExecOutput(h.Configurer.K0sCmdf("version"), exec.Sudo(h))
53+
downloadedVersion, err := h.Configurer.K0sBinaryVersion(h)
5154
if err != nil {
5255
if err := h.Configurer.DeleteFile(h, h.Configurer.K0sBinaryPath()); err != nil {
5356
log.Warnf("%s: failed to remove %s: %s", h, h.Configurer.K0sBinaryPath(), err.Error())
5457
}
55-
return fmt.Errorf("downloaded k0s binary is invalid: %s", err.Error())
58+
return fmt.Errorf("failed to get downloaded k0s binary version: %w", err)
5659
}
57-
output = strings.TrimPrefix(output, "v")
58-
if output != target {
59-
return fmt.Errorf("downloaded k0s binary version is %s not %s", output, target)
60+
if !targetVersion.Equal(downloadedVersion) {
61+
return fmt.Errorf("downloaded k0s binary version is %s not %s", downloadedVersion, targetVersion)
6062
}
6163

62-
h.Metadata.K0sBinaryVersion = target
64+
h.Metadata.K0sBinaryVersion = p.Config.Spec.K0s.Version
6365

6466
return nil
6567
}

phase/upgrade_controllers.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/Masterminds/semver"
88
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
99
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
10+
"github.com/k0sproject/version"
1011
log "github.com/sirupsen/logrus"
1112
)
1213

@@ -67,7 +68,11 @@ func (p *UpgradeControllers) Run() error {
6768
if err := h.WaitK0sServiceStopped(); err != nil {
6869
return err
6970
}
70-
if err := h.UpdateK0sBinary(p.Config.Spec.K0s.Version); err != nil {
71+
version, err := version.NewVersion(p.Config.Spec.K0s.Version)
72+
if err != nil {
73+
return err
74+
}
75+
if err := h.UpdateK0sBinary(version); err != nil {
7176
return err
7277
}
7378

phase/upgrade_workers.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/gammazero/workerpool"
88
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
99
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
10+
"github.com/k0sproject/version"
1011
log "github.com/sirupsen/logrus"
1112
)
1213

@@ -101,7 +102,11 @@ func (p *UpgradeWorkers) upgradeWorker(h *cluster.Host) error {
101102
if err := h.WaitK0sServiceStopped(); err != nil {
102103
return err
103104
}
104-
if err := h.UpdateK0sBinary(p.Config.Spec.K0s.Version); err != nil {
105+
version, err := version.NewVersion(p.Config.Spec.K0s.Version)
106+
if err != nil {
107+
return err
108+
}
109+
if err := h.UpdateK0sBinary(version); err != nil {
105110
return err
106111
}
107112

phase/upload_binaries.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
88
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
99
"github.com/k0sproject/rig/exec"
10+
"github.com/k0sproject/version"
1011
log "github.com/sirupsen/logrus"
1112
)
1213

@@ -63,7 +64,17 @@ func (p *UploadBinaries) uploadBinary(h *cluster.Host) error {
6364
return fmt.Errorf("failed to touch %s: %w", h.Configurer.K0sBinaryPath(), err)
6465
}
6566

66-
h.Metadata.K0sBinaryVersion = p.Config.Spec.K0s.Version
67+
uploadedVersion, err := h.Configurer.K0sBinaryVersion(h)
68+
if err != nil {
69+
return fmt.Errorf("failed to get uploaded k0s binary version: %w", err)
70+
}
71+
72+
h.Metadata.K0sBinaryVersion = uploadedVersion.String()
73+
log.Debugf("%s: has k0s binary version %s", h, h.Metadata.K0sBinaryVersion)
74+
75+
if version, err := version.NewVersion(p.Config.Spec.K0s.Version); err == nil && !version.Equal(uploadedVersion) {
76+
return fmt.Errorf("uploaded k0s binary version is %s not %s", uploadedVersion, version)
77+
}
6778

6879
return nil
6980
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/k0sproject/rig/exec"
1919
"github.com/k0sproject/rig/os"
2020
"github.com/k0sproject/rig/os/registry"
21+
"github.com/k0sproject/version"
2122
log "github.com/sirupsen/logrus"
2223
)
2324

@@ -83,6 +84,7 @@ type configurer interface {
8384
Arch(os.Host) (string, error)
8485
K0sCmdf(string, ...interface{}) string
8586
K0sBinaryPath() string
87+
K0sBinaryVersion(os.Host) (*version.Version, error)
8688
K0sConfigPath() string
8789
K0sJoinTokenPath() string
8890
WriteFile(os.Host, string, string, string) error
@@ -93,7 +95,7 @@ type configurer interface {
9395
ReadFile(os.Host, string) (string, error)
9496
FileExist(os.Host, string) bool
9597
Chmod(os.Host, string, string, ...exec.Option) error
96-
DownloadK0s(os.Host, string, string) error
98+
DownloadK0s(os.Host, *version.Version, string) error
9799
DownloadURL(os.Host, string, string, ...exec.Option) error
98100
InstallPackage(os.Host, ...string) error
99101
FileContains(os.Host, string, string) bool
@@ -296,7 +298,7 @@ func (h *Host) K0sServiceName() string {
296298
}
297299

298300
// UpdateK0sBinary updates the binary on the host either by downloading or uploading, based on the config
299-
func (h *Host) UpdateK0sBinary(version string) error {
301+
func (h *Host) UpdateK0sBinary(version *version.Version) error {
300302
if h.UploadBinaryPath != "" {
301303
if err := h.Upload(h.UploadBinaryPath, h.Configurer.K0sBinaryPath(), exec.Sudo(h)); err != nil {
302304
return err
@@ -308,17 +310,18 @@ func (h *Host) UpdateK0sBinary(version string) error {
308310
if err := h.Configurer.DownloadK0s(h, version, h.Metadata.Arch); err != nil {
309311
return err
310312
}
313+
}
311314

312-
output, err := h.ExecOutput(h.Configurer.K0sCmdf("version"), exec.Sudo(h))
313-
if err != nil {
314-
return fmt.Errorf("downloaded k0s binary is invalid: %s", err.Error())
315-
}
316-
output = strings.TrimPrefix(output, "v")
317-
if output != version {
318-
return fmt.Errorf("downloaded k0s binary version is %s not %s", output, version)
319-
}
315+
updatedVersion, err := h.Configurer.K0sBinaryVersion(h)
316+
if err != nil {
317+
return fmt.Errorf("failed to get updated k0s binary version: %w", err)
320318
}
321-
h.Metadata.K0sBinaryVersion = version
319+
if !version.Equal(updatedVersion) {
320+
return fmt.Errorf("updated k0s binary version is %s not %s", updatedVersion, version)
321+
}
322+
323+
h.Metadata.K0sBinaryVersion = version.String()
324+
322325
return nil
323326
}
324327

0 commit comments

Comments
 (0)