Skip to content

Commit 468ff02

Browse files
Add Tagging to lattice target group resource (#59)
* Add targetgroup taggings * Add targetgroup tagging when it is created due to HTTProute * Add tg tagging unit test for serviceexport * Add unit test for tagging tg created by HTTProute backendref * Fixed the tg tagging unit test failure * Address CR comments
1 parent 4a5c675 commit 468ff02

File tree

4 files changed

+137
-58
lines changed

4 files changed

+137
-58
lines changed

pkg/deploy/lattice/target_group_manager.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,20 @@ func (s *defaultTargetGroupManager) Create(ctx context.Context, targetGroup *lat
7373
Config: config,
7474
Name: &targetGroup.Spec.Name,
7575
Type: &targetGroupType,
76+
Tags: make(map[string]*string),
7677
}
78+
createTargetGroupInput.Tags[latticemodel.K8SServiceNameKey] = &targetGroup.Spec.Config.K8SServiceName
79+
createTargetGroupInput.Tags[latticemodel.K8SServiceNamespaceKey] = &targetGroup.Spec.Config.K8SServiceNamespace
80+
if targetGroup.Spec.Config.IsServiceExport {
81+
value := latticemodel.K8SServiceExportType
82+
createTargetGroupInput.Tags[latticemodel.K8SParentRefTypeKey] = &value
83+
} else {
84+
value := latticemodel.K8SHTTPRouteType
85+
createTargetGroupInput.Tags[latticemodel.K8SParentRefTypeKey] = &value
86+
createTargetGroupInput.Tags[latticemodel.K8SHTTPRouteNameKey] = &targetGroup.Spec.Config.K8SHTTPRouteName
87+
createTargetGroupInput.Tags[latticemodel.K8SHTTPRouteNamespaceKey] = &targetGroup.Spec.Config.K8SHTTPRouteNamespace
88+
}
89+
7790
vpcLatticeSess := s.cloud.Lattice()
7891
resp, err := vpcLatticeSess.CreateTargetGroupWithContext(ctx, &createTargetGroupInput)
7992
glog.V(2).Infof("create target group >>>> req [%v], resp[%v] err[%v]\n", createTargetGroupInput, resp, err)

pkg/deploy/lattice/target_group_manager_test.go

Lines changed: 91 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -21,59 +21,101 @@ func Test_CreateTargetGroup_TGNotExist_Active(t *testing.T) {
2121
defer c.Finish()
2222
ctx := context.TODO()
2323

24-
tgSpec := latticemodel.TargetGroupSpec{
25-
Name: "test",
26-
Config: latticemodel.TargetGroupConfig{
27-
Port: int32(8080),
28-
Protocol: "HTTP",
29-
VpcID: config.VpcID,
30-
EKSClusterName: "",
31-
IsServiceImport: false,
32-
},
33-
}
34-
tgCreateInput := latticemodel.TargetGroup{
35-
ResourceMeta: core.ResourceMeta{},
36-
Spec: tgSpec,
37-
}
38-
mockVpcLatticeSess := mocks.NewMockLattice(c)
39-
arn := "12345678912345678912"
40-
id := "12345678912345678912"
41-
name := "test"
42-
tgStatus := vpclattice.TargetGroupStatusActive
43-
tgCreateOutput := &vpclattice.CreateTargetGroupOutput{
44-
Arn: &arn,
45-
Id: &id,
46-
Name: &name,
47-
Status: &tgStatus,
48-
}
49-
p := int64(8080)
50-
prot := "HTTP"
51-
emptystring := ""
52-
config := &vpclattice.TargetGroupConfig{
53-
Port: &p,
54-
Protocol: &prot,
55-
VpcIdentifier: &config.VpcID,
56-
ProtocolVersion: &emptystring,
57-
}
24+
tg_types := [2]string{"by-backendref", "by-serviceexport"}
25+
26+
for _, tg_type := range tg_types {
27+
var tgSpec latticemodel.TargetGroupSpec
28+
29+
if tg_type == "by-serviceexport" {
30+
// testing targetgroup for serviceexport
31+
tgSpec = latticemodel.TargetGroupSpec{
32+
Name: "test",
33+
Config: latticemodel.TargetGroupConfig{
34+
Port: int32(8080),
35+
Protocol: "HTTP",
36+
VpcID: config.VpcID,
37+
EKSClusterName: "",
38+
IsServiceImport: false,
39+
IsServiceExport: true,
40+
K8SServiceName: "exportsvc1",
41+
K8SServiceNamespace: "default",
42+
},
43+
}
44+
} else if tg_type == "by-backendref" {
45+
// testing targetgroup for serviceexport
46+
tgSpec = latticemodel.TargetGroupSpec{
47+
Name: "test",
48+
Config: latticemodel.TargetGroupConfig{
49+
Port: int32(8080),
50+
Protocol: "HTTP",
51+
VpcID: config.VpcID,
52+
EKSClusterName: "",
53+
IsServiceImport: false,
54+
IsServiceExport: false,
55+
K8SServiceName: "backend-svc1",
56+
K8SServiceNamespace: "default",
57+
K8SHTTPRouteName: "httproute1",
58+
K8SHTTPRouteNamespace: "default",
59+
},
60+
}
61+
}
62+
tgCreateInput := latticemodel.TargetGroup{
63+
ResourceMeta: core.ResourceMeta{},
64+
Spec: tgSpec,
65+
}
66+
mockVpcLatticeSess := mocks.NewMockLattice(c)
67+
arn := "12345678912345678912"
68+
id := "12345678912345678912"
69+
name := "test"
70+
tgStatus := vpclattice.TargetGroupStatusActive
71+
tgCreateOutput := &vpclattice.CreateTargetGroupOutput{
72+
Arn: &arn,
73+
Id: &id,
74+
Name: &name,
75+
Status: &tgStatus,
76+
}
77+
p := int64(8080)
78+
prot := "HTTP"
79+
emptystring := ""
80+
config := &vpclattice.TargetGroupConfig{
81+
Port: &p,
82+
Protocol: &prot,
83+
VpcIdentifier: &config.VpcID,
84+
ProtocolVersion: &emptystring,
85+
}
5886

59-
createTargetGroupInput := vpclattice.CreateTargetGroupInput{
60-
Config: config,
61-
Name: &name,
62-
Type: &emptystring,
63-
}
87+
createTargetGroupInput := vpclattice.CreateTargetGroupInput{
88+
Config: config,
89+
Name: &name,
90+
Type: &emptystring,
91+
Tags: make(map[string]*string),
92+
}
93+
createTargetGroupInput.Tags[latticemodel.K8SServiceNameKey] = &tgSpec.Config.K8SServiceName
94+
createTargetGroupInput.Tags[latticemodel.K8SServiceNamespaceKey] = &tgSpec.Config.K8SServiceNamespace
95+
96+
if tg_type == "by-serviceexport" {
97+
value := latticemodel.K8SServiceExportType
98+
createTargetGroupInput.Tags[latticemodel.K8SParentRefTypeKey] = &value
99+
} else if tg_type == "by-backendref" {
100+
value := latticemodel.K8SHTTPRouteType
101+
createTargetGroupInput.Tags[latticemodel.K8SParentRefTypeKey] = &value
102+
createTargetGroupInput.Tags[latticemodel.K8SHTTPRouteNameKey] = &tgSpec.Config.K8SHTTPRouteName
103+
createTargetGroupInput.Tags[latticemodel.K8SHTTPRouteNamespaceKey] = &tgSpec.Config.K8SHTTPRouteNamespace
104+
}
64105

65-
listTgOutput := []*vpclattice.TargetGroupSummary{}
106+
listTgOutput := []*vpclattice.TargetGroupSummary{}
66107

67-
mockCloud := mocks_aws.NewMockCloud(c)
68-
mockVpcLatticeSess.EXPECT().ListTargetGroupsAsList(ctx, gomock.Any()).Return(listTgOutput, nil)
69-
mockVpcLatticeSess.EXPECT().CreateTargetGroupWithContext(ctx, &createTargetGroupInput).Return(tgCreateOutput, nil)
70-
mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes()
71-
tgManager := NewTargetGroupManager(mockCloud)
72-
resp, err := tgManager.Create(ctx, &tgCreateInput)
108+
mockCloud := mocks_aws.NewMockCloud(c)
109+
mockVpcLatticeSess.EXPECT().ListTargetGroupsAsList(ctx, gomock.Any()).Return(listTgOutput, nil)
110+
mockVpcLatticeSess.EXPECT().CreateTargetGroupWithContext(ctx, &createTargetGroupInput).Return(tgCreateOutput, nil)
111+
mockCloud.EXPECT().Lattice().Return(mockVpcLatticeSess).AnyTimes()
112+
tgManager := NewTargetGroupManager(mockCloud)
113+
resp, err := tgManager.Create(ctx, &tgCreateInput)
73114

74-
assert.Nil(t, err)
75-
assert.Equal(t, resp.TargetGroupARN, arn)
76-
assert.Equal(t, resp.TargetGroupID, id)
115+
assert.Nil(t, err)
116+
assert.Equal(t, resp.TargetGroupARN, arn)
117+
assert.Equal(t, resp.TargetGroupID, id)
118+
}
77119
}
78120

79121
// target group status is failed, and is active after creation

pkg/gateway/model_build_targetgroup.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,12 @@ func (t *targetGroupModelBuildTask) BuildTargetGroup(ctx context.Context) error
184184
Config: latticemodel.TargetGroupConfig{
185185
VpcID: config.VpcID,
186186
//Port: backendServicePort,
187-
IsServiceImport: false,
188-
Protocol: "HTTP",
189-
ProtocolVersion: vpclattice.TargetGroupProtocolVersionHttp1,
187+
IsServiceImport: false,
188+
IsServiceExport: true,
189+
K8SServiceName: t.serviceExport.Name,
190+
K8SServiceNamespace: t.serviceExport.Namespace,
191+
Protocol: "HTTP",
192+
ProtocolVersion: vpclattice.TargetGroupProtocolVersionHttp1,
190193
},
191194
}
192195

@@ -341,12 +344,17 @@ func (t *latticeServiceModelBuildTask) buildHTTPTargetGroupSpec(ctx context.Cont
341344
Name: tgName,
342345
Type: latticemodel.TargetGroupTypeIP,
343346
Config: latticemodel.TargetGroupConfig{
344-
VpcID: vpc,
345-
EKSClusterName: ekscluster,
346-
IsServiceImport: isServiceImport,
347-
Protocol: "HTTP",
348-
ProtocolVersion: vpclattice.TargetGroupProtocolVersionHttp1,
349-
Port: backendServicePort,
347+
VpcID: vpc,
348+
EKSClusterName: ekscluster,
349+
IsServiceImport: isServiceImport,
350+
IsServiceExport: false,
351+
K8SServiceName: string(httpBackendRef.Name),
352+
K8SServiceNamespace: namespace,
353+
K8SHTTPRouteName: t.httpRoute.Name,
354+
K8SHTTPRouteNamespace: t.httpRoute.Namespace,
355+
Protocol: "HTTP",
356+
ProtocolVersion: vpclattice.TargetGroupProtocolVersionHttp1,
357+
Port: backendServicePort,
350358
},
351359
IsDeleted: isDeleted,
352360
}, nil

pkg/model/lattice/targetgroup.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import (
44
"github.com/aws/aws-application-networking-k8s/pkg/model/core"
55
)
66

7+
const (
8+
K8SServiceNameKey = "K8SServiceName"
9+
K8SServiceNamespaceKey = "K8SServiceNamespace"
10+
K8SParentRefTypeKey = "K8SParentRefTypeKey"
11+
K8SHTTPRouteNameKey = "K8SHTTPRouteName"
12+
K8SHTTPRouteNamespaceKey = "K8SHTTPRouteNamespace"
13+
K8SServiceExportType = "K8SServiceExportType"
14+
K8SHTTPRouteType = "K8SHTTPRouteType"
15+
)
16+
717
type TargetGroup struct {
818
core.ResourceMeta `json:"-"`
919
Spec TargetGroupSpec `json:"spec"`
@@ -25,6 +35,12 @@ type TargetGroupConfig struct {
2535
VpcID string `json:"vpcid"`
2636
EKSClusterName string `json:"eksclustername"`
2737
IsServiceImport bool `json:"serviceimport"`
38+
// the following fields are used for AWS resource tagging
39+
IsServiceExport bool `json:"serviceexport"`
40+
K8SServiceName string `json:"k8sservice"`
41+
K8SServiceNamespace string `json:"k8sservicenamespace"`
42+
K8SHTTPRouteName string `json:"k8shttproutename"`
43+
K8SHTTPRouteNamespace string `json:"k8shttproutenamespace"`
2844
}
2945

3046
type TargetGroupStatus struct {

0 commit comments

Comments
 (0)