Skip to content

Commit b471968

Browse files
authored
Fix k0s version sorting (#317)
Signed-off-by: Kimmo Lehto <[email protected]>
1 parent 22c694a commit b471968

File tree

7 files changed

+41
-88
lines changed

7 files changed

+41
-88
lines changed

cmd/flags.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"github.com/k0sproject/k0sctl/integration/segment"
1919
"github.com/k0sproject/k0sctl/phase"
2020
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
21-
"github.com/k0sproject/k0sctl/version"
21+
k0sctl "github.com/k0sproject/k0sctl/version"
2222
"github.com/k0sproject/rig"
2323
"github.com/k0sproject/rig/exec"
2424
"github.com/logrusorgru/aurora"
@@ -131,7 +131,7 @@ func initConfig(ctx *cli.Context) error {
131131
}
132132

133133
func displayCopyright(ctx *cli.Context) error {
134-
fmt.Printf("k0sctl %s Copyright 2021, k0sctl authors.\n", version.Version)
134+
fmt.Printf("k0sctl %s Copyright 2021, k0sctl authors.\n", k0sctl.Version)
135135
if !ctx.Bool("disable-telemetry") {
136136
fmt.Println("Anonymized telemetry of usage will be sent to the authors.")
137137
}
@@ -361,7 +361,7 @@ func githubOrCachedRelease() (*github.Release, error) {
361361
}
362362
}
363363
log.Tracef("starting online k0sctl upgrade check")
364-
latest, err := github.LatestRelease("k0sproject/k0sctl", version.IsPre())
364+
latest, err := github.LatestRelease(k0sctl.IsPre())
365365
if err != nil {
366366
return nil, err
367367
}
@@ -379,7 +379,7 @@ func githubOrCachedRelease() (*github.Release, error) {
379379
}
380380

381381
func startCheckUpgrade(ctx *cli.Context) error {
382-
if ctx.Bool("disable-upgrade-check") || version.Environment == "development" {
382+
if ctx.Bool("disable-upgrade-check") || k0sctl.Environment == "development" {
383383
return nil
384384
}
385385

@@ -392,7 +392,7 @@ func startCheckUpgrade(ctx *cli.Context) error {
392392
upgradeChan <- nil
393393
return
394394
}
395-
if latest.IsNewer(version.Version) {
395+
if latest.IsNewer(k0sctl.Version) {
396396
upgradeChan <- latest
397397
} else {
398398
upgradeChan <- nil
@@ -403,7 +403,7 @@ func startCheckUpgrade(ctx *cli.Context) error {
403403
}
404404

405405
func reportCheckUpgrade(ctx *cli.Context) error {
406-
if ctx.Bool("disable-upgrade-check") || version.Environment == "development" {
406+
if ctx.Bool("disable-upgrade-check") || k0sctl.Environment == "development" {
407407
return nil
408408
}
409409

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/denisbrodbeck/machineid v1.0.1
1616
github.com/gammazero/workerpool v1.1.2
1717
github.com/gofrs/uuid v4.0.0+incompatible // indirect
18-
github.com/hashicorp/go-version v1.3.0
18+
github.com/hashicorp/go-version v1.4.0 // indirect
1919
github.com/k0sproject/dig v0.2.0
2020
github.com/k0sproject/rig v0.4.7
2121
github.com/logrusorgru/aurora v2.0.3+incompatible
@@ -42,6 +42,7 @@ require (
4242
github.com/alessio/shellescape v1.4.1
4343
github.com/go-ozzo/ozzo-validation/v4 v4.3.0
4444
github.com/go-playground/validator/v10 v10.9.0
45+
github.com/k0sproject/version v0.1.3
4546
)
4647

4748
require (

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ github.com/googleapis/gnostic v0.5.1/go.mod h1:6U4PtQXGIEt/Z3h5MAT7FNofLnw9vXk2c
193193
github.com/googleapis/gnostic v0.5.5/go.mod h1:7+EbHbldMins07ALC74bsA81Ovc97DwqyJO1AENw9kA=
194194
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
195195
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
196-
github.com/hashicorp/go-version v1.3.0 h1:McDWVJIU/y+u1BRV06dPaLfLCaT7fUTJLp5r04x7iNw=
197-
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
196+
github.com/hashicorp/go-version v1.4.0 h1:aAQzgqIrRKRa7w75CKpbBxYsmUoPjzVm1W59ca1L0J4=
197+
github.com/hashicorp/go-version v1.4.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
198198
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
199199
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
200200
github.com/hinshun/vt10x v0.0.0-20180616224451-1954e6464174 h1:WlZsjVhE8Af9IcZDGgJGQpNflI3+MJSBhsgT5PCtzBQ=
@@ -214,6 +214,8 @@ github.com/k0sproject/dig v0.2.0 h1:cNxEIl96g9kqSMfPSZLhpnZ0P8bWXKv08nxvsMHop5w=
214214
github.com/k0sproject/dig v0.2.0/go.mod h1:rBcqaQlJpcKdt2x/OE/lPvhGU50u/e95CSm5g/r4s78=
215215
github.com/k0sproject/rig v0.4.7 h1:ijTypz6X8HwZIpV2csrLTYhtZvpOrYpP3evv08jVwMg=
216216
github.com/k0sproject/rig v0.4.7/go.mod h1:4FKfoqz5HOE8v8pJ0K8BCvWw3ypB2vJINkAY4P+yTpo=
217+
github.com/k0sproject/version v0.1.3 h1:md6jfYT28VtUGe7qE8Fd/sxvtaCnlnLR3t0mnRneyIk=
218+
github.com/k0sproject/version v0.1.3/go.mod h1:oEjuz2ItQQtAnGyRgwEV9m5R6/9rjoFC6EiEEzbkFdI=
217219
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
218220
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
219221
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=

integration/github/github.go

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"strings"
1010
"time"
1111

12-
"github.com/hashicorp/go-version"
12+
k0sversion "github.com/k0sproject/version"
1313
)
1414

1515
const timeOut = time.Second * 10
@@ -29,11 +29,11 @@ type Release struct {
2929
}
3030

3131
func (r *Release) IsNewer(b string) bool {
32-
this, err := version.NewVersion(r.TagName)
32+
this, err := k0sversion.NewVersion(r.TagName)
3333
if err != nil {
3434
return false
3535
}
36-
other, err := version.NewVersion(b)
36+
other, err := k0sversion.NewVersion(b)
3737
if err != nil {
3838
return false
3939
}
@@ -42,7 +42,7 @@ func (r *Release) IsNewer(b string) bool {
4242

4343
// LatestK0sBinaryURL returns the url for the latest k0s release by arch and os
4444
func LatestK0sBinaryURL(arch, osKind string, preok bool) (string, error) {
45-
r, err := LatestRelease("k0sproject/k0s", preok)
45+
r, err := k0sversion.LatestReleaseByPrerelease(preok)
4646
if err != nil {
4747
return "", err
4848
}
@@ -66,82 +66,35 @@ func LatestK0sBinaryURL(arch, osKind string, preok bool) (string, error) {
6666

6767
// LatestK0sVersion returns the latest k0s version number (without v prefix)
6868
func LatestK0sVersion(preok bool) (string, error) {
69-
r, err := LatestRelease("k0sproject/k0s", preok)
69+
r, err := k0sversion.LatestReleaseByPrerelease(preok)
7070
if err != nil {
7171
return "", err
7272
}
7373
return strings.TrimPrefix(r.TagName, "v"), nil
7474
}
7575

76-
// versionCollection is a type that implements the sort.Interface interface
77-
// so that versions can be sorted.
78-
type versionCollection []*version.Version
79-
80-
func (v versionCollection) Len() int {
81-
return len(v)
82-
}
83-
84-
func (v versionCollection) Less(i, j int) bool {
85-
return vCompare(v[i], v[j]) < 0
86-
}
87-
88-
func (v versionCollection) Swap(i, j int) {
89-
v[i], v[j] = v[j], v[i]
90-
}
91-
92-
// vCompare compares this version to another version. This
93-
// returns -1, 0, or 1 if this version is smaller, equal,
94-
// or larger than the other version, respectively.
95-
func vCompare(a, b *version.Version) int {
96-
c := a.Compare(b)
97-
if c != 0 {
98-
// versions already differ enough to use the version pkg result
99-
return c
100-
}
101-
102-
vA := a.String()
103-
104-
// go to plain string comparison
105-
s := []string{vA, b.String()}
106-
sort.Strings(s)
107-
108-
if vA == s[0] {
109-
return -1
110-
}
111-
112-
return 1
113-
}
114-
115-
// LatestRelease returns the semantically sorted latest version from github releases page for a repo.
116-
// Set preok true to allow returning pre-release versions. Assumes the repository has release tags with
117-
// semantic version numbers (optionally v-prefixed).
118-
func LatestRelease(repo string, preok bool) (Release, error) {
119-
var gotV bool
76+
// LatestRelease returns the semantically sorted latest k0sctl version from github
77+
func LatestRelease(preok bool) (Release, error) {
12078
var releases []Release
121-
if err := unmarshalURLBody(fmt.Sprintf("https://api.github.com/repos/%s/releases?per_page=20&page=1", repo), &releases); err != nil {
79+
if err := unmarshalURLBody("https://api.github.com/repos/k0sproject/k0sctl/releases?per_page=20&page=1", &releases); err != nil {
12280
return Release{}, err
12381
}
12482

125-
var versions []*version.Version
83+
var versions k0sversion.Collection
12684
for _, v := range releases {
12785
if v.PreRelease && !preok {
12886
continue
12987
}
130-
if version, err := version.NewVersion(strings.TrimPrefix(v.TagName, "v")); err == nil {
131-
gotV = strings.HasPrefix(v.TagName, "v")
88+
if version, err := k0sversion.NewVersion(strings.TrimPrefix(v.TagName, "v")); err == nil {
13289
versions = append(versions, version)
13390
}
13491
}
135-
vc := versionCollection(versions)
136-
sort.Sort(vc)
92+
sort.Sort(versions)
13793

13894
latest := versions[len(versions)-1].String()
139-
if gotV {
140-
latest = "v" + latest
141-
}
14295

14396
for _, v := range releases {
144-
if v.TagName == latest {
97+
if strings.TrimPrefix(v.TagName, "v") == latest {
14598
return v, nil
14699
}
147100
}

phase/gather_k0s_facts.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"fmt"
66
"strings"
77

8-
"github.com/Masterminds/semver"
98
"github.com/k0sproject/dig"
109
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
1110
"github.com/k0sproject/rig/exec"
11+
"github.com/k0sproject/version"
1212
log "github.com/sirupsen/logrus"
1313
"gopkg.in/yaml.v2"
1414
)
@@ -157,12 +157,12 @@ func (p *GatherK0sFacts) needsUpgrade(h *cluster.Host) bool {
157157
}
158158

159159
log.Debugf("%s: checking if %s is an upgrade from %s", h, p.Config.Spec.K0s.Version, h.Metadata.K0sRunningVersion)
160-
target, err := semver.NewVersion(p.Config.Spec.K0s.Version)
160+
target, err := version.NewVersion(p.Config.Spec.K0s.Version)
161161
if err != nil {
162162
log.Warnf("%s: failed to parse target version: %s", h, err.Error())
163163
return false
164164
}
165-
current, err := semver.NewVersion(h.Metadata.K0sRunningVersion)
165+
current, err := version.NewVersion(h.Metadata.K0sRunningVersion)
166166
if err != nil {
167167
log.Warnf("%s: failed to parse running version: %s", h, err.Error())
168168
return false

phase/validate_facts.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package phase
33
import (
44
"fmt"
55

6-
"github.com/Masterminds/semver"
6+
"github.com/k0sproject/version"
77
log "github.com/sirupsen/logrus"
88
)
99

@@ -39,12 +39,12 @@ func (p *ValidateFacts) validateDowngrade() error {
3939
return nil
4040
}
4141

42-
cfgV, err := semver.NewVersion(p.Config.Spec.K0s.Version)
42+
cfgV, err := version.NewVersion(p.Config.Spec.K0s.Version)
4343
if err != nil {
4444
return err
4545
}
4646

47-
runV, err := semver.NewVersion(p.Config.Spec.K0sLeader().Metadata.K0sRunningVersion)
47+
runV, err := version.NewVersion(p.Config.Spec.K0sLeader().Metadata.K0sRunningVersion)
4848
if err != nil {
4949
return err
5050
}
@@ -67,12 +67,12 @@ func (p *ValidateFacts) validateDefaultVersion() error {
6767
return nil
6868
}
6969

70-
cfgV, err := semver.NewVersion(p.Config.Spec.K0s.Version)
70+
cfgV, err := version.NewVersion(p.Config.Spec.K0s.Version)
7171
if err != nil {
7272
return err
7373
}
7474

75-
runV, err := semver.NewVersion(p.Config.Spec.K0sLeader().Metadata.K0sRunningVersion)
75+
runV, err := version.NewVersion(p.Config.Spec.K0sLeader().Metadata.K0sRunningVersion)
7676
if err != nil {
7777
return err
7878
}

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

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import (
88
"strings"
99
"time"
1010

11-
"github.com/Masterminds/semver"
1211
"github.com/alessio/shellescape"
1312
"github.com/avast/retry-go"
1413
"github.com/creasty/defaults"
1514
validation "github.com/go-ozzo/ozzo-validation/v4"
1615
"github.com/k0sproject/dig"
17-
"github.com/k0sproject/k0sctl/integration/github"
18-
"github.com/k0sproject/k0sctl/version"
16+
k0sctl "github.com/k0sproject/k0sctl/version"
1917
"github.com/k0sproject/rig/exec"
18+
"github.com/k0sproject/version"
2019
"gopkg.in/yaml.v2"
2120
)
2221

@@ -54,17 +53,17 @@ func validateVersion(value interface{}) error {
5453
return fmt.Errorf("not a string")
5554
}
5655

57-
v, err := semver.NewVersion(vs)
56+
v, err := version.NewVersion(vs)
5857
if err != nil {
5958
return err
6059
}
6160

62-
min, err := semver.NewVersion(K0sMinVersion)
61+
min, err := version.NewVersion(K0sMinVersion)
6362
if err != nil {
6463
return fmt.Errorf("internal error: k0sminversion can't be parsed: %s", err)
6564
}
6665

67-
if min.GreaterThan(v) {
66+
if v.LessThan(min) {
6867
return fmt.Errorf("version: minimum supported k0s version is %s", K0sMinVersion)
6968
}
7069

@@ -80,12 +79,10 @@ func (k *K0s) Validate() error {
8079

8180
// SetDefaults (implements defaults Setter interface) defaults the version to latest k0s version
8281
func (k *K0s) SetDefaults() {
83-
if defaults.CanUpdate(k.Version) {
84-
preok := version.IsPre() || version.Version == "0.0.0"
85-
if latest, err := github.LatestK0sVersion(preok); err == nil {
86-
k.Version = latest
87-
k.Metadata.VersionDefaulted = true
88-
}
82+
latest, err := version.LatestReleaseByPrerelease(k0sctl.IsPre() || k0sctl.Version == "0.0.0")
83+
if err == nil {
84+
k.Version = latest.String()
85+
k.Metadata.VersionDefaulted = true
8986
}
9087

9188
k.Version = strings.TrimPrefix(k.Version, "v")

0 commit comments

Comments
 (0)