|
| 1 | +// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"). You may |
| 4 | +// not use this file except in compliance with the License. A copy of the |
| 5 | +// License is located at |
| 6 | +// |
| 7 | +// http://aws.amazon.com/apache2.0/ |
| 8 | +// |
| 9 | +// or in the "license" file accompanying this file. This file is distributed |
| 10 | +// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +// express or implied. See the License for the specific language governing |
| 12 | +// permissions and limitations under the License. |
| 13 | + |
| 14 | +package policy |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + |
| 19 | + ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare" |
| 20 | + ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition" |
| 21 | + ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log" |
| 22 | + svcsdk "github.com/aws/aws-sdk-go/service/iam" |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + |
| 25 | + svcapitypes "github.com/aws-controllers-k8s/iam-controller/apis/v1alpha1" |
| 26 | +) |
| 27 | + |
| 28 | +func (rm *resourceManager) customUpdatePolicy( |
| 29 | + ctx context.Context, |
| 30 | + desired *resource, |
| 31 | + latest *resource, |
| 32 | + delta *ackcompare.Delta, |
| 33 | +) (*resource, error) { |
| 34 | + ko := desired.ko.DeepCopy() |
| 35 | + |
| 36 | + rm.setStatusDefaults(ko) |
| 37 | + |
| 38 | + if err := rm.syncTags(ctx, &resource{ko}); err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + // There really isn't a status of a policy... it either exists or doesn't. |
| 42 | + // If we get here, that means the update was successful and the desired |
| 43 | + // state of the policy matches what we provided... |
| 44 | + ackcondition.SetSynced(&resource{ko}, corev1.ConditionTrue, nil, nil) |
| 45 | + |
| 46 | + return &resource{ko}, nil |
| 47 | +} |
| 48 | + |
| 49 | +// syncTags examines the Tags in the supplied Policy and calls the |
| 50 | +// ListPolicyTags, TagPolicy and UntagPolicy APIs to ensure that the set of |
| 51 | +// associated Tags stays in sync with the Policy.Spec.Tags |
| 52 | +func (rm *resourceManager) syncTags( |
| 53 | + ctx context.Context, |
| 54 | + r *resource, |
| 55 | +) (err error) { |
| 56 | + rlog := ackrtlog.FromContext(ctx) |
| 57 | + exit := rlog.Trace("rm.syncTags") |
| 58 | + defer exit(err) |
| 59 | + toAdd := []*svcapitypes.Tag{} |
| 60 | + toDelete := []*svcapitypes.Tag{} |
| 61 | + |
| 62 | + existingTags, err := rm.getTags(ctx, r) |
| 63 | + if err != nil { |
| 64 | + return err |
| 65 | + } |
| 66 | + |
| 67 | + for _, t := range r.ko.Spec.Tags { |
| 68 | + if !inTags(*t.Key, *t.Value, existingTags) { |
| 69 | + toAdd = append(toAdd, t) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + for _, t := range existingTags { |
| 74 | + if !inTags(*t.Key, *t.Value, r.ko.Spec.Tags) { |
| 75 | + toDelete = append(toDelete, t) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if len(toDelete) > 0 { |
| 80 | + for _, t := range toDelete { |
| 81 | + rlog.Debug("removing tag from policy", "key", *t.Key, "value", *t.Value) |
| 82 | + } |
| 83 | + if err = rm.removeTags(ctx, r, toDelete); err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if len(toAdd) > 0 { |
| 89 | + for _, t := range toAdd { |
| 90 | + rlog.Debug("adding tag to policy", "key", *t.Key, "value", *t.Value) |
| 91 | + } |
| 92 | + if err = rm.addTags(ctx, r, toAdd); err != nil { |
| 93 | + return err |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +// inTags returns true if the supplied key and value can be found in the |
| 101 | +// supplied list of Tag structs. |
| 102 | +// |
| 103 | +// TODO(jaypipes): When we finally standardize Tag handling in ACK, move this |
| 104 | +// to the ACK common runtime/ or pkg/ repos |
| 105 | +func inTags( |
| 106 | + key string, |
| 107 | + value string, |
| 108 | + tags []*svcapitypes.Tag, |
| 109 | +) bool { |
| 110 | + for _, t := range tags { |
| 111 | + if *t.Key == key && *t.Value == value { |
| 112 | + return true |
| 113 | + } |
| 114 | + } |
| 115 | + return false |
| 116 | +} |
| 117 | + |
| 118 | +// getTags returns the list of tags attached to the Policy |
| 119 | +func (rm *resourceManager) getTags( |
| 120 | + ctx context.Context, |
| 121 | + r *resource, |
| 122 | +) ([]*svcapitypes.Tag, error) { |
| 123 | + var err error |
| 124 | + var resp *svcsdk.ListPolicyTagsOutput |
| 125 | + rlog := ackrtlog.FromContext(ctx) |
| 126 | + exit := rlog.Trace("rm.getTags") |
| 127 | + defer exit(err) |
| 128 | + |
| 129 | + input := &svcsdk.ListPolicyTagsInput{} |
| 130 | + input.PolicyArn = (*string)(r.ko.Status.ACKResourceMetadata.ARN) |
| 131 | + res := []*svcapitypes.Tag{} |
| 132 | + |
| 133 | + for { |
| 134 | + resp, err = rm.sdkapi.ListPolicyTagsWithContext(ctx, input) |
| 135 | + if err != nil || resp == nil { |
| 136 | + break |
| 137 | + } |
| 138 | + for _, t := range resp.Tags { |
| 139 | + res = append(res, &svcapitypes.Tag{Key: t.Key, Value: t.Value}) |
| 140 | + } |
| 141 | + if resp.IsTruncated != nil && !*resp.IsTruncated { |
| 142 | + break |
| 143 | + } |
| 144 | + } |
| 145 | + rm.metrics.RecordAPICall("GET", "ListPolicyTags", err) |
| 146 | + return res, err |
| 147 | +} |
| 148 | + |
| 149 | +// addTags adds the supplied Tags to the supplied Policy resource |
| 150 | +func (rm *resourceManager) addTags( |
| 151 | + ctx context.Context, |
| 152 | + r *resource, |
| 153 | + tags []*svcapitypes.Tag, |
| 154 | +) (err error) { |
| 155 | + rlog := ackrtlog.FromContext(ctx) |
| 156 | + exit := rlog.Trace("rm.addTags") |
| 157 | + defer exit(err) |
| 158 | + |
| 159 | + input := &svcsdk.TagPolicyInput{} |
| 160 | + input.PolicyArn = (*string)(r.ko.Status.ACKResourceMetadata.ARN) |
| 161 | + inTags := []*svcsdk.Tag{} |
| 162 | + for _, t := range tags { |
| 163 | + inTags = append(inTags, &svcsdk.Tag{Key: t.Key, Value: t.Value}) |
| 164 | + } |
| 165 | + input.Tags = inTags |
| 166 | + |
| 167 | + _, err = rm.sdkapi.TagPolicyWithContext(ctx, input) |
| 168 | + rm.metrics.RecordAPICall("CREATE", "TagPolicy", err) |
| 169 | + return err |
| 170 | +} |
| 171 | + |
| 172 | +// removeTags removes the supplied Tags from the supplied Policy resource |
| 173 | +func (rm *resourceManager) removeTags( |
| 174 | + ctx context.Context, |
| 175 | + r *resource, |
| 176 | + tags []*svcapitypes.Tag, |
| 177 | +) (err error) { |
| 178 | + rlog := ackrtlog.FromContext(ctx) |
| 179 | + exit := rlog.Trace("rm.removeTags") |
| 180 | + defer exit(err) |
| 181 | + |
| 182 | + input := &svcsdk.UntagPolicyInput{} |
| 183 | + input.PolicyArn = (*string)(r.ko.Status.ACKResourceMetadata.ARN) |
| 184 | + inTagKeys := []*string{} |
| 185 | + for _, t := range tags { |
| 186 | + inTagKeys = append(inTagKeys, t.Key) |
| 187 | + } |
| 188 | + input.TagKeys = inTagKeys |
| 189 | + |
| 190 | + _, err = rm.sdkapi.UntagPolicyWithContext(ctx, input) |
| 191 | + rm.metrics.RecordAPICall("DELETE", "UntagPolicy", err) |
| 192 | + return err |
| 193 | +} |
0 commit comments