Skip to content

Commit

Permalink
feat(k8s): improve human marshaller for cluster (#1201)
Browse files Browse the repository at this point in the history
Co-authored-by: Patrik <patrik@ptrk.io>
  • Loading branch information
remyleone and Sh4d1 authored Jul 10, 2020
1 parent fe1a6ae commit 7cf7d2d
Show file tree
Hide file tree
Showing 6 changed files with 480 additions and 0 deletions.
1 change: 1 addition & 0 deletions internal/namespaces/k8s/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func GetCommands() *core.Commands {
k8sPoolWaitCommand(),
))

human.RegisterMarshalerFunc(k8s.Cluster{}, clusterMarshalerFunc)
human.RegisterMarshalerFunc(k8s.ClusterStatus(0), human.EnumMarshalFunc(clusterStatusMarshalSpecs))
human.RegisterMarshalerFunc(k8s.PoolStatus(0), human.EnumMarshalFunc(poolStatusMarshalSpecs))
human.RegisterMarshalerFunc(k8s.NodeStatus(0), human.EnumMarshalFunc(nodeStatusMarshalSpecs))
Expand Down
24 changes: 24 additions & 0 deletions internal/namespaces/k8s/v1/custom_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ const (
clusterActionDelete
)

func clusterMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
type tmp k8s.Cluster
cluster := tmp(i.(k8s.Cluster))

// Sections
opt.Sections = []*human.MarshalSection{
{
FieldName: "AutoscalerConfig",
Title: "Autoscaler configuration",
},
{
FieldName: "AutoUpgrade",
Title: "Auto-upgrade settings",
},
}

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

return str, nil
}

func clusterAvailableVersionsListBuilder(c *core.Command) *core.Command {
c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
originalRes, err := runner(ctx, argsI)
Expand Down
20 changes: 20 additions & 0 deletions internal/namespaces/k8s/v1/custom_cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package k8s

import (
"testing"

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

func Test_GetCluster(t *testing.T) {
t.Run("Simple", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: createCluster("Cluster", kapsuleVersion, 1),
Cmd: "scw k8s cluster get {{ .Cluster.ID }}",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: deleteCluster("Cluster"),
}))
}
8 changes: 8 additions & 0 deletions internal/namespaces/k8s/v1/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ const (
// Clusters
//

// createCluster creates a basic cluster with "poolSize" dev1-m as nodes, the given version and
// register it in the context Meta at metaKey.
func createCluster(metaKey string, version string, poolSize int) core.BeforeFunc {
return core.ExecStoreBeforeCmd(
metaKey,
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))
}

// createClusterAndWaitAndKubeconfig creates a basic cluster with 1 dev1-m as node, the given version and
// register it in the context Meta at metaKey.
func createClusterAndWaitAndKubeconfig(metaKey string, kubeconfigMetaKey string, version string) core.BeforeFunc {
Expand Down
Loading

0 comments on commit 7cf7d2d

Please sign in to comment.