Skip to content

Commit

Permalink
Merge pull request crossplane-contrib#1362 from crossplane-contrib/ba…
Browse files Browse the repository at this point in the history
…ckport-1355-to-release-0.28

[Backport release-0.28] fixed infinite default tags adding in iam user
  • Loading branch information
muvaf authored Jun 17, 2022
2 parents 9e1ce16 + f2e3f72 commit c08ed6a
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 13 deletions.
13 changes: 13 additions & 0 deletions pkg/controller/iam/openidconnectprovider/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,19 @@ func TestInitialize(t *testing.T) {
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(),
Expand Down
13 changes: 13 additions & 0 deletions pkg/controller/iam/policy/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,19 @@ func TestInitialize(t *testing.T) {
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(),
Expand Down
23 changes: 15 additions & 8 deletions pkg/controller/iam/role/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,24 @@ func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error {
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
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
}
Expand Down
43 changes: 41 additions & 2 deletions pkg/controller/iam/role/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ func TestDelete(t *testing.T) {

func TestInitialize(t *testing.T) {
type args struct {
cr *v1beta1.Role
cr resource.Managed
kube client.Client
}
type want struct {
Expand All @@ -471,6 +471,15 @@ func TestInitialize(t *testing.T) {
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"})),
Expand All @@ -480,7 +489,7 @@ func TestInitialize(t *testing.T) {
cr: role(withTags(resource.GetExternalTags(role()), map[string]string{"foo": "bar"})),
},
},
"Check Tag values": {
"DefaultTags": {
args: args{
cr: role(withTags(map[string]string{"foo": "bar"}), withGroupVersionKind()),
kube: &test.MockClient{MockUpdate: test.NewMockUpdateFn(nil)},
Expand All @@ -489,6 +498,36 @@ func TestInitialize(t *testing.T) {
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(),
Expand Down
8 changes: 5 additions & 3 deletions pkg/controller/iam/user/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,11 @@ func (t *tagger) Initialize(ctx context.Context, mgd resource.Managed) error {
defaultTags := resource.GetExternalTags(mgd)

for i, t := range cr.Spec.ForProvider.Tags {
if v, ok := defaultTags[t.Key]; ok && v != t.Value {
cr.Spec.ForProvider.Tags[i].Value = v
added = true
if v, ok := defaultTags[t.Key]; ok {
if v != t.Value {
cr.Spec.ForProvider.Tags[i].Value = v
added = true
}
delete(defaultTags, t.Key)
}
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/controller/iam/user/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,17 @@ func TestTagger_Initialize(t *testing.T) {
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(),
Expand Down

0 comments on commit c08ed6a

Please sign in to comment.