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

🌱 refactor tags package to support other services #1848

Merged
merged 2 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 15 additions & 11 deletions pkg/cloud/services/network/eips.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,8 @@ func (s *Service) allocateAddress(role string) (string, error) {
}

if err := wait.WaitForWithRetryable(wait.NewBackoff(), func() (bool, error) {
if err := tags.Apply(&tags.ApplyParams{
EC2Client: s.EC2Client,
BuildParams: infrav1.BuildParams{
ClusterName: s.scope.Name(),
ResourceID: *out.AllocationId,
Lifecycle: infrav1.ResourceLifecycleOwned,
Name: aws.String(fmt.Sprintf("%s-eip-%s", s.scope.Name(), role)),
Role: aws.String(role),
Additional: s.scope.AdditionalTags(),
},
}); err != nil {
buildParams := s.getEIPTagParams(*out.AllocationId, role)
if err := tags.Apply(&buildParams, s.applyTags); err != nil {
return false, err
}
return true, nil
Expand Down Expand Up @@ -159,3 +150,16 @@ func (s *Service) releaseAddresses() error {
}
return nil
}

func (s *Service) getEIPTagParams(allocationID, role string) infrav1.BuildParams {
name := fmt.Sprintf("%s-eip-%s", s.scope.Name(), role)

return infrav1.BuildParams{
ClusterName: s.scope.Name(),
ResourceID: allocationID,
Lifecycle: infrav1.ResourceLifecycleOwned,
Name: aws.String(name),
Role: aws.String(role),
Additional: s.scope.AdditionalTags(),
}
}
6 changes: 2 additions & 4 deletions pkg/cloud/services/network/gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ func (s *Service) reconcileInternetGateways() error {

// Make sure tags are up to date.
if err := wait.WaitForWithRetryable(wait.NewBackoff(), func() (bool, error) {
if err := tags.Ensure(converters.TagsToMap(gateway.Tags), &tags.ApplyParams{
EC2Client: s.EC2Client,
BuildParams: s.getGatewayTagParams(*gateway.InternetGatewayId),
}); err != nil {
buildParams := s.getGatewayTagParams(*gateway.InternetGatewayId)
if err := tags.Ensure(converters.TagsToMap(gateway.Tags), &buildParams, s.applyTags); err != nil {
return false, err
}
return true, nil
Expand Down
6 changes: 2 additions & 4 deletions pkg/cloud/services/network/natgateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,8 @@ func (s *Service) reconcileNatGateways() error {
if ngw, ok := existing[sn.ID]; ok {
// Make sure tags are up to date.
if err := wait.WaitForWithRetryable(wait.NewBackoff(), func() (bool, error) {
if err := tags.Ensure(converters.TagsToMap(ngw.Tags), &tags.ApplyParams{
EC2Client: s.EC2Client,
BuildParams: s.getNatGatewayTagParams(*ngw.NatGatewayId),
}); err != nil {
buildParams := s.getNatGatewayTagParams(*ngw.NatGatewayId)
if err := tags.Ensure(converters.TagsToMap(ngw.Tags), &buildParams, s.applyTags); err != nil {
return false, err
}
return true, nil
Expand Down
12 changes: 4 additions & 8 deletions pkg/cloud/services/network/routetables.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ func (s *Service) reconcileRouteTables() error {

// Make sure tags are up to date.
if err := wait.WaitForWithRetryable(wait.NewBackoff(), func() (bool, error) {
if err := tags.Ensure(converters.TagsToMap(rt.Tags), &tags.ApplyParams{
EC2Client: s.EC2Client,
BuildParams: s.getRouteTableTagParams(*rt.RouteTableId, sn.IsPublic, sn.AvailabilityZone),
}); err != nil {
buildParams := s.getRouteTableTagParams(*rt.RouteTableId, sn.IsPublic, sn.AvailabilityZone)
if err := tags.Ensure(converters.TagsToMap(rt.Tags), &buildParams, s.applyTags); err != nil {
return false, err
}
return true, nil
Expand Down Expand Up @@ -234,10 +232,8 @@ func (s *Service) createRouteTableWithRoutes(routes []*ec2.Route, isPublic bool,
record.Eventf(s.scope.InfraCluster(), "SuccessfulCreateRouteTable", "Created managed RouteTable %q", *out.RouteTable.RouteTableId)

if err := wait.WaitForWithRetryable(wait.NewBackoff(), func() (bool, error) {
if err := tags.Apply(&tags.ApplyParams{
EC2Client: s.EC2Client,
BuildParams: s.getRouteTableTagParams(*out.RouteTable.RouteTableId, isPublic, zone),
}); err != nil {
buildParams := s.getRouteTableTagParams(*out.RouteTable.RouteTableId, isPublic, zone)
if err := tags.Apply(&buildParams, s.applyTags); err != nil {
return false, err
}
return true, nil
Expand Down
6 changes: 2 additions & 4 deletions pkg/cloud/services/network/securitygroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,8 @@ func (s *Service) reconcileSecurityGroups() error {

// Make sure tags are up to date.
if err := wait.WaitForWithRetryable(wait.NewBackoff(), func() (bool, error) {
if err := tags.Ensure(existing.Tags, &tags.ApplyParams{
EC2Client: s.EC2Client,
BuildParams: s.getSecurityGroupTagParams(existing.Name, existing.ID, role),
}); err != nil {
buildParams := s.getSecurityGroupTagParams(existing.Name, existing.ID, role)
if err := tags.Ensure(existing.Tags, &buildParams, s.applyTags); err != nil {
return false, err
}
return true, nil
Expand Down
6 changes: 2 additions & 4 deletions pkg/cloud/services/network/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ func (s *Service) reconcileSubnets() error {
subnetTags := sub.Tags
// Make sure tags are up to date if we have a managed VPC.
if err := wait.WaitForWithRetryable(wait.NewBackoff(), func() (bool, error) {
if err := tags.Ensure(existingSubnet.Tags, &tags.ApplyParams{
EC2Client: s.EC2Client,
BuildParams: s.getSubnetTagParams(existingSubnet.ID, existingSubnet.IsPublic, existingSubnet.AvailabilityZone, subnetTags),
}); err != nil {
buildParams := s.getSubnetTagParams(existingSubnet.ID, existingSubnet.IsPublic, existingSubnet.AvailabilityZone, subnetTags)
if err := tags.Ensure(existingSubnet.Tags, &buildParams, s.applyTags); err != nil {
return false, err
}
return true, nil
Expand Down
47 changes: 47 additions & 0 deletions pkg/cloud/services/network/tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2020 The Kubernetes 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.
*/

package network

import (
"github.com/pkg/errors"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"

infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1alpha3"
)

func (s *Service) applyTags(params *infrav1.BuildParams) error {
tags := infrav1.Build(*params)

awsTags := make([]*ec2.Tag, 0, len(tags))
for k, v := range tags {
tag := &ec2.Tag{
Key: aws.String(k),
Value: aws.String(v),
}
awsTags = append(awsTags, tag)
}

createTagsInput := &ec2.CreateTagsInput{
Resources: aws.StringSlice([]string{params.ResourceID}),
Tags: awsTags,
}

_, err := s.EC2Client.CreateTags(createTagsInput)
return errors.Wrapf(err, "failed to tag resource %q in cluster %q", params.ResourceID, params.ClusterName)
}
6 changes: 2 additions & 4 deletions pkg/cloud/services/network/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ func (s *Service) reconcileVPC() error {

// Make sure attributes are configured
if err := wait.WaitForWithRetryable(wait.NewBackoff(), func() (bool, error) {
if err := tags.Ensure(vpc.Tags, &tags.ApplyParams{
EC2Client: s.EC2Client,
BuildParams: s.getVPCTagParams(vpc.ID),
}); err != nil {
buildParams := s.getVPCTagParams(vpc.ID)
if err := tags.Ensure(vpc.Tags, &buildParams, s.applyTags); err != nil {
return false, err
}
return true, nil
Expand Down
45 changes: 20 additions & 25 deletions pkg/cloud/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,36 @@ limitations under the License.
package tags

import (
"fmt"
"sort"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/pkg/errors"
infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1alpha3"
)

// ApplyParams are function parameters used to apply tags on an aws resource.
type ApplyParams struct {
infrav1.BuildParams
EC2Client ec2iface.EC2API
}
var (
ErrBuildParamsRequired = errors.New("no build params supplied")
ErrApplyFuncRequired = errors.New("no tags apply function supplied")
)

// TagsApplyFunc is used to define a function that will apply tags
type TagsApplyFunc func(params *infrav1.BuildParams) error

// Apply tags a resource with tags including the cluster tag.
func Apply(params *ApplyParams) error {
tags := infrav1.Build(params.BuildParams)

awsTags := make([]*ec2.Tag, 0, len(tags))
for k, v := range tags {
tag := &ec2.Tag{
Key: aws.String(k),
Value: aws.String(v),
}
awsTags = append(awsTags, tag)
func Apply(params *infrav1.BuildParams, fn TagsApplyFunc) error {
if params == nil {
return ErrBuildParamsRequired
}

createTagsInput := &ec2.CreateTagsInput{
Resources: aws.StringSlice([]string{params.ResourceID}),
Tags: awsTags,
if fn == nil {
return ErrApplyFuncRequired
}

_, err := params.EC2Client.CreateTags(createTagsInput)
return errors.Wrapf(err, "failed to tag resource %q in cluster %q", params.ResourceID, params.ClusterName)
if err := fn(params); err != nil {
return fmt.Errorf("failed applying tags: %w", err)
}
return nil
}

// BuildParamsToTagSpecification builds a TagSpecification for the specified resource type
Expand Down Expand Up @@ -79,10 +74,10 @@ func BuildParamsToTagSpecification(ec2ResourceType string, params infrav1.BuildP
}

// Ensure applies the tags if the current tags differ from the params.
func Ensure(current infrav1.Tags, params *ApplyParams) error {
diff := computeDiff(current, params.BuildParams)
func Ensure(current infrav1.Tags, params *infrav1.BuildParams, fn TagsApplyFunc) error {
randomvariable marked this conversation as resolved.
Show resolved Hide resolved
diff := computeDiff(current, *params)
if len(diff) > 0 {
return Apply(params)
return Apply(params, fn)
}
return nil
}
Expand Down