diff --git a/pkg/controller/acm/controller.go b/pkg/controller/acm/controller.go index 44f67656db..6f69d8dcfc 100644 --- a/pkg/controller/acm/controller.go +++ b/pkg/controller/acm/controller.go @@ -50,8 +50,6 @@ const ( errUpdate = "failed to update the Certificate resource" errSDK = "empty Certificate received from ACM API" - errKubeUpdateFailed = "cannot late initialize Certificate" - errAddTagsFailed = "cannot add tags to Certificate" errListTagsFailed = "failed to list tags for Certificate" errRemoveTagsFailed = "failed to remove tags for Certificate" @@ -71,7 +69,6 @@ func SetupCertificate(mgr ctrl.Manager, o controller.Options) error { managed.WithConnectionPublishers(), managed.WithPollInterval(o.PollInterval), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), managed.WithConnectionPublishers(cps...), @@ -236,29 +233,3 @@ func (e *external) Delete(ctx context.Context, mgd resource.Managed) error { return errorutils.Wrap(resource.Ignore(acm.IsErrorNotFound, err), errDelete) } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*v1beta1.Certificate) - if !ok { - return errors.New(errUnexpectedObject) - } - added := false - tagMap := map[string]string{} - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[t.Key] = t.Value - } - for k, v := range resource.GetExternalTags(mgd) { - if tagMap[k] != v { - cr.Spec.ForProvider.Tags = append(cr.Spec.ForProvider.Tags, v1beta1.Tag{Key: k, Value: v}) - added = true - } - } - if !added { - return nil - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/acmpca/certificateauthority/controller.go b/pkg/controller/acmpca/certificateauthority/controller.go index bc6846c66c..b808553593 100644 --- a/pkg/controller/acmpca/certificateauthority/controller.go +++ b/pkg/controller/acmpca/certificateauthority/controller.go @@ -70,7 +70,6 @@ func SetupCertificateAuthority(mgr ctrl.Manager, o controller.Options) error { managed.WithExternalConnecter(&connector{client: mgr.GetClient(), newClientFn: acmpca.NewClient}), managed.WithConnectionPublishers(), managed.WithPollInterval(o.PollInterval), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), managed.WithConnectionPublishers(cps...), @@ -267,33 +266,3 @@ func (e *external) Delete(ctx context.Context, mgd resource.Managed) error { return errorutils.Wrap(resource.Ignore(acmpca.IsErrorNotFound, err), errDelete) } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*v1beta1.CertificateAuthority) - if !ok { - return errors.New(errUnexpectedObject) - } - added := false - tagMap := map[string]string{} - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[t.Key] = t.Value - } - for k, v := range resource.GetExternalTags(mgd) { - if p, ok := tagMap[k]; !ok || v != p { - cr.Spec.ForProvider.Tags = append(cr.Spec.ForProvider.Tags, v1beta1.Tag{Key: k, Value: v}) - added = true - } - } - if !added { - return nil - } - err := t.kube.Update(ctx, cr) - if err != nil { - return errors.Wrap(err, errKubeUpdateFailed) - } - return nil -} diff --git a/pkg/controller/acmpca/certificateauthority/controller_test.go b/pkg/controller/acmpca/certificateauthority/controller_test.go index 0975ae9aaa..a3dda54fe9 100644 --- a/pkg/controller/acmpca/certificateauthority/controller_test.go +++ b/pkg/controller/acmpca/certificateauthority/controller_test.go @@ -29,9 +29,7 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" "github.com/pkg/errors" - "sigs.k8s.io/controller-runtime/pkg/client" "github.com/crossplane-contrib/provider-aws/apis/acmpca/v1beta1" acmpca "github.com/crossplane-contrib/provider-aws/pkg/clients/acmpca" @@ -59,10 +57,6 @@ var ( title = "someTitle" errBoom = errors.New("boom") - - sortTags = cmpopts.SortSlices(func(a, b v1beta1.Tag) bool { - return a.Key > b.Key - }) ) type args struct { @@ -107,24 +101,6 @@ func withCertificateAuthorityStatus() certificateAuthorityModifier { } } -func withTags(tagMaps ...map[string]string) certificateAuthorityModifier { - var tagList []v1beta1.Tag - for _, tagMap := range tagMaps { - for k, v := range tagMap { - tagList = append(tagList, v1beta1.Tag{Key: k, Value: v}) - } - } - return func(r *v1beta1.CertificateAuthority) { - r.Spec.ForProvider.Tags = tagList - } -} - -func withGroupVersionKind() certificateAuthorityModifier { - return func(r *v1beta1.CertificateAuthority) { - r.TypeMeta.SetGroupVersionKind(v1beta1.CertificateAuthorityGroupVersionKind) - } -} - func certificateAuthority(m ...certificateAuthorityModifier) *v1beta1.CertificateAuthority { cr := &v1beta1.CertificateAuthority{} meta.SetExternalName(cr, certificateAuthorityArn) @@ -554,69 +530,3 @@ func TestDelete(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type args struct { - cr resource.Managed - kube client.Client - } - type want struct { - cr *v1beta1.CertificateAuthority - err error - } - - cases := map[string]struct { - args - want - }{ - "InvalidInput": { - args: args{ - cr: unexpectedItem, - }, - want: want{ - err: errors.New(errUnexpectedObject), - }, - }, - "Successful": { - args: args{ - cr: certificateAuthority(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: certificateAuthority(withTags(resource.GetExternalTags(certificateAuthority()), map[string]string{"foo": "bar"})), - }, - }, - "Check Tag values": { - args: args{ - cr: certificateAuthority(withTags(map[string]string{"foo": "bar"}), withGroupVersionKind()), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: certificateAuthority(withTags(resource.GetExternalTags(certificateAuthority(withGroupVersionKind())), map[string]string{"foo": "bar"}), withGroupVersionKind()), - }, - }, - "UpdateFailed": { - args: args{ - cr: certificateAuthority(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errors.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, sortTags); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/cache/replicationgroup/managed.go b/pkg/controller/cache/replicationgroup/managed.go index a6a4a79d40..4609b3f5c2 100644 --- a/pkg/controller/cache/replicationgroup/managed.go +++ b/pkg/controller/cache/replicationgroup/managed.go @@ -19,7 +19,6 @@ package replicationgroup import ( "context" "reflect" - "sort" "github.com/aws/aws-sdk-go-v2/aws" awselasticache "github.com/aws/aws-sdk-go-v2/service/elasticache" @@ -74,7 +73,7 @@ func SetupReplicationGroup(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), newClientFn: elasticache.NewClient}), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), @@ -304,34 +303,6 @@ func (e *external) updateTags(ctx context.Context, tags []v1beta1.Tag, arn *stri return nil } -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*v1beta1.ReplicationGroup) - if !ok { - return errors.New(errNotReplicationGroup) - } - tagMap := map[string]string{} - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[t.Key] = t.Value - } - for k, v := range resource.GetExternalTags(mg) { - tagMap[k] = v - } - cr.Spec.ForProvider.Tags = make([]v1beta1.Tag, len(tagMap)) - i := 0 - for k, v := range tagMap { - cr.Spec.ForProvider.Tags[i] = v1beta1.Tag{Key: k, Value: v} - i++ - } - sort.Slice(cr.Spec.ForProvider.Tags, func(i, j int) bool { - return cr.Spec.ForProvider.Tags[i].Key < cr.Spec.ForProvider.Tags[j].Key - }) - return errors.Wrap(t.kube.Update(ctx, cr), errUpdateReplicationGroupCR) -} - func getCacheClusterList(ctx context.Context, client awselasticache.DescribeCacheClustersAPIClient, idList []string) ([]awselasticachetypes.CacheCluster, error) { if len(idList) < 1 { return nil, nil diff --git a/pkg/controller/cache/replicationgroup/managed_test.go b/pkg/controller/cache/replicationgroup/managed_test.go index a537b3e8ce..1f9acba042 100644 --- a/pkg/controller/cache/replicationgroup/managed_test.go +++ b/pkg/controller/cache/replicationgroup/managed_test.go @@ -27,17 +27,13 @@ import ( xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" - "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" "github.com/pkg/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" "github.com/crossplane-contrib/provider-aws/apis/cache/v1beta1" "github.com/crossplane-contrib/provider-aws/pkg/clients/elasticache/fake" - errorutils "github.com/crossplane-contrib/provider-aws/pkg/utils/errors" ) const ( @@ -118,16 +114,6 @@ func withClusterEnabled(e bool) replicationGroupModifier { return func(r *v1beta1.ReplicationGroup) { r.Status.AtProvider.ClusterEnabled = e } } -func withTags(tagMaps ...map[string]string) replicationGroupModifier { - var tagList []v1beta1.Tag - for _, tagMap := range tagMaps { - for k, v := range tagMap { - tagList = append(tagList, v1beta1.Tag{Key: k, Value: v}) - } - } - return func(r *v1beta1.ReplicationGroup) { r.Spec.ForProvider.Tags = tagList } -} - func withNumNodeGroups(n int) replicationGroupModifier { return func(r *v1beta1.ReplicationGroup) { r.Spec.ForProvider.NumNodeGroups = &n } } @@ -836,55 +822,6 @@ func TestDelete(t *testing.T) { } } -func TestInitialize(t *testing.T) { - type args struct { - cr *v1beta1.ReplicationGroup - kube client.Client - } - type want struct { - cr *v1beta1.ReplicationGroup - err error - } - - cases := map[string]struct { - args - want - }{ - "Successful": { - args: args{ - cr: replicationGroup(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: replicationGroup(withTags(resource.GetExternalTags(replicationGroup()), map[string]string{"foo": "bar"})), - }, - }, - "UpdateFailed": { - args: args{ - cr: replicationGroup(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errorBoom)}, - }, - want: want{ - err: errorutils.Wrap(errorBoom, errUpdateReplicationGroupCR), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, cmpopts.SortSlices(func(a, b v1beta1.Tag) bool { return a.Key > b.Key })); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} - func TestUpdateReplicationGroupNumCacheClusters(t *testing.T) { cases := []struct { diff --git a/pkg/controller/database/rdsinstance/rdsinstance.go b/pkg/controller/database/rdsinstance/rdsinstance.go index 48befe82ea..5cd8b342f4 100644 --- a/pkg/controller/database/rdsinstance/rdsinstance.go +++ b/pkg/controller/database/rdsinstance/rdsinstance.go @@ -19,7 +19,6 @@ package rdsinstance import ( "context" "reflect" - "sort" "github.com/aws/aws-sdk-go-v2/aws" awsrds "github.com/aws/aws-sdk-go-v2/service/rds" @@ -73,7 +72,7 @@ func SetupRDSInstance(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), newClientFn: rds.NewClient}), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), @@ -317,31 +316,3 @@ func (e *external) Delete(ctx context.Context, mg resource.Managed) error { _, err = e.client.DeleteDBInstance(ctx, &input) return errorutils.Wrap(resource.Ignore(rds.IsErrorNotFound, err), errDeleteFailed) } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*v1beta1.RDSInstance) - if !ok { - return errors.New(errNotRDSInstance) - } - tagMap := map[string]string{} - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[t.Key] = t.Value - } - for k, v := range resource.GetExternalTags(mg) { - tagMap[k] = v - } - cr.Spec.ForProvider.Tags = make([]v1beta1.Tag, len(tagMap)) - i := 0 - for k, v := range tagMap { - cr.Spec.ForProvider.Tags[i] = v1beta1.Tag{Key: k, Value: v} - i++ - } - sort.Slice(cr.Spec.ForProvider.Tags, func(i, j int) bool { - return cr.Spec.ForProvider.Tags[i].Key < cr.Spec.ForProvider.Tags[j].Key - }) - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/database/rdsinstance/rdsinstance_test.go b/pkg/controller/database/rdsinstance/rdsinstance_test.go index 5140248fbe..bae6aeac23 100644 --- a/pkg/controller/database/rdsinstance/rdsinstance_test.go +++ b/pkg/controller/database/rdsinstance/rdsinstance_test.go @@ -25,10 +25,8 @@ import ( awsrdstypes "github.com/aws/aws-sdk-go-v2/service/rds/types" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" - "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -985,48 +983,3 @@ func TestDelete(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type want struct { - cr *v1beta1.RDSInstance - err error - } - - cases := map[string]struct { - args - want - }{ - "Successful": { - args: args{ - cr: instance(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: instance(withTags(resource.GetExternalTags(instance()), map[string]string{"foo": "bar"})), - }, - }, - "UpdateFailed": { - args: args{ - cr: instance(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errorutils.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, cmpopts.SortSlices(func(a, b v1beta1.Tag) bool { return a.Key > b.Key })); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/docdb/dbcluster/setup.go b/pkg/controller/docdb/dbcluster/setup.go index 87c928628f..c0d1fbf58b 100644 --- a/pkg/controller/docdb/dbcluster/setup.go +++ b/pkg/controller/docdb/dbcluster/setup.go @@ -67,7 +67,7 @@ func SetupDBCluster(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), @@ -484,20 +484,6 @@ func getConnectionDetails(cr *svcapitypes.DBCluster) managed.ConnectionDetails { } } -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*svcapitypes.DBCluster) - if !ok { - return errors.New(errNotDBCluster) - } - - cr.Spec.ForProvider.Tags = svcutils.AddExternalTags(mg, cr.Spec.ForProvider.Tags) - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} - func (e *hooks) savePasswordSecret(ctx context.Context, cr *svcapitypes.DBCluster, pw string) error { if cr.Spec.ForProvider.MasterUserPasswordSecretRef == nil { return errors.New("no MasterUserPasswordSecretRef given, unable to store password") diff --git a/pkg/controller/docdb/dbcluster/setup_test.go b/pkg/controller/docdb/dbcluster/setup_test.go index d3f0ff9ece..18aefb1318 100644 --- a/pkg/controller/docdb/dbcluster/setup_test.go +++ b/pkg/controller/docdb/dbcluster/setup_test.go @@ -38,8 +38,6 @@ import ( svcapitypes "github.com/crossplane-contrib/provider-aws/apis/docdb/v1alpha1" "github.com/crossplane-contrib/provider-aws/pkg/clients/docdb/fake" - svcutils "github.com/crossplane-contrib/provider-aws/pkg/controller/docdb/utils" - errorutils "github.com/crossplane-contrib/provider-aws/pkg/utils/errors" "github.com/crossplane-contrib/provider-aws/pkg/utils/pointer" ) @@ -87,7 +85,6 @@ var ( testErrCreateDBClusterFailed = "CreateDBCluster failed" testErrDeleteDBClusterFailed = "DeleteDBCluster failed" testErrModifyDBClusterFailed = "ModifyDBCluster failed" - testErrBoom = "boom" testErrGetSecret = "testErrGetSecret" timeNow = time.Now() @@ -326,14 +323,6 @@ func withRestoreToPointInTime(v *svcapitypes.RestorePointInTimeConfiguration) do } } -func mergeTags(lists ...[]*svcapitypes.Tag) []*svcapitypes.Tag { - res := []*svcapitypes.Tag{} - for _, list := range lists { - res = append(res, list...) - } - return res -} - func generateConnectionDetails(username, password, readerEndpoint, endpoint string, port int) managed.ConnectionDetails { mcd := managed.ConnectionDetails{ @@ -3095,71 +3084,3 @@ func TestUpdate(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type want struct { - cr *svcapitypes.DBCluster - err error - } - - cases := map[string]struct { - args - want - }{ - "Successful": { - args: args{ - cr: instance(withTags( - &svcapitypes.Tag{Key: pointer.ToOrNilIfZeroValue(testTagKey), Value: pointer.ToOrNilIfZeroValue(testTagValue)}, - &svcapitypes.Tag{Key: pointer.ToOrNilIfZeroValue(testOtherTagKey), Value: pointer.ToOrNilIfZeroValue(testOtherTagValue)}, - )), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: instance(withTags( - mergeTags( - []*svcapitypes.Tag{ - {Key: pointer.ToOrNilIfZeroValue(testTagKey), Value: pointer.ToOrNilIfZeroValue(testTagValue)}, - {Key: pointer.ToOrNilIfZeroValue(testOtherTagKey), Value: pointer.ToOrNilIfZeroValue(testOtherTagValue)}, - }, - svcutils.GetExternalTags(instance()), - )..., - )), - }, - }, - "UpdateFailed": { - args: args{ - cr: instance(withTags( - &svcapitypes.Tag{Key: pointer.ToOrNilIfZeroValue(testTagKey), Value: pointer.ToOrNilIfZeroValue(testTagValue)}, - &svcapitypes.Tag{Key: pointer.ToOrNilIfZeroValue(testOtherTagKey), Value: pointer.ToOrNilIfZeroValue(testOtherTagValue)}, - )), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errors.New(testErrBoom))}, - }, - want: want{ - cr: instance(withTags( - mergeTags( - []*svcapitypes.Tag{ - {Key: pointer.ToOrNilIfZeroValue(testTagKey), Value: pointer.ToOrNilIfZeroValue(testTagValue)}, - {Key: pointer.ToOrNilIfZeroValue(testOtherTagKey), Value: pointer.ToOrNilIfZeroValue(testOtherTagValue)}, - }, - svcutils.GetExternalTags(instance()), - )..., - )), - err: errorutils.Wrap(errors.New(testErrBoom), errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, test.EquateConditions()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/docdb/dbclusterparametergroup/setup.go b/pkg/controller/docdb/dbclusterparametergroup/setup.go index a2c1f52fa9..8ccccf5fbc 100644 --- a/pkg/controller/docdb/dbclusterparametergroup/setup.go +++ b/pkg/controller/docdb/dbclusterparametergroup/setup.go @@ -61,7 +61,7 @@ func SetupDBClusterParameterGroup(mgr ctrl.Manager, o controller.Options) error reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), @@ -259,17 +259,3 @@ func generateAPIParameter(p *svcsdk.Parameter, o *svcapitypes.CustomParameter) * return o } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*svcapitypes.DBClusterParameterGroup) - if !ok { - return errors.New(errNotDBClusterParameterGroup) - } - - cr.Spec.ForProvider.Tags = svcutils.AddExternalTags(mg, cr.Spec.ForProvider.Tags) - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/docdb/dbclusterparametergroup/setup_test.go b/pkg/controller/docdb/dbclusterparametergroup/setup_test.go index 281397d7d8..9f2c02a7c3 100644 --- a/pkg/controller/docdb/dbclusterparametergroup/setup_test.go +++ b/pkg/controller/docdb/dbclusterparametergroup/setup_test.go @@ -33,8 +33,6 @@ import ( svcapitypes "github.com/crossplane-contrib/provider-aws/apis/docdb/v1alpha1" "github.com/crossplane-contrib/provider-aws/pkg/clients/docdb/fake" - svcutils "github.com/crossplane-contrib/provider-aws/pkg/controller/docdb/utils" - errorutils "github.com/crossplane-contrib/provider-aws/pkg/utils/errors" "github.com/crossplane-contrib/provider-aws/pkg/utils/pointer" ) @@ -135,14 +133,6 @@ func withTags(values ...*svcapitypes.Tag) docDBModifier { } } -func mergeTags(lists ...[]*svcapitypes.Tag) []*svcapitypes.Tag { - res := []*svcapitypes.Tag{} - for _, list := range lists { - res = append(res, list...) - } - return res -} - func TestObserve(t *testing.T) { type want struct { cr *svcapitypes.DBClusterParameterGroup @@ -1145,71 +1135,3 @@ func TestUpdate(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type want struct { - cr *svcapitypes.DBClusterParameterGroup - err error - } - - cases := map[string]struct { - args - want - }{ - "Successful": { - args: args{ - cr: instance(withTags( - &svcapitypes.Tag{Key: pointer.ToOrNilIfZeroValue(testTagKey), Value: pointer.ToOrNilIfZeroValue(testTagValue)}, - &svcapitypes.Tag{Key: pointer.ToOrNilIfZeroValue(testOtherTagKey), Value: pointer.ToOrNilIfZeroValue(testOtherTagValue)}, - )), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: instance(withTags( - mergeTags( - []*svcapitypes.Tag{ - {Key: pointer.ToOrNilIfZeroValue(testTagKey), Value: pointer.ToOrNilIfZeroValue(testTagValue)}, - {Key: pointer.ToOrNilIfZeroValue(testOtherTagKey), Value: pointer.ToOrNilIfZeroValue(testOtherTagValue)}, - }, - svcutils.GetExternalTags(instance()), - )..., - )), - }, - }, - "UpdateFailed": { - args: args{ - cr: instance(withTags( - &svcapitypes.Tag{Key: pointer.ToOrNilIfZeroValue(testTagKey), Value: pointer.ToOrNilIfZeroValue(testTagValue)}, - &svcapitypes.Tag{Key: pointer.ToOrNilIfZeroValue(testOtherTagKey), Value: pointer.ToOrNilIfZeroValue(testOtherTagValue)}, - )), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errors.New(testErrBoom))}, - }, - want: want{ - cr: instance(withTags( - mergeTags( - []*svcapitypes.Tag{ - {Key: pointer.ToOrNilIfZeroValue(testTagKey), Value: pointer.ToOrNilIfZeroValue(testTagValue)}, - {Key: pointer.ToOrNilIfZeroValue(testOtherTagKey), Value: pointer.ToOrNilIfZeroValue(testOtherTagValue)}, - }, - svcutils.GetExternalTags(instance()), - )..., - )), - err: errorutils.Wrap(errors.New(testErrBoom), errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, test.EquateConditions()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/docdb/dbinstance/setup.go b/pkg/controller/docdb/dbinstance/setup.go index 971f6863ac..ddeb9207f3 100644 --- a/pkg/controller/docdb/dbinstance/setup.go +++ b/pkg/controller/docdb/dbinstance/setup.go @@ -29,7 +29,6 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" - "github.com/pkg/errors" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -40,11 +39,6 @@ import ( "github.com/crossplane-contrib/provider-aws/pkg/utils/pointer" ) -const ( - errNotDBInstance = "managed resource is not a DocDB instance custom resource" - errKubeUpdateFailed = "cannot update DocDB instance custom resource" -) - // SetupDBInstance adds a controller that reconciles a DBInstance. func SetupDBInstance(mgr ctrl.Manager, o controller.Options) error { name := managed.ControllerName(svcapitypes.DBInstanceGroupKind) @@ -58,7 +52,7 @@ func SetupDBInstance(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), @@ -217,17 +211,3 @@ func getConnectionDetails(cr *svcapitypes.DBInstance) managed.ConnectionDetails xpv1.ResourceCredentialsSecretPortKey: []byte(strconv.Itoa(int(pointer.Int64Value(cr.Status.AtProvider.Endpoint.Port)))), } } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*svcapitypes.DBInstance) - if !ok { - return errors.New(errNotDBInstance) - } - - cr.Spec.ForProvider.Tags = svcutils.AddExternalTags(mg, cr.Spec.ForProvider.Tags) - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/docdb/dbsubnetgroup/setup.go b/pkg/controller/docdb/dbsubnetgroup/setup.go index d73fbdfe82..c3f9a2b79b 100644 --- a/pkg/controller/docdb/dbsubnetgroup/setup.go +++ b/pkg/controller/docdb/dbsubnetgroup/setup.go @@ -28,7 +28,6 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" - "github.com/pkg/errors" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" @@ -39,11 +38,6 @@ import ( "github.com/crossplane-contrib/provider-aws/pkg/utils/pointer" ) -const ( - errNotDBSubnetGroup = "managed resource is not a DBSubnetGroup custom resource" - errKubeUpdateFailed = "cannot update DocDBSubnetGroup custom resource" -) - // SetupDBSubnetGroup adds a controller that reconciles a DBSubnetGroup. func SetupDBSubnetGroup(mgr ctrl.Manager, o controller.Options) error { name := managed.ControllerName(svcapitypes.DBSubnetGroupKind) @@ -57,7 +51,7 @@ func SetupDBSubnetGroup(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), @@ -185,17 +179,3 @@ func filterList(cr *svcapitypes.DBSubnetGroup, list *svcsdk.DescribeDBSubnetGrou DBSubnetGroups: []*svcsdk.DBSubnetGroup{}, } } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*svcapitypes.DBSubnetGroup) - if !ok { - return errors.New(errNotDBSubnetGroup) - } - - cr.Spec.ForProvider.Tags = svcutils.AddExternalTags(mg, cr.Spec.ForProvider.Tags) - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/dynamodb/table/hooks.go b/pkg/controller/dynamodb/table/hooks.go index 64c1e4f3d7..2a6bb9fa47 100644 --- a/pkg/controller/dynamodb/table/hooks.go +++ b/pkg/controller/dynamodb/table/hooks.go @@ -34,7 +34,6 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" cpresource "github.com/crossplane/crossplane-runtime/pkg/resource" - "github.com/google/go-cmp/cmp" "github.com/pkg/errors" "k8s.io/utils/ptr" ctrl "sigs.k8s.io/controller-runtime" @@ -64,9 +63,7 @@ func SetupTable(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&customConnector{kube: mgr.GetClient()}), - managed.WithInitializers( - managed.NewNameAsExternalName(mgr.GetClient()), - &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), @@ -209,36 +206,6 @@ func postObserve(_ context.Context, cr *svcapitypes.Table, resp *svcsdk.Describe return obs, nil } -type tagger struct { - kube client.Client -} - -func (e *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*svcapitypes.Table) - if !ok { - return errors.New(errUnexpectedObject) - } - tagMap := map[string]string{} - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[pointer.StringValue(t.Key)] = pointer.StringValue(t.Value) - } - for k, v := range resource.GetExternalTags(cr) { - tagMap[k] = v - } - tags := make([]*svcapitypes.Tag, 0) - for k, v := range tagMap { - tags = append(tags, &svcapitypes.Tag{Key: pointer.ToOrNilIfZeroValue(k), Value: pointer.ToOrNilIfZeroValue(v)}) - } - sort.Slice(tags, func(i, j int) bool { - return pointer.StringValue(tags[i].Key) < pointer.StringValue(tags[j].Key) - }) - if cmp.Equal(cr.Spec.ForProvider.Tags, tags) { - return nil - } - cr.Spec.ForProvider.Tags = tags - return errors.Wrap(e.kube.Update(ctx, cr), "cannot update Table Spec") -} - func lateInitialize(in *svcapitypes.TableParameters, t *svcsdk.DescribeTableOutput) error { //nolint:gocyclo if t == nil { return nil diff --git a/pkg/controller/ec2/address/controller.go b/pkg/controller/ec2/address/controller.go index 92a415382d..1f09e6fb72 100644 --- a/pkg/controller/ec2/address/controller.go +++ b/pkg/controller/ec2/address/controller.go @@ -18,7 +18,6 @@ package address import ( "context" - "sort" "time" "github.com/aws/aws-sdk-go-v2/aws" @@ -70,7 +69,6 @@ func SetupAddress(mgr ctrl.Manager, o controller.Options) error { managed.WithCreationGracePeriod(3 * time.Minute), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), managed.WithConnectionPublishers(), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), @@ -235,31 +233,3 @@ func (e *external) Delete(ctx context.Context, mgd resource.Managed) error { return errorutils.Wrap(resource.Ignore(ec2.IsAddressNotFoundErr, err), errDelete) } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*v1beta1.Address) - if !ok { - return errors.New(errUnexpectedObject) - } - tagMap := map[string]string{} - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[t.Key] = t.Value - } - for k, v := range resource.GetExternalTags(mgd) { - tagMap[k] = v - } - cr.Spec.ForProvider.Tags = make([]v1beta1.Tag, len(tagMap)) - i := 0 - for k, v := range tagMap { - cr.Spec.ForProvider.Tags[i] = v1beta1.Tag{Key: k, Value: v} - i++ - } - sort.Slice(cr.Spec.ForProvider.Tags, func(i, j int) bool { - return cr.Spec.ForProvider.Tags[i].Key < cr.Spec.ForProvider.Tags[j].Key - }) - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/ec2/address/controller_test.go b/pkg/controller/ec2/address/controller_test.go index ca7bb8f73b..d3d83533c5 100644 --- a/pkg/controller/ec2/address/controller_test.go +++ b/pkg/controller/ec2/address/controller_test.go @@ -25,10 +25,8 @@ import ( xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" - "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" "github.com/pkg/errors" "sigs.k8s.io/controller-runtime/pkg/client" @@ -54,16 +52,6 @@ type args struct { type addressModifier func(*v1beta1.Address) -func withTags(tagMaps ...map[string]string) addressModifier { - var tagList []v1beta1.Tag - for _, tagMap := range tagMaps { - for k, v := range tagMap { - tagList = append(tagList, v1beta1.Tag{Key: k, Value: v}) - } - } - return func(r *v1beta1.Address) { r.Spec.ForProvider.Tags = tagList } -} - func withExternalName(name string) addressModifier { return func(r *v1beta1.Address) { meta.SetExternalName(r, name) } } @@ -432,52 +420,3 @@ func TestRelease(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type args struct { - cr *v1beta1.Address - kube client.Client - } - type want struct { - cr *v1beta1.Address - err error - } - - cases := map[string]struct { - args - want - }{ - "Successful": { - args: args{ - cr: address(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: address(withTags(resource.GetExternalTags(address()), map[string]string{"foo": "bar"})), - }, - }, - "UpdateFailed": { - args: args{ - cr: address(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errors.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, cmpopts.SortSlices(func(a, b v1beta1.Tag) bool { return a.Key > b.Key })); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/ec2/instance/controller.go b/pkg/controller/ec2/instance/controller.go index 5444d16fa1..b922df621b 100644 --- a/pkg/controller/ec2/instance/controller.go +++ b/pkg/controller/ec2/instance/controller.go @@ -18,7 +18,6 @@ package instance import ( "context" - "sort" "github.com/aws/aws-sdk-go-v2/aws" awsec2 "github.com/aws/aws-sdk-go-v2/service/ec2" @@ -70,7 +69,6 @@ func SetupInstance(mgr ctrl.Manager, o controller.Options) error { managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), newClientFn: ec2.NewInstanceClient}), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), managed.WithConnectionPublishers(), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), @@ -364,31 +362,3 @@ func (e *external) Delete(ctx context.Context, mgd resource.Managed) error { return errorutils.Wrap(resource.Ignore(ec2.IsInstanceNotFoundErr, err), errDelete) } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*svcapitypes.Instance) - if !ok { - return errors.New(errUnexpectedObject) - } - tagMap := map[string]string{} - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[t.Key] = t.Value - } - for k, v := range resource.GetExternalTags(mgd) { - tagMap[k] = v - } - cr.Spec.ForProvider.Tags = make([]svcapitypes.Tag, len(tagMap)) - i := 0 - for k, v := range tagMap { - cr.Spec.ForProvider.Tags[i] = svcapitypes.Tag{Key: k, Value: v} - i++ - } - sort.Slice(cr.Spec.ForProvider.Tags, func(i, j int) bool { - return cr.Spec.ForProvider.Tags[i].Key < cr.Spec.ForProvider.Tags[j].Key - }) - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/ec2/instance/controller_test.go b/pkg/controller/ec2/instance/controller_test.go index 3635a83525..96be6c0b07 100644 --- a/pkg/controller/ec2/instance/controller_test.go +++ b/pkg/controller/ec2/instance/controller_test.go @@ -26,10 +26,8 @@ import ( xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" - "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" "github.com/pkg/errors" "sigs.k8s.io/controller-runtime/pkg/client" @@ -69,16 +67,6 @@ func withStatus(s manualv1alpha1.InstanceObservation) instanceModifier { return func(r *manualv1alpha1.Instance) { r.Status.AtProvider = s } } -func withTags(tagMaps ...map[string]string) instanceModifier { - var tagList []manualv1alpha1.Tag - for _, tagMap := range tagMaps { - for k, v := range tagMap { - tagList = append(tagList, manualv1alpha1.Tag{Key: k, Value: v}) - } - } - return func(r *manualv1alpha1.Instance) { r.Spec.ForProvider.Tags = tagList } -} - func instance(m ...instanceModifier) *manualv1alpha1.Instance { cr := &manualv1alpha1.Instance{} for _, f := range m { @@ -418,52 +406,3 @@ func TestUpdate(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type args struct { - cr *manualv1alpha1.Instance - kube client.Client - } - type want struct { - cr *manualv1alpha1.Instance - err error - } - - cases := map[string]struct { - args - want - }{ - "Successful": { - args: args{ - cr: instance(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: instance(withTags(resource.GetExternalTags(instance()), map[string]string{"foo": "bar"})), - }, - }, - "UpdateFailed": { - args: args{ - cr: instance(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errors.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, cmpopts.SortSlices(func(a, b manualv1alpha1.Tag) bool { return a.Key < b.Key })); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/ec2/launchtemplate/setup.go b/pkg/controller/ec2/launchtemplate/setup.go index 3b3f5b4701..ac4e8c4f86 100644 --- a/pkg/controller/ec2/launchtemplate/setup.go +++ b/pkg/controller/ec2/launchtemplate/setup.go @@ -2,7 +2,6 @@ package launchtemplate import ( "context" - "sort" "github.com/aws/aws-sdk-go/aws" svcsdk "github.com/aws/aws-sdk-go/service/ec2" @@ -13,9 +12,7 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" - "github.com/pkg/errors" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" svcapitypes "github.com/crossplane-contrib/provider-aws/apis/ec2/v1alpha1" "github.com/crossplane-contrib/provider-aws/apis/v1alpha1" @@ -36,7 +33,7 @@ func SetupLaunchTemplate(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithPollInterval(o.PollInterval), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), managed.WithConnectionPublishers(cps...), @@ -118,46 +115,3 @@ func postObserve(_ context.Context, cr *svcapitypes.LaunchTemplate, resp *svcsdk cr.SetConditions(xpv1.Available()) return obs, nil } - -const ( - errKubeUpdateFailed = "cannot update LaunchTemplate custom resource" -) - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*svcapitypes.LaunchTemplate) - if !ok { - return errors.New(errUnexpectedObject) - } - var launchTemplateTags svcapitypes.TagSpecification - for _, tagSpecification := range cr.Spec.ForProvider.TagSpecifications { - if aws.StringValue(tagSpecification.ResourceType) == "launch-template" { - launchTemplateTags = *tagSpecification - } - } - - tagMap := map[string]string{} - tagMap["Name"] = cr.Name - for _, t := range launchTemplateTags.Tags { - tagMap[aws.StringValue(t.Key)] = aws.StringValue(t.Value) - } - for k, v := range resource.GetExternalTags(mgd) { - tagMap[k] = v - } - launchTemplateTags.Tags = make([]*svcapitypes.Tag, len(tagMap)) - launchTemplateTags.ResourceType = aws.String("launch-template") - i := 0 - for k, v := range tagMap { - launchTemplateTags.Tags[i] = &svcapitypes.Tag{Key: aws.String(k), Value: aws.String(v)} - i++ - } - sort.Slice(launchTemplateTags.Tags, func(i, j int) bool { - return aws.StringValue(launchTemplateTags.Tags[i].Key) < aws.StringValue(launchTemplateTags.Tags[j].Key) - }) - - cr.Spec.ForProvider.TagSpecifications = []*svcapitypes.TagSpecification{&launchTemplateTags} - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/ec2/subnet/controller.go b/pkg/controller/ec2/subnet/controller.go index 91f1b2c859..b66366de34 100644 --- a/pkg/controller/ec2/subnet/controller.go +++ b/pkg/controller/ec2/subnet/controller.go @@ -18,7 +18,6 @@ package subnet import ( "context" - "sort" "time" "github.com/aws/aws-sdk-go-v2/aws" @@ -70,7 +69,6 @@ func SetupSubnet(mgr ctrl.Manager, o controller.Options) error { managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), newClientFn: ec2.NewSubnetClient}), managed.WithCreationGracePeriod(3 * time.Minute), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithConnectionPublishers(), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), @@ -263,31 +261,3 @@ func (e *external) Delete(ctx context.Context, mgd resource.Managed) error { return errorutils.Wrap(resource.Ignore(ec2.IsSubnetNotFoundErr, err), errDelete) } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*v1beta1.Subnet) - if !ok { - return errors.New(errUnexpectedObject) - } - tagMap := map[string]string{} - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[t.Key] = t.Value - } - for k, v := range resource.GetExternalTags(mgd) { - tagMap[k] = v - } - cr.Spec.ForProvider.Tags = make([]v1beta1.Tag, len(tagMap)) - i := 0 - for k, v := range tagMap { - cr.Spec.ForProvider.Tags[i] = v1beta1.Tag{Key: k, Value: v} - i++ - } - sort.Slice(cr.Spec.ForProvider.Tags, func(i, j int) bool { - return cr.Spec.ForProvider.Tags[i].Key < cr.Spec.ForProvider.Tags[j].Key - }) - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/ec2/transitgateway/setup.go b/pkg/controller/ec2/transitgateway/setup.go index f18cdca6e4..82a931d3fd 100644 --- a/pkg/controller/ec2/transitgateway/setup.go +++ b/pkg/controller/ec2/transitgateway/setup.go @@ -2,7 +2,6 @@ package transitgateway import ( "context" - "sort" "github.com/aws/aws-sdk-go/aws" svcsdk "github.com/aws/aws-sdk-go/service/ec2" @@ -13,9 +12,7 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" - "github.com/pkg/errors" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" svcapitypes "github.com/crossplane-contrib/provider-aws/apis/ec2/v1alpha1" "github.com/crossplane-contrib/provider-aws/apis/v1alpha1" @@ -23,10 +20,6 @@ import ( "github.com/crossplane-contrib/provider-aws/pkg/utils/pointer" ) -const ( - errKubeUpdateFailed = "cannot update TransitGateway" -) - // SetupTransitGateway adds a controller that reconciles TransitGateway. func SetupTransitGateway(mgr ctrl.Manager, o controller.Options) error { name := managed.ControllerName(svcapitypes.TransitGatewayGroupKind) @@ -48,7 +41,7 @@ func SetupTransitGateway(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), + managed.WithInitializers(), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), managed.WithConnectionPublishers(cps...), } @@ -135,42 +128,3 @@ func LateInitialize(cr *svcapitypes.TransitGatewayParameters, obj *svcsdk.Descri return nil } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*svcapitypes.TransitGateway) - if !ok { - return errors.New(errUnexpectedObject) - } - var transitGatewayTags svcapitypes.TagSpecification - for _, tagSpecification := range cr.Spec.ForProvider.TagSpecifications { - if aws.StringValue(tagSpecification.ResourceType) == "transit-gateway" { - transitGatewayTags = *tagSpecification - } - } - - tagMap := map[string]string{} - tagMap["Name"] = cr.Name - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[aws.StringValue(t.Key)] = aws.StringValue(t.Value) - } - for k, v := range resource.GetExternalTags(mgd) { - tagMap[k] = v - } - transitGatewayTags.Tags = make([]*svcapitypes.Tag, len(tagMap)) - transitGatewayTags.ResourceType = aws.String("transit-gateway") - i := 0 - for k, v := range tagMap { - transitGatewayTags.Tags[i] = &svcapitypes.Tag{Key: aws.String(k), Value: aws.String(v)} - i++ - } - sort.Slice(transitGatewayTags.Tags, func(i, j int) bool { - return aws.StringValue(transitGatewayTags.Tags[i].Key) < aws.StringValue(transitGatewayTags.Tags[j].Key) - }) - - cr.Spec.ForProvider.TagSpecifications = []*svcapitypes.TagSpecification{&transitGatewayTags} - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/ec2/transitgatewayroutetable/setup.go b/pkg/controller/ec2/transitgatewayroutetable/setup.go index 3369635123..cc756bede9 100644 --- a/pkg/controller/ec2/transitgatewayroutetable/setup.go +++ b/pkg/controller/ec2/transitgatewayroutetable/setup.go @@ -2,10 +2,8 @@ package transitgatewayroutetable import ( "context" - "sort" "strings" - "github.com/aws/aws-sdk-go/aws" svcsdk "github.com/aws/aws-sdk-go/service/ec2" svcsdkapi "github.com/aws/aws-sdk-go/service/ec2/ec2iface" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" @@ -25,10 +23,6 @@ import ( "github.com/crossplane-contrib/provider-aws/pkg/utils/pointer" ) -const ( - errKubeUpdateFailed = "cannot update TransitGateway" -) - // SetupTransitGatewayRouteTable adds a controller that reconciles TransitGatewayRouteTable. func SetupTransitGatewayRouteTable(mgr ctrl.Manager, o controller.Options) error { name := managed.ControllerName(svcapitypes.RouteGroupKind) @@ -52,7 +46,7 @@ func SetupTransitGatewayRouteTable(mgr ctrl.Manager, o controller.Options) error managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), + managed.WithInitializers(), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), managed.WithConnectionPublishers(cps...), } @@ -148,42 +142,3 @@ func postDelete(_ context.Context, cr *svcapitypes.TransitGatewayRouteTable, obj } return err } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd cpresource.Managed) error { - cr, ok := mgd.(*svcapitypes.TransitGatewayRouteTable) - if !ok { - return errors.New(errUnexpectedObject) - } - var transitGatewayRouteTableTags svcapitypes.TagSpecification - for _, tagSpecification := range cr.Spec.ForProvider.TagSpecifications { - if pointer.StringValue(tagSpecification.ResourceType) == "transit-gateway-route-table" { - transitGatewayRouteTableTags = *tagSpecification - } - } - - tagMap := map[string]string{} - tagMap["Name"] = cr.Name - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[pointer.StringValue(t.Key)] = pointer.StringValue(t.Value) - } - for k, v := range cpresource.GetExternalTags(mgd) { - tagMap[k] = v - } - transitGatewayRouteTableTags.Tags = make([]*svcapitypes.Tag, len(tagMap)) - transitGatewayRouteTableTags.ResourceType = aws.String("transit-gateway-route-table") - i := 0 - for k, v := range tagMap { - transitGatewayRouteTableTags.Tags[i] = &svcapitypes.Tag{Key: aws.String(k), Value: aws.String(v)} - i++ - } - sort.Slice(transitGatewayRouteTableTags.Tags, func(i, j int) bool { - return pointer.StringValue(transitGatewayRouteTableTags.Tags[i].Key) < pointer.StringValue(transitGatewayRouteTableTags.Tags[j].Key) - }) - - cr.Spec.ForProvider.TagSpecifications = []*svcapitypes.TagSpecification{&transitGatewayRouteTableTags} - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/ec2/transitgatewayvpcattachment/setup.go b/pkg/controller/ec2/transitgatewayvpcattachment/setup.go index 595f88f8df..87f544f9e1 100644 --- a/pkg/controller/ec2/transitgatewayvpcattachment/setup.go +++ b/pkg/controller/ec2/transitgatewayvpcattachment/setup.go @@ -2,7 +2,6 @@ package transitgatewayvpcattachment import ( "context" - "sort" "github.com/aws/aws-sdk-go/aws" svcsdk "github.com/aws/aws-sdk-go/service/ec2" @@ -24,10 +23,6 @@ import ( "github.com/crossplane-contrib/provider-aws/pkg/utils/pointer" ) -const ( - errKubeUpdateFailed = "cannot update TransitGatewayAttachment" -) - // SetupTransitGatewayVPCAttachment adds a controller that reconciles TransitGatewayVPCAttachment. func SetupTransitGatewayVPCAttachment(mgr ctrl.Manager, o controller.Options) error { name := managed.ControllerName(svcapitypes.TransitGatewayVPCAttachmentGroupKind) @@ -49,7 +44,6 @@ func SetupTransitGatewayVPCAttachment(mgr ctrl.Manager, o controller.Options) er reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), managed.WithConnectionPublishers(cps...), } @@ -139,42 +133,3 @@ func postCreate(ctx context.Context, cr *svcapitypes.TransitGatewayVPCAttachment meta.SetExternalName(cr, aws.StringValue(obj.TransitGatewayVpcAttachment.TransitGatewayAttachmentId)) return cre, nil } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*svcapitypes.TransitGatewayVPCAttachment) - if !ok { - return errors.New(errUnexpectedObject) - } - var transitGatewayAttachmentTags svcapitypes.TagSpecification - for _, tagSpecification := range cr.Spec.ForProvider.TagSpecifications { - if aws.StringValue(tagSpecification.ResourceType) == "transit-gateway-attachment" { - transitGatewayAttachmentTags = *tagSpecification - } - } - - tagMap := map[string]string{} - tagMap["Name"] = cr.Name - for _, t := range transitGatewayAttachmentTags.Tags { - tagMap[aws.StringValue(t.Key)] = aws.StringValue(t.Value) - } - for k, v := range resource.GetExternalTags(mgd) { - tagMap[k] = v - } - transitGatewayAttachmentTags.Tags = make([]*svcapitypes.Tag, len(tagMap)) - transitGatewayAttachmentTags.ResourceType = aws.String("transit-gateway-attachment") - i := 0 - for k, v := range tagMap { - transitGatewayAttachmentTags.Tags[i] = &svcapitypes.Tag{Key: aws.String(k), Value: aws.String(v)} - i++ - } - sort.Slice(transitGatewayAttachmentTags.Tags, func(i, j int) bool { - return aws.StringValue(transitGatewayAttachmentTags.Tags[i].Key) < aws.StringValue(transitGatewayAttachmentTags.Tags[j].Key) - }) - - cr.Spec.ForProvider.TagSpecifications = []*svcapitypes.TagSpecification{&transitGatewayAttachmentTags} - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/ec2/vpc/controller.go b/pkg/controller/ec2/vpc/controller.go index 0495f5c6cd..602906b966 100644 --- a/pkg/controller/ec2/vpc/controller.go +++ b/pkg/controller/ec2/vpc/controller.go @@ -18,7 +18,6 @@ package vpc import ( "context" - "sort" "time" "github.com/aws/aws-sdk-go-v2/aws" @@ -72,7 +71,6 @@ func SetupVPC(mgr ctrl.Manager, o controller.Options) error { managed.WithCreationGracePeriod(3 * time.Minute), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), managed.WithConnectionPublishers(), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), @@ -301,31 +299,3 @@ func (e *external) Delete(ctx context.Context, mgd resource.Managed) error { return errorutils.Wrap(resource.Ignore(ec2.IsVPCNotFoundErr, err), errDelete) } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*v1beta1.VPC) - if !ok { - return errors.New(errUnexpectedObject) - } - tagMap := map[string]string{} - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[t.Key] = t.Value - } - for k, v := range resource.GetExternalTags(mgd) { - tagMap[k] = v - } - cr.Spec.ForProvider.Tags = make([]v1beta1.Tag, len(tagMap)) - i := 0 - for k, v := range tagMap { - cr.Spec.ForProvider.Tags[i] = v1beta1.Tag{Key: k, Value: v} - i++ - } - sort.Slice(cr.Spec.ForProvider.Tags, func(i, j int) bool { - return cr.Spec.ForProvider.Tags[i].Key < cr.Spec.ForProvider.Tags[j].Key - }) - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/ec2/vpc/controller_test.go b/pkg/controller/ec2/vpc/controller_test.go index a558c254d6..0925960a8b 100644 --- a/pkg/controller/ec2/vpc/controller_test.go +++ b/pkg/controller/ec2/vpc/controller_test.go @@ -26,10 +26,8 @@ import ( xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" - "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" "github.com/pkg/errors" "sigs.k8s.io/controller-runtime/pkg/client" @@ -56,16 +54,6 @@ type args struct { type vpcModifier func(*v1beta1.VPC) -func withTags(tagMaps ...map[string]string) vpcModifier { - var tagList []v1beta1.Tag - for _, tagMap := range tagMaps { - for k, v := range tagMap { - tagList = append(tagList, v1beta1.Tag{Key: k, Value: v}) - } - } - return func(r *v1beta1.VPC) { r.Spec.ForProvider.Tags = tagList } -} - func withExternalName(name string) vpcModifier { return func(r *v1beta1.VPC) { meta.SetExternalName(r, name) } } @@ -452,52 +440,3 @@ func TestDelete(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type args struct { - cr *v1beta1.VPC - kube client.Client - } - type want struct { - cr *v1beta1.VPC - err error - } - - cases := map[string]struct { - args - want - }{ - "Successful": { - args: args{ - cr: vpc(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: vpc(withTags(resource.GetExternalTags(vpc()), map[string]string{"foo": "bar"})), - }, - }, - "UpdateFailed": { - args: args{ - cr: vpc(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errors.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, cmpopts.SortSlices(func(a, b v1beta1.Tag) bool { return a.Key > b.Key })); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/ec2/vpcendpoint/setup.go b/pkg/controller/ec2/vpcendpoint/setup.go index ae0d97acce..5c0b8fec4e 100644 --- a/pkg/controller/ec2/vpcendpoint/setup.go +++ b/pkg/controller/ec2/vpcendpoint/setup.go @@ -2,7 +2,6 @@ package vpcendpoint import ( "context" - "sort" "strings" "github.com/aws/aws-sdk-go/aws" @@ -41,7 +40,6 @@ func SetupVPCEndpoint(mgr ctrl.Manager, o controller.Options) error { managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), managed.WithConnectionPublishers(cps...), } @@ -356,52 +354,3 @@ compare: return true } - -const ( - errKubeUpdateFailed = "cannot update Address custom resource" -) - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd cpresource.Managed) error { - cr, ok := mgd.(*svcapitypes.VPCEndpoint) - if !ok { - return errors.New(errUnexpectedObject) - } - var vpcEndpointTags svcapitypes.TagSpecification - for _, tagSpecification := range cr.Spec.ForProvider.TagSpecifications { - if tagSpecification == nil { - continue - } - if aws.StringValue(tagSpecification.ResourceType) == "vpc-endpoint" { - vpcEndpointTags = *tagSpecification - } - } - - var tagMap map[string]string - if cr.Spec.ForProvider.Tags != nil { - tagMap = cr.Spec.ForProvider.Tags - } else { - tagMap = map[string]string{} - } - - tagMap["Name"] = cr.Name - for k, v := range cpresource.GetExternalTags(mgd) { - tagMap[k] = v - } - vpcEndpointTags.Tags = make([]*svcapitypes.Tag, len(tagMap)) - vpcEndpointTags.ResourceType = aws.String("vpc-endpoint") - i := 0 - for k, v := range tagMap { - vpcEndpointTags.Tags[i] = &svcapitypes.Tag{Key: aws.String(k), Value: aws.String(v)} - i++ - } - sort.Slice(vpcEndpointTags.Tags, func(i, j int) bool { - return aws.StringValue(vpcEndpointTags.Tags[i].Key) < aws.StringValue(vpcEndpointTags.Tags[j].Key) - }) - - cr.Spec.ForProvider.TagSpecifications = []*svcapitypes.TagSpecification{&vpcEndpointTags} - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/ec2/vpcendpoint/setup_test.go b/pkg/controller/ec2/vpcendpoint/setup_test.go index 7debc35d1b..49525f9a55 100644 --- a/pkg/controller/ec2/vpcendpoint/setup_test.go +++ b/pkg/controller/ec2/vpcendpoint/setup_test.go @@ -27,12 +27,10 @@ import ( xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" - cpresource "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/utils/ptr" "sigs.k8s.io/controller-runtime/pkg/client" "github.com/crossplane-contrib/provider-aws/apis/ec2/v1alpha1" @@ -60,10 +58,6 @@ type args struct { type vpcEndpointModifier func(*v1alpha1.VPCEndpoint) -func withName(name string) vpcEndpointModifier { - return func(r *v1alpha1.VPCEndpoint) { r.SetName(name) } -} - func withExternalName(name string) vpcEndpointModifier { return func(r *v1alpha1.VPCEndpoint) { meta.SetExternalName(r, name) } } @@ -573,138 +567,3 @@ func TestObserve(t *testing.T) { }) } } - -func TestTagger(t *testing.T) { - type want struct { - cr *v1alpha1.VPCEndpoint - err error - } - - tag := func(k, v string) *v1alpha1.Tag { - return &v1alpha1.Tag{Key: ptr.To(k), Value: ptr.To(v)} - } - - cases := map[string]struct { - args - want - }{ - "ShouldAddTagsIfSpecIsNil": { - args: args{ - kube: &test.MockClient{ - MockUpdate: test.NewMockUpdateFn(nil), - }, - cr: vpcEndpoint( - withName("test"), - withSpec(v1alpha1.VPCEndpointParameters{}), - ), - }, - want: want{ - cr: vpcEndpoint( - withName("test"), - withSpec(v1alpha1.VPCEndpointParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-endpoint"), - Tags: []*v1alpha1.Tag{ - tag("Name", "test"), - tag(cpresource.ExternalResourceTagKeyKind, ""), - tag(cpresource.ExternalResourceTagKeyName, "test"), - }, - }, - }, - }), - ), - }, - }, - "ShouldOverwriteTags": { - args: args{ - kube: &test.MockClient{ - MockUpdate: test.NewMockUpdateFn(nil), - }, - cr: vpcEndpoint( - withName("test"), - withSpec(v1alpha1.VPCEndpointParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-endpoint"), - Tags: []*v1alpha1.Tag{ - tag(cpresource.ExternalResourceTagKeyName, "preset"), - }, - }, - }, - }), - ), - }, - want: want{ - cr: vpcEndpoint( - withName("test"), - withSpec(v1alpha1.VPCEndpointParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-endpoint"), - Tags: []*v1alpha1.Tag{ - tag("Name", "test"), - tag(cpresource.ExternalResourceTagKeyKind, ""), - tag(cpresource.ExternalResourceTagKeyName, "test"), - }, - }, - }, - }), - ), - }, - }, - "ShouldMergeTags": { - args: args{ - kube: &test.MockClient{ - MockUpdate: test.NewMockUpdateFn(nil), - }, - cr: vpcEndpoint( - withName("test"), - withSpec(v1alpha1.VPCEndpointParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-endpoint"), - Tags: []*v1alpha1.Tag{ - tag("Name", "test"), - tag(cpresource.ExternalResourceTagKeyKind, ""), - tag(cpresource.ExternalResourceTagKeyName, "test"), - }, - }, - }, - }), - ), - }, - want: want{ - cr: vpcEndpoint( - withName("test"), - withSpec(v1alpha1.VPCEndpointParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-endpoint"), - Tags: []*v1alpha1.Tag{ - tag("Name", "test"), - tag(cpresource.ExternalResourceTagKeyKind, ""), - tag(cpresource.ExternalResourceTagKeyName, "test"), - }, - }, - }, - }), - ), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - ta := tagger{kube: tc.args.kube} - err := ta.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, test.EquateConditions()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/ec2/vpcendpointserviceconfiguration/setup.go b/pkg/controller/ec2/vpcendpointserviceconfiguration/setup.go index b8220f04dc..f206431b34 100644 --- a/pkg/controller/ec2/vpcendpointserviceconfiguration/setup.go +++ b/pkg/controller/ec2/vpcendpointserviceconfiguration/setup.go @@ -2,7 +2,6 @@ package vpcendpointserviceconfiguration import ( "context" - "sort" "github.com/aws/aws-sdk-go/aws" svcsdk "github.com/aws/aws-sdk-go/service/ec2" @@ -16,7 +15,6 @@ import ( cpresource "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/pkg/errors" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" svcapitypes "github.com/crossplane-contrib/provider-aws/apis/ec2/v1alpha1" "github.com/crossplane-contrib/provider-aws/apis/v1alpha1" @@ -26,10 +24,6 @@ import ( "github.com/crossplane-contrib/provider-aws/pkg/utils/pointer" ) -const ( - errKubeUpdateFailed = "cannot update VPCEndpointServiceConfiguration" -) - // SetupVPCEndpointServiceConfiguration adds a controller that reconciles VPCEndpointServiceConfiguration. func SetupVPCEndpointServiceConfiguration(mgr ctrl.Manager, o controller.Options) error { name := managed.ControllerName(svcapitypes.VPCEndpointServiceConfigurationGroupKind) @@ -55,7 +49,7 @@ func SetupVPCEndpointServiceConfiguration(mgr ctrl.Manager, o controller.Options reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), managed.WithConnectionPublishers(cps...), } @@ -214,48 +208,6 @@ func (u *updater) delete(ctx context.Context, mg cpresource.Managed) error { return errorutils.Wrap(cpresource.Ignore(ec2.IsVPCNotFoundErr, err), errDelete) } -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd cpresource.Managed) error { - cr, ok := mgd.(*svcapitypes.VPCEndpointServiceConfiguration) - if !ok { - return errors.New(errUnexpectedObject) - } - var vpcEndpointTags svcapitypes.TagSpecification - for _, tagSpecification := range cr.Spec.ForProvider.TagSpecifications { - if tagSpecification == nil { - continue - } - if aws.StringValue(tagSpecification.ResourceType) == "vpc-endpoint-service" { - vpcEndpointTags = *tagSpecification - } - } - - tagMap := map[string]string{} - tagMap["Name"] = cr.Name - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[aws.StringValue(t.Key)] = aws.StringValue(t.Value) - } - for k, v := range cpresource.GetExternalTags(mgd) { - tagMap[k] = v - } - vpcEndpointTags.Tags = make([]*svcapitypes.Tag, len(tagMap)) - vpcEndpointTags.ResourceType = aws.String("vpc-endpoint-service") - i := 0 - for k, v := range tagMap { - vpcEndpointTags.Tags[i] = &svcapitypes.Tag{Key: aws.String(k), Value: aws.String(v)} - i++ - } - sort.Slice(vpcEndpointTags.Tags, func(i, j int) bool { - return aws.StringValue(vpcEndpointTags.Tags[i].Key) < aws.StringValue(vpcEndpointTags.Tags[j].Key) - }) - - cr.Spec.ForProvider.TagSpecifications = []*svcapitypes.TagSpecification{&vpcEndpointTags} - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} - // DifferenceARN returns the lists of ARNs that need to be removed and added according // to current and desired states. func DifferenceARN(local []*string, remote []*string) ([]*string, []*string) { diff --git a/pkg/controller/ec2/vpcendpointserviceconfiguration/setup_test.go b/pkg/controller/ec2/vpcendpointserviceconfiguration/setup_test.go deleted file mode 100644 index a00bf6215d..0000000000 --- a/pkg/controller/ec2/vpcendpointserviceconfiguration/setup_test.go +++ /dev/null @@ -1,188 +0,0 @@ -/* -Copyright 2023 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 vpcendpointserviceconfiguration - -import ( - "context" - "testing" - - "github.com/aws/aws-sdk-go/aws" - cpresource "github.com/crossplane/crossplane-runtime/pkg/resource" - "github.com/crossplane/crossplane-runtime/pkg/test" - "github.com/google/go-cmp/cmp" - "k8s.io/utils/ptr" - "sigs.k8s.io/controller-runtime/pkg/client" - - "github.com/crossplane-contrib/provider-aws/apis/ec2/v1alpha1" -) - -type args struct { - kube client.Client - cr *v1alpha1.VPCEndpointServiceConfiguration -} - -type vPCEndpointServiceConfigurationModifier func(*v1alpha1.VPCEndpointServiceConfiguration) - -func withName(name string) vPCEndpointServiceConfigurationModifier { - return func(r *v1alpha1.VPCEndpointServiceConfiguration) { r.SetName(name) } -} - -func withSpec(p v1alpha1.VPCEndpointServiceConfigurationParameters) vPCEndpointServiceConfigurationModifier { - return func(o *v1alpha1.VPCEndpointServiceConfiguration) { o.Spec.ForProvider = p } -} -func vPCEndpointServiceConfiguration(m ...vPCEndpointServiceConfigurationModifier) *v1alpha1.VPCEndpointServiceConfiguration { - cr := &v1alpha1.VPCEndpointServiceConfiguration{} - for _, f := range m { - f(cr) - } - return cr -} - -func TestTagger(t *testing.T) { - type want struct { - cr *v1alpha1.VPCEndpointServiceConfiguration - err error - } - - tag := func(k, v string) *v1alpha1.Tag { - return &v1alpha1.Tag{Key: ptr.To(k), Value: ptr.To(v)} - } - - cases := map[string]struct { - args - want - }{ - "ShouldAddTagsIfSpecIsNil": { - args: args{ - kube: &test.MockClient{ - MockUpdate: test.NewMockUpdateFn(nil), - }, - cr: vPCEndpointServiceConfiguration( - withName("test"), - withSpec(v1alpha1.VPCEndpointServiceConfigurationParameters{}), - ), - }, - want: want{ - cr: vPCEndpointServiceConfiguration( - withName("test"), - withSpec(v1alpha1.VPCEndpointServiceConfigurationParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-endpoint-service"), - Tags: []*v1alpha1.Tag{ - tag("Name", "test"), - tag(cpresource.ExternalResourceTagKeyKind, ""), - tag(cpresource.ExternalResourceTagKeyName, "test"), - }, - }, - }, - }), - ), - }, - }, - "ShouldOverwriteTags": { - args: args{ - kube: &test.MockClient{ - MockUpdate: test.NewMockUpdateFn(nil), - }, - cr: vPCEndpointServiceConfiguration( - withName("test"), - withSpec(v1alpha1.VPCEndpointServiceConfigurationParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-endpoint-service"), - Tags: []*v1alpha1.Tag{ - tag(cpresource.ExternalResourceTagKeyName, "preset"), - }, - }, - }, - }), - ), - }, - want: want{ - cr: vPCEndpointServiceConfiguration( - withName("test"), - withSpec(v1alpha1.VPCEndpointServiceConfigurationParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-endpoint-service"), - Tags: []*v1alpha1.Tag{ - tag("Name", "test"), - tag(cpresource.ExternalResourceTagKeyKind, ""), - tag(cpresource.ExternalResourceTagKeyName, "test"), - }, - }, - }, - }), - ), - }, - }, - "ShouldMergeTags": { - args: args{ - kube: &test.MockClient{ - MockUpdate: test.NewMockUpdateFn(nil), - }, - cr: vPCEndpointServiceConfiguration( - withName("test"), - withSpec(v1alpha1.VPCEndpointServiceConfigurationParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-endpoint-service"), - Tags: []*v1alpha1.Tag{ - tag("Name", "test"), - tag(cpresource.ExternalResourceTagKeyKind, ""), - tag(cpresource.ExternalResourceTagKeyName, "test"), - }, - }, - }, - }), - ), - }, - want: want{ - cr: vPCEndpointServiceConfiguration( - withName("test"), - withSpec(v1alpha1.VPCEndpointServiceConfigurationParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-endpoint-service"), - Tags: []*v1alpha1.Tag{ - tag("Name", "test"), - tag(cpresource.ExternalResourceTagKeyKind, ""), - tag(cpresource.ExternalResourceTagKeyName, "test"), - }, - }, - }, - }), - ), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - ta := tagger{kube: tc.args.kube} - err := ta.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, test.EquateConditions()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/ec2/vpcpeeringconnection/setup.go b/pkg/controller/ec2/vpcpeeringconnection/setup.go index 860da41200..d55049b88f 100644 --- a/pkg/controller/ec2/vpcpeeringconnection/setup.go +++ b/pkg/controller/ec2/vpcpeeringconnection/setup.go @@ -3,7 +3,6 @@ package vpcpeeringconnection import ( "context" "reflect" - "sort" "time" "github.com/aws/aws-sdk-go/aws" @@ -27,10 +26,6 @@ import ( "github.com/crossplane-contrib/provider-aws/pkg/utils/pointer" ) -const ( - errKubeUpdateFailed = "cannot update VPCPeeringConnection" -) - // SetupVPCPeeringConnection adds a controller that reconciles VPCPeeringConnection. func SetupVPCPeeringConnection(mgr ctrl.Manager, o controller.Options) error { name := managed.ControllerName(svcapitypes.VPCPeeringConnectionGroupKind) @@ -54,7 +49,6 @@ func SetupVPCPeeringConnection(mgr ctrl.Manager, o controller.Options) error { managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithCreationGracePeriod(3 * time.Minute), managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), managed.WithConnectionPublishers(cps...), } @@ -235,45 +229,3 @@ func (e *custom) postCreate(_ context.Context, cr *svcapitypes.VPCPeeringConnect meta.SetExternalName(cr, aws.StringValue(obj.VpcPeeringConnection.VpcPeeringConnectionId)) return cre, nil } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*svcapitypes.VPCPeeringConnection) - if !ok { - return errors.New(errUnexpectedObject) - } - var vpcPeeringConnectionTags svcapitypes.TagSpecification - for _, tagSpecification := range cr.Spec.ForProvider.TagSpecifications { - if tagSpecification == nil { - continue - } - if aws.StringValue(tagSpecification.ResourceType) == "vpc-peering-connection" { - vpcPeeringConnectionTags = *tagSpecification - } - } - - tagMap := map[string]string{} - tagMap["Name"] = cr.Name - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[aws.StringValue(t.Key)] = aws.StringValue(t.Value) - } - for k, v := range resource.GetExternalTags(mgd) { - tagMap[k] = v - } - vpcPeeringConnectionTags.Tags = make([]*svcapitypes.Tag, len(tagMap)) - vpcPeeringConnectionTags.ResourceType = aws.String("vpc-peering-connection") - i := 0 - for k, v := range tagMap { - vpcPeeringConnectionTags.Tags[i] = &svcapitypes.Tag{Key: aws.String(k), Value: aws.String(v)} - i++ - } - sort.Slice(vpcPeeringConnectionTags.Tags, func(i, j int) bool { - return aws.StringValue(vpcPeeringConnectionTags.Tags[i].Key) < aws.StringValue(vpcPeeringConnectionTags.Tags[j].Key) - }) - - cr.Spec.ForProvider.TagSpecifications = []*svcapitypes.TagSpecification{&vpcPeeringConnectionTags} - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/ec2/vpcpeeringconnection/setup_test.go b/pkg/controller/ec2/vpcpeeringconnection/setup_test.go deleted file mode 100644 index 6d41f9a96d..0000000000 --- a/pkg/controller/ec2/vpcpeeringconnection/setup_test.go +++ /dev/null @@ -1,189 +0,0 @@ -/* -Copyright 2023 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 vpcpeeringconnection - -import ( - "context" - "testing" - - "github.com/aws/aws-sdk-go/aws" - cpresource "github.com/crossplane/crossplane-runtime/pkg/resource" - "github.com/crossplane/crossplane-runtime/pkg/test" - "github.com/google/go-cmp/cmp" - "k8s.io/utils/ptr" - "sigs.k8s.io/controller-runtime/pkg/client" - - "github.com/crossplane-contrib/provider-aws/apis/ec2/v1alpha1" -) - -type args struct { - kube client.Client - cr *v1alpha1.VPCPeeringConnection -} - -type vPCPeeringConnectionModifier func(*v1alpha1.VPCPeeringConnection) - -func withName(name string) vPCPeeringConnectionModifier { - return func(r *v1alpha1.VPCPeeringConnection) { r.SetName(name) } -} - -func withSpec(p v1alpha1.VPCPeeringConnectionParameters) vPCPeeringConnectionModifier { - return func(o *v1alpha1.VPCPeeringConnection) { o.Spec.ForProvider = p } -} - -func vPCPeeringConnection(m ...vPCPeeringConnectionModifier) *v1alpha1.VPCPeeringConnection { - cr := &v1alpha1.VPCPeeringConnection{} - for _, f := range m { - f(cr) - } - return cr -} - -func TestTagger(t *testing.T) { - type want struct { - cr *v1alpha1.VPCPeeringConnection - err error - } - - tag := func(k, v string) *v1alpha1.Tag { - return &v1alpha1.Tag{Key: ptr.To(k), Value: ptr.To(v)} - } - - cases := map[string]struct { - args - want - }{ - "ShouldAddTagsIfSpecIsNil": { - args: args{ - kube: &test.MockClient{ - MockUpdate: test.NewMockUpdateFn(nil), - }, - cr: vPCPeeringConnection( - withName("test"), - withSpec(v1alpha1.VPCPeeringConnectionParameters{}), - ), - }, - want: want{ - cr: vPCPeeringConnection( - withName("test"), - withSpec(v1alpha1.VPCPeeringConnectionParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-peering-connection"), - Tags: []*v1alpha1.Tag{ - tag("Name", "test"), - tag(cpresource.ExternalResourceTagKeyKind, ""), - tag(cpresource.ExternalResourceTagKeyName, "test"), - }, - }, - }, - }), - ), - }, - }, - "ShouldOverwriteTags": { - args: args{ - kube: &test.MockClient{ - MockUpdate: test.NewMockUpdateFn(nil), - }, - cr: vPCPeeringConnection( - withName("test"), - withSpec(v1alpha1.VPCPeeringConnectionParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-peering-connection"), - Tags: []*v1alpha1.Tag{ - tag(cpresource.ExternalResourceTagKeyName, "preset"), - }, - }, - }, - }), - ), - }, - want: want{ - cr: vPCPeeringConnection( - withName("test"), - withSpec(v1alpha1.VPCPeeringConnectionParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-peering-connection"), - Tags: []*v1alpha1.Tag{ - tag("Name", "test"), - tag(cpresource.ExternalResourceTagKeyKind, ""), - tag(cpresource.ExternalResourceTagKeyName, "test"), - }, - }, - }, - }), - ), - }, - }, - "ShouldMergeTags": { - args: args{ - kube: &test.MockClient{ - MockUpdate: test.NewMockUpdateFn(nil), - }, - cr: vPCPeeringConnection( - withName("test"), - withSpec(v1alpha1.VPCPeeringConnectionParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-peering-connection"), - Tags: []*v1alpha1.Tag{ - tag("Name", "test"), - tag(cpresource.ExternalResourceTagKeyKind, ""), - tag(cpresource.ExternalResourceTagKeyName, "test"), - }, - }, - }, - }), - ), - }, - want: want{ - cr: vPCPeeringConnection( - withName("test"), - withSpec(v1alpha1.VPCPeeringConnectionParameters{ - TagSpecifications: []*v1alpha1.TagSpecification{ - { - ResourceType: aws.String("vpc-peering-connection"), - Tags: []*v1alpha1.Tag{ - tag("Name", "test"), - tag(cpresource.ExternalResourceTagKeyKind, ""), - tag(cpresource.ExternalResourceTagKeyName, "test"), - }, - }, - }, - }), - ), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - ta := tagger{kube: tc.args.kube} - err := ta.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, test.EquateConditions()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/ecr/repository/controller.go b/pkg/controller/ecr/repository/controller.go index 20e24d6fde..d09a73e5a5 100644 --- a/pkg/controller/ecr/repository/controller.go +++ b/pkg/controller/ecr/repository/controller.go @@ -74,7 +74,7 @@ func SetupRepository(mgr ctrl.Manager, o controller.Options) error { managed.WithExternalConnecter(&connector{kube: mgr.GetClient()}), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), managed.WithConnectionPublishers(), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), @@ -255,32 +255,6 @@ func (e *external) Delete(ctx context.Context, mgd resource.Managed) error { return errorutils.Wrap(resource.Ignore(ecr.IsRepoNotFoundErr, err), errDelete) } -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*v1beta1.Repository) - if !ok { - return errors.New(errUnexpectedObject) - } - added := false - tagMap := map[string]string{} - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[t.Key] = t.Value - } - for k, v := range resource.GetExternalTags(mgd) { - if tagMap[k] != v { - cr.Spec.ForProvider.Tags = append(cr.Spec.ForProvider.Tags, v1beta1.Tag{Key: k, Value: v}) - added = true - } - } - if !added { - return nil - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} - func (e *external) updateTags(ctx context.Context, repo *v1beta1.Repository) error { resp, err := e.client.ListTagsForResource(ctx, &awsecr.ListTagsForResourceInput{ResourceArn: &repo.Status.AtProvider.RepositoryArn}) if err != nil { diff --git a/pkg/controller/ecr/repository/controller_test.go b/pkg/controller/ecr/repository/controller_test.go index 48f7020da8..b1cbc569c3 100644 --- a/pkg/controller/ecr/repository/controller_test.go +++ b/pkg/controller/ecr/repository/controller_test.go @@ -26,10 +26,8 @@ import ( xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" - "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" "github.com/pkg/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -67,16 +65,6 @@ type args struct { type repositoryModifier func(*v1beta1.Repository) -func withTags(tagMaps ...map[string]string) repositoryModifier { - var tagList []v1beta1.Tag - for _, tagMap := range tagMaps { - for k, v := range tagMap { - tagList = append(tagList, v1beta1.Tag{Key: k, Value: v}) - } - } - return func(r *v1beta1.Repository) { r.Spec.ForProvider.Tags = tagList } -} - func withExternalName(name string) repositoryModifier { return func(r *v1beta1.Repository) { meta.SetExternalName(r, name) } } @@ -625,60 +613,3 @@ func TestDelete(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type args struct { - cr *v1beta1.Repository - kube client.Client - } - type want struct { - cr *v1beta1.Repository - err error - } - - cases := map[string]struct { - args - want - }{ - "Successful": { - args: args{ - cr: repository(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: repository(withTags(resource.GetExternalTags(repository()), map[string]string{"foo": "bar"})), - }, - }, - "UpdateNotNeeded": { - args: args{ - cr: repository(withTags(resource.GetExternalTags(repository()), map[string]string{"foo": "bar"})), - }, - want: want{ - cr: repository(withTags(resource.GetExternalTags(repository()), map[string]string{"foo": "bar"})), - }, - }, - "UpdateFailed": { - args: args{ - cr: repository(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errors.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, cmpopts.SortSlices(func(a, b v1beta1.Tag) bool { return a.Key > b.Key })); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/eks/addon/setup.go b/pkg/controller/eks/addon/setup.go index dbd8418c98..7198275b4b 100644 --- a/pkg/controller/eks/addon/setup.go +++ b/pkg/controller/eks/addon/setup.go @@ -63,7 +63,6 @@ func SetupAddon(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), @@ -259,21 +258,3 @@ func preDelete(_ context.Context, cr *eksv1alpha1.Addon, obj *awseks.DeleteAddon obj.ClusterName = cr.Spec.ForProvider.ClusterName return false, nil } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*eksv1alpha1.Addon) - if !ok { - return errors.New(errNotEKSCluster) - } - if cr.Spec.ForProvider.Tags == nil { - cr.Spec.ForProvider.Tags = map[string]*string{} - } - for k, v := range resource.GetExternalTags(mg) { - cr.Spec.ForProvider.Tags[k] = pointer.ToOrNilIfZeroValue(v) - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/eks/cluster/cluster.go b/pkg/controller/eks/cluster/cluster.go index 92ddc33001..50dbfeb78b 100644 --- a/pkg/controller/eks/cluster/cluster.go +++ b/pkg/controller/eks/cluster/cluster.go @@ -68,7 +68,7 @@ func SetupCluster(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), newClientFn: eks.NewEKSClient, newSTSClientFn: eks.NewSTSClient}), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), @@ -233,21 +233,3 @@ func (e *external) Delete(ctx context.Context, mg resource.Managed) error { _, err := e.client.DeleteCluster(ctx, &awseks.DeleteClusterInput{Name: pointer.ToOrNilIfZeroValue(meta.GetExternalName(cr))}) return errorutils.Wrap(resource.Ignore(eks.IsErrorNotFound, err), errDeleteFailed) } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*v1beta1.Cluster) - if !ok { - return errors.New(errNotEKSCluster) - } - if cr.Spec.ForProvider.Tags == nil { - cr.Spec.ForProvider.Tags = map[string]string{} - } - for k, v := range resource.GetExternalTags(mg) { - cr.Spec.ForProvider.Tags[k] = v - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/eks/cluster/cluster_test.go b/pkg/controller/eks/cluster/cluster_test.go index 1c16654387..3269887d08 100644 --- a/pkg/controller/eks/cluster/cluster_test.go +++ b/pkg/controller/eks/cluster/cluster_test.go @@ -24,7 +24,6 @@ import ( awsekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" - "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -631,48 +630,3 @@ func TestDelete(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type want struct { - cr *v1beta1.Cluster - err error - } - - cases := map[string]struct { - args - want - }{ - "Successful": { - args: args{ - cr: cluster(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: cluster(withTags(resource.GetExternalTags(cluster()), (map[string]string{"foo": "bar"}))), - }, - }, - "UpdateFailed": { - args: args{ - cr: cluster(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errors.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/eks/fargateprofile/controller.go b/pkg/controller/eks/fargateprofile/controller.go index c6a33f49b9..a451e06e50 100644 --- a/pkg/controller/eks/fargateprofile/controller.go +++ b/pkg/controller/eks/fargateprofile/controller.go @@ -63,7 +63,7 @@ func SetupFargateProfile(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), newEKSClientFn: eks.NewEKSClient}), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), @@ -195,28 +195,3 @@ func (e *external) Delete(ctx context.Context, mg resource.Managed) error { _, err := e.client.DeleteFargateProfile(ctx, &awseks.DeleteFargateProfileInput{FargateProfileName: pointer.ToOrNilIfZeroValue(meta.GetExternalName(cr)), ClusterName: &cr.Spec.ForProvider.ClusterName}) return errorutils.Wrap(resource.Ignore(eks.IsErrorNotFound, err), errDeleteFailed) } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*v1beta1.FargateProfile) - if !ok { - return errors.New(errNotEKSFargateProfile) - } - changed := false - if cr.Spec.ForProvider.Tags == nil { - cr.Spec.ForProvider.Tags = map[string]string{} - } - for k, v := range resource.GetExternalTags(mg) { - if cr.Spec.ForProvider.Tags[k] != v { - cr.Spec.ForProvider.Tags[k] = v - changed = true - } - } - if !changed { - return nil - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/eks/fargateprofile/controller_test.go b/pkg/controller/eks/fargateprofile/controller_test.go index 4aea9fe5dd..1872e992b1 100644 --- a/pkg/controller/eks/fargateprofile/controller_test.go +++ b/pkg/controller/eks/fargateprofile/controller_test.go @@ -24,7 +24,6 @@ import ( awsekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" - "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -479,48 +478,3 @@ func TestDelete(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type want struct { - cr *v1beta1.FargateProfile - err error - } - - cases := map[string]struct { - args - want - }{ - "Successful": { - args: args{ - cr: fargateProfile(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: fargateProfile(withTags(resource.GetExternalTags(fargateProfile()), (map[string]string{"foo": "bar"}))), - }, - }, - "UpdateFailed": { - args: args{ - cr: fargateProfile(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errors.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/eks/identityproviderconfig/controller.go b/pkg/controller/eks/identityproviderconfig/controller.go index a98275ece5..540f2c3718 100644 --- a/pkg/controller/eks/identityproviderconfig/controller.go +++ b/pkg/controller/eks/identityproviderconfig/controller.go @@ -64,7 +64,7 @@ func SetupIdentityProviderConfig(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), newEKSClientFn: eks.NewEKSClient}), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), @@ -220,21 +220,3 @@ func (e *external) Delete(ctx context.Context, mg resource.Managed) error { _, err := e.client.DisassociateIdentityProviderConfig(ctx, eks.GenerateDisassociateIdentityProviderConfigInput(meta.GetExternalName(cr), cr.Spec.ForProvider.ClusterName)) return errorutils.Wrap(resource.Ignore(eks.IsErrorNotFound, err), errDeleteFailed) } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*manualv1alpha1.IdentityProviderConfig) - if !ok { - return errors.New(errNotEKSIdentityProviderConfig) - } - if cr.Spec.ForProvider.Tags == nil { - cr.Spec.ForProvider.Tags = map[string]string{} - } - for k, v := range resource.GetExternalTags(mg) { - cr.Spec.ForProvider.Tags[k] = v - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/eks/identityproviderconfig/controller_test.go b/pkg/controller/eks/identityproviderconfig/controller_test.go index 92e894301d..ba1719fc25 100644 --- a/pkg/controller/eks/identityproviderconfig/controller_test.go +++ b/pkg/controller/eks/identityproviderconfig/controller_test.go @@ -24,7 +24,6 @@ import ( awsekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" - "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -499,48 +498,3 @@ func TestDelete(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type want struct { - cr *manualv1alpha1.IdentityProviderConfig - err error - } - - cases := map[string]struct { - args - want - }{ - "Successful": { - args: args{ - cr: identityProviderConfig(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: identityProviderConfig(withTags(resource.GetExternalTags(identityProviderConfig()), map[string]string{"foo": "bar"})), - }, - }, - "UpdateFailed": { - args: args{ - cr: identityProviderConfig(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errors.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/eks/nodegroup/controller.go b/pkg/controller/eks/nodegroup/controller.go index ac2512a6a0..c658efef87 100644 --- a/pkg/controller/eks/nodegroup/controller.go +++ b/pkg/controller/eks/nodegroup/controller.go @@ -66,7 +66,7 @@ func SetupNodeGroup(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), newEKSClientFn: eks.NewEKSClient}), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), @@ -215,21 +215,3 @@ func (e *external) Delete(ctx context.Context, mg resource.Managed) error { _, err := e.client.DeleteNodegroup(ctx, &awseks.DeleteNodegroupInput{NodegroupName: pointer.ToOrNilIfZeroValue(meta.GetExternalName(cr)), ClusterName: &cr.Spec.ForProvider.ClusterName}) return errorutils.Wrap(resource.Ignore(eks.IsErrorNotFound, err), errDeleteFailed) } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*manualv1alpha1.NodeGroup) - if !ok { - return errors.New(errNotEKSNodeGroup) - } - if cr.Spec.ForProvider.Tags == nil { - cr.Spec.ForProvider.Tags = map[string]string{} - } - for k, v := range resource.GetExternalTags(mg) { - cr.Spec.ForProvider.Tags[k] = v - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/eks/nodegroup/controller_test.go b/pkg/controller/eks/nodegroup/controller_test.go index a63de195ee..4949748b0e 100644 --- a/pkg/controller/eks/nodegroup/controller_test.go +++ b/pkg/controller/eks/nodegroup/controller_test.go @@ -24,7 +24,6 @@ import ( awsekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types" xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" - "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" "github.com/pkg/errors" @@ -639,48 +638,3 @@ func TestDelete(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type want struct { - cr *manualv1alpha1.NodeGroup - err error - } - - cases := map[string]struct { - args - want - }{ - "Successful": { - args: args{ - cr: nodeGroup(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: nodeGroup(withTags(resource.GetExternalTags(nodeGroup()), (map[string]string{"foo": "bar"}))), - }, - }, - "UpdateFailed": { - args: args{ - cr: nodeGroup(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errors.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/iam/openidconnectprovider/controller.go b/pkg/controller/iam/openidconnectprovider/controller.go index e787018578..c081077d27 100644 --- a/pkg/controller/iam/openidconnectprovider/controller.go +++ b/pkg/controller/iam/openidconnectprovider/controller.go @@ -72,7 +72,6 @@ func SetupOpenIDConnectProvider(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), newClientFn: iam.NewOpenIDConnectProviderClient}), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), @@ -292,29 +291,3 @@ func (e *external) getOpenIDConnectProviderByTags(ctx context.Context, tags map[ } return nil, nil } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*v1beta1.OpenIDConnectProvider) - if !ok { - return errors.New(errUnexpectedObject) - } - added := false - tagMap := map[string]string{} - for _, t := range cr.Spec.ForProvider.Tags { - tagMap[t.Key] = t.Value - } - for k, v := range resource.GetExternalTags(mgd) { - if p, ok := tagMap[k]; !ok || v != p { - cr.Spec.ForProvider.Tags = append(cr.Spec.ForProvider.Tags, v1beta1.Tag{Key: k, Value: v}) - added = true - } - } - if !added { - return nil - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/iam/openidconnectprovider/controller_test.go b/pkg/controller/iam/openidconnectprovider/controller_test.go index 83f3b73c9f..4908c2d774 100644 --- a/pkg/controller/iam/openidconnectprovider/controller_test.go +++ b/pkg/controller/iam/openidconnectprovider/controller_test.go @@ -126,12 +126,6 @@ func withTags(tagMaps ...map[string]string) oidcProviderModifier { } } -func withGroupVersionKind() oidcProviderModifier { - return func(r *v1beta1.OpenIDConnectProvider) { - r.TypeMeta.SetGroupVersionKind(v1beta1.OpenIDConnectProviderGroupVersionKind) - } -} - func withClientIDList(l []string) oidcProviderModifier { return func(r *svcapitypes.OpenIDConnectProvider) { r.Spec.ForProvider.ClientIDList = l @@ -803,82 +797,3 @@ func TestDelete(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type args struct { - cr resource.Managed - kube client.Client - } - type want struct { - cr *v1beta1.OpenIDConnectProvider - err error - } - - cases := map[string]struct { - args - want - }{ - "InvalidInput": { - args: args{ - cr: unexpectedItem, - }, - want: want{ - err: errors.New(errUnexpectedObject), - }, - }, - "Successful": { - args: args{ - cr: oidcProvider(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: oidcProvider(withTags(resource.GetExternalTags(oidcProvider()), map[string]string{"foo": "bar"})), - }, - }, - "Check Tag values": { - args: args{ - cr: oidcProvider(withTags(map[string]string{"foo": "bar"}), withGroupVersionKind()), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: oidcProvider(withTags(resource.GetExternalTags(oidcProvider(withGroupVersionKind())), map[string]string{"foo": "bar"}), withGroupVersionKind()), - }, - }, - "NoChanges": { - args: args{ - cr: oidcProvider( - withTags(map[string]string{"foo": "bar"}), - withGroupVersionKind()), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: oidcProvider( - withTags(resource.GetExternalTags(oidcProvider(withGroupVersionKind())), map[string]string{"foo": "bar"}), - withGroupVersionKind()), - }, - }, - "UpdateFailed": { - args: args{ - cr: oidcProvider(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errors.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, sortTags); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/iam/policy/controller.go b/pkg/controller/iam/policy/controller.go index 36c4182db4..84e8c6b49f 100644 --- a/pkg/controller/iam/policy/controller.go +++ b/pkg/controller/iam/policy/controller.go @@ -71,7 +71,6 @@ func SetupPolicy(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), newClientFn: iam.NewPolicyClient, newSTSClientFn: iam.NewSTSClient}), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithConnectionPublishers(), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), @@ -391,36 +390,3 @@ func (e *external) getPolicyArnByNameAndPath(ctx context.Context, policyName str return aws.String(policyArn.String()), nil } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*v1beta1.Policy) - if !ok { - return errors.New(errUnexpectedObject) - } - added := false - defaultTags := resource.GetExternalTags(mgd) - - for i, t := range cr.Spec.ForProvider.Tags { - v, ok := defaultTags[t.Key] - if ok { - if v != t.Value { - cr.Spec.ForProvider.Tags[i].Value = v - added = true - } - delete(defaultTags, t.Key) - } - } - - for k, v := range defaultTags { - cr.Spec.ForProvider.Tags = append(cr.Spec.ForProvider.Tags, v1beta1.Tag{Key: k, Value: v}) - added = true - } - if !added { - return nil - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/iam/policy/controller_test.go b/pkg/controller/iam/policy/controller_test.go index 0785873098..0383759901 100644 --- a/pkg/controller/iam/policy/controller_test.go +++ b/pkg/controller/iam/policy/controller_test.go @@ -147,12 +147,6 @@ func withTags(tagMaps ...map[string]string) policyModifier { } } -func withGroupVersionKind() policyModifier { - return func(iamRole *v1beta1.Policy) { - iamRole.TypeMeta.SetGroupVersionKind(v1beta1.PolicyGroupVersionKind) - } -} - func policy(m ...policyModifier) *v1beta1.Policy { cr := &v1beta1.Policy{} cr.Spec.ForProvider.Name = name @@ -979,92 +973,3 @@ func TestDelete(t *testing.T) { }) } } - -func TestInitialize(t *testing.T) { - type args struct { - cr resource.Managed - kube client.Client - } - type want struct { - cr *v1beta1.Policy - err error - } - - cases := map[string]struct { - args - want - }{ - "Unexpected": { - args: args{ - cr: unexpectedItem, - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - err: errors.New(errUnexpectedObject), - }, - }, - "Successful": { - args: args{ - cr: policy(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: policy(withTags(resource.GetExternalTags(policy()), map[string]string{"foo": "bar"})), - }, - }, - "DefaultTags": { - args: args{ - cr: policy(withTags(map[string]string{"foo": "bar"}), withGroupVersionKind()), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: policy(withTags(resource.GetExternalTags(policy(withGroupVersionKind())), map[string]string{"foo": "bar"}), withGroupVersionKind()), - }, - }, - "UpdateDefaultTags": { - args: args{ - cr: policy(withTags(map[string]string{resource.ExternalResourceTagKeyKind: "bar"}), withGroupVersionKind()), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: policy(withTags(resource.GetExternalTags(policy(withGroupVersionKind()))), withGroupVersionKind()), - }, - }, - "NoChanges": { - args: args{ - cr: policy( - withTags(resource.GetExternalTags(policy(withGroupVersionKind())), map[string]string{"foo": "bar"}), - withGroupVersionKind()), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: policy( - withTags(resource.GetExternalTags(policy(withGroupVersionKind())), map[string]string{"foo": "bar"}), - withGroupVersionKind()), - }, - }, - "UpdateFailed": { - args: args{ - cr: policy(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errors.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, cmpopts.SortSlices(func(a, b v1beta1.Tag) bool { return a.Key > b.Key })); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/iam/role/controller.go b/pkg/controller/iam/role/controller.go index 0449a3cfe1..a4c6766914 100644 --- a/pkg/controller/iam/role/controller.go +++ b/pkg/controller/iam/role/controller.go @@ -69,7 +69,7 @@ func SetupRole(mgr ctrl.Manager, o controller.Options) error { managed.WithConnectionPublishers(), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), managed.WithConnectionPublishers(cps...), } @@ -270,36 +270,3 @@ func (e *external) Delete(ctx context.Context, mgd resource.Managed) error { return errorutils.Wrap(resource.Ignore(iam.IsErrorNotFound, err), errDelete) } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*v1beta1.Role) - if !ok { - return errors.New(errUnexpectedObject) - } - - added := false - defaultTags := resource.GetExternalTags(mgd) - - for i, t := range cr.Spec.ForProvider.Tags { - if v, ok := defaultTags[t.Key]; ok { - if v != t.Value { - cr.Spec.ForProvider.Tags[i].Value = v - added = true - } - delete(defaultTags, t.Key) - } - } - - for k, v := range defaultTags { - cr.Spec.ForProvider.Tags = append(cr.Spec.ForProvider.Tags, v1beta1.Tag{Key: k, Value: v}) - added = true - } - if !added { - return nil - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/iam/role/controller_test.go b/pkg/controller/iam/role/controller_test.go index 86e931e497..2d394da74b 100644 --- a/pkg/controller/iam/role/controller_test.go +++ b/pkg/controller/iam/role/controller_test.go @@ -29,9 +29,7 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/crossplane/crossplane-runtime/pkg/test" "github.com/google/go-cmp/cmp" - "github.com/google/go-cmp/cmp/cmpopts" "github.com/pkg/errors" - "sigs.k8s.io/controller-runtime/pkg/client" "github.com/crossplane-contrib/provider-aws/apis/iam/v1beta1" "github.com/crossplane-contrib/provider-aws/pkg/clients/iam" @@ -98,24 +96,6 @@ func withDescription() roleModifier { } } -func withTags(tagMaps ...map[string]string) roleModifier { - var tagList []v1beta1.Tag - for _, tagMap := range tagMaps { - for k, v := range tagMap { - tagList = append(tagList, v1beta1.Tag{Key: k, Value: v}) - } - } - return func(r *v1beta1.Role) { - r.Spec.ForProvider.Tags = tagList - } -} - -func withGroupVersionKind() roleModifier { - return func(iamRole *v1beta1.Role) { - iamRole.TypeMeta.SetGroupVersionKind(v1beta1.RoleGroupVersionKind) - } -} - func role(m ...roleModifier) *v1beta1.Role { cr := &v1beta1.Role{} for _, f := range m { @@ -468,100 +448,3 @@ func TestDelete(t *testing.T) { } } - -func TestInitialize(t *testing.T) { - type args struct { - cr resource.Managed - kube client.Client - } - type want struct { - cr *v1beta1.Role - err error - } - - cases := map[string]struct { - args - want - }{ - "Unexpected": { - args: args{ - cr: unexpectedItem, - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - err: errors.New(errUnexpectedObject), - }, - }, - "Successful": { - args: args{ - cr: role(withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: role(withTags(resource.GetExternalTags(role()), map[string]string{"foo": "bar"})), - }, - }, - "DefaultTags": { - args: args{ - cr: role(withTags(map[string]string{"foo": "bar"}), withGroupVersionKind()), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: role(withTags(resource.GetExternalTags(role(withGroupVersionKind())), map[string]string{"foo": "bar"}), withGroupVersionKind()), - }, - }, - "UpdateDefaultTags": { - args: args{ - cr: role( - withRoleName(&roleName), - withGroupVersionKind(), - withTags(map[string]string{resource.ExternalResourceTagKeyKind: "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: role( - withRoleName(&roleName), - withGroupVersionKind(), - withTags(resource.GetExternalTags(role(withRoleName(&roleName), withGroupVersionKind())))), - }, - }, - "NoChange": { - args: args{ - cr: role( - withRoleName(&roleName), - withGroupVersionKind(), - withTags(resource.GetExternalTags(role(withRoleName(&roleName), withGroupVersionKind())))), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: role( - withRoleName(&roleName), - withGroupVersionKind(), - withTags(resource.GetExternalTags(role(withRoleName(&roleName), withGroupVersionKind())))), - }, - }, - "UpdateFailed": { - args: args{ - cr: role(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errors.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, cmpopts.SortSlices(func(a, b v1beta1.Tag) bool { return a.Key > b.Key })); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/iam/user/controller.go b/pkg/controller/iam/user/controller.go index 8064018e6f..06369dfbe9 100644 --- a/pkg/controller/iam/user/controller.go +++ b/pkg/controller/iam/user/controller.go @@ -69,9 +69,7 @@ func SetupUser(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), newClientFn: iam.NewUserClient}), - managed.WithInitializers( - managed.NewNameAsExternalName(mgr.GetClient()), - &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithConnectionPublishers(), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), @@ -216,39 +214,6 @@ func (e *external) Delete(ctx context.Context, mgd resource.Managed) error { return errorutils.Wrap(resource.Ignore(iam.IsErrorNotFound, err), errDelete) } -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error { - cr, ok := mgd.(*v1beta1.User) - if !ok { - return errors.New(errUnexpectedObject) - } - - added := false - defaultTags := resource.GetExternalTags(mgd) - - for i, t := range cr.Spec.ForProvider.Tags { - if v, ok := defaultTags[t.Key]; ok { - if v != t.Value { - cr.Spec.ForProvider.Tags[i].Value = v - added = true - } - delete(defaultTags, t.Key) - } - } - - for k, v := range defaultTags { - cr.Spec.ForProvider.Tags = append(cr.Spec.ForProvider.Tags, v1beta1.Tag{Key: k, Value: v}) - added = true - } - if !added { - return nil - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} - func (e *external) updateUser(ctx context.Context, observed *awsiam.GetUserOutput, cr *v1beta1.User) error { if aws.ToString(observed.User.Path) != aws.ToString(cr.Spec.ForProvider.Path) { _, err := e.client.UpdateUser(ctx, &awsiam.UpdateUserInput{ diff --git a/pkg/controller/iam/user/controller_test.go b/pkg/controller/iam/user/controller_test.go index 8d9b72ec2e..e34c644a02 100644 --- a/pkg/controller/iam/user/controller_test.go +++ b/pkg/controller/iam/user/controller_test.go @@ -31,7 +31,6 @@ import ( "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "github.com/pkg/errors" - "sigs.k8s.io/controller-runtime/pkg/client" "github.com/crossplane-contrib/provider-aws/apis/iam/v1beta1" "github.com/crossplane-contrib/provider-aws/pkg/clients/iam/fake" @@ -99,12 +98,6 @@ func withTags(tagMaps ...map[string]string) userModifier { } } -func withGroupVersionKind() userModifier { - return func(iamRole *v1beta1.User) { - iamRole.TypeMeta.SetGroupVersionKind(v1beta1.PolicyGroupVersionKind) - } -} - func withPath(path *string) userModifier { return func(r *v1beta1.User) { r.Spec.ForProvider.Path = path @@ -692,100 +685,3 @@ func TestDelete(t *testing.T) { }) } } - -func TestTagger_Initialize(t *testing.T) { - type args struct { - cr resource.Managed - kube client.Client - } - type want struct { - cr *v1beta1.User - err error - } - - cases := map[string]struct { - args - want - }{ - "Unexpected": { - args: args{ - cr: unexpectedItem, - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - err: errors.New(errUnexpectedObject), - }, - }, - "Successful": { - args: args{ - cr: user( - withExternalName(userName), - withGroupVersionKind(), - withTags(map[string]string{"foo": "bar"})), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: user( - withExternalName(userName), - withGroupVersionKind(), - withTags(resource.GetExternalTags(user(withGroupVersionKind())), map[string]string{"foo": "bar"})), - }, - }, - "DefaultTags": { - args: args{ - cr: user(withExternalName(userName), - withTags(map[string]string{"foo": "bar"}), withGroupVersionKind()), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: user(withExternalName(userName), - withTags(resource.GetExternalTags(user(withGroupVersionKind())), map[string]string{"foo": "bar"}), withGroupVersionKind()), - }, - }, - "UpdateDefaultTags": { - args: args{ - cr: user(withExternalName(userName), - withTags(map[string]string{resource.ExternalResourceTagKeyKind: "bar"}), withGroupVersionKind()), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: user(withExternalName(userName), - withTags(resource.GetExternalTags(user(withGroupVersionKind()))), withGroupVersionKind()), - }, - }, - "NoChange": { - args: args{ - cr: user(withExternalName(userName), withGroupVersionKind(), - withTags(map[string]string{"foo": "bar"}, resource.GetExternalTags(user(withExternalName(userName), withGroupVersionKind())))), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)}, - }, - want: want{ - cr: user(withExternalName(userName), withGroupVersionKind(), - withTags(map[string]string{"foo": "bar"}, resource.GetExternalTags(user(withExternalName(userName), withGroupVersionKind())))), - }, - }, - "UpdateFailed": { - args: args{ - cr: user(), - kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(errBoom)}, - }, - want: want{ - err: errors.Wrap(errBoom, errKubeUpdateFailed), - }, - }, - } - - for name, tc := range cases { - t.Run(name, func(t *testing.T) { - e := &tagger{kube: tc.kube} - err := e.Initialize(context.Background(), tc.args.cr) - - if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - if diff := cmp.Diff(tc.want.cr, tc.args.cr, cmpopts.SortSlices(func(a, b v1beta1.Tag) bool { return a.Key > b.Key })); err == nil && diff != "" { - t.Errorf("r: -want, +got:\n%s", diff) - } - }) - } -} diff --git a/pkg/controller/mwaa/environment/setup.go b/pkg/controller/mwaa/environment/setup.go index 72e5609ed6..1d4e4d80e1 100644 --- a/pkg/controller/mwaa/environment/setup.go +++ b/pkg/controller/mwaa/environment/setup.go @@ -51,7 +51,7 @@ func SetupEnvironment(mgr ctrl.Manager, o controller.Options) error { } reconcilerOpts := []managed.ReconcilerOption{ - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), @@ -260,24 +260,3 @@ func diffTags(spec, current map[string]*string) (map[string]*string, []*string) return addTags, remove } - -type tagger struct { - kube client.Client -} - -// TODO(knappek): split this out as it is used in several controllers -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*svcapitypes.Environment) - if !ok { - return errors.New(errNotEnvironment) - } - - if cr.Spec.ForProvider.Tags == nil { - cr.Spec.ForProvider.Tags = map[string]*string{} - } - - for k, v := range resource.GetExternalTags(mg) { - cr.Spec.ForProvider.Tags[k] = pointer.ToOrNilIfZeroValue(v) - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/prometheusservice/rulegroupsnamespace/setup.go b/pkg/controller/prometheusservice/rulegroupsnamespace/setup.go index e32a389a13..603f8eb3c7 100644 --- a/pkg/controller/prometheusservice/rulegroupsnamespace/setup.go +++ b/pkg/controller/prometheusservice/rulegroupsnamespace/setup.go @@ -17,17 +17,10 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/resource" "github.com/pkg/errors" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" svcapitypes "github.com/crossplane-contrib/provider-aws/apis/prometheusservice/v1alpha1" "github.com/crossplane-contrib/provider-aws/apis/v1alpha1" "github.com/crossplane-contrib/provider-aws/pkg/features" - "github.com/crossplane-contrib/provider-aws/pkg/utils/pointer" -) - -const ( - errNotRuleGroupsNamespace = "managed resource is not an RuleGroupsNamespace custom resource" - errKubeUpdateFailed = "cannot update RuleGroupsNamespace custom resource" ) // SetupRuleGroupsNamespace adds a controller that reconciles RuleGroupsNamespace. @@ -54,7 +47,6 @@ func SetupRuleGroupsNamespace(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), @@ -135,24 +127,6 @@ func postObserve(_ context.Context, cr *svcapitypes.RuleGroupsNamespace, resp *s return obs, nil } -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*svcapitypes.RuleGroupsNamespace) - if !ok { - return errors.New(errNotRuleGroupsNamespace) - } - if cr.Spec.ForProvider.Tags == nil { - cr.Spec.ForProvider.Tags = map[string]*string{} - } - for k, v := range resource.GetExternalTags(mg) { - cr.Spec.ForProvider.Tags[k] = pointer.ToOrNilIfZeroValue(v) - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} - func isUpToDate(_ context.Context, cr *svcapitypes.RuleGroupsNamespace, resp *svcsdk.DescribeRuleGroupsNamespaceOutput) (bool, string, error) { // A rule that's currently creating, deleting, or updating can't be // updated, so we temporarily consider it to be up-to-date no matter diff --git a/pkg/controller/prometheusservice/workspace/setup.go b/pkg/controller/prometheusservice/workspace/setup.go index 26b6a48e3f..85107544d4 100644 --- a/pkg/controller/prometheusservice/workspace/setup.go +++ b/pkg/controller/prometheusservice/workspace/setup.go @@ -13,9 +13,7 @@ import ( "github.com/crossplane/crossplane-runtime/pkg/meta" "github.com/crossplane/crossplane-runtime/pkg/reconciler/managed" "github.com/crossplane/crossplane-runtime/pkg/resource" - "github.com/pkg/errors" ctrl "sigs.k8s.io/controller-runtime" - "sigs.k8s.io/controller-runtime/pkg/client" svcapitypes "github.com/crossplane-contrib/provider-aws/apis/prometheusservice/v1alpha1" "github.com/crossplane-contrib/provider-aws/apis/v1alpha1" @@ -23,11 +21,6 @@ import ( "github.com/crossplane-contrib/provider-aws/pkg/utils/pointer" ) -const ( - errNotWorkspace = "managed resource is not an Workspace custom resource" - errKubeUpdateFailed = "cannot update Workspace custom resource" -) - // SetupWorkspace adds a controller that reconciles Workspace for PrometheusService. func SetupWorkspace(mgr ctrl.Manager, o controller.Options) error { name := managed.ControllerName(svcapitypes.WorkspaceGroupKind) @@ -48,7 +41,6 @@ func SetupWorkspace(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), - managed.WithInitializers(&tagger{kube: mgr.GetClient()}), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), @@ -125,21 +117,3 @@ func postDelete(_ context.Context, cr *svcapitypes.Workspace, obj *svcsdk.Delete } return err } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*svcapitypes.Workspace) - if !ok { - return errors.New(errNotWorkspace) - } - if cr.Spec.ForProvider.Tags == nil { - cr.Spec.ForProvider.Tags = map[string]*string{} - } - for k, v := range resource.GetExternalTags(mg) { - cr.Spec.ForProvider.Tags[k] = pointer.ToOrNilIfZeroValue(v) - } - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/secretsmanager/secret/setup.go b/pkg/controller/secretsmanager/secret/setup.go index fdaa6e2533..030b4170f2 100644 --- a/pkg/controller/secretsmanager/secret/setup.go +++ b/pkg/controller/secretsmanager/secret/setup.go @@ -18,7 +18,6 @@ import ( "context" "encoding/json" "fmt" - "sort" svcsdk "github.com/aws/aws-sdk-go/service/secretsmanager" "github.com/aws/aws-sdk-go/service/secretsmanager/secretsmanageriface" @@ -78,7 +77,7 @@ func SetupSecret(mgr ctrl.Manager, o controller.Options) error { reconcilerOpts := []managed.ReconcilerOption{ managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))), @@ -437,35 +436,6 @@ func preDelete(_ context.Context, cr *svcapitypes.Secret, obj *svcsdk.DeleteSecr return false, nil } -type tagger struct { - kube client.Client -} - -// TODO(knappek): split this out as it is used in several controllers -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*svcapitypes.Secret) - if !ok { - return errors.New(errNotSecret) - } - tagMap := map[string]string{} - for _, tags := range cr.Spec.ForProvider.Tags { - tagMap[pointer.StringValue(tags.Key)] = pointer.StringValue(tags.Value) - } - for k, v := range resource.GetExternalTags(mg) { - tagMap[k] = v - } - cr.Spec.ForProvider.Tags = make([]*svcapitypes.Tag, len(tagMap)) - i := 0 - for k, v := range tagMap { - cr.Spec.ForProvider.Tags[i] = &svcapitypes.Tag{Key: pointer.ToOrNilIfZeroValue(k), Value: pointer.ToOrNilIfZeroValue(v)} - i++ - } - sort.Slice(cr.Spec.ForProvider.Tags, func(i, j int) bool { - return pointer.StringValue(cr.Spec.ForProvider.Tags[i].Key) < pointer.StringValue(cr.Spec.ForProvider.Tags[j].Key) - }) - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} - // DiffTags returns tags that should be added or removed. func DiffTags(spec []*svcapitypes.Tag, current []*svcsdk.Tag) (addTags []*svcsdk.Tag, remove []*string) { addMap := make(map[string]string, len(spec)) diff --git a/pkg/controller/sesv2/configurationset/setup.go b/pkg/controller/sesv2/configurationset/setup.go index c26b463330..7a703d5ca0 100644 --- a/pkg/controller/sesv2/configurationset/setup.go +++ b/pkg/controller/sesv2/configurationset/setup.go @@ -39,11 +39,6 @@ import ( "github.com/crossplane-contrib/provider-aws/pkg/utils/pointer" ) -const ( - errNotConfigurationSet = "managed resource is not a SES ConfigurationSet custom resource" - errKubeUpdateFailed = "cannot update SES ConfigurationSet custom resource" -) - // SetupConfigurationSet adds a controller that reconciles SES ConfigurationSet. func SetupConfigurationSet(mgr ctrl.Manager, o controller.Options) error { name := managed.ControllerName(svcapitypes.ConfigurationSetGroupKind) @@ -67,7 +62,7 @@ func SetupConfigurationSet(mgr ctrl.Manager, o controller.Options) error { Complete(managed.NewReconciler(mgr, resource.ManagedKind(svcapitypes.ConfigurationSetGroupVersionKind), managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))) @@ -288,16 +283,3 @@ func (e *hooks) update(ctx context.Context, mg resource.Managed) (managed.Extern return managed.ExternalUpdate{}, nil } - -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*svcapitypes.ConfigurationSet) - if !ok { - return errors.New(errNotConfigurationSet) - } - cr.Spec.ForProvider.Tags = svcutils.AddExternalTags(mg, cr.Spec.ForProvider.Tags) - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} diff --git a/pkg/controller/sesv2/emailidentity/setup.go b/pkg/controller/sesv2/emailidentity/setup.go index 0f225f7872..2538193e27 100644 --- a/pkg/controller/sesv2/emailidentity/setup.go +++ b/pkg/controller/sesv2/emailidentity/setup.go @@ -67,7 +67,7 @@ func SetupEmailIdentity(mgr ctrl.Manager, o controller.Options) error { resource.ManagedKind(svcapitypes.EmailIdentityGroupVersionKind), managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}), managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())), - managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient()), &tagger{kube: mgr.GetClient()}), + managed.WithInitializers(managed.NewNameAsExternalName(mgr.GetClient())), managed.WithPollInterval(o.PollInterval), managed.WithLogger(o.Logger.WithValues("controller", name)), managed.WithRecorder(event.NewAPIRecorder(mgr.GetEventRecorderFor(name))))) @@ -229,19 +229,6 @@ func (e *hooks) update(ctx context.Context, mg resource.Managed) (managed.Extern return managed.ExternalUpdate{}, nil } -type tagger struct { - kube client.Client -} - -func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error { - cr, ok := mg.(*svcapitypes.EmailIdentity) - if !ok { - return errors.New(errNotEmailIdentity) - } - cr.Spec.ForProvider.Tags = svcutils.AddExternalTags(mg, cr.Spec.ForProvider.Tags) - return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed) -} - func (e *hooks) getPrivateKeyFromRef(ctx context.Context, in *xpv1.SecretKeySelector) (newPrivateKey string, err error) { if in == nil { return "", nil