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

APICLI-528: adding option to pick context name in 'kubeconfig save' #1075

Merged
merged 4 commits into from
Dec 27, 2021
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
2 changes: 2 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ const (
ArgKubernetesLabel = "label"
// ArgKubernetesTaint is a Kubernetes taint argument.
ArgKubernetesTaint = "taint"
// ArgKubernetesAlias is a Kubernetes alias argument that saves authentication information under the specified context.
ArgKubernetesAlias = "alias"
// ArgKubeConfigExpirySeconds indicates the length of time the token in a kubeconfig will be valid in seconds.
ArgKubeConfigExpirySeconds = "expiry-seconds"
// ArgImage is an image argument.
Expand Down
12 changes: 12 additions & 0 deletions commands/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ This command adds the credentials for the specified cluster to your local kubeco
AddBoolFlag(cmdSaveConfig, doctl.ArgSetCurrentContext, "", true, "Boolean indicating whether to set the current kubectl context to that of the new cluster")
AddIntFlag(cmdSaveConfig, doctl.ArgKubeConfigExpirySeconds, "", 0,
"The length of time the cluster credentials will be valid for in seconds. By default, the credentials are automatically renewed as needed.")
AddStringFlag(cmdSaveConfig, doctl.ArgKubernetesAlias, "", "", "An alias for the cluster context name. Defaults to 'do-<region>-<cluster-name>'.")

CmdBuilder(cmd, k8sCmdService.RunKubernetesKubeconfigRemove, "remove <cluster-id|cluster-name>", "Remove a cluster's credentials from your local kubeconfig", `
This command removes the specified cluster's credentials from your local kubeconfig. After running this command, you will not be able to use `+"`"+`kubectl`+"`"+` to interact with your cluster.
Expand Down Expand Up @@ -1260,6 +1261,17 @@ func (s *KubernetesCommandService) RunKubernetesKubeconfigSave(c *CmdConfig) err
return err
}

alias, err := c.Doit.GetString(c.NS, doctl.ArgKubernetesAlias)
if err != nil {
return err
}

if alias != "" {
remoteKubeconfig.Contexts[alias] = remoteKubeconfig.Contexts[remoteKubeconfig.CurrentContext]
delete(remoteKubeconfig.Contexts, remoteKubeconfig.CurrentContext)
remoteKubeconfig.CurrentContext = alias
}

setCurrentContext, err := c.Doit.GetBool(c.NS, doctl.ArgSetCurrentContext)
if err != nil {
return err
Expand Down
34 changes: 34 additions & 0 deletions integration/kubernetes_clusters_kubeconfig_save_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,40 @@ var _ = suite("kubernetes/clusters/kubeconfig/save", func(t *testing.T, when spe
expect.Contains(string(fileBytes), "token: some-token")
})
})
when("passing alias", func() {
it("creates an alias for a config", func() {
f, err := ioutil.TempFile("", "fake-kube-config")
expect.NoError(err)

defer os.Remove(f.Name())

cmd := exec.Command(builtBinaryPath,
"-t", "some-magic-token",
"-u", server.URL,
"kubernetes",
"clusters",
"kubeconfig",
"save",
"--alias", "newalias_test",
"some-cluster-name",
)

cmd.Env = append(os.Environ(),
fmt.Sprintf("KUBECONFIG=%s", f.Name()),
)

output, err := cmd.CombinedOutput()

expect.NoError(err, fmt.Sprintf("received error output: %s", output))

fileBytes, err := ioutil.ReadAll(f)
expect.NoError(err)
err = f.Close()
expect.NoError(err)
expect.Contains(string(fileBytes), fmt.Sprintf("current-context: %s", "newalias_test"))
danaelhe marked this conversation as resolved.
Show resolved Hide resolved
expect.Contains(string(fileBytes), fmt.Sprintf("name: %s", "newalias_test"))
})
})
})

const (
Expand Down