Skip to content

Commit

Permalink
feat(k8s): add a default for choosing latest version in cluster create (
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone authored Feb 19, 2021
1 parent d243670 commit 3e8ba17
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ARGS:
name=<generated> 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)
Expand Down
16 changes: 16 additions & 0 deletions internal/namespaces/k8s/v1/custom_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions internal/namespaces/k8s/v1/custom_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit 3e8ba17

Please sign in to comment.