Skip to content

Commit 7cf7d2d

Browse files
remyleoneSh4d1
andauthored
feat(k8s): improve human marshaller for cluster (#1201)
Co-authored-by: Patrik <[email protected]>
1 parent fe1a6ae commit 7cf7d2d

File tree

6 files changed

+480
-0
lines changed

6 files changed

+480
-0
lines changed

internal/namespaces/k8s/v1/custom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func GetCommands() *core.Commands {
2424
k8sPoolWaitCommand(),
2525
))
2626

27+
human.RegisterMarshalerFunc(k8s.Cluster{}, clusterMarshalerFunc)
2728
human.RegisterMarshalerFunc(k8s.ClusterStatus(0), human.EnumMarshalFunc(clusterStatusMarshalSpecs))
2829
human.RegisterMarshalerFunc(k8s.PoolStatus(0), human.EnumMarshalFunc(poolStatusMarshalSpecs))
2930
human.RegisterMarshalerFunc(k8s.NodeStatus(0), human.EnumMarshalFunc(nodeStatusMarshalSpecs))

internal/namespaces/k8s/v1/custom_cluster.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ const (
4141
clusterActionDelete
4242
)
4343

44+
func clusterMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
45+
type tmp k8s.Cluster
46+
cluster := tmp(i.(k8s.Cluster))
47+
48+
// Sections
49+
opt.Sections = []*human.MarshalSection{
50+
{
51+
FieldName: "AutoscalerConfig",
52+
Title: "Autoscaler configuration",
53+
},
54+
{
55+
FieldName: "AutoUpgrade",
56+
Title: "Auto-upgrade settings",
57+
},
58+
}
59+
60+
str, err := human.Marshal(cluster, opt)
61+
if err != nil {
62+
return "", err
63+
}
64+
65+
return str, nil
66+
}
67+
4468
func clusterAvailableVersionsListBuilder(c *core.Command) *core.Command {
4569
c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
4670
originalRes, err := runner(ctx, argsI)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package k8s
2+
3+
import (
4+
"testing"
5+
6+
"github.com/scaleway/scaleway-cli/internal/core"
7+
)
8+
9+
func Test_GetCluster(t *testing.T) {
10+
t.Run("Simple", core.Test(&core.TestConfig{
11+
Commands: GetCommands(),
12+
BeforeFunc: createCluster("Cluster", kapsuleVersion, 1),
13+
Cmd: "scw k8s cluster get {{ .Cluster.ID }}",
14+
Check: core.TestCheckCombine(
15+
core.TestCheckGolden(),
16+
core.TestCheckExitCode(0),
17+
),
18+
AfterFunc: deleteCluster("Cluster"),
19+
}))
20+
}

internal/namespaces/k8s/v1/helpers_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ const (
1717
// Clusters
1818
//
1919

20+
// createCluster creates a basic cluster with "poolSize" dev1-m as nodes, the given version and
21+
// register it in the context Meta at metaKey.
22+
func createCluster(metaKey string, version string, poolSize int) core.BeforeFunc {
23+
return core.ExecStoreBeforeCmd(
24+
metaKey,
25+
fmt.Sprintf("scw k8s cluster create name=cli-test version=%s cni=cilium pools.0.node-type=DEV1-M pools.0.size=%d pools.0.name=default", version, poolSize))
26+
}
27+
2028
// createClusterAndWaitAndKubeconfig creates a basic cluster with 1 dev1-m as node, the given version and
2129
// register it in the context Meta at metaKey.
2230
func createClusterAndWaitAndKubeconfig(metaKey string, kubeconfigMetaKey string, version string) core.BeforeFunc {

0 commit comments

Comments
 (0)