From a43c12ee95ad33beb19d36cb6291d828aa0919b6 Mon Sep 17 00:00:00 2001 From: Arunvel Sriram Date: Mon, 12 Nov 2018 00:10:58 +0530 Subject: [PATCH] Implement renaming context --- pkg/kubeconfig/kubeconfig.go | 41 +++++++++++++++++------- pkg/kubeconfig/kubeconfig_test.go | 52 +++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 12 deletions(-) diff --git a/pkg/kubeconfig/kubeconfig.go b/pkg/kubeconfig/kubeconfig.go index cf8f142..98dc358 100644 --- a/pkg/kubeconfig/kubeconfig.go +++ b/pkg/kubeconfig/kubeconfig.go @@ -31,11 +31,6 @@ func (k KubeConfig) Delete(context string) error { // regional Kubernetes cluster func (k KubeConfig) AddRegionalCluster(project string, cluster string, region string, context string) error { kubeconfig := path.Join(k.dir, context) - envs := []string{ - "CLOUDSDK_CONTAINER_USE_V1_API_CLIENT=false", - "CLOUDSDK_CONTAINER_USE_V1_API=false", - fmt.Sprintf("KUBECONFIG=%s", kubeconfig), - } args := []string{ "beta", "container", @@ -45,8 +40,12 @@ func (k KubeConfig) AddRegionalCluster(project string, cluster string, region st fmt.Sprintf("--region=%s", region), fmt.Sprintf("--project=%s", project), } - _, err := k.commander.Execute("gcloud", args, envs) - if err != nil { + envs := []string{ + "CLOUDSDK_CONTAINER_USE_V1_API_CLIENT=false", + "CLOUDSDK_CONTAINER_USE_V1_API=false", + fmt.Sprintf("KUBECONFIG=%s", kubeconfig), + } + if _, err := k.commander.Execute("gcloud", args, envs); err != nil { return err } @@ -57,9 +56,6 @@ func (k KubeConfig) AddRegionalCluster(project string, cluster string, region st // zonal Kubernetes cluster func (k KubeConfig) AddZonalCluster(project string, cluster string, zone string, context string) error { kubeconfig := path.Join(k.dir, context) - envs := []string{ - fmt.Sprintf("KUBECONFIG=%s", kubeconfig), - } args := []string{ "container", "clusters", @@ -68,8 +64,29 @@ func (k KubeConfig) AddZonalCluster(project string, cluster string, zone string, fmt.Sprintf("--zone=%s", zone), fmt.Sprintf("--project=%s", project), } - _, err := k.commander.Execute("gcloud", args, envs) - if err != nil { + envs := []string{ + fmt.Sprintf("KUBECONFIG=%s", kubeconfig), + } + if _, err := k.commander.Execute("gcloud", args, envs); err != nil { + return err + } + + return nil +} + +// RenameContext renames a Kubernetes context +func (k KubeConfig) RenameContext(oldCtx string, newCtx string) error { + kubeconfig := path.Join(k.dir, newCtx) + args := []string{ + "config", + "rename-context", + oldCtx, + newCtx, + } + envs := []string{ + fmt.Sprintf("KUBECONFIG=%s", kubeconfig), + } + if _, err := k.commander.Execute("kubectl", args, envs); err != nil { return err } diff --git a/pkg/kubeconfig/kubeconfig_test.go b/pkg/kubeconfig/kubeconfig_test.go index 18b21d5..03d5c87 100644 --- a/pkg/kubeconfig/kubeconfig_test.go +++ b/pkg/kubeconfig/kubeconfig_test.go @@ -203,3 +203,55 @@ func TestAddZonalCluster(t *testing.T) { assert.EqualError(t, err, "some error") }) } + +func TestRenameContext(t *testing.T) { + t.Run("should rename a context", func(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + mockFS := mock.NewFileSystem(ctrl) + mockFS.EXPECT().HomeDir().Return("/Users/test", nil) + + mockCmdr := mock.NewCommander(ctrl) + args := []string{ + "config", + "rename-context", + "old-context-name", + "new-context-name", + } + envs := []string{ + "KUBECONFIG=/Users/test/.kube/configs/new-context-name", + } + mockCmdr.EXPECT().Execute("kubectl", args, envs).Return("Context renamed", nil) + + kubeCfg, _ := kubeconfig.New(mockFS, mockCmdr) + err := kubeCfg.RenameContext("old-context-name", "new-context-name") + + assert.Nil(t, err) + }) + + t.Run("should return error if renaming context fails", func(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + mockFS := mock.NewFileSystem(ctrl) + mockFS.EXPECT().HomeDir().Return("/Users/test", nil) + + mockCmdr := mock.NewCommander(ctrl) + args := []string{ + "config", + "rename-context", + "old-context-name", + "new-context-name", + } + envs := []string{ + "KUBECONFIG=/Users/test/.kube/configs/new-context-name", + } + mockCmdr.EXPECT().Execute("kubectl", args, envs).Return("", fmt.Errorf("some error")) + + kubeCfg, _ := kubeconfig.New(mockFS, mockCmdr) + err := kubeCfg.RenameContext("old-context-name", "new-context-name") + + assert.EqualError(t, err, "some error") + }) +}