Skip to content
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

add crossplane-external-name tag to external resources #409

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion pkg/config/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
"github.com/crossplane/crossplane-runtime/pkg/meta"
"github.com/crossplane/crossplane-runtime/pkg/reconciler/managed"
xpresource "github.com/crossplane/crossplane-runtime/pkg/resource"
fwresource "github.com/hashicorp/terraform-plugin-framework/resource"
Expand Down Expand Up @@ -315,7 +316,7 @@ func (t *Tagger) Initialize(ctx context.Context, mg xpresource.Managed) error {
if err != nil {
return err
}
pavedByte, err := setExternalTagsWithPaved(xpresource.GetExternalTags(mg), paved, t.fieldName)
pavedByte, err := setExternalTagsWithPaved(getExternalTags(mg), paved, t.fieldName)
if err != nil {
return err
}
Expand All @@ -328,11 +329,20 @@ func (t *Tagger) Initialize(ctx context.Context, mg xpresource.Managed) error {
return nil
}

const externalResourceTagKeyExternalName = "crossplane-external-name"

func getExternalTags(mg xpresource.Managed) map[string]string {
externalTags := xpresource.GetExternalTags(mg)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the only call to xpresource.GetExternalTags I could find in the code, so I'm assuming this is the best and only place to provide this additional tag. If there are other places I'm missing, please let me know and I'll adjust.

externalTags[externalResourceTagKeyExternalName] = mg.GetAnnotations()[meta.AnnotationKeyExternalName]
return externalTags
}

func setExternalTagsWithPaved(externalTags map[string]string, paved *fieldpath.Paved, fieldName string) ([]byte, error) {
tags := map[string]*string{
xpresource.ExternalResourceTagKeyKind: ptr.To(externalTags[xpresource.ExternalResourceTagKeyKind]),
xpresource.ExternalResourceTagKeyName: ptr.To(externalTags[xpresource.ExternalResourceTagKeyName]),
xpresource.ExternalResourceTagKeyProvider: ptr.To(externalTags[xpresource.ExternalResourceTagKeyProvider]),
externalResourceTagKeyExternalName: ptr.To(externalTags[externalResourceTagKeyExternalName]),
}

if err := paved.SetValue(fmt.Sprintf("spec.forProvider.%s", fieldName), tags); err != nil {
Expand Down
77 changes: 72 additions & 5 deletions pkg/config/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ import (

"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
"github.com/crossplane/crossplane-runtime/pkg/meta"
xpresource "github.com/crossplane/crossplane-runtime/pkg/resource"
"github.com/crossplane/crossplane-runtime/pkg/resource/fake"
"github.com/crossplane/crossplane-runtime/pkg/test"
"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
kind = "ACoolService"
name = "example-service"
provider = "ACoolProvider"
kind = "ACoolService"
name = "example-service"
provider = "ACoolProvider"
externalName = "anExternalName"
)

func TestTagger_Initialize(t *testing.T) {
Expand Down Expand Up @@ -82,18 +85,38 @@ func TestSetExternalTagsWithPaved(t *testing.T) {
args
want
}{
"Successful": {
"Successful without external-name": {
args: args{
externalTags: map[string]string{
xpresource.ExternalResourceTagKeyKind: kind,
xpresource.ExternalResourceTagKeyName: name,
xpresource.ExternalResourceTagKeyProvider: provider,
},
paved: fieldpath.Pave(map[string]any{}),
fieldName: "tags",
},
want: want{
pavedString: fmt.Sprintf(`{"spec":{"forProvider":{"tags":{"%s":"%s","%s":"%s","%s":"%s","%s":"%s"}}}}`,
externalResourceTagKeyExternalName, "",
xpresource.ExternalResourceTagKeyKind, kind,
xpresource.ExternalResourceTagKeyName, name,
xpresource.ExternalResourceTagKeyProvider, provider),
},
},
"Successful with external-name": {
args: args{
externalTags: map[string]string{
xpresource.ExternalResourceTagKeyKind: kind,
xpresource.ExternalResourceTagKeyName: name,
xpresource.ExternalResourceTagKeyProvider: provider,
externalResourceTagKeyExternalName: externalName,
},
paved: fieldpath.Pave(map[string]any{}),
fieldName: "tags",
},
want: want{
pavedString: fmt.Sprintf(`{"spec":{"forProvider":{"tags":{"%s":"%s","%s":"%s","%s":"%s"}}}}`,
pavedString: fmt.Sprintf(`{"spec":{"forProvider":{"tags":{"%s":"%s","%s":"%s","%s":"%s","%s":"%s"}}}}`,
externalResourceTagKeyExternalName, externalName,
xpresource.ExternalResourceTagKeyKind, kind,
xpresource.ExternalResourceTagKeyName, name,
xpresource.ExternalResourceTagKeyProvider, provider),
Expand All @@ -112,3 +135,47 @@ func TestSetExternalTagsWithPaved(t *testing.T) {
})
}
}

func TestGetExternalTags(t *testing.T) {
cases := map[string]struct {
mg xpresource.Managed
want map[string]string
}{
"Without external-name": {
mg: &fake.Managed{
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
},
want: map[string]string{
xpresource.ExternalResourceTagKeyKind: "",
xpresource.ExternalResourceTagKeyName: name,
externalResourceTagKeyExternalName: "",
},
},
"With external-name": {
mg: &fake.Managed{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Annotations: map[string]string{
meta.AnnotationKeyExternalName: externalName,
},
},
},
want: map[string]string{
xpresource.ExternalResourceTagKeyKind: "",
xpresource.ExternalResourceTagKeyName: name,
externalResourceTagKeyExternalName: externalName,
},
},
}

for n, tc := range cases {
t.Run(n, func(t *testing.T) {
got := getExternalTags(tc.mg)
if diff := cmp.Diff(tc.want, got, test.EquateErrors()); diff != "" {
t.Fatalf("generateTypeName(...): -want, +got: %s", diff)
}
})
}
}