From 8658d9ee8170f3b77df09a5ee3ec686de0f9c956 Mon Sep 17 00:00:00 2001 From: peefy Date: Mon, 6 May 2024 00:46:50 +0800 Subject: [PATCH 1/2] fix: use resouce GVK and name instead of name Signed-off-by: peefy --- pkg/resource/res.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/resource/res.go b/pkg/resource/res.go index 69000b6..425edc9 100644 --- a/pkg/resource/res.go +++ b/pkg/resource/res.go @@ -163,7 +163,8 @@ func MatchResources(desired map[resource.Name]*resource.DesiredComposed, data [] // otherwise we lost something somewhere for _, d := range data { // PatchDesired - if found, ok := desired[resource.Name(d.GetName())]; ok { + name := getName(&d) + if found, ok := desired[resource.Name(name)]; ok { if _, ok := matches[found]; !ok { matches[found] = []map[string]interface{}{d.Object} } else { @@ -249,7 +250,7 @@ func AddResourcesTo(o any, opts *AddResourcesOptions) error { // Resources desired := val for _, d := range opts.Data { - name := resource.Name(d.GetName()) + name := resource.Name(getName(&d)) // If the value exists, merge its existing value with the patches if v, ok := desired[name]; ok { mergedData := merged(d.Object, v) @@ -473,10 +474,7 @@ func ProcessResources(dxr *resource.Composite, oxr *resource.Composite, desired meta.RemoveAnnotations(cd.Resource, AnnotationKeyReady) } // Patch desired with custom name from annotation or default to resource meta name. - name, found := cd.Resource.GetAnnotations()[AnnotationKeyCompositionResourceName] - if !found { - name = cd.Resource.GetName() - } + name := getName(&cd.Resource.Unstructured) meta.RemoveAnnotations(cd.Resource, AnnotationKeyCompositionResourceName) desired[resource.Name(name)] = cd } @@ -488,3 +486,11 @@ func ProcessResources(dxr *resource.Composite, oxr *resource.Composite, desired result.setSuccessMsgs() return result, nil } + +func getName(o *unstructured.Unstructured) string { + name, found := o.GetAnnotations()[AnnotationKeyCompositionResourceName] + if !found { + name = o.GetAPIVersion() + "-" + o.GetKind() + "-" + o.GetNamespace() + "-" + o.GetName() + } + return name +} From a54345ae56f044d7d1d4a594bfd6c8a41ade70de Mon Sep 17 00:00:00 2001 From: peefy Date: Mon, 6 May 2024 00:51:22 +0800 Subject: [PATCH 2/2] test: fix resource name test cases Signed-off-by: peefy --- fn_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fn_test.go b/fn_test.go index 2184d49..08978a2 100644 --- a/fn_test.go +++ b/fn_test.go @@ -63,7 +63,7 @@ func TestRunFunction(t *testing.T) { Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"XR"}`), }, Resources: map[string]*fnv1beta1.Resource{ - "": { + "example.org/v1-Generated--": { Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"Generated"}`), }, }, @@ -101,7 +101,7 @@ func TestRunFunction(t *testing.T) { Resource: resource.MustStructJSON(`{"apiVersion":"example.org/v1","kind":"XR"}`), }, Resources: map[string]*fnv1beta1.Resource{ - "": { + "sql.gcp.upbound.io/v1beta1-DatabaseInstance--": { Resource: resource.MustStructJSON(`{"apiVersion": "sql.gcp.upbound.io/v1beta1", "kind": "DatabaseInstance", "spec": {"forProvider": {"project": "test-project", "settings": [{"databaseFlags": [{"name": "log_checkpoints", "value": "on"}]}]}}}`), }, },