-
Notifications
You must be signed in to change notification settings - Fork 836
fix: updating expansion templates to add owner ref in expanded resources #4262
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
base: master
Are you sure you want to change the base?
fix: updating expansion templates to add owner ref in expanded resources #4262
Conversation
Signed-off-by: Jaydip Gabani <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds owner reference metadata to resources generated through expansion templates, establishing proper parent-child relationships between source and expanded resources. This enables Kubernetes garbage collection and provides better tracking of resource ownership.
Key changes:
- New
ensureOwnerReferencefunction that adds owner references to expanded resources - Integration of owner reference creation in the expansion workflow
- Updated test fixtures to include owner references in expected outputs
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| pkg/expansion/metadata.go | New file implementing ensureOwnerReference to add parent owner references to expanded resources |
| pkg/expansion/system.go | Calls ensureOwnerReference after setting the resource name during expansion |
| pkg/expansion/system_test.go | Adds comprehensive unit tests for expandResource function covering various scenarios |
| pkg/expansion/fixtures/fixtures.go | Updates all test fixture Pod/Job/custom resource definitions to include owner references pointing to their parent resources |
pkg/expansion/metadata.go
Outdated
| ownerRef := map[string]interface{}{ | ||
| "apiVersion": parentAPIVersion, | ||
| "kind": parentKind, | ||
| "name": parentName, |
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The owner reference is missing the required uid field. According to Kubernetes API validation (k8s.io/apimachinery/pkg/api/validation.validateOwnerReference), UID is a required field for owner references and must not be empty. Without the UID, the owner reference will fail validation when the resource is submitted to the Kubernetes API server.
Add the UID field to the owner reference:
ownerRef := map[string]interface{}{
"apiVersion": parentAPIVersion,
"kind": parentKind,
"name": parentName,
"uid": string(parent.GetUID()),
}| "name": parentName, | |
| "name": parentName, | |
| "uid": string(parent.GetUID()), |
pkg/expansion/metadata.go
Outdated
| if parentAPIVersion == "" || parentKind == "" || parentName == "" { | ||
| return | ||
| } |
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The validation should also check if the parent UID is empty before proceeding. Without a UID, the owner reference would be invalid according to Kubernetes API validation requirements.
Update the validation to include UID:
parentUID := parent.GetUID()
if parentAPIVersion == "" || parentKind == "" || parentName == "" || parentUID == "" {
return
}And then use parentUID when creating the owner reference.
pkg/expansion/metadata.go
Outdated
| if refMap["apiVersion"] == parentAPIVersion && refMap["kind"] == parentKind && refMap["name"] == parentName { | ||
| return | ||
| } |
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The duplicate detection logic is incomplete. According to Kubernetes API specifications, owner references are uniquely identified by their UID, not just by apiVersion, kind, and name. Two resources with the same name could have different UIDs (e.g., after deletion and recreation).
Update the duplicate check to use UID:
if refMap["uid"] == string(parent.GetUID()) {
return
}This ensures proper deduplication based on the actual resource instance, not just its name.
pkg/expansion/metadata.go
Outdated
| } | ||
|
|
||
| refs = append(refs, ownerRef) | ||
| _ = unstructured.SetNestedSlice(resultant.Object, refs, "metadata", "ownerReferences") |
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The error from SetNestedSlice is silently ignored. If there's an issue setting the owner references (e.g., due to metadata corruption), this failure will be invisible to callers and could lead to resources being created without proper ownership tracking.
Consider either:
- Logging the error for debugging purposes, or
- Changing the function signature to return an error that can be handled by the caller
For example:
if err := unstructured.SetNestedSlice(resultant.Object, refs, "metadata", "ownerReferences"); err != nil {
// log error or return it
}
pkg/expansion/metadata.go
Outdated
| if err != nil { | ||
| return | ||
| } |
Copilot
AI
Nov 22, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The error from NestedSlice is silently ignored. While returning early on error is reasonable, silently failing could mask underlying issues with the resource's metadata structure. Consider logging this error for debugging purposes, especially since this is new functionality being added.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #4262 +/- ##
===========================================
- Coverage 54.49% 40.70% -13.79%
===========================================
Files 134 251 +117
Lines 12329 17750 +5421
===========================================
+ Hits 6719 7226 +507
- Misses 5116 9897 +4781
- Partials 494 627 +133
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Signed-off-by: Jaydip Gabani <[email protected]>
Signed-off-by: Jaydip Gabani <[email protected]>
What this PR does / why we need it:
This pull request enhances the resource expansion logic in the
pkg/expansionpackage by ensuring that generated resources correctly set theirownerReferencesmetadata to point back to their parent resource. TWhich issue(s) this PR fixes (optional, using
fixes #<issue number>(, fixes #<issue_number>, ...)format, will close the issue(s) when the PR gets merged):Fixes #4258
Special notes for your reviewer:
Is this a backward compatibility issue?
IMO it shouldn't be, but need one more ack on this.