diff --git a/pkg/crc/machine/kubeconfig.go b/pkg/crc/machine/kubeconfig.go index 7cc4df9691..6f685ca066 100644 --- a/pkg/crc/machine/kubeconfig.go +++ b/pkg/crc/machine/kubeconfig.go @@ -72,7 +72,7 @@ func writeKubeconfig(ip string, clusterConfig *types.ClusterConfig, ingressHTTPS if err != nil { return err } - if err := addContext(cfg, clusterConfig.ClusterAPI, adminContext, "kubeadmin", kubeadminToken); err != nil { + if err := addContext(cfg, clusterConfig.ClusterAPI, adminContext, "kubeadmin", kubeadminToken, "default"); err != nil { return err } @@ -80,7 +80,7 @@ func writeKubeconfig(ip string, clusterConfig *types.ClusterConfig, ingressHTTPS if err != nil { return err } - if err := addContext(cfg, clusterConfig.ClusterAPI, developerContext, "developer", developerToken); err != nil { + if err := addContext(cfg, clusterConfig.ClusterAPI, developerContext, "developer", developerToken, ""); err != nil { return err } @@ -142,7 +142,7 @@ func hostname(clusterAPI string) (string, error) { return strings.ReplaceAll(h, ".", "-"), nil } -func addContext(cfg *api.Config, clusterAPI, context, username, token string) error { +func addContext(cfg *api.Config, clusterAPI, context, username, token, namespace string) error { host, err := hostname(clusterAPI) if err != nil { return err @@ -160,7 +160,7 @@ func addContext(cfg *api.Config, clusterAPI, context, username, token string) er cfg.Contexts[context] = &api.Context{ Cluster: host, AuthInfo: clusterUser, - Namespace: "default", + Namespace: namespace, } return nil } diff --git a/pkg/crc/machine/kubeconfig_test.go b/pkg/crc/machine/kubeconfig_test.go index 49d09b8204..3be2f2ced6 100644 --- a/pkg/crc/machine/kubeconfig_test.go +++ b/pkg/crc/machine/kubeconfig_test.go @@ -160,29 +160,40 @@ func Test_addContext(t *testing.T) { username string context string token string + namespace string + } + + type expected struct { + user string + namespace string } tests := []struct { in input - expected string + expected expected }{ { - input{"https://abcdd.api.com", "foo", "foo@abcdd", "secretToken"}, - "foo/abcdd-api-com", + input{"https://abcdd.api.com", "foo", "foo@abcdd", "secretToken", "kube-system"}, + expected{"foo/abcdd-api-com", "kube-system"}, + }, + { + input{"https://api.crc.testing:6443", "kubeadmin", "kubeadm", "secretToken", "default"}, + expected{"kubeadmin/api-crc-testing:6443", "default"}, }, { - input{"https://api.crc.testing:6443", "kubeadmin", "kubeadm", "secretToken"}, - "kubeadmin/api-crc-testing:6443", + input{"https://api.crc.testing:6443", "kubeadmin", "kubeadm", "secretToken", ""}, + expected{"kubeadmin/api-crc-testing:6443", ""}, }, } cfg := api.NewConfig() for _, tt := range tests { - err := addContext(cfg, tt.in.clusterAPI, tt.in.context, tt.in.username, tt.in.token) + err := addContext(cfg, tt.in.clusterAPI, tt.in.context, tt.in.username, tt.in.token, tt.in.namespace) assert.NoError(t, err) assert.Contains(t, cfg.Contexts, tt.in.context, "Expected context not found") - assert.Contains(t, cfg.AuthInfos, tt.expected, "Expected AuthInfo not found") - assert.Contains(t, cfg.AuthInfos[tt.expected].Token, tt.in.token, "Expected token not found") + assert.Equal(t, cfg.Contexts[tt.in.context].Namespace, tt.expected.namespace, "Expected namespace not found") + assert.Contains(t, cfg.AuthInfos, tt.expected.user, "Expected AuthInfo not found") + assert.Contains(t, cfg.AuthInfos[tt.expected.user].Token, tt.in.token, "Expected token not found") } }