-
Notifications
You must be signed in to change notification settings - Fork 29
render: Ignore the observed copy of the XR in --observed-resources #256
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
Open
Bham06
wants to merge
1
commit into
crossplane:main
Choose a base branch
from
Bham06:fix-render-observed-xr
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /* | ||
| Copyright 2026 The Crossplane Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package render | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
|
|
||
| "github.com/crossplane/crossplane-runtime/v2/pkg/resource/unstructured/composed" | ||
| ucomposite "github.com/crossplane/crossplane-runtime/v2/pkg/resource/unstructured/composite" | ||
| ) | ||
|
|
||
| func TestFilterObservedXR(t *testing.T) { | ||
| type args struct { | ||
| xr *ucomposite.Unstructured | ||
| observed []composed.Unstructured | ||
| } | ||
|
|
||
| // composite builds a composite resource (XR) with the given identity. | ||
| composite := func(apiVersion, kind, namespace, name string) *ucomposite.Unstructured { | ||
| xr := ucomposite.New() | ||
| xr.SetAPIVersion(apiVersion) | ||
| xr.SetKind(kind) | ||
| xr.SetNamespace(namespace) | ||
| xr.SetName(name) | ||
| return xr | ||
| } | ||
|
|
||
| // observed builds an observed composed resource with the given identity. | ||
| observed := func(apiVersion, kind, namespace, name string) composed.Unstructured { | ||
| o := composed.New() | ||
| o.SetAPIVersion(apiVersion) | ||
| o.SetKind(kind) | ||
| o.SetNamespace(namespace) | ||
| o.SetName(name) | ||
| return *o | ||
| } | ||
|
|
||
| // keys projects resources to comparable "apiVersion/kind/namespace/name" | ||
| // strings so we compare identity rather than the underlying maps. | ||
| keys := func(rs []composed.Unstructured) []string { | ||
| out := make([]string, len(rs)) | ||
| for i := range rs { | ||
| out[i] = fmt.Sprintf("%s/%s/%s/%s", rs[i].GetAPIVersion(), rs[i].GetKind(), rs[i].GetNamespace(), rs[i].GetName()) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| cases := map[string]struct { | ||
| reason string | ||
| args args | ||
| want []string | ||
| }{ | ||
| "DropsObservedCopyOfXR": { | ||
| // The observed file contains a copy of the XR (as `crossplane | ||
| // render` emits it) alongside a real composed resource. Only the | ||
| // composed resource should survive. | ||
| reason: "An observed resource matching the XR's GVK, namespace and name should be dropped.", | ||
| args: args{ | ||
| xr: composite("example.org/v1", "XR", "default", "my-xr"), | ||
| observed: []composed.Unstructured{ | ||
| observed("example.org/v1", "XR", "default", "my-xr"), | ||
| observed("example.org/v1", "Composed", "default", "cd-a"), | ||
| }, | ||
| }, | ||
| want: []string{"example.org/v1/Composed/default/cd-a"}, | ||
| }, | ||
| "KeepsAllWhenNoXRCopy": { | ||
| reason: "Observed resources should be returned untouched when none of them is the XR.", | ||
| args: args{ | ||
| xr: composite("example.org/v1", "XR", "default", "my-xr"), | ||
| observed: []composed.Unstructured{ | ||
| observed("example.org/v1", "Composed", "default", "cd-a"), | ||
| observed("example.org/v1", "Composed", "default", "cd-b"), | ||
| }, | ||
| }, | ||
| want: []string{ | ||
| "example.org/v1/Composed/default/cd-a", | ||
| "example.org/v1/Composed/default/cd-b", | ||
| }, | ||
| }, | ||
| "DropsClusterScopedXRCopy": { | ||
| // Cluster-scoped XR: namespace is empty on both the XR and its copy. | ||
| reason: "An observed copy of a cluster scoped XR should be dropped, matching on an empty namespace.", | ||
| args: args{ | ||
| xr: composite("example.org/v1", "XR", "", "my-xr"), | ||
| observed: []composed.Unstructured{ | ||
| observed("example.org/v1", "XR", "", "my-xr"), | ||
| observed("example.org/v1", "Composed", "", "cd-a"), | ||
| }, | ||
| }, | ||
| want: []string{"example.org/v1/Composed//cd-a"}, | ||
| }, | ||
| "KeepsSameNameDifferentKind": { | ||
| // A resource sharing the XR's name and namespace but a different | ||
| // kind is not the XR and must be kept. | ||
| reason: "An observed resource sharing the XR's namespace and name but not its kind should be kept.", | ||
| args: args{ | ||
| xr: composite("example.org/v1", "XR", "default", "my-xr"), | ||
| observed: []composed.Unstructured{ | ||
| observed("example.org/v1", "Other", "default", "my-xr"), | ||
| }, | ||
| }, | ||
| want: []string{"example.org/v1/Other/default/my-xr"}, | ||
| }, | ||
| "EmptyObserved": { | ||
| reason: "No observed resources should produce no observed resources.", | ||
| args: args{ | ||
| xr: composite("example.org/v1", "XR", "default", "my-xr"), | ||
| observed: nil, | ||
| }, | ||
| want: []string{}, | ||
| }, | ||
| } | ||
|
|
||
| for name, tc := range cases { | ||
| t.Run(name, func(t *testing.T) { | ||
| got := filterObservedXR(tc.args.observed, tc.args.xr) | ||
| if diff := cmp.Diff(tc.want, keys(got)); diff != "" { | ||
| t.Errorf("\n%s\nfilterObservedXR(...): -want, +got:\n%s", tc.reason, diff) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
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.
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.
May be worth adding a
SameKindDifferentNamecase as well :-).