Skip to content

Commit a43c12e

Browse files
committed
Implement renaming context
1 parent 1b62775 commit a43c12e

File tree

2 files changed

+81
-12
lines changed

2 files changed

+81
-12
lines changed

pkg/kubeconfig/kubeconfig.go

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ func (k KubeConfig) Delete(context string) error {
3131
// regional Kubernetes cluster
3232
func (k KubeConfig) AddRegionalCluster(project string, cluster string, region string, context string) error {
3333
kubeconfig := path.Join(k.dir, context)
34-
envs := []string{
35-
"CLOUDSDK_CONTAINER_USE_V1_API_CLIENT=false",
36-
"CLOUDSDK_CONTAINER_USE_V1_API=false",
37-
fmt.Sprintf("KUBECONFIG=%s", kubeconfig),
38-
}
3934
args := []string{
4035
"beta",
4136
"container",
@@ -45,8 +40,12 @@ func (k KubeConfig) AddRegionalCluster(project string, cluster string, region st
4540
fmt.Sprintf("--region=%s", region),
4641
fmt.Sprintf("--project=%s", project),
4742
}
48-
_, err := k.commander.Execute("gcloud", args, envs)
49-
if err != nil {
43+
envs := []string{
44+
"CLOUDSDK_CONTAINER_USE_V1_API_CLIENT=false",
45+
"CLOUDSDK_CONTAINER_USE_V1_API=false",
46+
fmt.Sprintf("KUBECONFIG=%s", kubeconfig),
47+
}
48+
if _, err := k.commander.Execute("gcloud", args, envs); err != nil {
5049
return err
5150
}
5251

@@ -57,9 +56,6 @@ func (k KubeConfig) AddRegionalCluster(project string, cluster string, region st
5756
// zonal Kubernetes cluster
5857
func (k KubeConfig) AddZonalCluster(project string, cluster string, zone string, context string) error {
5958
kubeconfig := path.Join(k.dir, context)
60-
envs := []string{
61-
fmt.Sprintf("KUBECONFIG=%s", kubeconfig),
62-
}
6359
args := []string{
6460
"container",
6561
"clusters",
@@ -68,8 +64,29 @@ func (k KubeConfig) AddZonalCluster(project string, cluster string, zone string,
6864
fmt.Sprintf("--zone=%s", zone),
6965
fmt.Sprintf("--project=%s", project),
7066
}
71-
_, err := k.commander.Execute("gcloud", args, envs)
72-
if err != nil {
67+
envs := []string{
68+
fmt.Sprintf("KUBECONFIG=%s", kubeconfig),
69+
}
70+
if _, err := k.commander.Execute("gcloud", args, envs); err != nil {
71+
return err
72+
}
73+
74+
return nil
75+
}
76+
77+
// RenameContext renames a Kubernetes context
78+
func (k KubeConfig) RenameContext(oldCtx string, newCtx string) error {
79+
kubeconfig := path.Join(k.dir, newCtx)
80+
args := []string{
81+
"config",
82+
"rename-context",
83+
oldCtx,
84+
newCtx,
85+
}
86+
envs := []string{
87+
fmt.Sprintf("KUBECONFIG=%s", kubeconfig),
88+
}
89+
if _, err := k.commander.Execute("kubectl", args, envs); err != nil {
7390
return err
7491
}
7592

pkg/kubeconfig/kubeconfig_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,55 @@ func TestAddZonalCluster(t *testing.T) {
203203
assert.EqualError(t, err, "some error")
204204
})
205205
}
206+
207+
func TestRenameContext(t *testing.T) {
208+
t.Run("should rename a context", func(t *testing.T) {
209+
ctrl := gomock.NewController(t)
210+
defer ctrl.Finish()
211+
212+
mockFS := mock.NewFileSystem(ctrl)
213+
mockFS.EXPECT().HomeDir().Return("/Users/test", nil)
214+
215+
mockCmdr := mock.NewCommander(ctrl)
216+
args := []string{
217+
"config",
218+
"rename-context",
219+
"old-context-name",
220+
"new-context-name",
221+
}
222+
envs := []string{
223+
"KUBECONFIG=/Users/test/.kube/configs/new-context-name",
224+
}
225+
mockCmdr.EXPECT().Execute("kubectl", args, envs).Return("Context renamed", nil)
226+
227+
kubeCfg, _ := kubeconfig.New(mockFS, mockCmdr)
228+
err := kubeCfg.RenameContext("old-context-name", "new-context-name")
229+
230+
assert.Nil(t, err)
231+
})
232+
233+
t.Run("should return error if renaming context fails", func(t *testing.T) {
234+
ctrl := gomock.NewController(t)
235+
defer ctrl.Finish()
236+
237+
mockFS := mock.NewFileSystem(ctrl)
238+
mockFS.EXPECT().HomeDir().Return("/Users/test", nil)
239+
240+
mockCmdr := mock.NewCommander(ctrl)
241+
args := []string{
242+
"config",
243+
"rename-context",
244+
"old-context-name",
245+
"new-context-name",
246+
}
247+
envs := []string{
248+
"KUBECONFIG=/Users/test/.kube/configs/new-context-name",
249+
}
250+
mockCmdr.EXPECT().Execute("kubectl", args, envs).Return("", fmt.Errorf("some error"))
251+
252+
kubeCfg, _ := kubeconfig.New(mockFS, mockCmdr)
253+
err := kubeCfg.RenameContext("old-context-name", "new-context-name")
254+
255+
assert.EqualError(t, err, "some error")
256+
})
257+
}

0 commit comments

Comments
 (0)