From 7980ee3c56ebe1059b4f6e12a7edfe127a976b6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Fri, 19 Feb 2021 18:01:54 +0100 Subject: [PATCH 1/3] feat(k8s): add a default for choosing latest version in cluster create --- internal/namespaces/k8s/v1/custom_cluster.go | 16 ++++++++++++++++ internal/namespaces/k8s/v1/custom_version.go | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/internal/namespaces/k8s/v1/custom_cluster.go b/internal/namespaces/k8s/v1/custom_cluster.go index b34b09e567..42af847f6a 100644 --- a/internal/namespaces/k8s/v1/custom_cluster.go +++ b/internal/namespaces/k8s/v1/custom_cluster.go @@ -87,6 +87,22 @@ func clusterCreateBuilder(c *core.Command) *core.Command { c.WaitFunc = waitForClusterFunc(clusterActionCreate) c.ArgSpecs.GetByName("cni").Default = core.DefaultValueSetter("cilium") + c.ArgSpecs.GetByName("version").Default = core.DefaultValueSetter("latest") + + c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) { + args := argsI.(*k8s.CreateClusterRequest) + + // Handle default latest version for k8s cluster + if args.Version == "latest" { + latestVersion, err := getLatestK8SVersion(core.ExtractClient(ctx)) + if err != nil { + return nil, fmt.Errorf("could not retrieve latest K8S version") + } + args.Version = latestVersion + } + + return runner(ctx, args) + } return c } diff --git a/internal/namespaces/k8s/v1/custom_version.go b/internal/namespaces/k8s/v1/custom_version.go index f4a82cd086..04beddd813 100644 --- a/internal/namespaces/k8s/v1/custom_version.go +++ b/internal/namespaces/k8s/v1/custom_version.go @@ -2,10 +2,12 @@ package k8s import ( "context" + "fmt" "github.com/scaleway/scaleway-cli/internal/core" "github.com/scaleway/scaleway-cli/internal/human" k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1" + "github.com/scaleway/scaleway-sdk-go/scw" ) func versionListBuilder(c *core.Command) *core.Command { @@ -61,3 +63,19 @@ func versionMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) return str, nil } + +func getLatestK8SVersion(scwClient *scw.Client) (string, error) { + api := k8s.NewAPI(scwClient) + versions, err := api.ListVersions(&k8s.ListVersionsRequest{}) + if err != nil { + return "", fmt.Errorf("could not get latest K8S version: %s", err) + } + + latestVersion := "" + for _, version := range versions.Versions { + if version.Name > latestVersion { + latestVersion = version.Name + } + } + return latestVersion, nil +} From 44ee9424230df6dcf237a48035af6aaa54bb7c50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Fri, 19 Feb 2021 18:02:32 +0100 Subject: [PATCH 2/3] Fix --- cmd/scw/testdata/test-all-usage-k8s-cluster-create-usage.golden | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/scw/testdata/test-all-usage-k8s-cluster-create-usage.golden b/cmd/scw/testdata/test-all-usage-k8s-cluster-create-usage.golden index ab84a6b340..665068cd9c 100644 --- a/cmd/scw/testdata/test-all-usage-k8s-cluster-create-usage.golden +++ b/cmd/scw/testdata/test-all-usage-k8s-cluster-create-usage.golden @@ -17,7 +17,7 @@ ARGS: name= The name of the cluster [description] The description of the cluster [tags.{index}] The tags associated with the cluster - version The Kubernetes version of the cluster + version=latest The Kubernetes version of the cluster cni=cilium The Container Network Interface (CNI) plugin that will run in the cluster (unknown_cni | cilium | calico | weave | flannel) [enable-dashboard] The enablement of the Kubernetes Dashboard in the cluster [ingress] The Ingress Controller that will run in the cluster (unknown_ingress | none | nginx | traefik | traefik2) From 5d22c356638a5884c3b351fd91918cd6143f6fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Fri, 19 Feb 2021 18:22:41 +0100 Subject: [PATCH 3/3] Fix --- internal/namespaces/k8s/v1/custom_version.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/internal/namespaces/k8s/v1/custom_version.go b/internal/namespaces/k8s/v1/custom_version.go index 04beddd813..b8f91a97fd 100644 --- a/internal/namespaces/k8s/v1/custom_version.go +++ b/internal/namespaces/k8s/v1/custom_version.go @@ -4,6 +4,7 @@ import ( "context" "fmt" + "github.com/hashicorp/go-version" "github.com/scaleway/scaleway-cli/internal/core" "github.com/scaleway/scaleway-cli/internal/human" k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1" @@ -71,11 +72,12 @@ func getLatestK8SVersion(scwClient *scw.Client) (string, error) { return "", fmt.Errorf("could not get latest K8S version: %s", err) } - latestVersion := "" - for _, version := range versions.Versions { - if version.Name > latestVersion { - latestVersion = version.Name + latestVersion, _ := version.NewVersion("0.0.0") + for _, v := range versions.Versions { + newVersion, _ := version.NewVersion(v.Name) + if newVersion.GreaterThan(latestVersion) { + latestVersion = newVersion } } - return latestVersion, nil + return latestVersion.String(), nil }