Skip to content

Commit 3e1516c

Browse files
committed
feat(cmd/run): resolve node for both pod and node
Signed-off-by: Lorenzo Fontana <[email protected]>
1 parent c51504d commit 3e1516c

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

pkg/cmd/run.go

+33-11
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,14 @@ func (o *RunOptions) Complete(factory factory.Factory, cmd *cobra.Command, args
175175

176176
// Check we got a pod or a node
177177
o.isPod = false
178+
179+
var node *v1.Node
180+
178181
switch v := obj.(type) {
179182
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+
}
180186
o.isPod = true
181187
found := false
182188
o.podUID = string(v.UID)
@@ -194,27 +200,43 @@ func (o *RunOptions) Complete(factory factory.Factory, cmd *cobra.Command, args
194200
}
195201
}
196202

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-
202203
if !found {
203204
return fmt.Errorf("no containers found for the provided pod/container combination")
204205
}
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+
205221
break
206222
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
213224
break
214225
default:
215226
return fmt.Errorf("first argument must be %s", usageString)
216227
}
217228

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+
218240
// Prepare client
219241
o.clientConfig, err = factory.ToRESTConfig()
220242
if err != nil {

0 commit comments

Comments
 (0)