@@ -23,6 +23,7 @@ import (
23
23
"strconv"
24
24
"strings"
25
25
"sync"
26
+ "time"
26
27
27
28
ackv1alpha1 "github.com/aws-controllers-k8s/elbv2-controller/apis/v1alpha1"
28
29
"github.com/kr/pretty"
@@ -131,6 +132,7 @@ func (n *NlbPlugin) Alias() string {
131
132
func (n * NlbPlugin ) Init (c client.Client , options cloudprovider.CloudProviderOptions , ctx context.Context ) error {
132
133
n .mutex .Lock ()
133
134
defer n .mutex .Unlock ()
135
+ startWatchTargetGroup (ctx )
134
136
nlbOptions , ok := options .(provideroptions.AmazonsWebServicesOptions )
135
137
if ! ok {
136
138
return cperrors .ToPluginError (fmt .Errorf ("failed to convert options to nlbOptions" ), cperrors .InternalError )
@@ -562,7 +564,22 @@ func getACKTargetGroupARN(tg *ackv1alpha1.TargetGroup) (string, error) {
562
564
}
563
565
}
564
566
565
- func startWatchTargetGroup (ctx context.Context ) error {
567
+ func startWatchTargetGroup (ctx context.Context ) {
568
+ go func () {
569
+ for {
570
+ if err := watchTargetGroup (ctx ); err != nil {
571
+ log .Errorf ("error watching TargetGroup: %v" , err )
572
+ }
573
+ select {
574
+ case <- ctx .Done ():
575
+ return
576
+ case <- time .After (time .Second * 3 ): // Retry after a delay
577
+ }
578
+ }
579
+ }()
580
+ }
581
+
582
+ func watchTargetGroup (ctx context.Context ) error {
566
583
kubeConfig , err := config .GetConfig ()
567
584
if err != nil {
568
585
return fmt .Errorf ("failed to get Kubernetes config: %v" , err )
@@ -573,42 +590,44 @@ func startWatchTargetGroup(ctx context.Context) error {
573
590
}
574
591
utilruntime .Must (ackv1alpha1 .AddToScheme (cw .Scheme ()))
575
592
utilruntime .Must (elbv2api .AddToScheme (cw .Scheme ()))
576
- watcher , err := cw .Watch (ctx , & ackv1alpha1.TargetGroupList {})
593
+ watcher , err := cw .Watch (ctx , & ackv1alpha1.TargetGroupList {},
594
+ client.MatchingLabels {ResourceTagKey : ResourceTagValue })
577
595
if err != nil {
578
596
return fmt .Errorf ("failed to watch TargetGroup: %v" , err )
579
597
}
580
- go func () {
581
- for event := range watcher .ResultChan () {
582
- if event .Type == watch .Modified || event .Type == watch .Added {
583
- targetGroup , ok := event .Object .(* ackv1alpha1.TargetGroup )
584
- if ! ok {
585
- log .Warning ("Failed to convert event.Object to TargetGroup" )
598
+ defer watcher .Stop ()
599
+ log .Info ("Start to watch TargetGroups successfully" )
600
+ for event := range watcher .ResultChan () {
601
+ if event .Type == watch .Modified || event .Type == watch .Added {
602
+ targetGroup , ok := event .Object .(* ackv1alpha1.TargetGroup )
603
+ if ! ok {
604
+ log .Warning ("Failed to convert event.Object to TargetGroup" )
605
+ continue
606
+ }
607
+ if targetGroup .Labels [AWSTargetGroupSyncStatus ] == "false" {
608
+ targetGroupARN , err := getACKTargetGroupARN (targetGroup )
609
+ if err != nil {
586
610
continue
587
611
}
588
- if targetGroup .Labels [AWSTargetGroupSyncStatus ] == "false" {
589
- targetGroupARN , err := getACKTargetGroupARN (targetGroup )
590
- if err != nil {
591
- continue
592
- }
593
- log .Infof ("targetGroup sync request watched, start to sync %s/%s, ARN: %s" ,
594
- targetGroup .GetNamespace (), targetGroup .GetName (), targetGroupARN )
595
- err = syncListenerAndTargetGroupBinding (ctx , cw , targetGroup , & targetGroupARN )
596
- if err != nil {
597
- log .Errorf ("syncListenerAndTargetGroupBinding by targetGroup %s error %v" ,
598
- pretty .Sprint (targetGroup ), err )
599
- }
612
+ log .Infof ("targetGroup sync request watched, start to sync %s/%s, ARN: %s" ,
613
+ targetGroup .GetNamespace (), targetGroup .GetName (), targetGroupARN )
614
+ err = syncListenerAndTargetGroupBinding (ctx , cw , targetGroup , & targetGroupARN )
615
+ if err != nil {
616
+ log .Errorf ("syncListenerAndTargetGroupBinding by targetGroup %s error %v" ,
617
+ pretty .Sprint (targetGroup ), err )
618
+ }
600
619
601
- patch := client .RawPatch (types .MergePatchType ,
602
- []byte (fmt .Sprintf (`{"metadata":{"labels":{"%s":"true"}}}` , AWSTargetGroupSyncStatus )))
603
- err = cw .Patch (ctx , targetGroup , patch )
604
- if err != nil {
605
- log .Warningf ("patch targetGroup %s %s error %v" ,
606
- pretty .Sprint (targetGroup ), AWSTargetGroupSyncStatus , err )
607
- }
620
+ patch := client .RawPatch (types .MergePatchType ,
621
+ []byte (fmt .Sprintf (`{"metadata":{"labels":{"%s":"true"}}}` , AWSTargetGroupSyncStatus )))
622
+ err = cw .Patch (ctx , targetGroup , patch )
623
+ if err != nil {
624
+ log .Warningf ("patch targetGroup %s %s error %v" ,
625
+ pretty .Sprint (targetGroup ), AWSTargetGroupSyncStatus , err )
608
626
}
609
627
}
610
628
}
611
- }()
629
+ }
630
+ log .Info ("TargetGroups watcher channel closed, restarting watcher..." )
612
631
return nil
613
632
}
614
633
0 commit comments