From d95526def14555ca24364401ae8681f69b1e2a6f Mon Sep 17 00:00:00 2001 From: Bekir Dogan Date: Tue, 29 Nov 2022 06:04:41 +0300 Subject: [PATCH] fix: service matching logic for pods was wrong fixes: #357 --- pkg/plugin/template_functions_dynamic.go | 34 ++++++++++++++++++++++++ pkg/plugin/templates/DaemonSet.tmpl | 2 +- pkg/plugin/templates/Pod.tmpl | 2 +- pkg/plugin/templates/StatefulSet.tmpl | 2 +- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/pkg/plugin/template_functions_dynamic.go b/pkg/plugin/template_functions_dynamic.go index 63edfd7..1aebc41 100644 --- a/pkg/plugin/template_functions_dynamic.go +++ b/pkg/plugin/template_functions_dynamic.go @@ -243,6 +243,40 @@ func (r RenderableObject) KubeGetServicesMatchingLabels(namespace string, labels } return out } +func (r RenderableObject) KubeGetServicesMatchingPod(namespace, podName string) (out []RenderableObject) { + out = make([]RenderableObject, 0) + if viper.GetBool("shallow") { + return + } + klog.V(5).InfoS("called KubeGetServicesMatchingPod", "r", r, "namespace", namespace, "podName", podName) + clientSet, _ := r.engine.f.KubernetesClientSet() + endpoints, err := clientSet.CoreV1().Endpoints(r.Namespace()).List(context.TODO(), metav1.ListOptions{}) + if err != nil { + klog.V(3).ErrorS(err, "error listing endpoints", "r", r, "namespace", namespace) + return + } + for _, ep := range endpoints.Items { + matched := false + for _, subset := range ep.Subsets { + for _, address := range subset.Addresses { + if address.TargetRef != nil && address.TargetRef.Kind == "Pod" && address.TargetRef.Name == podName { + matched = true + } + } + } + if matched { + svc, err := clientSet.CoreV1().Services(r.Namespace()).Get(context.TODO(), ep.Name, metav1.GetOptions{}) + if err != nil { + klog.V(3).ErrorS(err, "error getting matching service", "r", r, "namespace", namespace, "name", ep.Name) + continue + } + svc.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Service")) + svcUnstructured, _ := runtime.DefaultUnstructuredConverter.ToUnstructured(&svc) + out = append(out, r.newRenderableObject(svcUnstructured)) + } + } + return out +} func doesServiceMatchLabels(svc corev1.Service, labels map[string]string) bool { if svc.Spec.Type == "ExternalName" { diff --git a/pkg/plugin/templates/DaemonSet.tmpl b/pkg/plugin/templates/DaemonSet.tmpl index 829b19d..b3a4993 100644 --- a/pkg/plugin/templates/DaemonSet.tmpl +++ b/pkg/plugin/templates/DaemonSet.tmpl @@ -36,7 +36,7 @@ {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}} {{- $sectionHeader := false }} {{- $previousRevision := "" }} - {{- range .KubeGetByLabelsMap .Namespace "controllerrevisions" .Metadata.labels }} + {{- range .KubeGetByLabelsMap .Namespace "controllerrevisions" .Labels }} {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}} {{- if eq (index .Metadata.ownerReferences 0).name $.Name }} {{- if not $sectionHeader }} diff --git a/pkg/plugin/templates/Pod.tmpl b/pkg/plugin/templates/Pod.tmpl index 4285d6a..a2b9023 100644 --- a/pkg/plugin/templates/Pod.tmpl +++ b/pkg/plugin/templates/Pod.tmpl @@ -103,7 +103,7 @@ {{- define "matching_services" }} {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}} {{- if .Config.GetBool "include-matching-services" }} - {{- range $index, $svc := .KubeGetServicesMatchingLabels .Namespace .Labels }} + {{- range $index, $svc := .KubeGetServicesMatchingPod .Namespace .Name }} {{- if eq $index 0 }} {{- "Services matching this pod:" | nindent 2 }} {{- end }} diff --git a/pkg/plugin/templates/StatefulSet.tmpl b/pkg/plugin/templates/StatefulSet.tmpl index d730842..95d67de 100644 --- a/pkg/plugin/templates/StatefulSet.tmpl +++ b/pkg/plugin/templates/StatefulSet.tmpl @@ -51,7 +51,7 @@ {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}} {{- $sectionHeader := false }} {{- $previousRevision := "" }} - {{- range .KubeGetByLabelsMap .Namespace "controllerrevisions" .Metadata.labels }} + {{- range .KubeGetByLabelsMap .Namespace "controllerrevisions" .Labels }} {{- /*gotype: github.com/bergerx/kubectl-status/pkg/plugin.RenderableObject*/ -}} {{- if eq (index .Metadata.ownerReferences 0).name $.Name }} {{- if not $sectionHeader }}