-
Notifications
You must be signed in to change notification settings - Fork 41
Add support for Policy Tag updates #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,13 @@ | ||
| ack_generate_info: | ||
| build_date: "2022-01-24T21:17:47Z" | ||
| build_hash: cccec82a27ddd880095383360df1fdc8f530842f | ||
| go_version: go1.17.5 | ||
| build_date: "2022-02-01T21:34:16Z" | ||
| build_hash: 4ebcd703a95a2fbd71bd07130f92aa6813c1398b | ||
| go_version: go1.17.1 | ||
| version: v0.16.3 | ||
| api_directory_checksum: 5c586ade18ff0bb36fe5fcb6d3ffa78b36a2b2c6 | ||
| api_version: v1alpha1 | ||
| aws_sdk_go_version: v1.40.2 | ||
| generator_config_info: | ||
| file_checksum: e1e788f094e9560f25c4aa9d3aad9f9b3628bd3d | ||
| file_checksum: 2b79f78908b7e3c53c99ec7a56f25d986f107882 | ||
| original_file_name: generator.yaml | ||
| last_modification: | ||
| reason: API generation |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,8 +28,14 @@ resources: | |
| input_fields: | ||
| PolicyName: Name | ||
| hooks: | ||
| sdk_read_one_post_set_output: | ||
| template_path: hooks/policy/sdk_read_one_post_set_output.go.tpl | ||
| sdk_create_post_set_output: | ||
| template_path: hooks/policy/sdk_create_post_set_output.go.tpl | ||
| sdk_update_post_set_output: | ||
| template_path: hooks/policy/sdk_update_post_set_output.go.tpl | ||
| update_operation: | ||
| custom_method_name: customUpdatePolicy | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just noticed something... I see you've added a I prefer to use the generic hook points instead of creating a custom update method. Can you change that? LATER... Never mind, @RedbackThomson, I realized now that you had to use a custom update method because there isn't an actual There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah this is a non-obvious configuration problem you have when trying to use hooks. I'll remove the hook point, though. Good call.
RedbackThomson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| exceptions: | ||
| terminal_codes: | ||
| - InvalidInput | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
| // not use this file except in compliance with the License. A copy of the | ||
| // License is located at | ||
| // | ||
| // http://aws.amazon.com/apache2.0/ | ||
| // | ||
| // or in the "license" file accompanying this file. This file 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 policy | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare" | ||
| ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition" | ||
| ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log" | ||
| svcsdk "github.com/aws/aws-sdk-go/service/iam" | ||
| corev1 "k8s.io/api/core/v1" | ||
|
|
||
| svcapitypes "github.com/aws-controllers-k8s/iam-controller/apis/v1alpha1" | ||
| ) | ||
|
|
||
| func (rm *resourceManager) customUpdatePolicy( | ||
| ctx context.Context, | ||
| desired *resource, | ||
| latest *resource, | ||
| delta *ackcompare.Delta, | ||
| ) (*resource, error) { | ||
| ko := desired.ko.DeepCopy() | ||
|
|
||
| rm.setStatusDefaults(ko) | ||
|
|
||
| if err := rm.syncTags(ctx, &resource{ko}); err != nil { | ||
| return nil, err | ||
| } | ||
|
Comment on lines
+38
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this statement should go inside an if statement similar to: if delta.DifferentAt("Spec.Tags") { ... }In the future we should also deal with cases when a user modifies other fields like: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically, there won't be any calls any update APIs if there aren't any changes (there will be an additional List operation, however, so we should be able to remove that call by guarding this with a delta.DifferentAt()` conditional. 👍 |
||
| // There really isn't a status of a policy... it either exists or doesn't. | ||
| // If we get here, that means the update was successful and the desired | ||
| // state of the policy matches what we provided... | ||
| ackcondition.SetSynced(&resource{ko}, corev1.ConditionTrue, nil, nil) | ||
|
|
||
| return &resource{ko}, nil | ||
| } | ||
|
|
||
| // syncTags examines the Tags in the supplied Policy and calls the | ||
| // ListPolicyTags, TagPolicy and UntagPolicy APIs to ensure that the set of | ||
| // associated Tags stays in sync with the Policy.Spec.Tags | ||
| func (rm *resourceManager) syncTags( | ||
| ctx context.Context, | ||
| r *resource, | ||
| ) (err error) { | ||
| rlog := ackrtlog.FromContext(ctx) | ||
| exit := rlog.Trace("rm.syncTags") | ||
| defer exit(err) | ||
| toAdd := []*svcapitypes.Tag{} | ||
| toDelete := []*svcapitypes.Tag{} | ||
|
|
||
| existingTags, err := rm.getTags(ctx, r) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| for _, t := range r.ko.Spec.Tags { | ||
| if !inTags(*t.Key, *t.Value, existingTags) { | ||
| toAdd = append(toAdd, t) | ||
| } | ||
| } | ||
|
|
||
| for _, t := range existingTags { | ||
| if !inTags(*t.Key, *t.Value, r.ko.Spec.Tags) { | ||
| toDelete = append(toDelete, t) | ||
| } | ||
| } | ||
|
|
||
| if len(toAdd) > 0 { | ||
| for _, t := range toAdd { | ||
| rlog.Debug("adding tag to policy", "key", *t.Key, "value", *t.Value) | ||
| } | ||
| if err = rm.addTags(ctx, r, toAdd); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if len(toDelete) > 0 { | ||
| for _, t := range toDelete { | ||
| rlog.Debug("removing tag from policy", "key", *t.Key, "value", *t.Value) | ||
| } | ||
| if err = rm.removeTags(ctx, r, toDelete); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // inTags returns true if the supplied key and value can be found in the | ||
| // supplied list of Tag structs. | ||
| // | ||
| // TODO(jaypipes): When we finally standardize Tag handling in ACK, move this | ||
| // to the ACK common runtime/ or pkg/ repos | ||
| func inTags( | ||
| key string, | ||
| value string, | ||
| tags []*svcapitypes.Tag, | ||
| ) bool { | ||
| for _, t := range tags { | ||
| if *t.Key == key && *t.Value == value { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // getTags returns the list of tags attached to the Policy | ||
| func (rm *resourceManager) getTags( | ||
| ctx context.Context, | ||
| r *resource, | ||
| ) ([]*svcapitypes.Tag, error) { | ||
| var err error | ||
| var resp *svcsdk.ListPolicyTagsOutput | ||
| rlog := ackrtlog.FromContext(ctx) | ||
| exit := rlog.Trace("rm.getTags") | ||
| defer exit(err) | ||
|
|
||
| input := &svcsdk.ListPolicyTagsInput{} | ||
| input.PolicyArn = (*string)(r.ko.Status.ACKResourceMetadata.ARN) | ||
| res := []*svcapitypes.Tag{} | ||
|
|
||
| for { | ||
| resp, err = rm.sdkapi.ListPolicyTagsWithContext(ctx, input) | ||
| if err != nil || resp == nil { | ||
| break | ||
| } | ||
| for _, t := range resp.Tags { | ||
| res = append(res, &svcapitypes.Tag{Key: t.Key, Value: t.Value}) | ||
| } | ||
| if resp.IsTruncated != nil && !*resp.IsTruncated { | ||
| break | ||
| } | ||
| } | ||
| rm.metrics.RecordAPICall("GET", "ListPolicyTags", err) | ||
| return res, err | ||
| } | ||
|
|
||
| // addTags adds the supplied Tags to the supplied Policy resource | ||
| func (rm *resourceManager) addTags( | ||
| ctx context.Context, | ||
| r *resource, | ||
| tags []*svcapitypes.Tag, | ||
| ) (err error) { | ||
| rlog := ackrtlog.FromContext(ctx) | ||
| exit := rlog.Trace("rm.addTags") | ||
| defer exit(err) | ||
|
|
||
| input := &svcsdk.TagPolicyInput{} | ||
| input.PolicyArn = (*string)(r.ko.Status.ACKResourceMetadata.ARN) | ||
| inTags := []*svcsdk.Tag{} | ||
| for _, t := range tags { | ||
| inTags = append(inTags, &svcsdk.Tag{Key: t.Key, Value: t.Value}) | ||
| } | ||
| input.Tags = inTags | ||
|
|
||
| _, err = rm.sdkapi.TagPolicyWithContext(ctx, input) | ||
| rm.metrics.RecordAPICall("CREATE", "TagPolicy", err) | ||
| return err | ||
| } | ||
|
|
||
| // removeTags removes the supplied Tags from the supplied Policy resource | ||
| func (rm *resourceManager) removeTags( | ||
| ctx context.Context, | ||
| r *resource, | ||
| tags []*svcapitypes.Tag, | ||
| ) (err error) { | ||
| rlog := ackrtlog.FromContext(ctx) | ||
| exit := rlog.Trace("rm.removeTags") | ||
| defer exit(err) | ||
|
|
||
| input := &svcsdk.UntagPolicyInput{} | ||
| input.PolicyArn = (*string)(r.ko.Status.ACKResourceMetadata.ARN) | ||
| inTagKeys := []*string{} | ||
| for _, t := range tags { | ||
| inTagKeys = append(inTagKeys, t.Key) | ||
| } | ||
| input.TagKeys = inTagKeys | ||
|
|
||
| _, err = rm.sdkapi.UntagPolicyWithContext(ctx, input) | ||
| rm.metrics.RecordAPICall("DELETE", "UntagPolicy", err) | ||
| return err | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| if tags, err := rm.getTags(ctx, &resource{ko}); err != nil { | ||
| return nil, err | ||
| } else { | ||
| ko.Spec.Tags = tags | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| if err := rm.syncTags(ctx, &resource{ko}); err != nil { | ||
| return nil, err | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.