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

kubeconfig: remove default namespace from crc-developer context #4420

Merged
merged 1 commit into from
Nov 4, 2024
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
8 changes: 4 additions & 4 deletions pkg/crc/machine/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ 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
}

developerToken, err := getTokenForUser("developer", "developer", ip, ca, clusterConfig, ingressHTTPSPort)
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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens with the namespace set to "" ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with an empty namespace in the context we still get the same error, but oc config get-contexts no longer shows a namespace for the crc-developer context

% oc config get-contexts
CURRENT   NAME            CLUSTER                AUTHINFO                         NAMESPACE
          crc-admin       api-crc-testing:6443   kubeadmin/api-crc-testing:6443   default
*         crc-developer   api-crc-testing:6443   developer/api-crc-testing:6443

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not only that but now when user try to check project switch to any project then it provide better error message instead forbidden error, we should have that as part of commit log.

$ oc config get-contexts
CURRENT   NAME            CLUSTER                AUTHINFO                         NAMESPACE
          crc-admin       api-crc-testing:6443   kubeadmin/api-crc-testing:6443   default
*         crc-developer   api-crc-testing:6443   developer/api-crc-testing:6443   

$ oc project
No project has been set. Pass a project name to make that the default.

$ oc project demo
error: You are not a member of project "demo".
You are not a member of any projects. You can request a project to be created with the 'new-project' command.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are good reasons for the change, thanks for the explanations. Can this be added to the commit log?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, i've updated the commit log to contain the error messages shown after the changes in this PR

return err
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
27 changes: 19 additions & 8 deletions pkg/crc/machine/kubeconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", ""},
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anjannath we should add one more test case which have empty string as namespace which make sure the context is added to kubeconfig but without namespace.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the test

}

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")
}
}
Loading