Skip to content

Commit

Permalink
Finish refactor of AWS clients
Browse files Browse the repository at this point in the history
  • Loading branch information
bigkraig committed Oct 25, 2018
1 parent 5457d6f commit a43326a
Show file tree
Hide file tree
Showing 16 changed files with 228 additions and 276 deletions.
16 changes: 4 additions & 12 deletions internal/alb/lb/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/alb/tg"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/albctx"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws/albrgt"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws/albwafregional"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/controller/store"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/k8s"
Expand All @@ -34,8 +32,6 @@ type Controller interface {

func NewController(
cloud aws.CloudAPI,
rgt albrgt.ResourceGroupsTaggingAPIAPI,
waf *albwafregional.WAFRegional,
store store.Storer,
nameTagGen NameTagGenerator,
tgGroupController tg.GroupController,
Expand All @@ -45,8 +41,6 @@ func NewController(

return &defaultController{
cloud: cloud,
rgt: rgt,
waf: waf,
store: store,
nameTagGen: nameTagGen,
tgGroupController: tgGroupController,
Expand All @@ -68,8 +62,6 @@ type loadBalancerConfig struct {

type defaultController struct {
cloud aws.CloudAPI
rgt albrgt.ResourceGroupsTaggingAPIAPI
waf *albwafregional.WAFRegional
store store.Storer

nameTagGen NameTagGenerator
Expand Down Expand Up @@ -249,26 +241,26 @@ func (controller *defaultController) reconcileLBInstance(ctx context.Context, in
}

func (controller *defaultController) reconcileWAF(ctx context.Context, lbArn string, webACLID *string) error {
webACLSummary, err := controller.waf.GetWebACLSummary(aws.String(lbArn))
webACLSummary, err := controller.cloud.GetWebACLSummary(aws.String(lbArn))
if err != nil {
return fmt.Errorf("failed to check webACL on loadBalancer %v due to %v", lbArn, err)
}
switch {
case webACLSummary != nil && webACLID == nil:
{
if _, err := controller.waf.Disassociate(aws.String(lbArn)); err != nil {
if _, err := controller.cloud.Disassociate(aws.String(lbArn)); err != nil {
return fmt.Errorf("failed to disassociate webACL on loadBalancer %v due to %v", lbArn, err)
}
}
case webACLSummary != nil && webACLID != nil && aws.StringValue(webACLSummary.WebACLId) != aws.StringValue(webACLID):
{
if _, err := controller.waf.Associate(aws.String(lbArn), webACLID); err != nil {
if _, err := controller.cloud.Associate(aws.String(lbArn), webACLID); err != nil {
return fmt.Errorf("failed to associate webACL on loadBalancer %v due to %v", lbArn, err)
}
}
case webACLSummary == nil && webACLID != nil:
{
if _, err := controller.waf.Associate(aws.String(lbArn), webACLID); err != nil {
if _, err := controller.cloud.Associate(aws.String(lbArn), webACLID); err != nil {
return fmt.Errorf("failed to associate webACL on loadBalancer %v due to %v", lbArn, err)
}
}
Expand Down
13 changes: 4 additions & 9 deletions internal/alb/tags/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ import (
"strings"

"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi"
"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi/resourcegroupstaggingapiiface"
api "k8s.io/api/core/v1"

"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/aws/aws-sdk-go/service/elbv2/elbv2iface"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/albctx"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/log"
Expand Down Expand Up @@ -54,17 +52,14 @@ type Controller interface {
}

// NewController constructs a new tags controller
func NewController(cloud aws.CloudAPI, rgt resourcegroupstaggingapiiface.ResourceGroupsTaggingAPIAPI) Controller {
func NewController(cloud aws.CloudAPI) Controller {
return &controller{
cloud: cloud,
rgt: rgt,
}
}

type controller struct {
cloud aws.CloudAPI
elbv2 elbv2iface.ELBV2API
rgt resourcegroupstaggingapiiface.ResourceGroupsTaggingAPIAPI
}

func (c *controller) Reconcile(ctx context.Context, desired *Tags) error {
Expand All @@ -86,7 +81,7 @@ func (c *controller) Reconcile(ctx context.Context, desired *Tags) error {
ResourceARNList: []*string{aws.String(desired.Arn)},
Tags: aws.StringMap(modify),
}
if _, err := c.rgt.TagResources(p); err != nil {
if _, err := c.cloud.TagResources(p); err != nil {
albctx.GetEventf(ctx)(api.EventTypeWarning, "ERROR", "Error tagging %s: %s", desired.Arn, err.Error())
return err
}
Expand All @@ -99,7 +94,7 @@ func (c *controller) Reconcile(ctx context.Context, desired *Tags) error {
ResourceARNList: []*string{aws.String(desired.Arn)},
TagKeys: aws.StringSlice(remove),
}
if _, err := c.rgt.UntagResources(p); err != nil {
if _, err := c.cloud.UntagResources(p); err != nil {
albctx.GetEventf(ctx)(api.EventTypeWarning, "ERROR", "Error tagging %s: %s", desired.Arn, err.Error())
return err
}
Expand All @@ -112,7 +107,7 @@ func (c *controller) elbTags(ctx context.Context, arn string) (t *Tags, err erro
var r *elbv2.DescribeTagsOutput
t = NewTags()

if r, err = c.elbv2.DescribeTags(&elbv2.DescribeTagsInput{ResourceArns: []*string{aws.String(arn)}}); err == nil {
if r, err = c.cloud.DescribeELBV2Tags(&elbv2.DescribeTagsInput{ResourceArns: []*string{aws.String(arn)}}); err == nil {
for _, tagDescription := range r.TagDescriptions {
for _, tag := range tagDescription.Tags {
t.Tags[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
Expand Down
7 changes: 2 additions & 5 deletions internal/alb/tg/targetgroup_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/alb/tags"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/aws/albrgt"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/annotations/action"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/backend"
"github.com/kubernetes-sigs/aws-alb-ingress-controller/internal/ingress/controller/store"
Expand All @@ -29,14 +28,13 @@ type GroupController interface {

// NewGroupController creates an GroupController
func NewGroupController(
cloud aws.CloudAPI, rgt albrgt.ResourceGroupsTaggingAPIAPI,
cloud aws.CloudAPI,
store store.Storer,
nameTagGen NameTagGenerator,
tagsController tags.Controller,
endpointResolver backend.EndpointResolver) GroupController {
tgController := NewController(cloud, store, nameTagGen, tagsController, endpointResolver)
return &defaultGroupController{
rgt: rgt,
cloud: cloud,
nameTagGen: nameTagGen,
tgController: tgController,
Expand All @@ -46,7 +44,6 @@ func NewGroupController(
var _ GroupController = (*defaultGroupController)(nil)

type defaultGroupController struct {
rgt albrgt.ResourceGroupsTaggingAPIAPI
cloud aws.CloudAPI
nameTagGen NameTagGenerator

Expand Down Expand Up @@ -84,7 +81,7 @@ func (controller *defaultGroupController) GC(ctx context.Context, tgGroup Target
for _, tg := range tgGroup.TGByBackend {
usedTgArns.Insert(tg.Arn)
}
arns, err := controller.rgt.GetResourcesByFilters(tagFilters, albrgt.ResourceTypeEnumELBTargetGroup)
arns, err := controller.cloud.GetResourcesByFilters(tagFilters, aws.ResourceTypeEnumELBTargetGroup)
if err != nil {
return fmt.Errorf("failed to get targetGroups due to %v", err)
}
Expand Down
187 changes: 90 additions & 97 deletions internal/aws/albelbv2/dummy.go
Original file line number Diff line number Diff line change
@@ -1,99 +1,92 @@
package albelbv2

import (
"fmt"

"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/aws/aws-sdk-go/service/elbv2/elbv2iface"
util "github.com/kubernetes-sigs/aws-alb-ingress-controller/pkg/util/types"
)

type Dummy struct {
elbv2iface.ELBV2API
resp interface{}

outputs output
}

type output map[string]interface{}

func (o output) error(s string) error {
if v, ok := o[s]; ok && v != nil {
return v.(error)
}
return nil
}

func NewDummy() *Dummy {
d := &Dummy{}
d.outputs = make(output)
return d
}

// ClusterLoadBalancers ...
func (d *Dummy) ClusterLoadBalancers() ([]*elbv2.LoadBalancer, error) {
return nil, nil
}

// ClusterTargetGroups ...
func (d *Dummy) ClusterTargetGroups() (map[string][]*elbv2.TargetGroup, error) {
return nil, nil
}

// UpdateTags ...
func (d *Dummy) UpdateTags(arn *string, old util.ELBv2Tags, new util.ELBv2Tags) error { return nil }

// RemoveTargetGroup ...
func (d *Dummy) RemoveTargetGroup(arn *string) error { return nil }

// RemoveListener ...
func (d *Dummy) RemoveListener(arn *string) error { return nil }

// DescribeListenersForLoadBalancer ...
func (d *Dummy) DescribeListenersForLoadBalancer(loadBalancerArn *string) ([]*elbv2.Listener, error) {
return nil, nil
}

// Status ...
func (d *Dummy) Status() func() error { return nil }

// CreateListener ...
func (d *Dummy) CreateListener(in *elbv2.CreateListenerInput) (*elbv2.CreateListenerOutput, error) {
return d.outputs["CreateListenerOutput"].(*elbv2.CreateListenerOutput), d.outputs.error("CreateListenerError")
}

// ModifyListener ...
func (d *Dummy) ModifyListener(in *elbv2.ModifyListenerInput) (*elbv2.ModifyListenerOutput, error) {
return d.outputs["ModifyListenerOutput"].(*elbv2.ModifyListenerOutput), d.outputs.error("ModifyListenerError")
}

// CreateRule ...
func (d *Dummy) CreateRule(in *elbv2.CreateRuleInput) (*elbv2.CreateRuleOutput, error) {
return d.outputs["CreateRuleOutput"].(*elbv2.CreateRuleOutput), d.outputs.error("CreateRuleError")
}

// ModifyRule ...
func (d *Dummy) ModifyRule(in *elbv2.ModifyRuleInput) (*elbv2.ModifyRuleOutput, error) {
fmt.Println(d.outputs.error("ModifyRuleError"))
return d.outputs["ModifyRuleOutput"].(*elbv2.ModifyRuleOutput), d.outputs.error("ModifyRuleError")
}

// DeleteRule ...
func (d *Dummy) DeleteRule(in *elbv2.DeleteRuleInput) (*elbv2.DeleteRuleOutput, error) {
return d.outputs["DeleteRuleOutput"].(*elbv2.DeleteRuleOutput), d.outputs.error("DeleteRuleError")
}

// GetRules ...
func (d *Dummy) GetRules(l string) ([]*elbv2.Rule, error) {
return nil, nil
}

// GetLoadBalancerByArn ...
func (d *Dummy) GetLoadBalancerByArn(arn string) (*elbv2.LoadBalancer, error) {
return d.outputs["GetLoadBalancerByArn"].(*elbv2.LoadBalancer), d.outputs.error("GetLoadBalancerByArn")
}

// SetField ...
func (d *Dummy) SetField(field string, v interface{}) {
d.outputs[field] = v
}
//
// type Dummy struct {
// elbv2iface.ELBV2API
// resp interface{}
//
// outputs output
// }
//
// type output map[string]interface{}
//
// func (o output) error(s string) error {
// if v, ok := o[s]; ok && v != nil {
// return v.(error)
// }
// return nil
// }
//
// func NewDummy() *Dummy {
// d := &Dummy{}
// d.outputs = make(output)
// return d
// }
//
// // ClusterLoadBalancers ...
// func (d *Dummy) ClusterLoadBalancers() ([]*elbv2.LoadBalancer, error) {
// return nil, nil
// }
//
// // ClusterTargetGroups ...
// func (d *Dummy) ClusterTargetGroups() (map[string][]*elbv2.TargetGroup, error) {
// return nil, nil
// }
//
// // UpdateTags ...
// func (d *Dummy) UpdateTags(arn *string, old util.ELBv2Tags, new util.ELBv2Tags) error { return nil }
//
// // RemoveTargetGroup ...
// func (d *Dummy) RemoveTargetGroup(arn *string) error { return nil }
//
// // RemoveListener ...
// func (d *Dummy) RemoveListener(arn *string) error { return nil }
//
// // DescribeListenersForLoadBalancer ...
// func (d *Dummy) DescribeListenersForLoadBalancer(loadBalancerArn *string) ([]*elbv2.Listener, error) {
// return nil, nil
// }
//
// // Status ...
// func (d *Dummy) Status() func() error { return nil }
//
// // CreateListener ...
// func (d *Dummy) CreateListener(in *elbv2.CreateListenerInput) (*elbv2.CreateListenerOutput, error) {
// return d.outputs["CreateListenerOutput"].(*elbv2.CreateListenerOutput), d.outputs.error("CreateListenerError")
// }
//
// // ModifyListener ...
// func (d *Dummy) ModifyListener(in *elbv2.ModifyListenerInput) (*elbv2.ModifyListenerOutput, error) {
// return d.outputs["ModifyListenerOutput"].(*elbv2.ModifyListenerOutput), d.outputs.error("ModifyListenerError")
// }
//
// // CreateRule ...
// func (d *Dummy) CreateRule(in *elbv2.CreateRuleInput) (*elbv2.CreateRuleOutput, error) {
// return d.outputs["CreateRuleOutput"].(*elbv2.CreateRuleOutput), d.outputs.error("CreateRuleError")
// }
//
// // ModifyRule ...
// func (d *Dummy) ModifyRule(in *elbv2.ModifyRuleInput) (*elbv2.ModifyRuleOutput, error) {
// fmt.Println(d.outputs.error("ModifyRuleError"))
// return d.outputs["ModifyRuleOutput"].(*elbv2.ModifyRuleOutput), d.outputs.error("ModifyRuleError")
// }
//
// // DeleteRule ...
// func (d *Dummy) DeleteRule(in *elbv2.DeleteRuleInput) (*elbv2.DeleteRuleOutput, error) {
// return d.outputs["DeleteRuleOutput"].(*elbv2.DeleteRuleOutput), d.outputs.error("DeleteRuleError")
// }
//
// // GetRules ...
// func (d *Dummy) GetRules(l string) ([]*elbv2.Rule, error) {
// return nil, nil
// }
//
// // GetLoadBalancerByArn ...
// func (d *Dummy) GetLoadBalancerByArn(arn string) (*elbv2.LoadBalancer, error) {
// return d.outputs["GetLoadBalancerByArn"].(*elbv2.LoadBalancer), d.outputs.error("GetLoadBalancerByArn")
// }
//
// // SetField ...
// func (d *Dummy) SetField(field string, v interface{}) {
// d.outputs[field] = v
// }
32 changes: 14 additions & 18 deletions internal/aws/albrgt/dummy.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
package albrgt

import (
"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi/resourcegroupstaggingapiiface"
)

type Dummy struct {
resourcegroupstaggingapiiface.ResourceGroupsTaggingAPIAPI
resp interface{}
respError error
}

func (d *Dummy) GetClusterResources() (*Resources, error) {
return d.resp.(*Resources), d.respError
}

func (d *Dummy) SetResponse(i interface{}, e error) {
d.resp = i
d.respError = e
}
// type Dummy struct {
// resourcegroupstaggingapiiface.ResourceGroupsTaggingAPIAPI
// resp interface{}
// respError error
// }
//
// func (d *Dummy) GetClusterResources() (*Resources, error) {
// return d.resp.(*Resources), d.respError
// }
//
// func (d *Dummy) SetResponse(i interface{}, e error) {
// d.resp = i
// d.respError = e
// }
5 changes: 0 additions & 5 deletions internal/aws/albrgt/resource_types.go

This file was deleted.

Loading

0 comments on commit a43326a

Please sign in to comment.