Skip to content

Commit 5af27e4

Browse files
committed
Use custom type for context key to avoid collisions
1 parent e97168d commit 5af27e4

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

main.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ func main() {
4646
}
4747
}
4848

49+
type ctxKey string
50+
51+
var kubeClientKey = ctxKey("klient")
52+
4953
func rootCmd() *cobra.Command {
5054
kflags := genericclioptions.NewConfigFlags(true)
5155

@@ -71,7 +75,7 @@ You can also list nodes for a given node pool/group by name.`,
7175
return err
7276
}
7377

74-
ctx = context.WithValue(ctx, "klient", klient)
78+
ctx = context.WithValue(ctx, kubeClientKey, klient)
7579

7680
cmd.SetContext(ctx)
7781

@@ -148,7 +152,7 @@ func listCmd() *cobra.Command {
148152
RunE: func(cmd *cobra.Command, args []string) error {
149153
ctx := cmd.Context()
150154

151-
klient := ctx.Value("klient").(kubernetes.Interface)
155+
klient := ctx.Value(kubeClientKey).(kubernetes.Interface)
152156

153157
res, err := klient.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
154158
if err != nil {
@@ -222,7 +226,7 @@ func nodesCmd() *cobra.Command {
222226

223227
ctx := cmd.Context()
224228

225-
klient := ctx.Value("klient").(kubernetes.Interface)
229+
klient := ctx.Value(kubeClientKey).(kubernetes.Interface)
226230

227231
res, err := klient.CoreV1().Nodes().List(ctx, metav1.ListOptions{})
228232
if err != nil {

0 commit comments

Comments
 (0)