From 3e8ba171990002f3bdb0771627da998001efbf18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20L=C3=A9one?= Date: Fri, 19 Feb 2021 18:26:51 +0100 Subject: [PATCH] feat(k8s): add a default for choosing latest version in cluster create (#1781) --- ...-all-usage-k8s-cluster-create-usage.golden | 2 +- internal/namespaces/k8s/v1/custom_cluster.go | 16 +++++++++++++++ internal/namespaces/k8s/v1/custom_version.go | 20 +++++++++++++++++++ 3 files changed, 37 insertions(+), 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) 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..b8f91a97fd 100644 --- a/internal/namespaces/k8s/v1/custom_version.go +++ b/internal/namespaces/k8s/v1/custom_version.go @@ -2,10 +2,13 @@ package k8s 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" + "github.com/scaleway/scaleway-sdk-go/scw" ) func versionListBuilder(c *core.Command) *core.Command { @@ -61,3 +64,20 @@ 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, _ := version.NewVersion("0.0.0") + for _, v := range versions.Versions { + newVersion, _ := version.NewVersion(v.Name) + if newVersion.GreaterThan(latestVersion) { + latestVersion = newVersion + } + } + return latestVersion.String(), nil +}