Skip to content

Commit

Permalink
feat(validate): another take on improving version validation
Browse files Browse the repository at this point in the history
Ref: #108
  • Loading branch information
budimanjojo committed Apr 17, 2023
1 parent 860eb6a commit dc7fc98
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<a href="#about-the-project">About The Project</a>
</li>
<li>
<a href="#supported-talos-versions">Supported Talos Versions</a>
<a href="#getting-started">Getting Started</a>
<ul>
<li><a href="#installation">Installation</a></li>
Expand Down Expand Up @@ -66,6 +67,13 @@ The generated files contain unencrypted secrets and you don't want people to get

<p align="right">(<a href="#top">back to top</a>)</p>

## Supported Talos Versions

Currently, the supported major and minor Talos versions are:
- `v1.2`
- `v1.3`
- `v1.4`

## Getting Started

Scenario 1 (You already have your talos config but not GitOps it yet):
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/siderolabs/talos/pkg/machinery v1.4.0-beta.1
github.com/spf13/cobra v1.7.0
go.mozilla.org/sops/v3 v3.7.3
golang.org/x/mod v0.8.0
gopkg.in/yaml.v3 v3.0.1
sigs.k8s.io/yaml v1.3.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,6 @@ github.com/siderolabs/go-pointer v1.0.0/go.mod h1:HTRFUNYa3R+k0FFKNv11zgkaCLzEkW
github.com/siderolabs/net v0.4.0 h1:1bOgVay/ijPkJz4qct98nHsiB/ysLQU0KLoBC4qLm7I=
github.com/siderolabs/net v0.4.0/go.mod h1:/ibG+Hm9HU27agp5r9Q3eZicEfjquzNzQNux5uEk0kM=
github.com/siderolabs/protoenc v0.2.0 h1:QFxWIAo//12+/bm27GNYoK/TpQGTYsRrrZCu9jSghvU=
github.com/siderolabs/talos/pkg/machinery v1.4.0-beta.0 h1:1ObtJd1BxnfU+U6jVPznwzGLs/KpDX0PoTTFWG/qkJM=
github.com/siderolabs/talos/pkg/machinery v1.4.0-beta.0/go.mod h1:6xBiNySzTjbqAnnlX35jyZ6OM6t/5aB9SPP7R+xRf5U=
github.com/siderolabs/talos/pkg/machinery v1.4.0-beta.1 h1:mSb0/mIAbKxOGkdfAo5kv5l3QYzeL1eaAlnDOXDzCUM=
github.com/siderolabs/talos/pkg/machinery v1.4.0-beta.1/go.mod h1:6xBiNySzTjbqAnnlX35jyZ6OM6t/5aB9SPP7R+xRf5U=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down Expand Up @@ -483,6 +481,8 @@ golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvx
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180530234432-1e491301e022/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/validate/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

type Config struct {
ClusterName string `validate:"required"`
TalosVersion string `validate:"isSemVer"`
TalosVersion string `validate:"isSupportedTalosVersion"`
KubernetesVersion string `validate:"isSupportedK8sVersion"`
Endpoint string `validate:"isTalosEndpoint"`
Domain string `validate:"isDomain"`
Expand Down Expand Up @@ -176,6 +176,7 @@ func (c Config) Messages() map[string]string {
"isUint": "{field} is not a valid unsigned integer",
"isDomainOrIP": "{field} is not a valid domain or IP address",
"isValidNetworkInterfaces": "{field} requires one of `interface` and `deviceSelector` to be set",
"isSupportedTalosVersion": "{field} is not a supported Talos version",
"isSupportedK8sVersion": "{field} is not supported by the specified Talos version",
}
}
20 changes: 20 additions & 0 deletions pkg/config/validate/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/siderolabs/net"
"github.com/siderolabs/talos/pkg/machinery/api/machine"
"github.com/siderolabs/talos/pkg/machinery/compatibility"
"golang.org/x/mod/semver"
)

// IsRFC6902List returns true if `input` is list of RFC6902 JSON patch.
Expand Down Expand Up @@ -45,6 +46,25 @@ func (c Config) IsSemVer(version string) bool {
return false
}

// IsSupportedTalosVersion returns true if `version` is supported Talos version.
func (c Config) IsSupportedTalosVersion(version string) bool {
if !strings.HasPrefix(version, "v") {
version = "v" + version
}
majorMinor := semver.MajorMinor(version)

switch majorMinor {
case "v1.2":
return true
case "v1.3":
return true
case "v1.4":
return true
default:
return false
}
}

// IsSupportedK8sVersion returns true if Kubernetes `version` is supported by
// `c.TalosVersion`.
func (c Config) IsSupportedK8sVersion(version string) bool {
Expand Down
20 changes: 20 additions & 0 deletions pkg/config/validate/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ func TestIsRFC6902List(t *testing.T) {
}
}

func TestIsSupportedTalosVersion(t *testing.T) {
c := &Config{}
data := map[string]bool{
"1.2.0": true,
"v1.2": true,
"v1.3.99": true,
"v1.4.1-beta.0": true,
"v1.4.0-alpha.4": true,
"1.1": false,
"V1.2": false,
"v12.3": false,
"v1.2.3.4": false,
}
for k, v := range data {
if c.IsSupportedTalosVersion(k) != v {
t.Errorf("%s: got %t, want %t", k, c.IsSupportedTalosVersion(k), v)
}
}
}

func TestIsSupportedK8sVersion(t *testing.T) {
c := &Config{
TalosVersion: "v1.3.7",
Expand Down

0 comments on commit dc7fc98

Please sign in to comment.