@@ -75,6 +75,10 @@ func registerCompletionFuncForFlags(cmd *cobra.Command, o *options) error {
75
75
return err
76
76
}
77
77
78
+ if err := cmd .RegisterFlagCompletionFunc ("node" , nodeCompletionFunc (o )); err != nil {
79
+ return err
80
+ }
81
+
78
82
if err := cmd .RegisterFlagCompletionFunc ("context" , contextCompletionFunc (o )); err != nil {
79
83
return err
80
84
}
@@ -116,6 +120,32 @@ func namespaceCompletionFunc(o *options) func(cmd *cobra.Command, args []string,
116
120
}
117
121
}
118
122
123
+ // nodeCompletionFunc is a completion function that completes node names
124
+ // that match the toComplete prefix.
125
+ func nodeCompletionFunc (o * options ) func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
126
+ return func (_ * cobra.Command , _ []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
127
+ clientConfig := kubernetes .NewClientConfig (o .kubeConfig , o .context )
128
+ clientset , err := kubernetes .NewClientSet (clientConfig )
129
+ if err != nil {
130
+ return compError (err )
131
+ }
132
+
133
+ nodeList , err := clientset .CoreV1 ().Nodes ().List (context .TODO (), metav1.ListOptions {})
134
+ if err != nil {
135
+ return compError (err )
136
+ }
137
+
138
+ var comps []string
139
+ for _ , node := range nodeList .Items {
140
+ if strings .HasPrefix (node .GetName (), toComplete ) {
141
+ comps = append (comps , node .GetName ())
142
+ }
143
+ }
144
+
145
+ return comps , cobra .ShellCompDirectiveNoFileComp
146
+ }
147
+ }
148
+
119
149
// contextCompletionFunc is a completion function that completes contexts
120
150
// that match the toComplete prefix.
121
151
func contextCompletionFunc (o * options ) func (cmd * cobra.Command , args []string , toComplete string ) ([]string , cobra.ShellCompDirective ) {
0 commit comments