Skip to content

Commit

Permalink
fix: service matching logic for pods was wrong
Browse files Browse the repository at this point in the history
fixes: #357
  • Loading branch information
bergerx committed Nov 29, 2022
1 parent 41c11f0 commit d95526d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
34 changes: 34 additions & 0 deletions pkg/plugin/template_functions_dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/templates/DaemonSet.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/templates/Pod.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugin/templates/StatefulSet.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down

0 comments on commit d95526d

Please sign in to comment.