Skip to content

Commit

Permalink
feat(k8s): add marshallers for get versions (#1560)
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone authored Feb 15, 2021
1 parent 947db7c commit dd0fcd3
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/namespaces/k8s/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ func GetCommands() *core.Commands {
k8sPoolWaitCommand(),
))

human.RegisterMarshalerFunc(k8s.Version{}, versionMarshalerFunc)
human.RegisterMarshalerFunc(k8s.Cluster{}, clusterMarshalerFunc)
human.RegisterMarshalerFunc([]k8s.CNI{}, cniSliceMarshalerFunc)
human.RegisterMarshalerFunc([]k8s.Ingress{}, ingressSliceMarshalerFunc)
human.RegisterMarshalerFunc([]k8s.Runtime{}, runtimeSliceMarshalerFunc)
human.RegisterMarshalerFunc(k8s.ClusterStatus(""), human.EnumMarshalFunc(clusterStatusMarshalSpecs))
human.RegisterMarshalerFunc(k8s.PoolStatus(""), human.EnumMarshalFunc(poolStatusMarshalSpecs))
human.RegisterMarshalerFunc(k8s.NodeStatus(""), human.EnumMarshalFunc(nodeStatusMarshalSpecs))
Expand Down
70 changes: 70 additions & 0 deletions internal/namespaces/k8s/v1/custom_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package k8s

import (
"context"
"fmt"
"strings"

"github.com/scaleway/scaleway-cli/internal/core"
"github.com/scaleway/scaleway-cli/internal/human"
k8s "github.com/scaleway/scaleway-sdk-go/api/k8s/v1"
)

Expand All @@ -20,3 +23,70 @@ func versionListBuilder(c *core.Command) *core.Command {

return c
}

func runtimeSliceMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
runtimeSlice := i.([]k8s.Runtime)
var res []string
for _, value := range runtimeSlice {
res = append(res, fmt.Sprintf("- %s", value.String()))
}
return strings.Join(res, "\n"), nil
}

func ingressSliceMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
runtimeSlice := i.([]k8s.Ingress)
var res []string
for _, value := range runtimeSlice {
res = append(res, fmt.Sprintf("- %s", value.String()))
}
return strings.Join(res, "\n"), nil
}

func cniSliceMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
runtimeSlice := i.([]k8s.CNI)
var res []string
for _, value := range runtimeSlice {
res = append(res, fmt.Sprintf("- %s", value.String()))
}
return strings.Join(res, "\n"), nil
}

func versionMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
type tmp k8s.Version
version := tmp(i.(k8s.Version))

// Sections
opt.Sections = []*human.MarshalSection{
{
FieldName: "AvailableKubeletArgs",
Title: "Available Kubelet Arguments",
},
{
FieldName: "AvailableCnis",
Title: "Available CNIs",
},
{
FieldName: "AvailableIngresses",
Title: "Available Ingresses",
},
{
FieldName: "AvailableContainerRuntimes",
Title: "Available Container Runtimes",
},
{
FieldName: "AvailableFeatureGates",
Title: "Available Feature Gates",
},
{
FieldName: "AvailableAdmissionPlugins",
Title: "Available Admission Plugins",
},
}

str, err := human.Marshal(version, opt)
if err != nil {
return "", err
}

return str, nil
}
21 changes: 21 additions & 0 deletions internal/namespaces/k8s/v1/custom_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package k8s

import (
"testing"

"github.com/scaleway/scaleway-cli/internal/core"
)

func Test_GetVersion(t *testing.T) {
////
// Simple use cases
////
t.Run("simple", core.Test(&core.TestConfig{
Commands: GetCommands(),
Cmd: "scw k8s version get 1.20.2",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
}))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
version: 1
interactions:
- request:
body: ""
form: {}
headers:
User-Agent:
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.15.7; darwin; amd64) cli-e2e-test
url: https://api.scaleway.com/k8s/v1/regions/fr-par/versions/1.20.2
method: GET
response:
body: '{"region":"fr-par","name":"1.20.2","label":"Kubernetes 1.20.2","available_cnis":["cilium","calico","weave","flannel"],"available_ingresses":["none","nginx","traefik","traefik2"],"available_container_runtimes":["containerd","crio","docker"],"available_feature_gates":["TTLAfterFinished","HPAScaleToZero","ServiceTopology","EphemeralContainers","KubeletCredentialProviders","GenericEphemeralVolume"],"available_admission_plugins":["PodSecurityPolicy","PodNodeSelector","AlwaysPullImages","PodPreset","PodTolerationRestriction"],"available_kubelet_args":{"cpuCFSQuota":"bool","cpuCFSQuotaPeriod":"duration","cpuManagerPolicy":"enum:none|static","maxPods":"uint16"}}'
headers:
Content-Length:
- "662"
Content-Security-Policy:
- default-src 'none'; frame-ancestors 'none'
Content-Type:
- application/json
Date:
- Wed, 10 Feb 2021 15:25:17 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- 6b5b146e-bdd6-485f-b38c-a8782f4eba52
status: 200 OK
code: 200
duration: ""
75 changes: 75 additions & 0 deletions internal/namespaces/k8s/v1/testdata/test-get-version-simple.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
Name 1.20.2
Label Kubernetes 1.20.2
Region fr-par

Available Kubelet Arguments:
map[cpuCFSQuota:bool cpuCFSQuotaPeriod:duration cpuManagerPolicy:enum:none|static maxPods:uint16]

Available CNIs:
- cilium
- calico
- weave
- flannel

Available Ingresses:
- none
- nginx
- traefik
- traefik2

Available Container Runtimes:
- containerd
- crio
- docker

Available Feature Gates:
[TTLAfterFinished HPAScaleToZero ServiceTopology EphemeralContainers KubeletCredentialProviders GenericEphemeralVolume]

Available Admission Plugins:
[PodSecurityPolicy PodNodeSelector AlwaysPullImages PodPreset PodTolerationRestriction]
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
{
"name": "1.20.2",
"label": "Kubernetes 1.20.2",
"region": "fr-par",
"available_cnis": [
"cilium",
"calico",
"weave",
"flannel"
],
"available_ingresses": [
"none",
"nginx",
"traefik",
"traefik2"
],
"available_container_runtimes": [
"containerd",
"crio",
"docker"
],
"available_feature_gates": [
"TTLAfterFinished",
"HPAScaleToZero",
"ServiceTopology",
"EphemeralContainers",
"KubeletCredentialProviders",
"GenericEphemeralVolume"
],
"available_admission_plugins": [
"PodSecurityPolicy",
"PodNodeSelector",
"AlwaysPullImages",
"PodPreset",
"PodTolerationRestriction"
],
"available_kubelet_args": {
"cpuCFSQuota": "bool",
"cpuCFSQuotaPeriod": "duration",
"cpuManagerPolicy": "enum:none|static",
"maxPods": "uint16"
}
}

0 comments on commit dd0fcd3

Please sign in to comment.