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): improve human marshaller for cluster #1201

Merged
merged 5 commits into from
Jul 10, 2020
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
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