-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eks): Use generic tag initializer for addon
Signed-off-by: Maximilian Blatt <[email protected]> (external expert on behalf of DB Netz AG)
- Loading branch information
Showing
5 changed files
with
111 additions
and
23 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
Copyright 2021 The Crossplane Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License 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. | ||
*/ | ||
|
||
// common contains shared controller logic. | ||
package common |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package common | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/crossplane/crossplane-runtime/pkg/resource" | ||
"github.com/pkg/errors" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
"github.com/crossplane-contrib/provider-aws/apis" | ||
) | ||
|
||
const ( | ||
errNotTagged = "Resource does not implement the Tagged interface" | ||
errUpdateTags = "Failed to update tags for Tagged resource" | ||
) | ||
|
||
// Tagger is a controller initializer that adds all default tags to a managed | ||
// resource if it implementes the Tagged interface. | ||
type Tagger struct { | ||
kube client.Client | ||
} | ||
|
||
// NewTagger creates a new Tagger instance. | ||
func NewTagger(kube client.Client, _ apis.Tagged) *Tagger { | ||
return &Tagger{ | ||
kube: kube, | ||
} | ||
} | ||
|
||
// Initialize adds the default tags to the given managed resource if it | ||
// implements the Tagged interface. | ||
func (t *Tagger) Initialize(ctx context.Context, mg resource.Managed) error { | ||
tagged, ok := mg.(apis.Tagged) | ||
if !ok { | ||
return errors.New(errNotTagged) | ||
} | ||
shouldUpdate := false | ||
for k, v := range resource.GetExternalTags(mg) { | ||
if added := tagged.AddTag(k, v); added { | ||
shouldUpdate = true | ||
} | ||
} | ||
if shouldUpdate { | ||
return errors.Wrap(t.kube.Update(ctx, mg), errUpdateTags) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters