Skip to content

Commit 82ad20e

Browse files
committed
remove passing node uid in since not possible with downward api
1 parent d08f6bd commit 82ad20e

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

azure-iptables-monitor/iptables_monitor.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ func patchNodeLabel(clientset dynamic.Interface, labelValue bool, nodeName strin
9292
}
9393

9494
// createNodeEvent creates a Kubernetes event for the specified node
95-
func createNodeEvent(clientset *kubernetes.Clientset, nodeName string, nodeUID types.UID, reason, message, eventType string) error {
95+
func createNodeEvent(clientset *kubernetes.Clientset, nodeName string, reason, message, eventType string) error {
96+
node, err := clientset.CoreV1().Nodes().Get(context.TODO(), nodeName, metav1.GetOptions{})
97+
if err != nil {
98+
return fmt.Errorf("failed to get node UID for %s: %w", nodeName, err)
99+
}
100+
96101
now := metav1.NewTime(time.Now())
97102

98103
event := &corev1.Event{
@@ -103,7 +108,7 @@ func createNodeEvent(clientset *kubernetes.Clientset, nodeName string, nodeUID t
103108
InvolvedObject: corev1.ObjectReference{
104109
Kind: "Node",
105110
Name: nodeName,
106-
UID: nodeUID, // required for event to show up in node describe
111+
UID: node.UID, // required for event to show up in node describe
107112
APIVersion: "v1",
108113
},
109114
Reason: reason,
@@ -116,7 +121,7 @@ func createNodeEvent(clientset *kubernetes.Clientset, nodeName string, nodeUID t
116121
Component: "azure-iptables-monitor",
117122
},
118123
}
119-
_, err := clientset.CoreV1().Events("default").Create(
124+
_, err = clientset.CoreV1().Events("default").Create(
120125
context.TODO(),
121126
event,
122127
metav1.CreateOptions{},
@@ -263,13 +268,6 @@ func main() {
263268
klog.Fatalf("NODE_NAME environment variable not set")
264269
}
265270

266-
// get current node uid from environment variable
267-
currentNodeUIDStr := os.Getenv("NODE_UID")
268-
if currentNodeUIDStr == "" {
269-
klog.Fatalf("NODE_UID environment variable not set")
270-
}
271-
currentNodeUID := types.UID(currentNodeUIDStr)
272-
273271
klog.Infof("Starting iptables monitor for node: %s", currentNodeName)
274272

275273
var fileReader FileLineReader = OSFileLineReader{}
@@ -286,7 +284,7 @@ func main() {
286284
}
287285

288286
if *sendEvents && userIPTablesRulesFound {
289-
err = createNodeEvent(clientset, currentNodeName, currentNodeUID, "UnexpectedIPTablesRules", "Node has unexpected iptables rules", corev1.EventTypeWarning)
287+
err = createNodeEvent(clientset, currentNodeName, "UnexpectedIPTablesRules", "Node has unexpected iptables rules", corev1.EventTypeWarning)
290288
if err != nil {
291289
klog.Errorf("failed to create event: %v", err)
292290
}

0 commit comments

Comments
 (0)