From 449cfe7c2e9a8698a0c832d9d4f5bada7bd71eb3 Mon Sep 17 00:00:00 2001 From: Akshay Pant Date: Fri, 10 Oct 2025 18:03:53 +0530 Subject: [PATCH 1/2] fix(cli): correct namespace flag binding in tkn-pac - The --namespace/-n flag was incorrectly bound to the same variable as --kubeconfig, causing namespace values to overwrite the kubeconfig path - Fixed template condition to display repos when count > 0 - Added tests for using kube flags with tkn-pac Jira: https://issues.redhat.com/browse/SRVKP-7152 Signed-off-by: Akshay Pant Assisted-by: Claude-Sonnet-4.5 (via Cursor) --- pkg/cmd/tknpac/info/templates/info.tmpl | 2 +- pkg/params/info/kube.go | 2 +- pkg/params/info/kube_test.go | 52 +++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/pkg/cmd/tknpac/info/templates/info.tmpl b/pkg/cmd/tknpac/info/templates/info.tmpl index 020a2fe0e9..2145f3652a 100644 --- a/pkg/cmd/tknpac/info/templates/info.tmpl +++ b/pkg/cmd/tknpac/info/templates/info.tmpl @@ -14,7 +14,7 @@ Webhook URL: {{ .Info.HookConfig.URL }} {{- end }} {{- end }} -{{- if (gt (len .Repos) 1) }} +{{- if (gt (len .Repos) 0) }} {{ $.CS.Underline "Repositories CR" }}: {{ len .Repos }} Namespace URL diff --git a/pkg/params/info/kube.go b/pkg/params/info/kube.go index b415eb9fe3..c48ca979ac 100644 --- a/pkg/params/info/kube.go +++ b/pkg/params/info/kube.go @@ -36,7 +36,7 @@ func (k *KubeOpts) AddFlags(cmd *cobra.Command) { fmt.Sprintf("Path to the kubeconfig file to use for CLI requests (default: %s)", envkconfig)) cmd.PersistentFlags().StringVarP( - &k.ConfigPath, + &k.Namespace, "namespace", "n", "", "If present, the namespace scope for this CLI request") } diff --git a/pkg/params/info/kube_test.go b/pkg/params/info/kube_test.go index ac193e1809..353d895465 100644 --- a/pkg/params/info/kube_test.go +++ b/pkg/params/info/kube_test.go @@ -50,3 +50,55 @@ func TestKubeOptsWithEnv(t *testing.T) { }) } } + +func TestKubeOptsFlags(t *testing.T) { + testcases := []struct { + name string + flags []string + expectNS string + expectCfg string + }{ + { + name: "namespace flag only", + flags: []string{"--namespace", "test-ns"}, + expectNS: "test-ns", + expectCfg: "", + }, + { + name: "namespace flag short form", + flags: []string{"-n", "test-ns"}, + expectNS: "test-ns", + expectCfg: "", + }, + { + name: "kubeconfig flag only", + flags: []string{"--kubeconfig", "/home/user/.kube/config"}, + expectNS: "", + expectCfg: "/home/user/.kube/config", + }, + { + name: "both flags together", + flags: []string{"--namespace", "test-ns", "--kubeconfig", "/home/user/.kube/config"}, + expectNS: "test-ns", + expectCfg: "/home/user/.kube/config", + }, + { + name: "both flags short form", + flags: []string{"-n", "test-ns", "-k", "/home/user/.kube/config"}, + expectNS: "test-ns", + expectCfg: "/home/user/.kube/config", + }, + } + for _, tc := range testcases { + t.Run(tc.name, func(t *testing.T) { + k := &KubeOpts{} + cmd := &cobra.Command{} + k.AddFlags(cmd) + assert.NilError(t, cmd.ParseFlags(tc.flags)) + assert.Equal(t, k.Namespace, tc.expectNS, "namespace mismatch") + if tc.expectCfg != "" { + assert.Equal(t, k.ConfigPath, tc.expectCfg, "config path mismatch") + } + }) + } +} From 8b866063abc2f437a9c75bef858af760988a80ee Mon Sep 17 00:00:00 2001 From: Akshay Pant Date: Mon, 13 Oct 2025 11:07:30 +0530 Subject: [PATCH 2/2] test: Update kube_opts_flags tests to use dynamic kubeconfig paths - Set environment variables for HOME and KUBECONFIG in the test. - Updated test cases to use dynamic kubeconfig paths instead of hardcoded values. - Ensured that the expected configuration path reflects the changes in the test cases. This improves the flexibility and maintainability of the tests. Signed-off-by: Akshay Pant Assisted-by: Gemini gemini@google.com --- pkg/params/info/kube_test.go | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/pkg/params/info/kube_test.go b/pkg/params/info/kube_test.go index 353d895465..d22e5c46d8 100644 --- a/pkg/params/info/kube_test.go +++ b/pkg/params/info/kube_test.go @@ -52,6 +52,11 @@ func TestKubeOptsWithEnv(t *testing.T) { } func TestKubeOptsFlags(t *testing.T) { + t.Setenv("HOME", "/home/user") + t.Setenv("KUBECONFIG", "") + defaultKubeconfigPath := "/home/user/.kube/config" + customKubeconfigPath := "/custom/kube/config" + testcases := []struct { name string flags []string @@ -62,31 +67,31 @@ func TestKubeOptsFlags(t *testing.T) { name: "namespace flag only", flags: []string{"--namespace", "test-ns"}, expectNS: "test-ns", - expectCfg: "", + expectCfg: defaultKubeconfigPath, }, { name: "namespace flag short form", flags: []string{"-n", "test-ns"}, expectNS: "test-ns", - expectCfg: "", + expectCfg: defaultKubeconfigPath, }, { name: "kubeconfig flag only", - flags: []string{"--kubeconfig", "/home/user/.kube/config"}, + flags: []string{"--kubeconfig", customKubeconfigPath}, expectNS: "", - expectCfg: "/home/user/.kube/config", + expectCfg: customKubeconfigPath, }, { name: "both flags together", - flags: []string{"--namespace", "test-ns", "--kubeconfig", "/home/user/.kube/config"}, + flags: []string{"--namespace", "test-ns", "--kubeconfig", customKubeconfigPath}, expectNS: "test-ns", - expectCfg: "/home/user/.kube/config", + expectCfg: customKubeconfigPath, }, { name: "both flags short form", - flags: []string{"-n", "test-ns", "-k", "/home/user/.kube/config"}, + flags: []string{"-n", "test-ns", "-k", customKubeconfigPath}, expectNS: "test-ns", - expectCfg: "/home/user/.kube/config", + expectCfg: customKubeconfigPath, }, } for _, tc := range testcases { @@ -96,9 +101,7 @@ func TestKubeOptsFlags(t *testing.T) { k.AddFlags(cmd) assert.NilError(t, cmd.ParseFlags(tc.flags)) assert.Equal(t, k.Namespace, tc.expectNS, "namespace mismatch") - if tc.expectCfg != "" { - assert.Equal(t, k.ConfigPath, tc.expectCfg, "config path mismatch") - } + assert.Equal(t, k.ConfigPath, tc.expectCfg, "config path mismatch") }) } }