Skip to content

Commit 2119f72

Browse files
Merge pull request #62 from rksharma95/fix-nodelist-id
fix(server): use pod-name:pod-ip as node identity for kubearmor daemon
2 parents 8499e6f + 9f576e6 commit 2119f72

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

relay-server/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ require (
1313
github.com/cenkalti/backoff/v4 v4.2.1
1414
github.com/dustin/go-humanize v1.0.1
1515
github.com/elastic/go-elasticsearch/v7 v7.17.10
16-
github.com/golang/protobuf v1.5.4
1716
github.com/google/uuid v1.6.0
1817
github.com/kubearmor/KubeArmor/KubeArmor v0.0.0-20240412061210-e4422dd02342
1918
github.com/kubearmor/KubeArmor/protobuf v0.0.0-20240315075053-fee50c9428b9
@@ -35,6 +34,7 @@ require (
3534
github.com/go-openapi/jsonreference v0.21.0 // indirect
3635
github.com/go-openapi/swag v0.23.0 // indirect
3736
github.com/gogo/protobuf v1.3.2 // indirect
37+
github.com/golang/protobuf v1.5.4 // indirect
3838
github.com/google/gnostic-models v0.6.9-0.20230804172637-c7be7c783f49 // indirect
3939
github.com/google/go-cmp v0.6.0 // indirect
4040
github.com/google/gofuzz v1.2.0 // indirect

relay-server/server/k8sHandler.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ func (kh *K8sHandler) getKaPodInformer(ipsChan chan string) cache.SharedIndexInf
264264
}
265265

266266
if pod.Status.PodIP != "" {
267-
ipsChan <- pod.Status.PodIP
267+
// generate id <pod-name>:<pod-ip>
268+
ipsChan <- generateID(pod.Name, pod.Status.PodIP)
268269
}
269270
},
270271
UpdateFunc: func(old, new interface{}) {
@@ -279,8 +280,10 @@ func (kh *K8sHandler) getKaPodInformer(ipsChan chan string) cache.SharedIndexInf
279280
}
280281

281282
if newPod.Status.PodIP != "" && newPod.Status.PodIP != oldPod.Status.PodIP {
282-
ipsChan <- newPod.Status.PodIP
283-
DeleteClientEntry(oldPod.Status.PodIP)
283+
if oldPod.Status.PodIP != "" {
284+
DeleteClientEntry(generateID(oldPod.Name, oldPod.Status.PodIP))
285+
}
286+
ipsChan <- generateID(newPod.Name, newPod.Status.PodIP)
284287
}
285288
},
286289
DeleteFunc: func(obj interface{}) {
@@ -290,7 +293,7 @@ func (kh *K8sHandler) getKaPodInformer(ipsChan chan string) cache.SharedIndexInf
290293
}
291294

292295
if pod.Status.PodIP != "" {
293-
DeleteClientEntry(pod.Status.PodIP)
296+
DeleteClientEntry(generateID(pod.Name, pod.Status.PodIP))
294297
}
295298
},
296299
})
@@ -310,7 +313,23 @@ func (kh *K8sHandler) findExistingKaPodsIp(ctx context.Context, ipsChan chan str
310313

311314
for _, pod := range pods.Items {
312315
if pod.Status.PodIP != "" {
313-
ipsChan <- pod.Status.PodIP
316+
ipsChan <- generateID(pod.Name, pod.Status.PodIP)
314317
}
315318
}
316319
}
320+
321+
// ===========
322+
// == utils ==
323+
// ===========
324+
325+
func generateID(podName, podIP string) string {
326+
return fmt.Sprintf("%s:%s", podName, podIP)
327+
}
328+
329+
func extractIP(podID string) (string, error) {
330+
id := strings.Split(podID, ":")
331+
if len(id) != 2 {
332+
return "", fmt.Errorf("invalid ID format: %s", podID)
333+
}
334+
return id[1], nil
335+
}

relay-server/server/relayServer.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,14 +712,18 @@ func DeleteClientEntry(nodeIP string) {
712712
// == KubeArmor == //
713713
// =============== //
714714

715-
func connectToKubeArmor(nodeIP, port string) error {
715+
func connectToKubeArmor(nodeID, port string) error {
716716

717+
nodeIP, err := extractIP(nodeID)
718+
if err != nil {
719+
return err
720+
}
717721
// create connection info
718722
server := nodeIP + ":" + port
719723

720724
for Running {
721725
ClientListLock.RLock()
722-
_, found := ClientList[nodeIP]
726+
_, found := ClientList[nodeID]
723727
ClientListLock.RUnlock()
724728
if !found {
725729
// KubeArmor with this IP is deleted or the IP has changed

0 commit comments

Comments
 (0)