Skip to content

Commit ee995eb

Browse files
authored
Ignore v-prefix when comparing k0s versions (#518)
* Ignore v-prefix when comparing k0s versions Signed-off-by: Kimmo Lehto <[email protected]> * Raise lint timeout Signed-off-by: Kimmo Lehto <[email protected]> * VersionEquals to error returning VersionMustEqual Signed-off-by: Kimmo Lehto <[email protected]> * Different way to define timeout? Signed-off-by: Kimmo Lehto <[email protected]> --------- Signed-off-by: Kimmo Lehto <[email protected]>
1 parent 4fd61f1 commit ee995eb

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ jobs:
3232
uses: golangci/[email protected]
3333
with:
3434
version: latest
35+
args: --timeout=30m
3536

3637
- name: Build
3738
run: make k0sctl

phase/download_k0s.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,30 @@ func (p *DownloadK0s) Title() string {
2323
// Prepare the phase
2424
func (p *DownloadK0s) Prepare(config *v1beta1.Cluster) error {
2525
p.Config = config
26+
2627
p.hosts = p.Config.Spec.Hosts.Filter(func(h *cluster.Host) bool {
27-
// Nothing to upload
28+
// Nothing to download
2829
if h.UploadBinary {
2930
return false
3031
}
3132

32-
// Nothing to upload
33+
// No need to download, host is going to be reset
3334
if h.Reset {
3435
return false
3536
}
3637

3738
// The version is already correct
38-
if h.Metadata.K0sBinaryVersion == p.Config.Spec.K0s.Version {
39+
err := p.Config.Spec.K0s.VersionMustEqual(h.Metadata.K0sBinaryVersion)
40+
if err == nil {
41+
log.Debugf("%s: k0s version on target host is already %s", h, h.Metadata.K0sBinaryVersion)
3942
return false
4043
}
4144

45+
log.Debugf("%s: %v", h, err)
46+
4247
return true
4348
})
49+
4450
return nil
4551
}
4652

phase/upload_binaries.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,19 @@ func (p *UploadBinaries) Prepare(config *v1beta1.Cluster) error {
2929
return false
3030
}
3131

32-
// Nothing to upload
32+
// No need to upload, host is going to be reset
3333
if h.Reset {
3434
return false
3535
}
3636

37-
// The version is already correct
38-
if h.Metadata.K0sBinaryVersion == p.Config.Spec.K0s.Version {
39-
return false
40-
}
41-
42-
if !h.FileChanged(h.UploadBinaryPath, h.Configurer.K0sBinaryPath()) {
43-
return false
37+
// The version on target is incorrect
38+
if err := p.Config.Spec.K0s.VersionMustEqual(h.Metadata.K0sBinaryVersion); err != nil {
39+
log.Debugf("%s: %v", h, err)
40+
return true
4441
}
4542

46-
return true
43+
// If the file has been changed compared to local, re-upload and replace
44+
return h.FileChanged(h.UploadBinaryPath, h.Configurer.K0sBinaryPath())
4745
})
4846
return nil
4947
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,34 @@ func (k K0s) GetClusterID(h *Host) (string, error) {
169169
return h.ExecOutput(h.Configurer.KubectlCmdf(h, "get --data-dir=%s -n kube-system namespace kube-system -o template={{.metadata.uid}}", h.DataDir), exec.Sudo(h))
170170
}
171171

172+
// VersionMustEqual returns an error if the k0s version in the struct does not match the given version or
173+
// if either of the version strings can't be parsed
174+
func (k K0s) VersionMustEqual(b string) error {
175+
if k.Version == "" {
176+
return fmt.Errorf("k0s version not set")
177+
}
178+
179+
if b == "" {
180+
return fmt.Errorf("empty k0s version given")
181+
}
182+
183+
aVer, err := version.NewVersion(k.Version)
184+
if err != nil {
185+
return fmt.Errorf("failed to parse k0s version: %w", err)
186+
}
187+
188+
bVer, err := version.NewVersion(b)
189+
if err != nil {
190+
return fmt.Errorf("failed to parse given k0s version: %w", err)
191+
}
192+
193+
if aVer.String() != bVer.String() {
194+
return fmt.Errorf("k0s version mismatch: expected %s, got %s", bVer, aVer)
195+
}
196+
197+
return nil
198+
}
199+
172200
// TokenID returns a token id from a token string that can be used to invalidate the token
173201
func TokenID(s string) (string, error) {
174202
b64 := make([]byte, base64.StdEncoding.DecodedLen(len(s)))

0 commit comments

Comments
 (0)