Skip to content

Commit 6c25553

Browse files
authored
Add support for clusters running in private VPC (#627)
Add support for clusters running in private VPC
1 parent f08ce05 commit 6c25553

File tree

7 files changed

+532
-549
lines changed

7 files changed

+532
-549
lines changed

cmd/aws-application-networking-k8s/main.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ package main
1818

1919
import (
2020
"flag"
21+
"os"
22+
"strings"
23+
2124
"github.com/aws/aws-application-networking-k8s/pkg/webhook"
2225
"github.com/go-logr/zapr"
2326
"go.uber.org/zap/zapcore"
24-
"os"
2527
k8swebhook "sigs.k8s.io/controller-runtime/pkg/webhook"
26-
"strings"
2728

2829
"github.com/aws/aws-application-networking-k8s/pkg/aws"
2930
"github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"
@@ -121,13 +122,15 @@ func main() {
121122
"DefaultServiceNetwork", config.DefaultServiceNetwork,
122123
"ClusterName", config.ClusterName,
123124
"LogLevel", logLevel,
125+
"DisableTaggingServiceAPI", config.DisableTaggingServiceAPI,
124126
)
125127

126128
cloud, err := aws.NewCloud(log.Named("cloud"), aws.CloudConfig{
127-
VpcId: config.VpcID,
128-
AccountId: config.AccountID,
129-
Region: config.Region,
130-
ClusterName: config.ClusterName,
129+
VpcId: config.VpcID,
130+
AccountId: config.AccountID,
131+
Region: config.Region,
132+
ClusterName: config.ClusterName,
133+
TaggingServiceAPIDisabled: config.DisableTaggingServiceAPI,
131134
}, metrics.Registry)
132135
if err != nil {
133136
setupLog.Fatal("cloud client setup failed: %s", err)

pkg/aws/cloud.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aws
22

33
import (
4+
"context"
45
"fmt"
56

67
"github.com/prometheus/client_golang/prometheus"
@@ -10,7 +11,6 @@ import (
1011
"github.com/aws/aws-sdk-go/service/vpclattice"
1112
"golang.org/x/exp/maps"
1213

13-
"context"
1414
"github.com/aws/aws-application-networking-k8s/pkg/aws/metrics"
1515
"github.com/aws/aws-application-networking-k8s/pkg/aws/services"
1616
"github.com/aws/aws-application-networking-k8s/pkg/utils/gwlog"
@@ -24,10 +24,11 @@ const (
2424
//go:generate mockgen -destination cloud_mocks.go -package aws github.com/aws/aws-application-networking-k8s/pkg/aws Cloud
2525

2626
type CloudConfig struct {
27-
VpcId string
28-
AccountId string
29-
Region string
30-
ClusterName string
27+
VpcId string
28+
AccountId string
29+
Region string
30+
ClusterName string
31+
TaggingServiceAPIDisabled bool
3132
}
3233

3334
type Cloud interface {
@@ -82,7 +83,14 @@ func NewCloud(log gwlog.Logger, cfg CloudConfig, metricsRegisterer prometheus.Re
8283
}
8384

8485
lattice := services.NewDefaultLattice(sess, cfg.AccountId, cfg.Region)
85-
tagging := services.NewDefaultTagging(sess, cfg.Region)
86+
var tagging services.Tagging
87+
88+
if cfg.TaggingServiceAPIDisabled {
89+
tagging = services.NewLatticeTagging(sess, cfg.AccountId, cfg.Region, cfg.VpcId)
90+
} else {
91+
tagging = services.NewDefaultTagging(sess, cfg.Region)
92+
}
93+
8694
cl := NewDefaultCloudWithTagging(lattice, tagging, cfg)
8795
return cl, nil
8896
}

pkg/aws/cloud_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestGetManagedByTag(t *testing.T) {
3030
}
3131

3232
func TestDefaultTags(t *testing.T) {
33-
cfg := CloudConfig{"acc", "vpc", "region", "cluster"}
33+
cfg := CloudConfig{"acc", "vpc", "region", "cluster", false}
3434
c := NewDefaultCloud(nil, cfg)
3535
tags := c.DefaultTags()
3636
tagWant := getManagedByTag(cfg)

pkg/aws/services/tagging.go

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package services
22

33
import (
44
"context"
5+
"fmt"
6+
57
"github.com/aws/aws-application-networking-k8s/pkg/utils"
68
"github.com/aws/aws-sdk-go/aws"
79
"github.com/aws/aws-sdk-go/aws/session"
810
taggingapi "github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi"
911
taggingapiiface "github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi/resourcegroupstaggingapiiface"
12+
"github.com/aws/aws-sdk-go/service/vpclattice"
1013
)
1114

1215
//go:generate mockgen -destination tagging_mocks.go -package services github.com/aws/aws-application-networking-k8s/pkg/aws/services Tagging
@@ -26,8 +29,6 @@ const (
2629
type Tags = map[string]*string
2730

2831
type Tagging interface {
29-
taggingapiiface.ResourceGroupsTaggingAPIAPI
30-
3132
// Receives a list of arns and returns arn-to-tags map.
3233
GetTagsForArns(ctx context.Context, arns []string) (map[string]Tags, error)
3334

@@ -39,6 +40,11 @@ type defaultTagging struct {
3940
taggingapiiface.ResourceGroupsTaggingAPIAPI
4041
}
4142

43+
type latticeTagging struct {
44+
Lattice
45+
vpcId string
46+
}
47+
4248
func (t *defaultTagging) GetTagsForArns(ctx context.Context, arns []string) (map[string]Tags, error) {
4349
chunks := utils.Chunks(utils.SliceMap(arns, aws.String), maxArnsPerGetResourcesApi)
4450
result := make(map[string]Tags)
@@ -80,6 +86,67 @@ func NewDefaultTagging(sess *session.Session, region string) *defaultTagging {
8086
return &defaultTagging{ResourceGroupsTaggingAPIAPI: api}
8187
}
8288

89+
// Use VPC Lattice API instead of the Resource Groups Tagging API
90+
func NewLatticeTagging(sess *session.Session, acc string, region string, vpcId string) *latticeTagging {
91+
api := NewDefaultLattice(sess, acc, region)
92+
return &latticeTagging{Lattice: api, vpcId: vpcId}
93+
}
94+
95+
func (t *latticeTagging) GetTagsForArns(ctx context.Context, arns []string) (map[string]Tags, error) {
96+
result := map[string]Tags{}
97+
98+
for _, arn := range arns {
99+
tags, err := t.ListTagsForResourceWithContext(ctx,
100+
&vpclattice.ListTagsForResourceInput{ResourceArn: aws.String(arn)},
101+
)
102+
if err != nil {
103+
return nil, err
104+
}
105+
result[arn] = tags.Tags
106+
}
107+
return result, nil
108+
}
109+
110+
func (t *latticeTagging) FindResourcesByTags(ctx context.Context, resourceType ResourceType, tags Tags) ([]string, error) {
111+
if resourceType != ResourceTypeTargetGroup {
112+
return nil, fmt.Errorf("unsupported resource type %q for FindResourcesByTags", resourceType)
113+
}
114+
115+
tgs, err := t.ListTargetGroupsAsList(ctx, &vpclattice.ListTargetGroupsInput{
116+
VpcIdentifier: aws.String(t.vpcId),
117+
})
118+
if err != nil {
119+
return nil, err
120+
}
121+
122+
arns := make([]string, 0, len(tgs))
123+
124+
for _, tg := range tgs {
125+
resp, err := t.ListTagsForResourceWithContext(ctx,
126+
&vpclattice.ListTagsForResourceInput{ResourceArn: tg.Arn},
127+
)
128+
if err != nil {
129+
return nil, err
130+
}
131+
132+
if containsTags(resp.Tags, tags) {
133+
arns = append(arns, aws.StringValue(tg.Arn))
134+
}
135+
}
136+
137+
return arns, nil
138+
}
139+
140+
func containsTags(source, check Tags) bool {
141+
for k, v := range check {
142+
sourceV, ok := source[k]
143+
if !ok || aws.StringValue(sourceV) != aws.StringValue(v) {
144+
return false
145+
}
146+
}
147+
return len(check) != 0
148+
}
149+
83150
func convertTags(tags []*taggingapi.Tag) Tags {
84151
out := make(Tags)
85152
for _, tag := range tags {

0 commit comments

Comments
 (0)