Skip to content

Commit

Permalink
Ignore v-prefix when comparing k0s versions (#518)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
kke authored Aug 1, 2023
1 parent 4fd61f1 commit ee995eb
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
uses: golangci/[email protected]
with:
version: latest
args: --timeout=30m

- name: Build
run: make k0sctl
Expand Down
12 changes: 9 additions & 3 deletions phase/download_k0s.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,30 @@ func (p *DownloadK0s) Title() string {
// Prepare the phase
func (p *DownloadK0s) Prepare(config *v1beta1.Cluster) error {
p.Config = config

p.hosts = p.Config.Spec.Hosts.Filter(func(h *cluster.Host) bool {
// Nothing to upload
// Nothing to download
if h.UploadBinary {
return false
}

// Nothing to upload
// No need to download, host is going to be reset
if h.Reset {
return false
}

// The version is already correct
if h.Metadata.K0sBinaryVersion == p.Config.Spec.K0s.Version {
err := p.Config.Spec.K0s.VersionMustEqual(h.Metadata.K0sBinaryVersion)
if err == nil {
log.Debugf("%s: k0s version on target host is already %s", h, h.Metadata.K0sBinaryVersion)
return false
}

log.Debugf("%s: %v", h, err)

return true
})

return nil
}

Expand Down
16 changes: 7 additions & 9 deletions phase/upload_binaries.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,19 @@ func (p *UploadBinaries) Prepare(config *v1beta1.Cluster) error {
return false
}

// Nothing to upload
// No need to upload, host is going to be reset
if h.Reset {
return false
}

// The version is already correct
if h.Metadata.K0sBinaryVersion == p.Config.Spec.K0s.Version {
return false
}

if !h.FileChanged(h.UploadBinaryPath, h.Configurer.K0sBinaryPath()) {
return false
// The version on target is incorrect
if err := p.Config.Spec.K0s.VersionMustEqual(h.Metadata.K0sBinaryVersion); err != nil {
log.Debugf("%s: %v", h, err)
return true
}

return true
// If the file has been changed compared to local, re-upload and replace
return h.FileChanged(h.UploadBinaryPath, h.Configurer.K0sBinaryPath())
})
return nil
}
Expand Down
28 changes: 28 additions & 0 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/k0s.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,34 @@ func (k K0s) GetClusterID(h *Host) (string, error) {
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))
}

// VersionMustEqual returns an error if the k0s version in the struct does not match the given version or
// if either of the version strings can't be parsed
func (k K0s) VersionMustEqual(b string) error {
if k.Version == "" {
return fmt.Errorf("k0s version not set")
}

if b == "" {
return fmt.Errorf("empty k0s version given")
}

aVer, err := version.NewVersion(k.Version)
if err != nil {
return fmt.Errorf("failed to parse k0s version: %w", err)
}

bVer, err := version.NewVersion(b)
if err != nil {
return fmt.Errorf("failed to parse given k0s version: %w", err)
}

if aVer.String() != bVer.String() {
return fmt.Errorf("k0s version mismatch: expected %s, got %s", bVer, aVer)
}

return nil
}

// TokenID returns a token id from a token string that can be used to invalidate the token
func TokenID(s string) (string, error) {
b64 := make([]byte, base64.StdEncoding.DecodedLen(len(s)))
Expand Down

0 comments on commit ee995eb

Please sign in to comment.