Skip to content

Commit 1975716

Browse files
committed
Rename variable, add methid to expose kube configs directory
1 parent a43c12e commit 1975716

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

pkg/kubeconfig/kubeconfig.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ import (
1111

1212
// KubeConfig exposes methods to perform actions on kubeconfig
1313
type KubeConfig struct {
14-
filesystem filesystem.FileSystem
15-
commander commander.Commander
16-
dir string
14+
filesystem filesystem.FileSystem
15+
commander commander.Commander
16+
kubeCfgsDir string
1717
}
1818

1919
// Delete deletes the kubeconfig file for the given context
2020
func (k KubeConfig) Delete(context string) error {
21-
file := path.Join(k.dir, context)
21+
file := path.Join(k.kubeCfgsDir, context)
2222

2323
if err := k.filesystem.Remove(file); err != nil && !os.IsNotExist(err) {
2424
return err
@@ -30,7 +30,7 @@ func (k KubeConfig) Delete(context string) error {
3030
// AddRegionalCluster imports Kubernetes context for
3131
// regional Kubernetes cluster
3232
func (k KubeConfig) AddRegionalCluster(project string, cluster string, region string, context string) error {
33-
kubeconfig := path.Join(k.dir, context)
33+
kubeCfgFile := path.Join(k.kubeCfgsDir, context)
3434
args := []string{
3535
"beta",
3636
"container",
@@ -43,7 +43,7 @@ func (k KubeConfig) AddRegionalCluster(project string, cluster string, region st
4343
envs := []string{
4444
"CLOUDSDK_CONTAINER_USE_V1_API_CLIENT=false",
4545
"CLOUDSDK_CONTAINER_USE_V1_API=false",
46-
fmt.Sprintf("KUBECONFIG=%s", kubeconfig),
46+
fmt.Sprintf("KUBECONFIG=%s", kubeCfgFile),
4747
}
4848
if _, err := k.commander.Execute("gcloud", args, envs); err != nil {
4949
return err
@@ -55,7 +55,7 @@ func (k KubeConfig) AddRegionalCluster(project string, cluster string, region st
5555
// AddZonalCluster imports Kubernetes context for
5656
// zonal Kubernetes cluster
5757
func (k KubeConfig) AddZonalCluster(project string, cluster string, zone string, context string) error {
58-
kubeconfig := path.Join(k.dir, context)
58+
kubeCfgFile := path.Join(k.kubeCfgsDir, context)
5959
args := []string{
6060
"container",
6161
"clusters",
@@ -65,7 +65,7 @@ func (k KubeConfig) AddZonalCluster(project string, cluster string, zone string,
6565
fmt.Sprintf("--project=%s", project),
6666
}
6767
envs := []string{
68-
fmt.Sprintf("KUBECONFIG=%s", kubeconfig),
68+
fmt.Sprintf("KUBECONFIG=%s", kubeCfgFile),
6969
}
7070
if _, err := k.commander.Execute("gcloud", args, envs); err != nil {
7171
return err
@@ -76,15 +76,15 @@ func (k KubeConfig) AddZonalCluster(project string, cluster string, zone string,
7676

7777
// RenameContext renames a Kubernetes context
7878
func (k KubeConfig) RenameContext(oldCtx string, newCtx string) error {
79-
kubeconfig := path.Join(k.dir, newCtx)
79+
kubeCfgFile := path.Join(k.kubeCfgsDir, newCtx)
8080
args := []string{
8181
"config",
8282
"rename-context",
8383
oldCtx,
8484
newCtx,
8585
}
8686
envs := []string{
87-
fmt.Sprintf("KUBECONFIG=%s", kubeconfig),
87+
fmt.Sprintf("KUBECONFIG=%s", kubeCfgFile),
8888
}
8989
if _, err := k.commander.Execute("kubectl", args, envs); err != nil {
9090
return err
@@ -93,6 +93,11 @@ func (k KubeConfig) RenameContext(oldCtx string, newCtx string) error {
9393
return nil
9494
}
9595

96+
// KubeCfgsDir returns the directory in which kube configs are stored
97+
func (k KubeConfig) KubeCfgsDir() string {
98+
return k.kubeCfgsDir
99+
}
100+
96101
// New returns a new KubeConfig
97102
func New(fs filesystem.FileSystem, cmdr commander.Commander) (KubeConfig, error) {
98103
home, err := fs.HomeDir()
@@ -102,8 +107,8 @@ func New(fs filesystem.FileSystem, cmdr commander.Commander) (KubeConfig, error)
102107
kubeConfigsDir := path.Join(home, ".kube/configs")
103108

104109
return KubeConfig{
105-
filesystem: fs,
106-
commander: cmdr,
107-
dir: kubeConfigsDir,
110+
filesystem: fs,
111+
commander: cmdr,
112+
kubeCfgsDir: kubeConfigsDir,
108113
}, nil
109114
}

pkg/kubeconfig/kubeconfig_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,20 @@ func TestRenameContext(t *testing.T) {
255255
assert.EqualError(t, err, "some error")
256256
})
257257
}
258+
259+
func TestKubeCfgsDir(t *testing.T) {
260+
t.Run("should return the directory in which kube configs are stored", func(t *testing.T) {
261+
ctrl := gomock.NewController(t)
262+
defer ctrl.Finish()
263+
264+
mockFS := mock.NewFileSystem(ctrl)
265+
mockFS.EXPECT().HomeDir().Return("/Users/test", nil)
266+
267+
mockCmdr := mock.NewCommander(ctrl)
268+
269+
kubeCfg, _ := kubeconfig.New(mockFS, mockCmdr)
270+
dir := kubeCfg.KubeCfgsDir()
271+
272+
assert.Equal(t, "/Users/test/.kube/configs", dir)
273+
})
274+
}

0 commit comments

Comments
 (0)