Skip to content

Commit f8c4367

Browse files
fix(cli): correct namespace flag binding in tkn-pac (#2284)
* 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 <akshay.akshaypant@gmail.com> Assisted-by: Claude-Sonnet-4.5 (via Cursor)
1 parent f28aac7 commit f8c4367

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

pkg/cmd/tknpac/info/templates/info.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
Webhook URL: {{ .Info.HookConfig.URL }}
1515
{{- end }}
1616
{{- end }}
17-
{{- if (gt (len .Repos) 1) }}
17+
{{- if (gt (len .Repos) 0) }}
1818

1919
{{ $.CS.Underline "Repositories CR" }}: {{ len .Repos }}
2020
Namespace URL

pkg/params/info/kube.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (k *KubeOpts) AddFlags(cmd *cobra.Command) {
3636
fmt.Sprintf("Path to the kubeconfig file to use for CLI requests (default: %s)", envkconfig))
3737

3838
cmd.PersistentFlags().StringVarP(
39-
&k.ConfigPath,
39+
&k.Namespace,
4040
"namespace", "n", "",
4141
"If present, the namespace scope for this CLI request")
4242
}

pkg/params/info/kube_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,58 @@ func TestKubeOptsWithEnv(t *testing.T) {
5050
})
5151
}
5252
}
53+
54+
func TestKubeOptsFlags(t *testing.T) {
55+
t.Setenv("HOME", "/home/user")
56+
t.Setenv("KUBECONFIG", "")
57+
defaultKubeconfigPath := "/home/user/.kube/config"
58+
customKubeconfigPath := "/custom/kube/config"
59+
60+
testcases := []struct {
61+
name string
62+
flags []string
63+
expectNS string
64+
expectCfg string
65+
}{
66+
{
67+
name: "namespace flag only",
68+
flags: []string{"--namespace", "test-ns"},
69+
expectNS: "test-ns",
70+
expectCfg: defaultKubeconfigPath,
71+
},
72+
{
73+
name: "namespace flag short form",
74+
flags: []string{"-n", "test-ns"},
75+
expectNS: "test-ns",
76+
expectCfg: defaultKubeconfigPath,
77+
},
78+
{
79+
name: "kubeconfig flag only",
80+
flags: []string{"--kubeconfig", customKubeconfigPath},
81+
expectNS: "",
82+
expectCfg: customKubeconfigPath,
83+
},
84+
{
85+
name: "both flags together",
86+
flags: []string{"--namespace", "test-ns", "--kubeconfig", customKubeconfigPath},
87+
expectNS: "test-ns",
88+
expectCfg: customKubeconfigPath,
89+
},
90+
{
91+
name: "both flags short form",
92+
flags: []string{"-n", "test-ns", "-k", customKubeconfigPath},
93+
expectNS: "test-ns",
94+
expectCfg: customKubeconfigPath,
95+
},
96+
}
97+
for _, tc := range testcases {
98+
t.Run(tc.name, func(t *testing.T) {
99+
k := &KubeOpts{}
100+
cmd := &cobra.Command{}
101+
k.AddFlags(cmd)
102+
assert.NilError(t, cmd.ParseFlags(tc.flags))
103+
assert.Equal(t, k.Namespace, tc.expectNS, "namespace mismatch")
104+
assert.Equal(t, k.ConfigPath, tc.expectCfg, "config path mismatch")
105+
})
106+
}
107+
}

0 commit comments

Comments
 (0)