From b13578349dc55ce7d3e6c274d00214de56ce3e64 Mon Sep 17 00:00:00 2001 From: Patrik Cyvoct Date: Wed, 22 Apr 2020 11:20:54 +0200 Subject: [PATCH 1/2] feat(k8s): add option to keep kubeconfig context Signed-off-by: Patrik Cyvoct --- ...age-account-ssh-key-usage#01.stderr.golden | 22 -------------- ...k8s-kubeconfig-install-usage.stderr.golden | 19 ++++++++++-- ...s-kubeconfig-uninstall-usage.stderr.golden | 9 ++++++ .../k8s/v1/custom_kubeconfig_install.go | 30 +++++++++++++++---- .../k8s/v1/custom_kubeconfig_uninstall.go | 17 +++++++++-- 5 files changed, 65 insertions(+), 32 deletions(-) delete mode 100644 cmd/scw/testdata/test-all-usage-account-ssh-key-usage#01.stderr.golden diff --git a/cmd/scw/testdata/test-all-usage-account-ssh-key-usage#01.stderr.golden b/cmd/scw/testdata/test-all-usage-account-ssh-key-usage#01.stderr.golden deleted file mode 100644 index 536cfe3d2a..0000000000 --- a/cmd/scw/testdata/test-all-usage-account-ssh-key-usage#01.stderr.golden +++ /dev/null @@ -1,22 +0,0 @@ -Manage your Scaleway SSH keys. - -USAGE: - scw account ssh-key - -AVAILABLE COMMANDS: - list List all SSH keys - add Add a SSH key to your Scaleway account - get Get SSH key details - update Update an SSH key - remove Remove a SSH key from your Scaleway account - init Initialize SSH key - -FLAGS: - -h, --help help for ssh-key - -GLOBAL FLAGS: - -D, --debug Enable debug mode - -o, --output string Output format: json or human - -p, --profile string The config profile to use - -Use "scw account ssh-key [command] --help" for more information about a command. diff --git a/cmd/scw/testdata/test-all-usage-k8s-kubeconfig-install-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-k8s-kubeconfig-install-usage.stderr.golden index cf72c06b2f..ca0dd885c7 100644 --- a/cmd/scw/testdata/test-all-usage-k8s-kubeconfig-install-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-k8s-kubeconfig-install-usage.stderr.golden @@ -1,11 +1,20 @@ -Retrieve the kubeconfig for a specified cluster and write it on disk. It will merge the new kubeconfig in the file pointed by the KUBECONFIG variable. If empty it will default to $HOME/.kube/config. +Retrieve the kubeconfig for a specified cluster and write it on disk. +It will merge the new kubeconfig in the file pointed by the KUBECONFIG variable. If empty it will default to $HOME/.kube/config. USAGE: scw k8s kubeconfig install [arg=value ...] +EXAMPLES: + Install the kubeconfig for a given cluster and using the new context + scw k8s kubeconfig install 11111111-1111-1111-1111-111111111111 + + Install the kubeconfig for a given cluster without changing the current context + scw k8s kubeconfig install 11111111-1111-1111-1111-111111111111 + ARGS: - cluster-id Cluster ID from which to retrieve the kubeconfig - [region] Region to target. If none is passed will use default region from the config + cluster-id Cluster ID from which to retrieve the kubeconfig + [keep-current-context] Whether or not to keep the current kubeconfig context unmodified + [region] Region to target. If none is passed will use default region from the config FLAGS: -h, --help help for install @@ -14,3 +23,7 @@ GLOBAL FLAGS: -D, --debug Enable debug mode -o, --output string Output format: json or human -p, --profile string The config profile to use + +SEE ALSO: + # Uninstall a kubeconfig + scw k8s kubeconfig uninstall diff --git a/cmd/scw/testdata/test-all-usage-k8s-kubeconfig-uninstall-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-k8s-kubeconfig-uninstall-usage.stderr.golden index 1946142dcb..65bdb27d17 100644 --- a/cmd/scw/testdata/test-all-usage-k8s-kubeconfig-uninstall-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-k8s-kubeconfig-uninstall-usage.stderr.golden @@ -1,8 +1,13 @@ Remove specified cluster from kubeconfig file specified by the KUBECONFIG env, if empty it will default to $HOME/.kube/config. +If the current context points to this cluster, it will be set to an empty context. USAGE: scw k8s kubeconfig uninstall [arg=value ...] +EXAMPLES: + Uninstall the kubeconfig for a given cluster + scw k8s kubeconfig uninstall 11111111-1111-1111-1111-111111111111 + ARGS: cluster-id Cluster ID from which to uninstall the kubeconfig @@ -13,3 +18,7 @@ GLOBAL FLAGS: -D, --debug Enable debug mode -o, --output string Output format: json or human -p, --profile string The config profile to use + +SEE ALSO: + # Install a kubeconfig + scw k8s kubeconfig install diff --git a/internal/namespaces/k8s/v1/custom_kubeconfig_install.go b/internal/namespaces/k8s/v1/custom_kubeconfig_install.go index a9b3ac64b5..86a88c0927 100644 --- a/internal/namespaces/k8s/v1/custom_kubeconfig_install.go +++ b/internal/namespaces/k8s/v1/custom_kubeconfig_install.go @@ -19,14 +19,16 @@ const ( ) type k8sKubeconfigInstallRequest struct { - ClusterID string - Region scw.Region + ClusterID string + Region scw.Region + KeepCurentContext bool } func k8sKubeconfigInstallCommand() *core.Command { return &core.Command{ - Short: `Install a kubeconfig`, - Long: `Retrieve the kubeconfig for a specified cluster and write it on disk. It will merge the new kubeconfig in the file pointed by the KUBECONFIG variable. If empty it will default to $HOME/.kube/config.`, + Short: `Install a kubeconfig`, + Long: `Retrieve the kubeconfig for a specified cluster and write it on disk. +It will merge the new kubeconfig in the file pointed by the KUBECONFIG variable. If empty it will default to $HOME/.kube/config.`, Namespace: "k8s", Verb: "install", Resource: "kubeconfig", @@ -38,9 +40,25 @@ func k8sKubeconfigInstallCommand() *core.Command { Required: true, Positional: true, }, + { + Name: "keep-current-context", + Short: "Whether or not to keep the current kubeconfig context unmodified", + }, core.RegionArgSpec(), }, Run: k8sKubeconfigInstallRun, + Examples: []*core.Example{ + { + Short: "Install the kubeconfig for a given cluster and using the new context", + Request: `{"cluster_id": "11111111-1111-1111-1111-111111111111"}`, + }, + }, + SeeAlsos: []*core.SeeAlso{ + { + Command: "scw k8s kubeconfig uninstall", + Short: "Uninstall a kubeconfig", + }, + }, } } @@ -142,7 +160,9 @@ func k8sKubeconfigInstallRun(ctx context.Context, argsI interface{}) (i interfac } // set the current context to the new one - existingKubeconfig.CurrentContext = kubeconfig.Contexts[0].Name + "-" + request.ClusterID + if !request.KeepCurentContext { + existingKubeconfig.CurrentContext = kubeconfig.Contexts[0].Name + "-" + request.ClusterID + } // if it's a new file, set the correct config in the file if existingKubeconfig.APIVersion == "" { diff --git a/internal/namespaces/k8s/v1/custom_kubeconfig_uninstall.go b/internal/namespaces/k8s/v1/custom_kubeconfig_uninstall.go index 11da413e99..0adec6299f 100644 --- a/internal/namespaces/k8s/v1/custom_kubeconfig_uninstall.go +++ b/internal/namespaces/k8s/v1/custom_kubeconfig_uninstall.go @@ -17,8 +17,9 @@ type k8sKubeconfigUninstallRequest struct { func k8sKubeconfigUninstallCommand() *core.Command { return &core.Command{ - Short: `Uninstall a kubeconfig`, - Long: `Remove specified cluster from kubeconfig file specified by the KUBECONFIG env, if empty it will default to $HOME/.kube/config.`, + Short: `Uninstall a kubeconfig`, + Long: `Remove specified cluster from kubeconfig file specified by the KUBECONFIG env, if empty it will default to $HOME/.kube/config. +If the current context points to this cluster, it will be set to an empty context.`, Namespace: "k8s", Verb: "uninstall", Resource: "kubeconfig", @@ -32,6 +33,18 @@ func k8sKubeconfigUninstallCommand() *core.Command { }, }, Run: k8sKubeconfigUninstallRun, + Examples: []*core.Example{ + { + Short: "Uninstall the kubeconfig for a given cluster", + Request: `{"cluster_id": "11111111-1111-1111-1111-111111111111"}`, + }, + }, + SeeAlsos: []*core.SeeAlso{ + { + Command: "scw k8s kubeconfig install", + Short: "Install a kubeconfig", + }, + }, } } From d104ed5e27d55d185ec158b9d32999d229fce518 Mon Sep 17 00:00:00 2001 From: Patrik Cyvoct Date: Wed, 22 Apr 2020 11:23:14 +0200 Subject: [PATCH 2/2] feat(k8s): add option to keep kubeconfig context Signed-off-by: Patrik Cyvoct --- .../test-all-usage-k8s-kubeconfig-install-usage.stderr.golden | 3 --- 1 file changed, 3 deletions(-) diff --git a/cmd/scw/testdata/test-all-usage-k8s-kubeconfig-install-usage.stderr.golden b/cmd/scw/testdata/test-all-usage-k8s-kubeconfig-install-usage.stderr.golden index ca0dd885c7..12a90d7fbd 100644 --- a/cmd/scw/testdata/test-all-usage-k8s-kubeconfig-install-usage.stderr.golden +++ b/cmd/scw/testdata/test-all-usage-k8s-kubeconfig-install-usage.stderr.golden @@ -8,9 +8,6 @@ EXAMPLES: Install the kubeconfig for a given cluster and using the new context scw k8s kubeconfig install 11111111-1111-1111-1111-111111111111 - Install the kubeconfig for a given cluster without changing the current context - scw k8s kubeconfig install 11111111-1111-1111-1111-111111111111 - ARGS: cluster-id Cluster ID from which to retrieve the kubeconfig [keep-current-context] Whether or not to keep the current kubeconfig context unmodified