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 marshallers for get versions #1560

Merged
merged 9 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 7 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
17 changes: 17 additions & 0 deletions internal/human/marshal_func.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"reflect"
"sort"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -117,6 +118,22 @@ func init() {
}
return strings.Join(res, " "), nil
})
marshalerFuncs.Store(reflect.TypeOf(map[string]string{}), func(i interface{}, opt *MarshalOpt) (string, error) {
res := []string(nil)
m := i.(map[string]string)

// maps are unordered, we need to have a sorted key list to keep a consistent printing
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)

for _, k := range keys {
res = append(res, fmt.Sprintf("%s => %s", k, m[k]))
}
return strings.Join(res, "\n"), nil
})
}

// TODO: implement the same logic as args.RegisterMarshalFunc(), where i must be a pointer
Expand Down
10 changes: 5 additions & 5 deletions internal/human/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ func TestMarshal(t *testing.T) {
Structs.0.Bool false
Structs.0.Time a long while ago
Structs.0.Stringer a stringer
Map.key1 v1
Map.key2 v2
Stringer a stringer
StringerPtr a stringer
Size 13 kB
Map key1 => v1
key2 => v2
Stringer a stringer
StringerPtr a stringer
Size 13 kB
`,
}))

Expand Down
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 11:05:09 GMT
Server:
- Scaleway API-Gateway
Strict-Transport-Security:
- max-age=63072000
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Request-Id:
- ac046352-718b-47bb-8806-2e785b5a7b99
status: 200 OK
code: 200
duration: ""
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
Name 1.20.2
Label Kubernetes 1.20.2
Region fr-par

Available Kubelet Arguments:
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"
}
}