Skip to content

Commit

Permalink
feat(k8s): add option to keep kubeconfig context (#890)
Browse files Browse the repository at this point in the history
* feat(k8s): add option to keep kubeconfig context

Signed-off-by: Patrik Cyvoct <[email protected]>
  • Loading branch information
Sh4d1 authored Apr 22, 2020
1 parent 4960f73 commit 583adb2
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
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 <cluster-id> [arg=value ...]

EXAMPLES:
Install the kubeconfig for a given cluster and using the new 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
Expand All @@ -14,3 +20,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
Original file line number Diff line number Diff line change
@@ -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 <cluster-id> [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

Expand All @@ -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
30 changes: 25 additions & 5 deletions internal/namespaces/k8s/v1/custom_kubeconfig_install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
},
},
}
}

Expand Down Expand Up @@ -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 == "" {
Expand Down
17 changes: 15 additions & 2 deletions internal/namespaces/k8s/v1/custom_kubeconfig_uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
},
},
}
}

Expand Down

0 comments on commit 583adb2

Please sign in to comment.