From f6eaf25a2e114855731a9f3b4948ec3a5d7380d9 Mon Sep 17 00:00:00 2001 From: Antoine Date: Mon, 1 Jun 2020 21:33:56 +0200 Subject: [PATCH] :recycle: Some side effect refactoring Print usage When no k3d verb was specify Replace fake Cluster object create into getKubeConfig by realy complete object obtain thanks to cluster.GetCluster() function Use constant variable for node Label name use into populateClusterFieldsFromLabels() function, in forecast to rename label `k3d.cluster.secret` to `k3d.cluster.token` --- cmd/get/getKubeconfig.go | 6 +++++- cmd/root.go | 2 ++ pkg/cluster/cluster.go | 16 ++++++++-------- pkg/tools/tools.go | 2 +- pkg/types/types.go | 8 ++++++++ 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/cmd/get/getKubeconfig.go b/cmd/get/getKubeconfig.go index 2be6cc97..9ec20596 100644 --- a/cmd/get/getKubeconfig.go +++ b/cmd/get/getKubeconfig.go @@ -69,7 +69,11 @@ func NewCmdGetKubeconfig() *cobra.Command { } } else { for _, clusterName := range args { - clusters = append(clusters, &k3d.Cluster{Name: clusterName}) + retrievedCluster, err := cluster.GetCluster(cmd.Context(), runtimes.SelectedRuntime, &k3d.Cluster{Name: clusterName}) + if err != nil { + log.Fatalln(err) + } + clusters = append(clusters, retrievedCluster) } } diff --git a/cmd/root.go b/cmd/root.go index 3190234c..1c5ce172 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -65,6 +65,8 @@ All Nodes of a k3d cluster are part of the same docker network.`, Run: func(cmd *cobra.Command, args []string) { if flags.version { printVersion() + } else { + cmd.Usage() } }, } diff --git a/pkg/cluster/cluster.go b/pkg/cluster/cluster.go index 9eb74736..bc913614 100644 --- a/pkg/cluster/cluster.go +++ b/pkg/cluster/cluster.go @@ -79,11 +79,11 @@ func CreateCluster(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clus } cluster.Network.Name = networkID extraLabels := map[string]string{ - "k3d.cluster.network": networkID, - "k3d.cluster.network.external": strconv.FormatBool(cluster.Network.External), + k3d.NetworkLabelName: networkID, + k3d.NetworkExternalLabelName: strconv.FormatBool(cluster.Network.External), } if networkExists { - extraLabels["k3d.cluster.network.external"] = "true" // if the network wasn't created, we say that it's managed externally (important for cluster deletion) + extraLabels[k3d.NetworkExternalLabelName] = "true" // if the network wasn't created, we say that it's managed externally (important for cluster deletion) } /* @@ -105,7 +105,7 @@ func CreateCluster(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clus return err } - extraLabels["k3d.cluster.imageVolume"] = imageVolumeName + extraLabels[k3d.ImageVolumeLabelName] = imageVolumeName // attach volume to nodes for _, node := range cluster.Nodes { @@ -128,7 +128,7 @@ func CreateCluster(ctx context.Context, runtime k3drt.Runtime, cluster *k3d.Clus } node.Labels["k3d.cluster"] = cluster.Name node.Env = append(node.Env, fmt.Sprintf("K3S_TOKEN=%s", cluster.Secret)) - node.Labels["k3d.cluster.secret"] = cluster.Secret + node.Labels[k3d.SecretLabelName] = cluster.Secret node.Labels["k3d.cluster.url"] = connectionURL // append extra labels @@ -419,7 +419,7 @@ func populateClusterFieldsFromLabels(cluster *k3d.Cluster) error { // get the name of the cluster network if cluster.Network.Name == "" { - if networkName, ok := node.Labels["k3d.cluster.network"]; ok { + if networkName, ok := node.Labels[k3d.NetworkLabelName]; ok { cluster.Network.Name = networkName } } @@ -427,7 +427,7 @@ func populateClusterFieldsFromLabels(cluster *k3d.Cluster) error { // check if the network is external // since the struct value is a bool, initialized as false, we cannot check if it's unset if !cluster.Network.External && !networkExternalSet { - if networkExternalString, ok := node.Labels["k3d.cluster.network.external"]; ok { + if networkExternalString, ok := node.Labels[k3d.NetworkExternalLabelName]; ok { if networkExternal, err := strconv.ParseBool(networkExternalString); err == nil { cluster.Network.External = networkExternal networkExternalSet = true @@ -437,7 +437,7 @@ func populateClusterFieldsFromLabels(cluster *k3d.Cluster) error { // get image volume // TODO: enable external image volumes the same way we do it with networks if cluster.ImageVolume == "" { - if imageVolumeName, ok := node.Labels["k3d.cluster.imageVolume"]; ok { + if imageVolumeName, ok := node.Labels[k3d.ImageVolumeLabelName]; ok { cluster.ImageVolume = imageVolumeName } } diff --git a/pkg/tools/tools.go b/pkg/tools/tools.go index ddb8fdc4..5704b550 100644 --- a/pkg/tools/tools.go +++ b/pkg/tools/tools.go @@ -50,7 +50,7 @@ func LoadImagesIntoCluster(ctx context.Context, runtime runtimes.Runtime, images var ok bool for _, node := range cluster.Nodes { if node.Role == k3d.MasterRole || node.Role == k3d.WorkerRole { - if imageVolume, ok = node.Labels["k3d.cluster.imageVolume"]; ok { + if imageVolume, ok = node.Labels[k3d.ImageVolumeLabelName]; ok { break } } diff --git a/pkg/types/types.go b/pkg/types/types.go index dd67d33e..469219e6 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -74,6 +74,14 @@ var DefaultObjectLabels = map[string]string{ "app": "k3d", } +// List of k3d technical label name +const ( + SecretLabelName string = "k3d.cluster.secret" + ImageVolumeLabelName string = "k3d.cluster.imageVolume" + NetworkExternalLabelName string = "k3d.cluster.network.external" + NetworkLabelName string = "k3d.cluster.network" +) + // DefaultRoleCmds maps the node roles to their respective default commands var DefaultRoleCmds = map[Role][]string{ MasterRole: {"server"},