Skip to content

Commit 53558b7

Browse files
Add capability to allow lattice target group name include current vpc and httproute (#230)
* Refactor cache for TG to key on TG-name and routename * Fix unit test due to using routename as part of key * Add mode to create tg name includes routename and vpcid * Make sure the cluster local gateway is associated to VPC * Populate routename to targets build structure * Add minor comment * Add route into structure * Cleanup logging msgs * Address CR comments * Address CR comments
1 parent 55c1053 commit 53558b7

27 files changed

+181
-84
lines changed

controllers/eventhandlers/gateway.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ func NewEnqueueRequestGatewayEvent(client client.Client) handler.EventHandler {
3333
var ZeroTransitionTime = metav1.NewTime(time.Time{})
3434

3535
func (h *enqueueRequestsForGatewayEvent) Create(e event.CreateEvent, queue workqueue.RateLimitingInterface) {
36-
glog.V(6).Info("Gateway Create")
3736
gwNew := e.Object.(*gateway_api.Gateway)
37+
glog.V(2).Infof("Gateway Create and Spec is %v", gwNew.Spec)
3838

3939
// initialize transition time
4040
gwNew.Status.Conditions[0].LastTransitionTime = ZeroTransitionTime
@@ -48,6 +48,8 @@ func (h *enqueueRequestsForGatewayEvent) Update(e event.UpdateEvent, queue workq
4848
gwNew := e.ObjectNew.(*gateway_api.Gateway)
4949

5050
if !equality.Semantic.DeepEqual(gwOld.Spec, gwNew.Spec) {
51+
glog.V(2).Infof("Gateway Update old spec %v to new spec %v",
52+
gwOld.Spec, gwNew.Spec)
5153
// initialize transition time
5254
gwNew.Status.Conditions[0].LastTransitionTime = ZeroTransitionTime
5355
h.enqueueImpactedHTTPRoute(queue, gwNew)
@@ -101,7 +103,7 @@ func (h *enqueueRequestsForGatewayEvent) enqueueImpactedHTTPRoute(queue workqueu
101103
}
102104

103105
if gwClass.Spec.ControllerName == config.LatticeGatewayControllerName {
104-
glog.V(6).Infof("Trigger HTTPRoute from Gateway event , httpRoute %s", httpRoute.Name)
106+
glog.V(2).Infof("Trigger HTTPRoute from Gateway event , httpRoute %s", httpRoute.Name)
105107
queue.Add(reconcile.Request{
106108
NamespacedName: types.NamespacedName{
107109
Namespace: httpRoute.Namespace,

controllers/gateway_controller.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func (r *GatewayReconciler) reconcileGatewayResources(ctx context.Context, gw *g
238238
glog.V(6).Infof("serviceNetworkStatus : %v for %s error %v \n", serviceNetworkStatus, gw.Name, err)
239239

240240
if err = r.updateGatewayStatus(ctx, &serviceNetworkStatus, gw); err != nil {
241-
glog.V(2).Infof("Failed to updateGatewayStatus %v err %v\n", gw, err)
241+
glog.V(2).Infof("Failed to updateGatewayStatus err %v, gw %v\n", err, gw)
242242
return errors.New("failed to update gateway status")
243243
}
244244
return nil
@@ -273,6 +273,7 @@ func (r *GatewayReconciler) updateGatewayStatus(ctx context.Context, serviceNetw
273273
//gw.Annotations["gateway.networking.k8s.io/aws-gateway-id"] = serviceNetworkStatus.ID
274274

275275
if err := r.Client.Status().Patch(ctx, gw, client.MergeFrom(gwOld)); err != nil {
276+
glog.V(2).Infof("Failed to update gateway status %v for gateway %v", err, gw)
276277
return errors.Wrapf(err, "failed to update gateway status")
277278
}
278279

@@ -299,6 +300,7 @@ func (r *GatewayReconciler) updateGatewayAcceptStatus(ctx context.Context, gw *g
299300
gw.Status.Conditions[0].Type = string(gateway_api.GatewayConditionAccepted)
300301

301302
if err := r.Client.Status().Patch(ctx, gw, client.MergeFrom(gwOld)); err != nil {
303+
glog.V(2).Infof("Failed to Patch acceptance status, err %v gw %v", err, gw)
302304
return errors.Wrapf(err, "failed to update gateway status")
303305
}
304306

@@ -480,7 +482,7 @@ func UpdateGWListenerStatus(ctx context.Context, k8sclient client.Client, gw *ga
480482
glog.V(6).Infof("After update, the snapshot of listener status %v", gw.Status.Listeners)
481483

482484
if err := k8sclient.Status().Patch(ctx, gw, client.MergeFrom(gwOld)); err != nil {
483-
glog.V(2).Infof("liwwu1-failed to update gateway listener status %v, status %v", err, gw.Status.Listeners)
485+
glog.V(2).Infof("Failed to update gateway listener err: %v, status: %v", err, gw.Status.Listeners)
484486
return errors.Wrapf(err, "failed to update gateway status")
485487
}
486488

controllers/service_controller.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,32 +116,32 @@ func (r *ServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
116116

117117
// TODO also need to check serviceexport object to trigger building TargetGroup
118118
tgName := latticestore.TargetGroupName(svc.Name, svc.Namespace)
119-
tg, err := ds.GetTargetGroup(tgName, false) // isServiceImport = false
119+
TGs := ds.GetTargetGroupsByTG(tgName) // isServiceImport = false
120120

121-
if err == nil && (tg.ByBackendRef || tg.ByServiceExport) {
122-
glog.V(6).Infof("ServiceReconcile: endpoints change trigger target IP list registration %v\n", tgName)
121+
for _, tg := range TGs {
123122

124-
r.reconcileTargetsResource(ctx, svc)
123+
glog.V(6).Infof("endpoints change trigger target IP list registration %v and tg %v\n",
124+
tgName, tg)
125+
126+
r.reconcileTargetsResource(ctx, svc, tg.TargetGroupKey.RouteName)
125127

126-
} else {
127-
glog.V(6).Infof("Ignore non-relevant svc %v]\n", svc)
128128
}
129129

130130
return ctrl.Result{}, nil
131131
}
132132

133-
func (r *ServiceReconciler) reconcileTargetsResource(ctx context.Context, svc *corev1.Service) {
133+
func (r *ServiceReconciler) reconcileTargetsResource(ctx context.Context, svc *corev1.Service, routename string) {
134134

135135
if err := r.finalizerManager.AddFinalizers(ctx, svc, serviceFinalizer); err != nil {
136136
r.eventRecorder.Event(svc, corev1.EventTypeWarning, k8s.ServiceEventReasonFailedAddFinalizer, fmt.Sprintf("Failed and finalizer due %v", err))
137137
}
138138

139-
r.buildAndDeployModel(ctx, svc)
139+
r.buildAndDeployModel(ctx, svc, routename)
140140
}
141141

142-
func (r *ServiceReconciler) buildAndDeployModel(ctx context.Context, svc *corev1.Service) (core.Stack, *latticemodel.Targets, error) {
142+
func (r *ServiceReconciler) buildAndDeployModel(ctx context.Context, svc *corev1.Service, routename string) (core.Stack, *latticemodel.Targets, error) {
143143
svcLog := log.FromContext(ctx)
144-
stack, latticeTargets, err := r.modelBuilder.Build(ctx, svc)
144+
stack, latticeTargets, err := r.modelBuilder.Build(ctx, svc, routename)
145145

146146
if err != nil {
147147
r.eventRecorder.Event(svc, corev1.EventTypeWarning,

pkg/config/controller_config.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var AccountID = "yyyyyy"
2222
var Region = "us-west-2"
2323
var logLevel = defaultLogLevel
2424
var DefaultServiceNetwork = NoDefaultServiceNetwork
25+
var UseLongTGName = false
2526

2627
func GetLogLevel() string {
2728
logLevel = os.Getenv("GATEWAY_API_CONTROLLER_LOGLEVEL")
@@ -70,6 +71,16 @@ func ConfigInit() {
7071
glog.V(2).Infoln("CLUSTER_LOCAL_GATEWAY", DefaultServiceNetwork)
7172
}
7273

74+
tgNameLengthMode := os.Getenv("TARGET_GROUP_NAME_LEN_MODE")
75+
76+
glog.V(2).Infoln("TARGET_GROUP_NAME_LEN_MODE", tgNameLengthMode)
77+
78+
if tgNameLengthMode == "long" {
79+
UseLongTGName = true
80+
} else {
81+
UseLongTGName = false
82+
}
83+
7384
sess, _ := session.NewSession()
7485
metadata := NewEC2Metadata(sess)
7586

pkg/deploy/lattice/rule_manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (r *defaultRuleManager) Create(ctx context.Context, rule *latticemodel.Rule
183183
for _, tgRule := range rule.Spec.Action.TargetGroups {
184184

185185
tgName := latticestore.TargetGroupName(tgRule.Name, tgRule.Namespace)
186-
tg, err := r.latticeDataStore.GetTargetGroup(tgName, tgRule.IsServiceImport)
186+
tg, err := r.latticeDataStore.GetTargetGroup(tgName, tgRule.RouteName, tgRule.IsServiceImport)
187187

188188
if err != nil {
189189
glog.V(2).Infof("Faild to create rule due to unknown tg %v, err %v\n", tgName, err)
@@ -487,7 +487,7 @@ func (r *defaultRuleManager) findMatchingRule(ctx context.Context, rule *lattice
487487
for _, k8sTG := range rule.Spec.Action.TargetGroups {
488488
// get k8sTG id
489489
tgName := latticestore.TargetGroupName(k8sTG.Name, k8sTG.Namespace)
490-
k8sTGinStore, err := r.latticeDataStore.GetTargetGroup(tgName, k8sTG.IsServiceImport)
490+
k8sTGinStore, err := r.latticeDataStore.GetTargetGroup(tgName, rule.Spec.ServiceName, k8sTG.IsServiceImport)
491491

492492
if err != nil {
493493
glog.V(6).Infof("Failed to find k8s tg %v in store \n", k8sTG)

pkg/deploy/lattice/rule_manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ func Test_CreateRule(t *testing.T) {
548548
if !tt.noTargetGroupID {
549549
for _, tg := range tt.newRule.Spec.Action.TargetGroups {
550550
tgName := latticestore.TargetGroupName(tg.Name, tg.Namespace)
551-
latticeDataStore.AddTargetGroup(tgName, "vpc", "arn", "tg-id", tg.IsServiceImport)
551+
latticeDataStore.AddTargetGroup(tgName, "vpc", "arn", "tg-id", tg.IsServiceImport, "")
552552
}
553553

554554
}

pkg/deploy/lattice/service_network_synthesizer_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ func Test_SynthesizeTriggeredGateways(t *testing.T) {
151151
func(ctx context.Context, retGWList *gateway_api.GatewayList, arg3 ...interface{}) error {
152152
// return empty gatway
153153
for _, gw := range gwList.Items {
154-
fmt.Printf("liwwu>>> test append %v\n", gw)
155154
retGWList.Items = append(retGWList.Items, gw)
156155
}
157156
return nil

pkg/deploy/lattice/target_group_manager.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
lattice_aws "github.com/aws/aws-application-networking-k8s/pkg/aws"
1212
"github.com/aws/aws-application-networking-k8s/pkg/config"
13+
"github.com/aws/aws-application-networking-k8s/pkg/latticestore"
1314
latticemodel "github.com/aws/aws-application-networking-k8s/pkg/model/lattice"
1415
)
1516

@@ -30,6 +31,18 @@ func NewTargetGroupManager(cloud lattice_aws.Cloud) *defaultTargetGroupManager {
3031
}
3132
}
3233

34+
func getLatticeTGName(targetGroup *latticemodel.TargetGroup) string {
35+
var tgName string
36+
if config.UseLongTGName {
37+
tgName = latticestore.TargetGroupLongName(targetGroup.Spec.Name,
38+
targetGroup.Spec.Config.K8SHTTPRouteName, config.VpcID)
39+
} else {
40+
tgName = targetGroup.Spec.Name
41+
}
42+
43+
return tgName
44+
}
45+
3346
// Create will try to create a target group
3447
// return error when:
3548
//
@@ -50,28 +63,31 @@ func NewTargetGroupManager(cloud lattice_aws.Cloud) *defaultTargetGroupManager {
5063
func (s *defaultTargetGroupManager) Create(ctx context.Context, targetGroup *latticemodel.TargetGroup) (latticemodel.TargetGroupStatus, error) {
5164

5265
glog.V(6).Infof("Create Target Group API call for name %s \n", targetGroup.Spec.Name)
66+
67+
latticeTGName := getLatticeTGName(targetGroup)
5368
// check if exists
54-
tgSummary, err := s.findTGByName(ctx, targetGroup.Spec.Name)
69+
tgSummary, err := s.findTGByName(ctx, latticeTGName)
5570
if err != nil {
5671
return latticemodel.TargetGroupStatus{TargetGroupARN: "", TargetGroupID: ""}, err
5772
}
5873
if tgSummary != nil {
5974
return latticemodel.TargetGroupStatus{TargetGroupARN: aws.StringValue(tgSummary.Arn), TargetGroupID: aws.StringValue(tgSummary.Id)}, err
6075
}
6176

62-
glog.V(6).Infof("create targetgropu API here %v\n", targetGroup)
77+
glog.V(6).Infof("create targetgroup API here %v\n", targetGroup)
6378
port := int64(targetGroup.Spec.Config.Port)
64-
config := &vpclattice.TargetGroupConfig{
79+
tgConfig := &vpclattice.TargetGroupConfig{
6580
Port: &port,
6681
Protocol: &targetGroup.Spec.Config.Protocol,
6782
ProtocolVersion: &targetGroup.Spec.Config.ProtocolVersion,
6883
VpcIdentifier: &targetGroup.Spec.Config.VpcID,
6984
}
7085

7186
targetGroupType := string(targetGroup.Spec.Type)
87+
7288
createTargetGroupInput := vpclattice.CreateTargetGroupInput{
73-
Config: config,
74-
Name: &targetGroup.Spec.Name,
89+
Config: tgConfig,
90+
Name: &latticeTGName,
7591
Type: &targetGroupType,
7692
Tags: make(map[string]*string),
7793
}
@@ -116,8 +132,9 @@ func (s *defaultTargetGroupManager) Create(ctx context.Context, targetGroup *lat
116132

117133
func (s *defaultTargetGroupManager) Get(ctx context.Context, targetGroup *latticemodel.TargetGroup) (latticemodel.TargetGroupStatus, error) {
118134
glog.V(6).Infof("Create Lattice Target Group API call for name %s \n", targetGroup.Spec.Name)
135+
119136
// check if exists
120-
tgSummary, err := s.findTGByName(ctx, targetGroup.Spec.Name)
137+
tgSummary, err := s.findTGByName(ctx, getLatticeTGName(targetGroup))
121138
if err != nil {
122139
return latticemodel.TargetGroupStatus{TargetGroupARN: "", TargetGroupID: ""}, err
123140
}

pkg/deploy/lattice/target_group_synthesizer.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,11 @@ func (t *targetGroupSynthesizer) SynthesizeTriggeredTargetGroup(ctx context.Cont
107107
continue
108108
}
109109

110+
// for serviceimport, the httproutename is ""
111+
110112
t.latticeDataStore.AddTargetGroup(resTargetGroup.Spec.Name,
111-
resTargetGroup.Spec.Config.VpcID, tgStatus.TargetGroupARN, tgStatus.TargetGroupID, resTargetGroup.Spec.Config.IsServiceImport)
113+
resTargetGroup.Spec.Config.VpcID, tgStatus.TargetGroupARN, tgStatus.TargetGroupID,
114+
resTargetGroup.Spec.Config.IsServiceImport, "")
112115

113116
glog.V(6).Infof("targetGroup Synthesized successfully for %s: %v\n", resTargetGroup.Spec.Name, tgStatus)
114117

@@ -121,7 +124,7 @@ func (t *targetGroupSynthesizer) SynthesizeTriggeredTargetGroup(ctx context.Cont
121124
continue
122125
} else {
123126
glog.V(6).Infof("Synthersizing Target Group: successfully deleted target group %v\n", resTargetGroup)
124-
t.latticeDataStore.DelTargetGroup(resTargetGroup.Spec.Name, false)
127+
t.latticeDataStore.DelTargetGroup(resTargetGroup.Spec.Name, resTargetGroup.Spec.Config.K8SHTTPRouteName, false)
125128
}
126129

127130
} else {
@@ -136,7 +139,9 @@ func (t *targetGroupSynthesizer) SynthesizeTriggeredTargetGroup(ctx context.Cont
136139
}
137140

138141
t.latticeDataStore.AddTargetGroup(resTargetGroup.Spec.Name,
139-
resTargetGroup.Spec.Config.VpcID, tgStatus.TargetGroupARN, tgStatus.TargetGroupID, resTargetGroup.Spec.Config.IsServiceImport)
142+
resTargetGroup.Spec.Config.VpcID, tgStatus.TargetGroupARN,
143+
tgStatus.TargetGroupID, resTargetGroup.Spec.Config.IsServiceImport,
144+
resTargetGroup.Spec.Config.K8SHTTPRouteName)
140145

141146
glog.V(6).Infof("targetGroup Synthesized successfully for %v: %v\n", resTargetGroup.Spec, tgStatus)
142147
}
@@ -167,6 +172,7 @@ func (t *targetGroupSynthesizer) SynthesizeSDKTargetGroups(ctx context.Context)
167172
glog.V(6).Infof("SynthesizeSDKTargetGroups: here is sdkTGs %v len %v \n", sdkTGs, len(sdkTGs))
168173

169174
for _, sdkTG := range sdkTGs {
175+
tgRouteName := ""
170176

171177
if *sdkTG.getTargetGroupOutput.Config.VpcIdentifier != config.VpcID {
172178
glog.V(6).Infof("Ignore target group ARN %v Name %v for other VPCs",
@@ -235,6 +241,7 @@ func (t *targetGroupSynthesizer) SynthesizeSDKTargetGroups(ctx context.Context)
235241
*sdkTG.getTargetGroupOutput.Arn, *sdkTG.getTargetGroupOutput.Name)
236242

237243
httpName, ok := tgTags.Tags[latticemodel.K8SHTTPRouteNameKey]
244+
tgRouteName = *httpName
238245

239246
if !ok || httpName == nil {
240247
glog.V(6).Infof("Ignore TargetGroup(triggered by httpRoute) %v, %v have no httproute name tag",
@@ -279,17 +286,21 @@ func (t *targetGroupSynthesizer) SynthesizeSDKTargetGroups(ctx context.Context)
279286

280287
}
281288

282-
if tg, err := t.latticeDataStore.GetTargetGroup(*sdkTG.getTargetGroupOutput.Name, true); err == nil {
289+
// the routename for serviceimport is ""
290+
if tg, err := t.latticeDataStore.GetTargetGroup(*sdkTG.getTargetGroupOutput.Name, "", true); err == nil {
283291
glog.V(6).Infof("Ignore target group created by service import %v\n", tg)
284292
continue
285293
}
286294

287-
glog.V(2).Infof("Append stale SDK TG to stale list Name %v, ARN %v",
288-
*sdkTG.getTargetGroupOutput.Name, *sdkTG.getTargetGroupOutput.Id)
295+
glog.V(2).Infof("Append stale SDK TG to stale list Name %v, routename %v, ARN %v",
296+
*sdkTG.getTargetGroupOutput.Name, tgRouteName, *sdkTG.getTargetGroupOutput.Id)
289297

290298
staleSDKTGs = append(staleSDKTGs, latticemodel.TargetGroup{
291299
Spec: latticemodel.TargetGroupSpec{
292-
Name: *sdkTG.getTargetGroupOutput.Name,
300+
Name: *sdkTG.getTargetGroupOutput.Name,
301+
Config: latticemodel.TargetGroupConfig{
302+
K8SHTTPRouteName: tgRouteName,
303+
},
293304
LatticeID: *sdkTG.getTargetGroupOutput.Id,
294305
},
295306
})

pkg/deploy/lattice/target_group_synthesizer_test.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func Test_SynthesizeTriggeredServiceExport(t *testing.T) {
152152

153153
assert.Nil(t, err)
154154
tgName := latticestore.TargetGroupName(tt.svcExport.Name, tt.svcExport.Namespace)
155-
dsTG, err := ds.GetTargetGroup(tgName, false)
155+
dsTG, err := ds.GetTargetGroup(tgName, "", false)
156156

157157
if tg.Spec.IsDeleted {
158158
assert.NotNil(t, err)
@@ -290,11 +290,11 @@ func Test_SynthersizeTriggeredByServiceImport(t *testing.T) {
290290
// check datastore
291291
for _, tgImport := range tt.svcImportList {
292292
if tgImport.mgrErr {
293-
_, err := ds.GetTargetGroup(tgImport.name, true)
293+
_, err := ds.GetTargetGroup(tgImport.name, "", true)
294294
assert.NotNil(t, err)
295295

296296
} else {
297-
tg, err := ds.GetTargetGroup(tgImport.name, true)
297+
tg, err := ds.GetTargetGroup(tgImport.name, "", true)
298298
assert.Nil(t, err)
299299
assert.Equal(t, tgImport.tgARN, tg.ARN)
300300
assert.Equal(t, tgImport.tgID, tg.ID)
@@ -513,6 +513,9 @@ func Test_SynthesizeSDKTargetGroups(t *testing.T) {
513513
LatticeID: sdkTG.id,
514514
},
515515
}
516+
if sdkTG.hasHTTPRouteTypeTag {
517+
tgSpec.Spec.Config.K8SHTTPRouteName = routename
518+
}
516519

517520
if sdkTG.HTTPRouteExist {
518521

@@ -731,11 +734,13 @@ func Test_SynthesizeTriggeredService(t *testing.T) {
731734
// check datastore
732735
for _, tg := range tt.svcList {
733736
if tg.mgrErr {
734-
_, err := ds.GetTargetGroup(tg.name, false)
737+
//TODO, test routename
738+
_, err := ds.GetTargetGroup(tg.name, "", false)
735739
assert.NotNil(t, err)
736740

737741
} else {
738-
dsTG, err := ds.GetTargetGroup(tg.name, false)
742+
//TODO, test routename
743+
dsTG, err := ds.GetTargetGroup(tg.name, "", false)
739744
assert.Nil(t, err)
740745
assert.Equal(t, tg.tgARN, dsTG.ARN)
741746
assert.Equal(t, tg.tgID, dsTG.ID)

0 commit comments

Comments
 (0)