Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(k8s): add a default for choosing latest version in cluster create #1781

Merged
merged 3 commits into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}