Skip to content

Commit 66e0570

Browse files
committed
Optimize AWS TargetGroup watch logic
Signed-off-by: clarklee92 <[email protected]>
1 parent 67ef94e commit 66e0570

File tree

2 files changed

+47
-35
lines changed

2 files changed

+47
-35
lines changed

cloudprovider/amazonswebservices/amazonswebservices.go

-7
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package amazonswebservices
1818

1919
import (
20-
"context"
21-
2220
log "k8s.io/klog/v2"
2321

2422
"github.com/openkruise/kruise-game/cloudprovider"
@@ -60,10 +58,5 @@ func (ap *Provider) registerPlugin(plugin cloudprovider.Plugin) {
6058
}
6159

6260
func NewAmazonsWebServicesProvider() (cloudprovider.CloudProvider, error) {
63-
err := startWatchTargetGroup(context.Background())
64-
if err != nil {
65-
return nil, err
66-
}
67-
log.Info("start to watch TargetGroups successfully")
6861
return amazonsWebServicesProvider, nil
6962
}

cloudprovider/amazonswebservices/nlb.go

+47-28
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strconv"
2424
"strings"
2525
"sync"
26+
"time"
2627

2728
ackv1alpha1 "github.com/aws-controllers-k8s/elbv2-controller/apis/v1alpha1"
2829
"github.com/kr/pretty"
@@ -131,6 +132,7 @@ func (n *NlbPlugin) Alias() string {
131132
func (n *NlbPlugin) Init(c client.Client, options cloudprovider.CloudProviderOptions, ctx context.Context) error {
132133
n.mutex.Lock()
133134
defer n.mutex.Unlock()
135+
startWatchTargetGroup(ctx)
134136
nlbOptions, ok := options.(provideroptions.AmazonsWebServicesOptions)
135137
if !ok {
136138
return cperrors.ToPluginError(fmt.Errorf("failed to convert options to nlbOptions"), cperrors.InternalError)
@@ -562,7 +564,22 @@ func getACKTargetGroupARN(tg *ackv1alpha1.TargetGroup) (string, error) {
562564
}
563565
}
564566

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 {
566583
kubeConfig, err := config.GetConfig()
567584
if err != nil {
568585
return fmt.Errorf("failed to get Kubernetes config: %v", err)
@@ -573,42 +590,44 @@ func startWatchTargetGroup(ctx context.Context) error {
573590
}
574591
utilruntime.Must(ackv1alpha1.AddToScheme(cw.Scheme()))
575592
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})
577595
if err != nil {
578596
return fmt.Errorf("failed to watch TargetGroup: %v", err)
579597
}
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 {
586610
continue
587611
}
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+
}
600619

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)
608626
}
609627
}
610628
}
611-
}()
629+
}
630+
log.Info("TargetGroups watcher channel closed, restarting watcher...")
612631
return nil
613632
}
614633

0 commit comments

Comments
 (0)