Skip to content

Commit 50f0c4d

Browse files
authored
Add running pods to webhook notifications (#388)
1 parent e9fc5bd commit 50f0c4d

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

cmd/node-termination-handler.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,12 @@ func drainOrCordonIfNecessary(interruptionEventStore *interruptioneventstore.Sto
296296
}
297297
} else {
298298
log.Log().Str("node_name", nodeName).Msg("Node successfully cordoned")
299-
err = node.LogPods(nodeName)
299+
podNameList, err := node.FetchPodNameList(nodeName)
300+
if err != nil {
301+
log.Log().Err(err).Msgf("Unable to fetch running pods for node '%s' ", nodeName)
302+
}
303+
drainEvent.Pods = podNameList
304+
err = node.LogPods(podNameList, nodeName)
300305
if err != nil {
301306
log.Log().Err(err).Msg("There was a problem while trying to log all pod names on the node")
302307
}

pkg/monitor/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type InterruptionEvent struct {
3131
AutoScalingGroupName string
3232
NodeName string
3333
NodeLabels map[string]string
34+
Pods []string
3435
InstanceID string
3536
StartTime time.Time
3637
EndTime time.Time

pkg/node/node.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,20 +346,29 @@ func (n Node) TaintRebalanceRecommendation(nodeName string, eventID string) erro
346346
}
347347

348348
// LogPods logs all the pod names on a node
349-
func (n Node) LogPods(nodeName string) error {
350-
podList, err := n.fetchAllPods(nodeName)
351-
if err != nil {
352-
return fmt.Errorf("Unable to fetch all pods from API: %w", err)
353-
}
349+
func (n Node) LogPods(podList []string, nodeName string) error {
354350
podNamesArr := zerolog.Arr()
355-
for _, pod := range podList.Items {
356-
podNamesArr = podNamesArr.Str(pod.Name)
351+
for _, pod := range podList {
352+
podNamesArr = podNamesArr.Str(pod)
357353
}
358354
log.Log().Array("pod_names", podNamesArr).Str("node_name", nodeName).Msg("Pods on node")
359355

360356
return nil
361357
}
362358

359+
// FetchPodNameList fetches list of all the pods names running on given nodeName
360+
func (n Node) FetchPodNameList(nodeName string) ([]string, error) {
361+
podList, err := n.fetchAllPods(nodeName)
362+
if err != nil {
363+
return nil, err
364+
}
365+
var podNamesList []string
366+
for _, pod := range podList.Items {
367+
podNamesList = append(podNamesList, pod.Name)
368+
}
369+
return podNamesList, nil
370+
}
371+
363372
// TaintScheduledMaintenance adds the scheduled maintenance taint onto a node
364373
func (n Node) TaintScheduledMaintenance(nodeName string, eventID string) error {
365374
if !n.nthConfig.TaintNode {

pkg/node/node_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,31 @@ func TestMarkForUncordonAfterRebootAddActionLabelFailure(t *testing.T) {
240240
h.Assert(t, err != nil, "Failed to return error on MarkForUncordonAfterReboot failing to add action Label")
241241
}
242242

243+
func TestFetchPodsNameList(t *testing.T) {
244+
resetFlagsForTest()
245+
246+
client := fake.NewSimpleClientset(
247+
&v1.Pod{
248+
ObjectMeta: metav1.ObjectMeta{
249+
Name: "myPod",
250+
Labels: map[string]string{
251+
"spec.nodeName": nodeName,
252+
},
253+
},
254+
},
255+
&v1.Node{
256+
ObjectMeta: metav1.ObjectMeta{
257+
Name: nodeName,
258+
},
259+
},
260+
)
261+
262+
tNode := getNode(t, getDrainHelper(client))
263+
podList, err := tNode.FetchPodNameList(nodeName)
264+
h.Ok(t, err)
265+
h.Equals(t, []string{"myPod"}, podList)
266+
}
267+
243268
func TestLogPods(t *testing.T) {
244269
resetFlagsForTest()
245270

@@ -260,7 +285,7 @@ func TestLogPods(t *testing.T) {
260285
)
261286

262287
tNode := getNode(t, getDrainHelper(client))
263-
err := tNode.LogPods(nodeName)
288+
err := tNode.LogPods([]string{"myPod"}, nodeName)
264289
h.Ok(t, err)
265290
}
266291

0 commit comments

Comments
 (0)