Skip to content
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

add new AWSResourceManager.EnsureControllerTags() method #90

Merged
merged 5 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions mocks/pkg/types/aws_resource_manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions pkg/runtime/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,12 @@ func (r *resourceReconciler) Reconcile(ctx context.Context, req ctrlrt.Request)
if err != nil {
return ctrlrt.Result{}, err
}
rlog.Enter("rm.EnsureControllerTags")
err = rm.EnsureControllerTags(ctx, desired)
rlog.Exit("rm.EnsureControllerTags", err)
if err != nil {
return ctrlrt.Result{}, err
}
vijtrip2 marked this conversation as resolved.
Show resolved Hide resolved
latest, err := r.reconcile(ctx, rm, desired)
return r.HandleReconcileError(ctx, desired, latest, err)
}
Expand Down
52 changes: 52 additions & 0 deletions pkg/tags/resource_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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 tags

// ResourceTags represents the AWS tags which will be added to the AWS resource.
// Inside aws-sdk-go, Tags are represented using multiple types, Ex: map of
// string, list of structs etc...
// ResourceTags type will be used as a hub/mediator to merge tags represented
// using different types.
type ResourceTags struct {
vijtrip2 marked this conversation as resolved.
Show resolved Hide resolved
Tags map[string]string
}
vijtrip2 marked this conversation as resolved.
Show resolved Hide resolved

// NewResourceTags returns ResourceTags with empty tags
func NewResourceTags() ResourceTags {
return ResourceTags{make(map[string]string)}
}

// NewResourceTagsFrom creates ResourceTags using the tags passed
// inside the parameter
func NewResourceTagsFrom(tags map[string]string) ResourceTags {
return ResourceTags{tags}
}

// Merge merges the tags between two ResourceTags.
// If a tag-key is already present inside the original ResourceTag('rt'), it
// does not get overwritten.
func (rt *ResourceTags) Merge(other ResourceTags) {
if other.Tags != nil && len(other.Tags) > 0 {
// Initialize if the Tags field is nil
if rt.Tags == nil {
rt.Tags = make(map[string]string)
}
// Add all the tags which are not already present
for tk, tv := range other.Tags {
if _, found := rt.Tags[tk]; !found {
rt.Tags[tk] = tv
}
}
}
}
67 changes: 67 additions & 0 deletions pkg/tags/resource_tags_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 tags_test

import (
"testing"

"github.com/stretchr/testify/assert"

acktags "github.com/aws-controllers-k8s/runtime/pkg/tags"
)

func TestNewResourceTags(t *testing.T) {
assert := assert.New(t)
rt := acktags.NewResourceTags()
assert.NotNil(rt)
assert.Empty(rt.Tags)

tags := map[string]string{"tk": "tv"}
rt = acktags.NewResourceTagsFrom(tags)
assert.NotNil(rt)
assert.NotEmpty(rt.Tags)
assert.Equal("tv", rt.Tags["tk"])
}

func TestNewResourceTagsFrom(t *testing.T) {
assert := assert.New(t)

tags := map[string]string{"tk": "tv"}
rt := acktags.NewResourceTagsFrom(tags)
assert.NotNil(rt)
assert.NotEmpty(rt.Tags)
assert.Equal("tv", rt.Tags["tk"])
}

func TestResourceTags_Merge_NilTags(t *testing.T) {
assert := assert.New(t)

rt1 := acktags.NewResourceTagsFrom(nil)
rt2 := acktags.NewResourceTagsFrom(map[string]string{"tk": "tv", "tk2": "tv2"})
rt1.Merge(rt2)
assert.Equal("tv", rt1.Tags["tk"])
assert.Equal("tv2", rt1.Tags["tk2"])
assert.Equal(2, len(rt1.Tags))
}

func TestResourceTags_Merge(t *testing.T) {
assert := assert.New(t)

rt1 := acktags.NewResourceTagsFrom(map[string]string{"tk": "tv"})
rt2 := acktags.NewResourceTagsFrom(map[string]string{"tk": "tv1", "tk2": "tv2"})
rt1.Merge(rt2)
assert.Equal("tv", rt1.Tags["tk"])
assert.Equal("tv2", rt1.Tags["tk2"])
assert.Equal(2, len(rt1.Tags))
}
5 changes: 5 additions & 0 deletions pkg/types/aws_resource_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ type AWSResourceManager interface {
ResolveReferences(context.Context, client.Reader, AWSResource) (AWSResource, error)
// IsSynced returns true if a resource is synced.
IsSynced(context.Context, AWSResource) (bool, error)
// EnsureControllerTags ensures that ACK controller tags are present
// inside AWSResource.
// If AWSResource does not support tags, only then controller tags will
// be missing from AWSResource after this method call.
EnsureControllerTags(context.Context, AWSResource) error
vijtrip2 marked this conversation as resolved.
Show resolved Hide resolved
}

// AWSResourceManagerFactory returns an AWSResourceManager that can be used to
Expand Down