From 32d5cc43b8fcb3e37a209197adbc48ad5273c5e0 Mon Sep 17 00:00:00 2001 From: Lior Noy Date: Thu, 20 Jun 2024 17:15:01 +0300 Subject: [PATCH] Fix the pod name for comDetails Modifies the function for creating comDetails, so the pod name will be more accurate. Signed-off-by: Lior Noy --- endpointslices/endpointslices.go | 38 +++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/endpointslices/endpointslices.go b/endpointslices/endpointslices.go index 274d35d..d619ef7 100644 --- a/endpointslices/endpointslices.go +++ b/endpointslices/endpointslices.go @@ -3,6 +3,7 @@ package endpointslices import ( "context" "fmt" + "strings" log "github.com/sirupsen/logrus" corev1 "k8s.io/api/core/v1" @@ -200,6 +201,38 @@ func getContainerName(portNum int, pods []corev1.Pod) (string, error) { return res, nil } +func getPodName(pod *corev1.Pod) (string, error) { + var ( + res string + found bool + ) + + if len(pod.OwnerReferences) == 0 { + res, found = strings.CutSuffix(pod.Name, pod.Spec.NodeName) + if !found { + return "", fmt.Errorf("pod name %s is not ending with node name %s", pod.Name, pod.Spec.NodeName) + } + + return res, nil + } + + name := pod.OwnerReferences[0].Name + switch pod.OwnerReferences[0].Kind { + case "Node": + res, found = strings.CutSuffix(pod.Name, pod.Spec.NodeName) + if !found { + return "", fmt.Errorf("pod name %s is not ending with node name %s", pod.Name, pod.Spec.NodeName) + } + case "ReplicaSet": + a := strings.Split(name, "-") + res = strings.Join(a[:len(a)-1], "-") + default: + res = name + } + + return res, nil +} + func (epSliceinfo *EndpointSlicesInfo) toComDetails(nodes []corev1.Node) ([]types.ComDetails, error) { if len(epSliceinfo.EndpointSlice.OwnerReferences) == 0 { return nil, fmt.Errorf("empty OwnerReferences in EndpointSlice %s/%s. skipping", epSliceinfo.EndpointSlice.Namespace, epSliceinfo.EndpointSlice.Name) @@ -209,7 +242,10 @@ func (epSliceinfo *EndpointSlicesInfo) toComDetails(nodes []corev1.Node) ([]type // Get the Namespace and Pod's name from the service. namespace := epSliceinfo.Service.Namespace - name := epSliceinfo.EndpointSlice.OwnerReferences[0].Name + name, err := getPodName(&epSliceinfo.Pods[0]) + if err != nil { + return nil, fmt.Errorf("failed to get pod name for endpointslice %s: %w", epSliceinfo.EndpointSlice.Name, err) + } // Get the node roles of this endpointslice. (master or worker or both). roles := getEndpointSliceNodeRoles(epSliceinfo, nodes)