diff --git a/src/shared/serviceidresolver/serviceidresolver.go b/src/shared/serviceidresolver/serviceidresolver.go index 542f61053..00db003d1 100644 --- a/src/shared/serviceidresolver/serviceidresolver.go +++ b/src/shared/serviceidresolver/serviceidresolver.go @@ -29,17 +29,26 @@ func ResolvePodToServiceIdentityUsingAnnotationOnly(pod *corev1.Pod) (string, bo } // ResolvePodToServiceIdentity resolves a pod object to its otterize service ID, referenced in intents objects. -// This is done by recursion over the pod's owner reference hierarchy until reaching a root owner reference. +// It calls getOwnerObject to recursively iterates over the pod's owner reference hierarchy until reaching a root owner reference. // In case the pod is annotated with an "intents.otterize.com/service-name" annotation, that annotation's value will override // any owner reference name as the service name. func (r *Resolver) ResolvePodToServiceIdentity(ctx context.Context, pod *corev1.Pod) (string, error) { - log := logrus.WithFields(logrus.Fields{"pod": pod.Name, "namespace": pod.Namespace}) - annotatedServiceName, ok := ResolvePodToServiceIdentityUsingAnnotationOnly(pod) if ok { return annotatedServiceName, nil } + ownerObj, err := r.getOwnerObject(ctx, pod) + if err != nil { + return "", err + } + return ownerObj.GetName(), nil +} + +// getOwnerObject recursively iterates over the pod's owner reference hierarchy until reaching a root owner reference +// and returns it. +func (r *Resolver) getOwnerObject(ctx context.Context, pod *corev1.Pod) (client.Object, error) { + log := logrus.WithFields(logrus.Fields{"pod": pod.Name, "namespace": pod.Namespace}) var obj client.Object obj = pod for len(obj.GetOwnerReferences()) > 0 { @@ -54,9 +63,10 @@ func (r *Resolver) ResolvePodToServiceIdentity(ctx context.Context, pod *corev1. log.WithFields(logrus.Fields{"owner": owner.Name, "ownerKind": obj.GetObjectKind().GroupVersionKind()}).Warning( "permission error resolving owner, will use owner object as service identifier", ) - return owner.Name, nil + ownerObj.SetName(owner.Name) + return ownerObj, nil } else if err != nil { - return "", fmt.Errorf("error querying owner reference: %w", err) + return nil, fmt.Errorf("error querying owner reference: %w", err) } // recurse parent owner reference @@ -64,5 +74,5 @@ func (r *Resolver) ResolvePodToServiceIdentity(ctx context.Context, pod *corev1. } log.WithFields(logrus.Fields{"owner": obj.GetName(), "ownerKind": obj.GetObjectKind().GroupVersionKind()}).Debug("pod resolved to owner name") - return obj.GetName(), nil + return obj, nil }