Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 4 additions & 2 deletions controllers/gateway_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,9 @@ func UpdateGWListenerStatus(ctx context.Context, k8sClient client.Client, gw *gw
return err
}

// Add one of lattice domains as GW address. This can represent incorrect value in some cases (e.g. cross-account)
// TODO: support multiple endpoint addresses across services.
// Add one of lattice domains as GW address. This is supposed to be a single ingress endpoint (or a single pool of them)
// but we have different endpoints for each service. This can represent incorrect value in some cases (e.g. cross-account)
// Due to size limit, we cannot put all service addresses here.
if len(routes) > 0 {
gw.Status.Addresses = []gwv1beta1.GatewayAddress{}
addressType := gwv1beta1.HostnameAddressType
Expand All @@ -310,6 +311,7 @@ func UpdateGWListenerStatus(ctx context.Context, k8sClient client.Client, gw *gw
Type: &addressType,
Value: domain,
})
break
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions controllers/route_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ func TestRouteReconciler_ReconcileCreates(t *testing.T) {

mockCloud := aws2.NewMockCloud(c)
mockLattice := mocks.NewMockLattice(c)
mockTagging := mocks.NewMockTagging(c)
mockCloud.EXPECT().Lattice().Return(mockLattice).AnyTimes()
mockCloud.EXPECT().Tagging().Return(mockTagging).AnyTimes()
mockCloud.EXPECT().Config().Return(
aws2.CloudConfig{
VpcId: config.VpcID,
Expand Down Expand Up @@ -204,6 +206,7 @@ func TestRouteReconciler_ReconcileCreates(t *testing.T) {
},
}, nil) // will trigger DNS Update

mockTagging.EXPECT().FindResourcesByTags(ctx, gomock.Any(), gomock.Any()).Return(nil, nil)
mockLattice.EXPECT().ListTargetGroupsAsList(ctx, gomock.Any()).Return(
[]*vpclattice.TargetGroupSummary{}, nil).AnyTimes() // this will cause us to skip "unused delete" step
mockLattice.EXPECT().CreateTargetGroupWithContext(ctx, gomock.Any()).Return(
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/aws/aws-sdk-go v1.44.321
github.com/go-logr/zapr v1.2.3
github.com/golang/mock v1.6.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.24.1
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
Expand Down
18 changes: 17 additions & 1 deletion pkg/aws/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type CloudConfig struct {
type Cloud interface {
Config() CloudConfig
Lattice() services.Lattice
Tagging() services.Tagging

// creates lattice tags with default values populated
DefaultTags() services.Tags
Expand Down Expand Up @@ -70,7 +71,8 @@ func NewCloud(log gwlog.Logger, cfg CloudConfig) (Cloud, error) {
})

lattice := services.NewDefaultLattice(sess, cfg.Region)
cl := NewDefaultCloud(lattice, cfg)
tagging := services.NewDefaultTagging(sess, cfg.Region)
cl := NewDefaultCloudWithTagging(lattice, tagging, cfg)
return cl, nil
}

Expand All @@ -83,16 +85,30 @@ func NewDefaultCloud(lattice services.Lattice, cfg CloudConfig) Cloud {
}
}

func NewDefaultCloudWithTagging(lattice services.Lattice, tagging services.Tagging, cfg CloudConfig) Cloud {
return &defaultCloud{
cfg: cfg,
lattice: lattice,
tagging: tagging,
managedByTag: getManagedByTag(cfg),
}
}

type defaultCloud struct {
cfg CloudConfig
lattice services.Lattice
tagging services.Tagging
managedByTag string
}

func (c *defaultCloud) Lattice() services.Lattice {
return c.lattice
}

func (c *defaultCloud) Tagging() services.Tagging {
return c.tagging
}

func (c *defaultCloud) Config() CloudConfig {
return c.cfg
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/aws/cloud_mocks.go

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

99 changes: 99 additions & 0 deletions pkg/aws/services/tagging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package services

import (
"context"
"github.com/aws/aws-application-networking-k8s/pkg/utils"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
taggingapi "github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi"
taggingapiiface "github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi/resourcegroupstaggingapiiface"
)

//go:generate mockgen -destination tagging_mocks.go -package services github.com/aws/aws-application-networking-k8s/pkg/aws/services Tagging

type ResourceType string

const (
resourceTypePrefix = "vpc-lattice:"

ResourceTypeTargetGroup ResourceType = resourceTypePrefix + "targetgroup"

// https://docs.aws.amazon.com/resourcegroupstagging/latest/APIReference/API_GetResources.html#API_GetResources_RequestSyntax
maxArnsPerGetResourcesApi = 100
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you add link to the limit please

)

type Tags = map[string]*string

type Tagging interface {
taggingapiiface.ResourceGroupsTaggingAPIAPI

// Receives a list of arns and returns arn-to-tags map.
GetTagsForArns(ctx context.Context, arns []string) (map[string]Tags, error)

// Finds one resource that matches the given set of tags.
FindResourcesByTags(ctx context.Context, resourceType ResourceType, tags Tags) ([]string, error)
}

type defaultTagging struct {
taggingapiiface.ResourceGroupsTaggingAPIAPI
}

func (t *defaultTagging) GetTagsForArns(ctx context.Context, arns []string) (map[string]Tags, error) {
chunks := utils.Chunks(utils.SliceMap(arns, aws.String), maxArnsPerGetResourcesApi)
result := make(map[string]Tags)

for _, chunk := range chunks {
input := &taggingapi.GetResourcesInput{
ResourceARNList: chunk,
}
err := t.GetResourcesPagesWithContext(ctx, input, func(page *taggingapi.GetResourcesOutput, lastPage bool) bool {
for _, r := range page.ResourceTagMappingList {
result[*r.ResourceARN] = convertTags(r.Tags)
}
return true
})
if err != nil {
return nil, err
}
}
return result, nil
}

func (t *defaultTagging) FindResourcesByTags(ctx context.Context, resourceType ResourceType, tags Tags) ([]string, error) {
input := &taggingapi.GetResourcesInput{
TagFilters: convertTagsToFilter(tags),
ResourceTypeFilters: []*string{aws.String(string(resourceType))},
}
resp, err := t.GetResourcesWithContext(ctx, input)
if err != nil {
return nil, err
}
matchingArns := utils.SliceMap(resp.ResourceTagMappingList, func(t *taggingapi.ResourceTagMapping) string {
return aws.StringValue(t.ResourceARN)
})
return matchingArns, nil
}

func NewDefaultTagging(sess *session.Session, region string) *defaultTagging {
api := taggingapi.New(sess, &aws.Config{Region: aws.String(region)})
return &defaultTagging{ResourceGroupsTaggingAPIAPI: api}
}

func convertTags(tags []*taggingapi.Tag) Tags {
out := make(Tags)
for _, tag := range tags {
out[*tag.Key] = tag.Value
}
return out
}

func convertTagsToFilter(tags Tags) []*taggingapi.TagFilter {
filters := make([]*taggingapi.TagFilter, 0, len(tags))
for k, v := range tags {
filters = append(filters, &taggingapi.TagFilter{
Key: aws.String(k),
Values: []*string{v},
})
}
return filters
}
Loading