Skip to content

Commit

Permalink
Merge pull request #92 from Peefy/fix-resouce-name
Browse files Browse the repository at this point in the history
fix: use resouce GVK and name instead of name
  • Loading branch information
Peefy authored May 5, 2024
2 parents 93fe4d5 + a54345a commit 5ed2d72
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`),
},
},
Expand Down Expand Up @@ -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"}]}]}}}`),
},
},
Expand Down
18 changes: 12 additions & 6 deletions pkg/resource/res.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

0 comments on commit 5ed2d72

Please sign in to comment.