@@ -175,8 +175,14 @@ func (o *RunOptions) Complete(factory factory.Factory, cmd *cobra.Command, args
175
175
176
176
// Check we got a pod or a node
177
177
o .isPod = false
178
+
179
+ var node * v1.Node
180
+
178
181
switch v := obj .(type ) {
179
182
case * v1.Pod :
183
+ if len (v .Spec .NodeName ) == 0 {
184
+ return fmt .Errorf ("cannot attach a trace program to a pod that is not currently scheduled on a node" )
185
+ }
180
186
o .isPod = true
181
187
found := false
182
188
o .podUID = string (v .UID )
@@ -194,27 +200,43 @@ func (o *RunOptions) Complete(factory factory.Factory, cmd *cobra.Command, args
194
200
}
195
201
}
196
202
197
- if len (v .Spec .NodeName ) == 0 {
198
- return fmt .Errorf ("cannot attach a trace program to a pod that is not currently scheduled on a node" )
199
- }
200
- o .nodeName = v .Spec .NodeName
201
-
202
203
if ! found {
203
204
return fmt .Errorf ("no containers found for the provided pod/container combination" )
204
205
}
206
+
207
+ obj , err = factory .
208
+ NewBuilder ().
209
+ WithScheme (scheme .Scheme , scheme .Scheme .PrioritizedVersionsAllGroups ()... ).
210
+ ResourceNames ("nodes" , v .Spec .NodeName ).
211
+ Do ().Object ()
212
+
213
+ if err != nil {
214
+ return err
215
+ }
216
+
217
+ if n , ok := obj .(* v1.Node ); ok {
218
+ node = n
219
+ }
220
+
205
221
break
206
222
case * v1.Node :
207
- labels := v .GetLabels ()
208
- val , ok := labels ["kubernetes.io/hostname" ]
209
- if ! ok {
210
- return fmt .Errorf ("label kubernetes.io/hostname not found in node" )
211
- }
212
- o .nodeName = val
223
+ node = v
213
224
break
214
225
default :
215
226
return fmt .Errorf ("first argument must be %s" , usageString )
216
227
}
217
228
229
+ if node == nil {
230
+ return fmt .Errorf ("could not determine on which node to run the trace program" )
231
+ }
232
+
233
+ labels := node .GetLabels ()
234
+ val , ok := labels ["kubernetes.io/hostname" ]
235
+ if ! ok {
236
+ return fmt .Errorf ("label kubernetes.io/hostname not found in node" )
237
+ }
238
+ o .nodeName = val
239
+
218
240
// Prepare client
219
241
o .clientConfig , err = factory .ToRESTConfig ()
220
242
if err != nil {
0 commit comments