Skip to content

Commit

Permalink
chore: unify semver under github.com/blang/semver/v4
Browse files Browse the repository at this point in the history
Currently, we use `github.com/coreos/go-semver/semver` and `github.com/hashicorp/go-version`
for version parsing. As we use `github.com/blang/semver/v4` in our other projects, and it
has more features, it makes sense to use it across the projects. It also doesn't allocate
like crazy in `KubernetesVersion.SupportedWith`.

Signed-off-by: Dmitriy Matrenichev <dmitry.matrenichev@siderolabs.com>
  • Loading branch information
DmitriyMV committed Aug 3, 2023
1 parent 0f1920b commit 80238a0
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 79 deletions.
6 changes: 3 additions & 3 deletions cmd/talosctl/pkg/talos/helpers/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"os"
"strings"

hashiversion "github.com/hashicorp/go-version"
"github.com/blang/semver/v4"
"google.golang.org/grpc/metadata"

"github.com/siderolabs/talos/pkg/machinery/api/common"
Expand Down Expand Up @@ -51,7 +51,7 @@ func ClientVersionCheck(ctx context.Context, c *client.Client) error {
// ignore the error, as we are only interested in the nodes which respond
serverVersions, _ := c.Version(ctx) //nolint:errcheck

clientVersion, err := hashiversion.NewVersion(version.NewVersion().Tag)
clientVersion, err := semver.ParseTolerant(version.NewVersion().Tag)
if err != nil {
return fmt.Errorf("error parsing client version: %w", err)
}
Expand All @@ -61,7 +61,7 @@ func ClientVersionCheck(ctx context.Context, c *client.Client) error {
for _, msg := range serverVersions.GetMessages() {
node := msg.GetMetadata().GetHostname()

serverVersion, err := hashiversion.NewVersion(msg.GetVersion().Tag)
serverVersion, err := semver.ParseTolerant(msg.GetVersion().Tag)
if err != nil {
return fmt.Errorf("%s: error parsing server version: %w", node, err)
}
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ require (
github.com/containernetworking/cni v1.1.2
github.com/containernetworking/plugins v1.3.0
github.com/coreos/go-iptables v0.7.0
github.com/coreos/go-semver v0.3.1
github.com/cosi-project/runtime v0.3.1
github.com/docker/distribution v2.8.2+incompatible
github.com/docker/docker v24.0.5+incompatible
Expand Down Expand Up @@ -66,7 +65,6 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/hashicorp/go-getter/v2 v2.2.1
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-version v1.6.0
github.com/hetznercloud/hcloud-go/v2 v2.0.0
github.com/insomniacslk/dhcp v0.0.0-20230731140434-0f9eb93a696c
github.com/jsimonetti/rtnetlink v1.3.4
Expand Down Expand Up @@ -169,7 +167,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/sts v1.21.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/blang/semver/v4 v4.0.0
github.com/briandowns/spinner v1.19.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
Expand All @@ -181,6 +179,7 @@ require (
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
github.com/containerd/ttrpc v1.1.2 // indirect
github.com/containerd/typeurl v1.0.2 // indirect
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand Down Expand Up @@ -217,6 +216,7 @@ require (
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-memdb v1.3.4 // indirect
github.com/hashicorp/go-safetemp v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
Expand Down
6 changes: 3 additions & 3 deletions hack/cloud-image-uploader/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/pageblob"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/hashicorp/go-version"
"github.com/blang/semver/v4"
"github.com/siderolabs/gen/channel"
"github.com/ulikunitz/xz"
"golang.org/x/sync/errgroup"
Expand All @@ -51,12 +51,12 @@ type AzureUploader struct {

// extractVersion extracts the version number in the format of int.int.int for Azure and assigns to the Options.AzureTag value.
func (azu *AzureUploader) setVersion() error {
v, err := version.NewVersion(azu.Options.AzureAbbrevTag)
v, err := semver.ParseTolerant(azu.Options.AzureAbbrevTag)
if err != nil {
return err
}

versionCore := v.Core().String()
versionCore := fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)

if fmt.Sprintf("v%s", versionCore) != azu.Options.AzureAbbrevTag {
azu.Options.AzureGalleryName = "SideroGalleryTest"
Expand Down
3 changes: 1 addition & 2 deletions hack/cloud-image-uploader/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.7.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.3.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.1.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.3.0
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.1.0
github.com/Azure/go-autorest/autorest v0.11.29
github.com/Azure/go-autorest/autorest/azure/auth v0.5.12
github.com/aws/aws-sdk-go v1.44.312
github.com/blang/semver/v4 v4.0.0
github.com/google/uuid v1.3.0
github.com/hashicorp/go-version v1.6.0
github.com/siderolabs/gen v0.4.5
github.com/siderolabs/go-retry v0.3.2
github.com/spf13/pflag v1.0.5
Expand Down
6 changes: 2 additions & 4 deletions hack/cloud-image-uploader/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0/go.mod h1:okt5dMMTOFjX/aov
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.1.0 h1:Sg/D8VuUQ+bw+FOYJF+xRKcwizCOP13HL0Se8pWNBzE=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5 v5.1.0/go.mod h1:Kyqzdqq0XDoCm+o9aZ25wZBmBUBzPBzPAj1R5rYsT6I=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal v1.1.2 h1:mLY+pNLjCUeKhgnAJWAKhEUQM+RJQo2H1fuGSw1Ky1E=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/managementgroups/armmanagementgroups v1.0.0 h1:pPvTJ1dY0sA35JOeFq6TsY2xj6Z85Yo23Pj4wCCvu4o=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1 h1:7CBQ+Ei8SP2c6ydQTGCCrS35bDxgTMfoP2miAwK++OU=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.1.1/go.mod h1:c/wcGeGx5FUPbM/JltUYHZcKmigwyVLJlDq+4HdtXaw=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.3.0 h1:LcJtQjCXJUm1s7JpUHZvu+bpgURhCatxVNbGADXniX0=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.3.0/go.mod h1:+OgGVo0Httq7N5oayfvaLQ/Jq+2gJdqfp++Hyyl7Tws=
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.1.0 h1:nVocQV40OQne5613EeLayJiRAJuKlBGy+m22qWG+WRg=
Expand Down Expand Up @@ -47,6 +45,8 @@ github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0 h1:OBhqkivkhkM
github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0/go.mod h1:kgDmCTgBzIEPFElEF+FK0SdjAor06dRq2Go927dnQ6o=
github.com/aws/aws-sdk-go v1.44.312 h1:llrElfzeqG/YOLFFKjg1xNpZCFJ2xraIi3PqSuP+95k=
github.com/aws/aws-sdk-go v1.44.312/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -59,8 +59,6 @@ github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOW
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
Expand Down
33 changes: 27 additions & 6 deletions internal/pkg/extensions/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"path/filepath"
"strings"

hashiversion "github.com/hashicorp/go-version"
"github.com/blang/semver/v4"

"github.com/siderolabs/talos/pkg/machinery/extensions"
"github.com/siderolabs/talos/pkg/version"
Expand All @@ -27,25 +27,46 @@ func (ext *Extension) Validate() error {
}

func (ext *Extension) validateConstraints() error {
if ext.Manifest.Metadata.Compatibility.Talos.Version != "" {
talosVersion, err := hashiversion.NewVersion(version.Tag)
constraint := ext.Manifest.Metadata.Compatibility.Talos.Version

if constraint != "" {
talosVersion, err := semver.ParseTolerant(version.Tag)
if err != nil {
return err
}

versionConstraint, err := hashiversion.NewConstraint(ext.Manifest.Metadata.Compatibility.Talos.Version)
versionConstraint, err := semver.ParseRange(trim(constraint))
if err != nil {
return fmt.Errorf("error parsing Talos version constraint: %w", err)
}

if !versionConstraint.Check(talosVersion.Core()) {
return fmt.Errorf("version constraint %s can't be satisfied with Talos version %s", versionConstraint, talosVersion)
if !versionConstraint(coreVersion(talosVersion)) {
return fmt.Errorf("version constraint %s can't be satisfied with Talos version %s", constraint, talosVersion)
}
}

return nil
}

// trim removes 'v' symbol anywhere in string if it's located before the number.
func trim(constraint string) string {
for i := 0; i < len(constraint); i++ {
if constraint[i] == 'v' && i+1 < len(constraint) && constraint[i+1] >= '0' && constraint[i+1] <= '9' {
constraint = constraint[:i] + constraint[i+1:]
}
}

return constraint
}

func coreVersion(talosVersion semver.Version) semver.Version {
return semver.Version{
Major: talosVersion.Major,
Minor: talosVersion.Minor,
Patch: talosVersion.Patch,
}
}

//nolint:gocyclo
func (ext *Extension) validateContents() error {
return filepath.WalkDir(ext.rootfsPath, func(path string, d fs.DirEntry, err error) error {
Expand Down
8 changes: 4 additions & 4 deletions pkg/cluster/kubernetes/detect.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"fmt"
"strings"

"github.com/coreos/go-semver/semver"
"github.com/blang/semver/v4"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -52,15 +52,15 @@ func DetectLowestVersion(ctx context.Context, cluster UpgradeProvider, options U
continue
}

v, err := semver.NewVersion(strings.TrimLeft(container.Image[idx+1:], "v"))
v, err := semver.ParseTolerant(strings.TrimLeft(container.Image[idx+1:], "v"))
if err != nil {
options.Log("failed to parse %s container version %s", app, err)

continue
}

if version == nil || v.LessThan(*version) {
version = v
if version == nil || v.LT(*version) {
version = &v
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cluster/sonobuoy/sonobuoy.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"strings"
"time"

"github.com/coreos/go-semver/semver"
"github.com/blang/semver/v4"
"github.com/vmware-tanzu/sonobuoy/cmd/sonobuoy/app"
"github.com/vmware-tanzu/sonobuoy/pkg/client"
"github.com/vmware-tanzu/sonobuoy/pkg/config"
Expand Down Expand Up @@ -106,7 +106,7 @@ func CertifiedConformance(ctx context.Context, cluster cluster.K8sProvider) erro
RetrieveResults: true,
}

k8sVersion, err := semver.NewVersion(options.KubernetesVersion)
k8sVersion, err := semver.ParseTolerant(options.KubernetesVersion)
if err != nil {
return err
}
Expand Down
26 changes: 16 additions & 10 deletions pkg/machinery/compatibility/kubernetes_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ package compatibility
import (
"fmt"

"github.com/hashicorp/go-version"
"github.com/blang/semver/v4"
"github.com/siderolabs/gen/pair/ordered"

"github.com/siderolabs/talos/pkg/machinery/compatibility/talos12"
"github.com/siderolabs/talos/pkg/machinery/compatibility/talos13"
Expand All @@ -17,28 +18,28 @@ import (

// KubernetesVersion embeds Kubernetes version.
type KubernetesVersion struct {
version version.Version
vers semver.Version
}

// ParseKubernetesVersion parses Kubernetes version.
func ParseKubernetesVersion(v string) (*KubernetesVersion, error) {
parsed, err := version.NewVersion(v)
parsed, err := semver.ParseTolerant(v)
if err != nil {
return nil, err
}

return &KubernetesVersion{
version: *parsed,
vers: parsed,
}, nil
}

func (v *KubernetesVersion) String() string {
return v.version.String()
return v.vers.String()
}

// SupportedWith checks if the Kubernetes version is supported with specified version of Talos.
func (v *KubernetesVersion) SupportedWith(target *TalosVersion) error {
var minK8sVersion, maxK8sVersion *version.Version
var minK8sVersion, maxK8sVersion semver.Version

switch target.majorMinor {
case talos12.MajorMinor: // upgrades to 1.2.x
Expand All @@ -53,12 +54,17 @@ func (v *KubernetesVersion) SupportedWith(target *TalosVersion) error {
return fmt.Errorf("compatibility with version %s is not supported", target.String())
}

if v.version.Core().LessThan(minK8sVersion) {
return fmt.Errorf("version of Kubernetes %s is too old to be used with Talos %s", v.version.String(), target.version.String())
core := ordered.MakeTriple(v.vers.Major, v.vers.Minor, v.vers.Patch)
minK8sVersionCore := ordered.MakeTriple(minK8sVersion.Major, minK8sVersion.Minor, minK8sVersion.Patch)

if core.LessThan(minK8sVersionCore) {
return fmt.Errorf("version of Kubernetes %s is too old to be used with Talos %s", v.vers.String(), target.version.String())
}

if v.version.Core().GreaterThanOrEqual(maxK8sVersion) {
return fmt.Errorf("version of Kubernetes %s is too new to be used with Talos %s", v.version.String(), target.version.String())
maxK8sVersionCore := ordered.MakeTriple(maxK8sVersion.Major, maxK8sVersion.Minor, maxK8sVersion.Patch)

if core.Compare(maxK8sVersionCore) >= 0 {
return fmt.Errorf("version of Kubernetes %s is too new to be used with Talos %s", v.vers.String(), target.version.String())
}

return nil
Expand Down
16 changes: 9 additions & 7 deletions pkg/machinery/compatibility/talos12/talos12.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
// Package talos12 provides compatibility constants for Talos 1.2.
package talos12

import "github.com/hashicorp/go-version"
import (
"github.com/blang/semver/v4"
)

// MajorMinor is the major.minor version of Talos 1.2.
var MajorMinor = [2]int{1, 2}
var MajorMinor = [2]uint64{1, 2}

// MinimumHostUpgradeVersion is the minimum version of Talos that can be upgraded to 1.2.
var MinimumHostUpgradeVersion = version.Must(version.NewVersion("1.0.0"))
var MinimumHostUpgradeVersion = semver.MustParse("1.0.0")

// MaximumHostDowngradeVersion is the maximum (not inclusive) version of Talos that can be downgraded to 1.3.
var MaximumHostDowngradeVersion = version.Must(version.NewVersion("1.3.0"))
var MaximumHostDowngradeVersion = semver.MustParse("1.3.0")

// DeniedHostUpgradeVersions are the versions of Talos that cannot be upgraded to 1.2.
var DeniedHostUpgradeVersions = []*version.Version{}
var DeniedHostUpgradeVersions = []semver.Version{}

// MinimumKubernetesVersion is the minimum version of Kubernetes is supported with 1.2.
var MinimumKubernetesVersion = version.Must(version.NewVersion("1.23.0"))
var MinimumKubernetesVersion = semver.MustParse("1.23.0")

// MaximumKubernetesVersion is the maximum version of Kubernetes is supported with 1.2.
var MaximumKubernetesVersion = version.Must(version.NewVersion("1.25.99"))
var MaximumKubernetesVersion = semver.MustParse("1.25.99")
16 changes: 9 additions & 7 deletions pkg/machinery/compatibility/talos13/talos13.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
// Package talos13 provides compatibility constants for Talos 1.3.
package talos13

import "github.com/hashicorp/go-version"
import (
"github.com/blang/semver/v4"
)

// MajorMinor is the major.minor version of Talos 1.3.
var MajorMinor = [2]int{1, 3}
var MajorMinor = [2]uint64{1, 3}

// MinimumHostUpgradeVersion is the minimum version of Talos that can be upgraded to 1.3.
var MinimumHostUpgradeVersion = version.Must(version.NewVersion("1.0.0"))
var MinimumHostUpgradeVersion = semver.MustParse("1.0.0")

// MaximumHostDowngradeVersion is the maximum (not inclusive) version of Talos that can be downgraded to 1.3.
var MaximumHostDowngradeVersion = version.Must(version.NewVersion("1.5.0"))
var MaximumHostDowngradeVersion = semver.MustParse("1.5.0")

// DeniedHostUpgradeVersions are the versions of Talos that cannot be upgraded to 1.3.
var DeniedHostUpgradeVersions = []*version.Version{}
var DeniedHostUpgradeVersions = []semver.Version{}

// MinimumKubernetesVersion is the minimum version of Kubernetes is supported with 1.3.
var MinimumKubernetesVersion = version.Must(version.NewVersion("1.24.0"))
var MinimumKubernetesVersion = semver.MustParse("1.24.0")

// MaximumKubernetesVersion is the maximum version of Kubernetes is supported with 1.3.
var MaximumKubernetesVersion = version.Must(version.NewVersion("1.26.99"))
var MaximumKubernetesVersion = semver.MustParse("1.26.99")
Loading

0 comments on commit 80238a0

Please sign in to comment.