Skip to content

Commit

Permalink
feat(eks): Use generic tag initializer for addon
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Blatt <[email protected]>
(external expert on behalf of DB Netz AG)
  • Loading branch information
MisterMX committed Jul 4, 2022
1 parent 7fe0866 commit d9d13ad
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 23 deletions.
34 changes: 34 additions & 0 deletions apis/eks/v1alpha1/zz_generated.tagged.go

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

3 changes: 3 additions & 0 deletions apis/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ limitations under the License.
// Generate crossplane-runtime methodsets (resource.Claim, etc)
//go:generate go run -tags generate github.com/crossplane/crossplane-tools/cmd/angryjet generate-methodsets --header-file=../hack/boilerplate.go.txt ./...

// Generate tag methods for each managed resource
//go:generate go run -tags generate github.com/crossplane-contrib/provider-aws/cmd/codegen generate-methodsets --header-file=../hack/boilerplate.go.txt ./...

// TODO(muvaf): Remove the last mockgen usage in Addon resource and move to
// current convention with tests.
//go:generate go run github.com/golang/mock/mockgen --build_flags=--mod=mod -package eksiface -copyright_file ../hack/boilerplate.go.txt -destination ../pkg/clients/eks/fake/eksiface/fake.go github.com/aws/aws-sdk-go/service/eks/eksiface EKSAPI
Expand Down
18 changes: 18 additions & 0 deletions pkg/controller/common/doc.go
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
48 changes: 48 additions & 0 deletions pkg/controller/common/tagger.go
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
}
31 changes: 8 additions & 23 deletions pkg/controller/eks/addon/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ import (
eksv1alpha1 "github.com/crossplane-contrib/provider-aws/apis/eks/v1alpha1"
"github.com/crossplane-contrib/provider-aws/apis/v1alpha1"
awsclients "github.com/crossplane-contrib/provider-aws/pkg/clients"
"github.com/crossplane-contrib/provider-aws/pkg/controller/common"
"github.com/crossplane-contrib/provider-aws/pkg/features"
)

const (
errNotEKSCluster = "managed resource is not an EKS cluster custom resource"
errKubeUpdateFailed = "cannot update EKS cluster custom resource"
errTagResource = "cannot tag resource"
errUntagResource = "cannot untag resource"
errNotEKSCluster = "managed resource is not an EKS cluster custom resource"
errTagResource = "cannot tag resource"
errUntagResource = "cannot untag resource"
)

// SetupAddon adds a controller that reconciles Clusters.
Expand All @@ -65,7 +65,10 @@ func SetupAddon(mgr ctrl.Manager, o controller.Options) error {
Complete(managed.NewReconciler(mgr,
resource.ManagedKind(eksv1alpha1.AddonGroupVersionKind),
managed.WithExternalConnecter(&connector{kube: mgr.GetClient(), opts: opts}),
managed.WithInitializers(managed.NewDefaultProviderConfig(mgr.GetClient()), &tagger{kube: mgr.GetClient()}),
managed.WithInitializers(
managed.NewDefaultProviderConfig(mgr.GetClient()),
common.NewTagger(mgr.GetClient(), &eksv1alpha1.Addon{}),
),
managed.WithReferenceResolver(managed.NewAPISimpleReferenceResolver(mgr.GetClient())),
managed.WithPollInterval(o.PollInterval),
managed.WithLogger(o.Logger.WithValues("controller", name)),
Expand Down Expand Up @@ -199,21 +202,3 @@ func preDelete(_ context.Context, cr *eksv1alpha1.Addon, obj *awseks.DeleteAddon
obj.ClusterName = cr.Spec.ForProvider.ClusterName
return false, nil
}

type tagger struct {
kube client.Client
}

func (t *tagger) Initialize(ctx context.Context, mg resource.Managed) error {
cr, ok := mg.(*eksv1alpha1.Addon)
if !ok {
return errors.New(errNotEKSCluster)
}
if cr.Spec.ForProvider.Tags == nil {
cr.Spec.ForProvider.Tags = map[string]*string{}
}
for k, v := range resource.GetExternalTags(mg) {
cr.Spec.ForProvider.Tags[k] = awsclients.String(v)
}
return errors.Wrap(t.kube.Update(ctx, cr), errKubeUpdateFailed)
}

0 comments on commit d9d13ad

Please sign in to comment.