Skip to content

Commit

Permalink
feat!: Remove Crossplane standard tags
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Blatt (external expert on behalf of DB Netz) <[email protected]>
  • Loading branch information
MisterMX committed Nov 7, 2023
1 parent 01b886b commit cf4baab
Show file tree
Hide file tree
Showing 56 changed files with 22 additions and 2,789 deletions.
29 changes: 0 additions & 29 deletions pkg/controller/acm/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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...),
Expand Down Expand Up @@ -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)
}
31 changes: 0 additions & 31 deletions pkg/controller/acmpca/certificateauthority/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...),
Expand Down Expand Up @@ -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
}
90 changes: 0 additions & 90 deletions pkg/controller/acmpca/certificateauthority/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
})
}
}
31 changes: 1 addition & 30 deletions pkg/controller/cache/replicationgroup/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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
Expand Down
63 changes: 0 additions & 63 deletions pkg/controller/cache/replicationgroup/managed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 }
}
Expand Down Expand Up @@ -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 {
Expand Down
31 changes: 1 addition & 30 deletions pkg/controller/database/rdsinstance/rdsinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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)
}
Loading

0 comments on commit cf4baab

Please sign in to comment.