-
Notifications
You must be signed in to change notification settings - Fork 67
Add support for additional tags on vpc lattice resources #829
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 all 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Additional Tags | ||
|
||
The AWS Gateway API Controller automatically applies some tags to resources it creates. In addition, you can use annotations to specify additional tags. | ||
|
||
The `application-networking.k8s.aws/tags` annotation specifies additional tags that will be applied to AWS resources created. | ||
|
||
## Supported Resources | ||
|
||
- **HTTPRoute** - Tags applied to VPC Lattice Services, Listeners, Rules, Target Groups, and Service Network Service Associations | ||
- **ServiceExport** - Tags applied to VPC Lattice Target Groups | ||
- **AccessLogPolicy** - Tags applied to VPC Lattice Access Log Subscriptions | ||
- **VpcAssociationPolicy** - Tags applied to VPC Lattice Service Network VPC Associations | ||
|
||
## Usage | ||
|
||
Add comma separated key=value pairs to the annotation: | ||
|
||
```yaml | ||
apiVersion: gateway.networking.k8s.io/v1 | ||
kind: HTTPRoute | ||
metadata: | ||
name: inventory-route | ||
annotations: | ||
application-networking.k8s.aws/tags: "Environment=Production,Team=Backend" | ||
spec: | ||
# ... rest of spec | ||
``` | ||
|
||
```yaml | ||
apiVersion: application-networking.k8s.aws/v1alpha1 | ||
kind: ServiceExport | ||
metadata: | ||
name: payment-service | ||
annotations: | ||
application-networking.k8s.aws/tags: "Environment=Production,Service=Payment" | ||
spec: | ||
# ... rest of spec | ||
``` |
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"context" | ||
"fmt" | ||
|
||
"github.com/aws/aws-application-networking-k8s/pkg/k8s" | ||
"github.com/aws/aws-application-networking-k8s/pkg/utils" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
|
@@ -34,6 +35,9 @@ type Tagging interface { | |
|
||
// Finds one resource that matches the given set of tags. | ||
FindResourcesByTags(ctx context.Context, resourceType ResourceType, tags Tags) ([]string, error) | ||
|
||
// Updates tags for a given resource ARN | ||
UpdateTags(ctx context.Context, resourceArn string, newTags Tags) error | ||
} | ||
|
||
type defaultTagging struct { | ||
|
@@ -165,3 +169,72 @@ func convertTagsToFilter(tags Tags) []*taggingapi.TagFilter { | |
} | ||
return filters | ||
} | ||
|
||
func (t *defaultTagging) UpdateTags(ctx context.Context, resourceArn string, newTags Tags) error { | ||
existingTags, err := t.GetTagsForArns(ctx, []string{resourceArn}) | ||
if err != nil { | ||
return fmt.Errorf("failed to get existing tags: %w", err) | ||
} | ||
|
||
currentTags := k8s.GetNonAWSManagedTags(existingTags[resourceArn]) | ||
filteredNewTags := k8s.GetNonAWSManagedTags(newTags) | ||
|
||
tagsToAdd, tagsToRemove := k8s.CalculateTagDifference(currentTags, filteredNewTags) | ||
|
||
if len(tagsToRemove) > 0 { | ||
_, err := t.UntagResourcesWithContext(ctx, &taggingapi.UntagResourcesInput{ | ||
ResourceARNList: []*string{aws.String(resourceArn)}, | ||
TagKeys: tagsToRemove, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to remove tags: %w", err) | ||
} | ||
} | ||
|
||
if len(tagsToAdd) > 0 { | ||
_, err := t.TagResourcesWithContext(ctx, &taggingapi.TagResourcesInput{ | ||
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. Do the recent exponential throttling improvements apply here as well? 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. Incase of an error, it would retry with an exponential backoff. |
||
ResourceARNList: []*string{aws.String(resourceArn)}, | ||
Tags: tagsToAdd, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to add/update tags: %w", err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t *latticeTagging) UpdateTags(ctx context.Context, resourceArn string, newTags Tags) error { | ||
existingTags, err := t.ListTagsForResourceWithContext(ctx, &vpclattice.ListTagsForResourceInput{ | ||
ResourceArn: aws.String(resourceArn), | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to get existing tags: %w", err) | ||
} | ||
|
||
currentTags := k8s.GetNonAWSManagedTags(existingTags.Tags) | ||
filteredNewTags := k8s.GetNonAWSManagedTags(newTags) | ||
|
||
tagsToAdd, tagsToRemove := k8s.CalculateTagDifference(currentTags, filteredNewTags) | ||
|
||
if len(tagsToRemove) > 0 { | ||
_, err := t.UntagResourceWithContext(ctx, &vpclattice.UntagResourceInput{ | ||
ResourceArn: aws.String(resourceArn), | ||
TagKeys: tagsToRemove, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to remove tags: %w", err) | ||
} | ||
} | ||
|
||
if len(tagsToAdd) > 0 { | ||
_, err := t.TagResourceWithContext(ctx, &vpclattice.TagResourceInput{ | ||
ResourceArn: aws.String(resourceArn), | ||
Tags: tagsToAdd, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to add/update tags: %w", err) | ||
} | ||
} | ||
return nil | ||
} |
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,31 @@ | ||
package predicates | ||
|
||
import ( | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
|
||
"github.com/aws/aws-application-networking-k8s/pkg/k8s" | ||
) | ||
|
||
var AdditionalTagsAnnotationChangedPredicate = predicate.Funcs{ | ||
UpdateFunc: func(e event.UpdateEvent) bool { | ||
oldAnnotations := e.ObjectOld.GetAnnotations() | ||
newAnnotations := e.ObjectNew.GetAnnotations() | ||
|
||
oldAdditionalTags := getAdditionalTagsAnnotation(oldAnnotations) | ||
newAdditionalTags := getAdditionalTagsAnnotation(newAnnotations) | ||
|
||
return oldAdditionalTags != newAdditionalTags | ||
}, | ||
CreateFunc: func(e event.CreateEvent) bool { | ||
annotations := e.Object.GetAnnotations() | ||
return getAdditionalTagsAnnotation(annotations) != "" | ||
}, | ||
} | ||
|
||
func getAdditionalTagsAnnotation(annotations map[string]string) string { | ||
if annotations == nil { | ||
return "" | ||
} | ||
return annotations[k8s.TagsAnnotationKey] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be useful to do some pre-validation if we can based on general AWS tagging guidelines
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added validation for:
aws:
^([\p{L}\p{Z}\p{N}_.:\/=+\-@]*)$
Taken from: https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/API_Tag.html
Changes: 34889a4