@@ -6,8 +6,10 @@ import (
66 "github.com/golang/glog"
77 "strings"
88
9+ "k8s.io/apimachinery/pkg/types"
910 "sigs.k8s.io/controller-runtime/pkg/client"
1011 "sigs.k8s.io/gateway-api/apis/v1alpha2"
12+ mcs_api "sigs.k8s.io/mcs-api/pkg/apis/v1alpha1"
1113
1214 lattice_aws "github.com/aws/aws-application-networking-k8s/pkg/aws"
1315 "github.com/aws/aws-application-networking-k8s/pkg/config"
@@ -45,10 +47,10 @@ func (t *targetGroupSynthesizer) Synthesize(ctx context.Context) error {
4547
4648 /* TODO, resolve bug that this might delete other HTTPRoute's TG before they have chance
4749 * to be reconcile during controller restart
50+ */
4851 if err := t .SynthesizeSDKTargetGroups (ctx ); err != nil {
4952 ret = LATTICE_RETRY
5053 }
51- */
5254
5355 if ret != "" {
5456 return errors .New (ret )
@@ -158,48 +160,140 @@ func (t *targetGroupSynthesizer) SynthesizeSDKTargetGroups(ctx context.Context)
158160 sdkTGs , err := t .targetGroupManager .List (ctx )
159161
160162 if err != nil {
161- glog .V (6 ).Infof ("SynthesizeSDKTargetGroups: failed to retrieve sdk TGs %v\n " , err )
163+ glog .V (2 ).Infof ("SynthesizeSDKTargetGroups: failed to retrieve sdk TGs %v\n " , err )
162164 return nil
163165 }
164166
165167 glog .V (6 ).Infof ("SynthesizeSDKTargetGroups: here is sdkTGs %v len %v \n " , sdkTGs , len (sdkTGs ))
166168
167169 for _ , sdkTG := range sdkTGs {
168170
169- if * sdkTG .Config .VpcIdentifier != config .VpcID {
170- glog .V (6 ).Infof ("Ignore target group for other VPCs, other :%v, current vpc %v\n " , * sdkTG .Config .VpcIdentifier , config .VpcID )
171+ if * sdkTG .getTargetGroupOutput .Config .VpcIdentifier != config .VpcID {
172+ glog .V (6 ).Infof ("Ignore target group ARN %v Name %v for other VPCs" ,
173+ * sdkTG .getTargetGroupOutput .Arn , * sdkTG .getTargetGroupOutput .Name )
171174 continue
172175 }
173- if tg , err := t .latticeDataStore .GetTargetGroup (* sdkTG .Name , true ); err == nil {
174- glog .V (6 ).Infof ("Ignore target group created by service import %v\n " , tg )
176+
177+ // does target group have K8S tags, ignore if it is not tagged
178+ tgTags := sdkTG .targetGroupTags
179+
180+ if tgTags == nil || tgTags .Tags == nil {
181+ glog .V (6 ).Infof ("Ignore target group not tagged for K8S, %v, %v \n " ,
182+ * sdkTG .getTargetGroupOutput .Arn , * sdkTG .getTargetGroupOutput .Name )
175183 continue
176184 }
177185
178- tg , err := t .latticeDataStore .GetTargetGroup (* sdkTG .Name , false )
179- if err != nil {
180- staleSDKTGs = append (staleSDKTGs , latticemodel.TargetGroup {
181- Spec : latticemodel.TargetGroupSpec {
182- Name : * sdkTG .Name ,
183- LatticeID : * sdkTG .Id ,
184- },
185- })
186- } else {
187- if tg .ByServiceExport {
186+ parentRef , ok := tgTags .Tags [latticemodel .K8SParentRefTypeKey ]
187+ if ! ok || parentRef == nil {
188+ glog .V (6 ).Infof ("Ignore target group that have no K8S parentRef tag :%v, %v \n " ,
189+ * sdkTG .getTargetGroupOutput .Arn , * sdkTG .getTargetGroupOutput .Name )
190+ continue
191+ }
192+
193+ srvName , ok := tgTags .Tags [latticemodel .K8SServiceNameKey ]
194+
195+ if ! ok || srvName == nil {
196+ glog .V (6 ).Infof ("Ignore TargetGroup have no servicename tag: %v, %v" ,
197+ * sdkTG .getTargetGroupOutput .Arn , * sdkTG .getTargetGroupOutput .Name )
198+ continue
199+ }
200+
201+ srvNamespace , ok := tgTags .Tags [latticemodel .K8SServiceNamespaceKey ]
202+
203+ if ! ok || srvNamespace == nil {
204+ glog .V (6 ).Infof ("Ignore TargetGroup have no servicenamespace tag: %v, %v" ,
205+ * sdkTG .getTargetGroupOutput .Arn , * sdkTG .getTargetGroupOutput .Name )
206+ continue
207+ }
208+
209+ // if its parentref is service export, check the parent service export exist
210+ // Ignore if service export exists
211+ if * parentRef == latticemodel .K8SServiceExportType {
212+ glog .V (6 ).Infof ("TargetGroup %v, %v is referenced by ServiceExport" ,
213+ * sdkTG .getTargetGroupOutput .Arn , * sdkTG .getTargetGroupOutput .Name )
214+
215+ glog .V (6 ).Infof ("Determine serviceexport name=%v, namespace=%v exists for targetGroup %v" ,
216+ * srvName , * srvNamespace , * sdkTG .getTargetGroupOutput .Arn )
217+
218+ srvExportName := types.NamespacedName {
219+ Namespace : * srvNamespace ,
220+ Name : * srvName ,
221+ }
222+ srvExport := & mcs_api.ServiceExport {}
223+ if err := t .client .Get (ctx , srvExportName , srvExport ); err == nil {
224+
225+ glog .V (6 ).Infof ("Ignore TargetGroup(triggered by serviceexport) %v, %v since serviceexport object is found" ,
226+ * sdkTG .getTargetGroupOutput .Arn , * sdkTG .getTargetGroupOutput .Name )
188227 continue
189228 }
229+ }
230+
231+ // if its parentref is HTTP/route, check the parent HTTPRoute exist
232+ // Ignore if httpRoute does NOT exist
233+ if * parentRef == latticemodel .K8SHTTPRouteType {
234+ glog .V (6 ).Infof ("TargetGroup %v, %v is referenced by HTTPRoute" ,
235+ * sdkTG .getTargetGroupOutput .Arn , * sdkTG .getTargetGroupOutput .Name )
190236
191- if t .isTargetGroupUsedByHTTPRoute (ctx , * sdkTG .Name ) {
237+ httpName , ok := tgTags .Tags [latticemodel .K8SHTTPRouteNameKey ]
238+
239+ if ! ok || httpName == nil {
240+ glog .V (6 ).Infof ("Ignore TargetGroup(triggered by httpRoute) %v, %v have no httproute name tag" ,
241+ * sdkTG .getTargetGroupOutput .Arn , * sdkTG .getTargetGroupOutput .Name )
192242 continue
193243 }
194- staleSDKTGs = append (staleSDKTGs , latticemodel.TargetGroup {
195- Spec : latticemodel.TargetGroupSpec {
196- Name : * sdkTG .Name ,
197- LatticeID : * sdkTG .Id ,
198- },
199- })
200244
245+ httpNamespace , ok := tgTags .Tags [latticemodel .K8SHTTPRouteNamespaceKey ]
246+
247+ if ! ok || httpNamespace == nil {
248+ glog .V (6 ).Infof ("Ignore TargetGroup(triggered by httpRoute) %v, %v have no httproute namespace tag" ,
249+ * sdkTG .getTargetGroupOutput .Arn , * sdkTG .getTargetGroupOutput .Name )
250+ continue
251+ }
252+
253+ httprouteName := types.NamespacedName {
254+ Namespace : * httpNamespace ,
255+ Name : * httpName ,
256+ }
257+
258+ httpRoute := & v1alpha2.HTTPRoute {}
259+
260+ tgName := latticestore .TargetGroupName (* srvName , * srvNamespace )
261+
262+ if err := t .client .Get (ctx , httprouteName , httpRoute ); err != nil {
263+ glog .V (6 ).Infof ("tgname %v is not used by httproute %v\n " , tgName , httpRoute )
264+
265+ } else {
266+
267+ isUsed := t .isTargetGroupUsedByaHTTPRoute (ctx , tgName , httpRoute )
268+
269+ if isUsed {
270+
271+ glog .V (6 ).Infof ("Ignore TargetGroup(triggered by HTTProute) %v, %v since httproute object is found" ,
272+ * sdkTG .getTargetGroupOutput .Arn , * sdkTG .getTargetGroupOutput .Name )
273+
274+ continue
275+ } else {
276+ glog .V (6 ).Infof ("tgname %v is not used by httproute %v\n " , tgName , httpRoute )
277+ }
278+ }
279+
280+ }
281+
282+ if tg , err := t .latticeDataStore .GetTargetGroup (* sdkTG .getTargetGroupOutput .Name , true ); err == nil {
283+ glog .V (6 ).Infof ("Ignore target group created by service import %v\n " , tg )
284+ continue
201285 }
202286
287+ glog .V (2 ).Infof ("Append stale SDK TG to stale list Name %v, ARN %v" ,
288+ * sdkTG .getTargetGroupOutput .Name , * sdkTG .getTargetGroupOutput .Id )
289+
290+ staleSDKTGs = append (staleSDKTGs , latticemodel.TargetGroup {
291+ Spec : latticemodel.TargetGroupSpec {
292+ Name : * sdkTG .getTargetGroupOutput .Name ,
293+ LatticeID : * sdkTG .getTargetGroupOutput .Id ,
294+ },
295+ })
296+
203297 }
204298
205299 glog .V (6 ).Infof ("SynthesizeSDKTargetGroups, here is the stale target groups list %v stalelen %d\n " , staleSDKTGs , len (staleSDKTGs ))
@@ -211,8 +305,7 @@ func (t *targetGroupSynthesizer) SynthesizeSDKTargetGroups(ctx context.Context)
211305 err := t .targetGroupManager .Delete (ctx , & sdkTG )
212306 glog .V (6 ).Infof ("SynthesizeSDKTargetGroups, deleting stale target group %v \n " , err )
213307
214- // TODO find out the error code
215- if err != nil && ! strings .Contains (err .Error (), "ConflictException" ) {
308+ if err != nil && ! strings .Contains (err .Error (), "TargetGroup is referenced in routing configuration, listeners or rules of service." ) {
216309 ret_err = true
217310 }
218311 // continue on even when there is an err
@@ -227,35 +320,24 @@ func (t *targetGroupSynthesizer) SynthesizeSDKTargetGroups(ctx context.Context)
227320
228321}
229322
230- // TODO put following routine to common library
231- func (t * targetGroupSynthesizer ) isTargetGroupUsedByHTTPRoute (ctx context.Context , tgName string ) bool {
232-
233- httpRouteList := & v1alpha2.HTTPRouteList {}
234-
235- t .client .List (ctx , httpRouteList )
236-
237- //glog.V(6).Infof("isTargetGroupUsedByHTTPRoute: tgName %v-- %v\n", tgName, httpRouteList)
238-
239- for _ , httpRoute := range httpRouteList .Items {
240- for _ , httpRule := range httpRoute .Spec .Rules {
241- for _ , httpBackendRef := range httpRule .BackendRefs {
242- //glog.V(6).Infof("isTargetGroupUsedByHTTPRoute: httpBackendRef: %v \n", httpBackendRef)
243- if string (* httpBackendRef .BackendObjectReference .Kind ) != "Service" {
244- continue
245- }
246- namespace := "default"
323+ func (t * targetGroupSynthesizer ) isTargetGroupUsedByaHTTPRoute (ctx context.Context , tgName string , httpRoute * v1alpha2.HTTPRoute ) bool {
247324
248- if httpBackendRef .BackendObjectReference .Namespace != nil {
249- namespace = string (* httpBackendRef .BackendObjectReference .Namespace )
250- }
251- refTGName := latticestore .TargetGroupName (string (httpBackendRef .BackendObjectReference .Name ), namespace )
252- //glog.V(6).Infof("refTGName: %s\n", refTGName)
325+ for _ , httpRule := range httpRoute .Spec .Rules {
326+ for _ , httpBackendRef := range httpRule .BackendRefs {
327+ if string (* httpBackendRef .BackendObjectReference .Kind ) != "Service" {
328+ continue
329+ }
330+ namespace := "default"
253331
254- if tgName == refTGName {
255- return true
256- }
332+ if httpBackendRef .BackendObjectReference .Namespace != nil {
333+ namespace = string (* httpBackendRef .BackendObjectReference .Namespace )
334+ }
335+ refTGName := latticestore .TargetGroupName (string (httpBackendRef .BackendObjectReference .Name ), namespace )
257336
337+ if tgName == refTGName {
338+ return true
258339 }
340+
259341 }
260342 }
261343
0 commit comments