-
Notifications
You must be signed in to change notification settings - Fork 24
move owner fetcher from member-operator #496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MatousJobanek
merged 4 commits into
codeready-toolchain:master
from
MatousJobanek:owner-fetcher
Nov 3, 2025
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package owners | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
| "k8s.io/apimachinery/pkg/runtime/schema" | ||
| "k8s.io/client-go/discovery" | ||
| "k8s.io/client-go/dynamic" | ||
| ) | ||
|
|
||
| // OwnerFetcher fetches the owner references of Kubernetes objects by traversing | ||
| // the owner reference chain up to the top-level owner. | ||
| type OwnerFetcher struct { | ||
| resourceLists []*metav1.APIResourceList // All available API in the cluster | ||
| discoveryClient discovery.ServerResourcesInterface | ||
| dynamicClient dynamic.Interface | ||
| } | ||
|
|
||
| // NewOwnerFetcher creates a new OwnerFetcher with the provided discovery and dynamic clients. | ||
| // The discovery client is used to fetch available API resources, and the dynamic client is used | ||
| // to retrieve owner objects from the cluster. | ||
| func NewOwnerFetcher(discoveryClient discovery.ServerResourcesInterface, dynamicClient dynamic.Interface) *OwnerFetcher { | ||
| return &OwnerFetcher{ | ||
| discoveryClient: discoveryClient, | ||
| dynamicClient: dynamicClient, | ||
| } | ||
| } | ||
|
|
||
| // ObjectWithGVR contains an unstructured Kubernetes object along with its | ||
| // GroupVersionResource (GVR) for identifying the resource type. | ||
| type ObjectWithGVR struct { | ||
| Object *unstructured.Unstructured | ||
| GVR *schema.GroupVersionResource | ||
| } | ||
|
|
||
| // GetOwners recursively retrieves all owner references for the given object, starting from | ||
| // the immediate owner up to the top-level owner. It returns a slice of ObjectWithGVR in order | ||
| // from top-level owner to immediate owner. Returns nil if the object has no owner. | ||
| func (o *OwnerFetcher) GetOwners(ctx context.Context, obj metav1.Object) ([]*ObjectWithGVR, error) { | ||
| if o.resourceLists == nil { | ||
| // Get all API resources from the cluster using the discovery client. We need it for constructing GVRs for unstructured objects. | ||
| // Do it here once, so we do not have to list it multiple times before listing/getting every unstructured resource. | ||
| resourceLists, err := o.discoveryClient.ServerPreferredResources() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| o.resourceLists = resourceLists | ||
| } | ||
|
|
||
| // get the controller owner (it's possible to have only one controller owner) | ||
| owners := obj.GetOwnerReferences() | ||
| var ownerReference metav1.OwnerReference | ||
| var nonControllerOwner metav1.OwnerReference | ||
| for _, ownerRef := range owners { | ||
| // try to get the controller owner as the preferred one | ||
| if ownerRef.Controller != nil && *ownerRef.Controller { | ||
| ownerReference = ownerRef | ||
| break | ||
| } else if nonControllerOwner.Name == "" { | ||
| // take only the first non-controller owner | ||
| nonControllerOwner = ownerRef | ||
| } | ||
| } | ||
| // if no controller owner was found, then use the first non-controller owner (if present) | ||
| if ownerReference.Name == "" { | ||
| ownerReference = nonControllerOwner | ||
| } | ||
| if ownerReference.Name == "" { | ||
| return nil, nil // No owner | ||
| } | ||
| // Get the GVR for the owner | ||
| gvr, err := gvrForKind(ownerReference.Kind, ownerReference.APIVersion, o.resourceLists) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| // Get the owner object | ||
| ownerObject, err := o.dynamicClient.Resource(*gvr).Namespace(obj.GetNamespace()).Get(ctx, ownerReference.Name, metav1.GetOptions{}) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| owner := &ObjectWithGVR{ | ||
| Object: ownerObject, | ||
| GVR: gvr, | ||
| } | ||
| // Recursively try to find the top owner | ||
| ownerOwners, err := o.GetOwners(ctx, ownerObject) | ||
| if err != nil || ownerOwners == nil { | ||
| return append(ownerOwners, owner), err | ||
| } | ||
| return append(ownerOwners, owner), nil | ||
| } | ||
|
|
||
| // gvrForKind returns GVR for the kind, if it's found in the available API list in the cluster | ||
| // returns an error if not found or failed to parse the API version | ||
| func gvrForKind(kind, apiVersion string, resourceLists []*metav1.APIResourceList) (*schema.GroupVersionResource, error) { | ||
| gvr, err := findGVRForKind(kind, apiVersion, resourceLists) | ||
| if gvr == nil && err == nil { | ||
| return nil, fmt.Errorf("no resource found for kind %s in %s", kind, apiVersion) | ||
| } | ||
| return gvr, err | ||
| } | ||
|
|
||
| // findGVRForKind returns GVR for the kind, if it's found in the available API list in the cluster | ||
| // if not found then returns nil, nil | ||
| // returns nil, error if failed to parse the API version | ||
| func findGVRForKind(kind, apiVersion string, resourceLists []*metav1.APIResourceList) (*schema.GroupVersionResource, error) { | ||
| // Parse the group and version from the APIVersion (e.g., "apps/v1" -> group: "apps", version: "v1") | ||
| gv, err := schema.ParseGroupVersion(apiVersion) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse APIVersion %s: %w", apiVersion, err) | ||
| } | ||
|
|
||
| // Look for a matching resource | ||
| for _, resourceList := range resourceLists { | ||
| if resourceList.GroupVersion == apiVersion { | ||
| for _, apiResource := range resourceList.APIResources { | ||
| if apiResource.Kind == kind { | ||
| // Construct the GVR | ||
| return &schema.GroupVersionResource{ | ||
| Group: gv.Group, | ||
| Version: gv.Version, | ||
| Resource: apiResource.Name, | ||
| }, nil | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nil, nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.