Skip to content

Commit

Permalink
Adding owner object function (#91)
Browse files Browse the repository at this point in the history
* Adding owner object function

* Tweaks to service identity resolving
  • Loading branch information
evyatarmeged authored Dec 11, 2022
1 parent 6ff6f66 commit 5ec15b5
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/shared/serviceidresolver/serviceidresolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -54,15 +63,16 @@ 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
obj = ownerObj
}

log.WithFields(logrus.Fields{"owner": obj.GetName(), "ownerKind": obj.GetObjectKind().GroupVersionKind()}).Debug("pod resolved to owner name")
return obj.GetName(), nil
return obj, nil
}

0 comments on commit 5ec15b5

Please sign in to comment.