Skip to content

Commit 4487653

Browse files
committed
Fix UT
Signed-off-by: clarklee92 <[email protected]>
1 parent 3a01ce2 commit 4487653

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed

cloudprovider/amazonswebservices/nlb.go

+27-6
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (n *NlbPlugin) Alias() string {
133133
func (n *NlbPlugin) Init(c client.Client, options cloudprovider.CloudProviderOptions, ctx context.Context) error {
134134
n.mutex.Lock()
135135
defer n.mutex.Unlock()
136-
err := startWatchTargetGroup(ctx)
136+
err := startWatchTargetGroup()
137137
if err != nil {
138138
return err
139139
}
@@ -245,13 +245,29 @@ func (n *NlbPlugin) OnPodUpdated(c client.Client, pod *corev1.Pod, ctx context.C
245245

246246
// enable network
247247
if !networkManager.GetNetworkDisabled() {
248-
tgbList := &elbv2api.TargetGroupBindingList{}
249-
err = c.List(ctx, tgbList, client.MatchingLabels{ResourceTagKey: ResourceTagValue, SvcSelectorKey: pod.GetName()})
248+
selector := client.MatchingLabels{
249+
ResourceTagKey: ResourceTagValue,
250+
SvcSelectorKey: pod.GetName(),
251+
}
252+
var tgbList elbv2api.TargetGroupBindingList
253+
err = c.List(ctx, &tgbList, selector)
250254
if err != nil {
251255
return pod, cperrors.ToPluginError(err, cperrors.ApiCallError)
252256
}
253257
if len(tgbList.Items) != len(svc.Spec.Ports) {
254-
return pod, cperrors.ToPluginError(n.syncTargetGroupAndService(lbConfig, pod, c, ctx), cperrors.ApiCallError)
258+
var tgList ackv1alpha1.TargetGroupList
259+
err = c.List(ctx, &tgList, selector)
260+
if err != nil {
261+
return pod, cperrors.ToPluginError(err, cperrors.ApiCallError)
262+
}
263+
patch := client.RawPatch(types.MergePatchType,
264+
[]byte(fmt.Sprintf(`{"metadata":{"labels":{"%s":"false"}}}`, AWSTargetGroupSyncStatus)))
265+
for _, tg := range tgList.Items {
266+
err = c.Patch(ctx, &tg, patch)
267+
if err != nil {
268+
return pod, cperrors.ToPluginError(err, cperrors.ApiCallError)
269+
}
270+
}
255271
}
256272
}
257273

@@ -580,6 +596,8 @@ func watchTargetGroup(ctx context.Context) error {
580596
if err != nil {
581597
continue
582598
}
599+
log.Infof("targetGroup sync request watched, start to sync %s/%s, ARN: %s",
600+
targetGroup.GetNamespace(), targetGroup.GetName(), targetGroupARN)
583601
err = syncListenerAndTargetGroupBinding(ctx, cw, targetGroup, &targetGroupARN)
584602
if err != nil {
585603
log.Errorf("syncListenerAndTargetGroupBinding by targetGroup %s error %v",
@@ -600,10 +618,13 @@ func watchTargetGroup(ctx context.Context) error {
600618
return nil
601619
}
602620

603-
func startWatchTargetGroup(ctx context.Context) error {
621+
func startWatchTargetGroup() error {
604622
var err error
605623
startOnce.Do(func() {
606-
err = watchTargetGroup(ctx)
624+
err = watchTargetGroup(context.Background())
625+
if err == nil {
626+
log.Info("start to watch TargetGroups successfully")
627+
}
607628
})
608629
return err
609630
}

cloudprovider/amazonswebservices/nlb_test.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,16 @@ func TestParseLbConfig(t *testing.T) {
193193

194194
func TestInitLbCache(t *testing.T) {
195195
test := struct {
196+
n *NlbPlugin
196197
svcList []corev1.Service
197-
minPort int32
198-
maxPort int32
199198
cache map[string]portAllocated
200199
podAllocate map[string]*nlbPorts
201200
}{
202-
minPort: 951,
203-
maxPort: 1000,
201+
n: &NlbPlugin{
202+
minPort: 951,
203+
maxPort: 1000,
204+
},
205+
204206
cache: map[string]portAllocated{
205207
"arn:aws:elasticloadbalancing:us-east-1:888888888888:loadbalancer/net/aaa/3b332e6841f23870": map[int32]bool{
206208
988: true,
@@ -275,15 +277,15 @@ func TestInitLbCache(t *testing.T) {
275277
},
276278
}
277279

278-
actualCache, actualPodAllocate := initLbCache(test.svcList, test.minPort, test.maxPort)
280+
test.n.initLbCache(test.svcList)
279281
for arn, pa := range test.cache {
280282
for port, isAllocated := range pa {
281-
if actualCache[arn][port] != isAllocated {
282-
t.Errorf("nlb arn %s port %d isAllocated, expect: %t, actual: %t", arn, port, isAllocated, actualCache[arn][port])
283+
if test.n.cache[arn][port] != isAllocated {
284+
t.Errorf("nlb arn %s port %d isAllocated, expect: %t, actual: %t", arn, port, isAllocated, test.n.cache[arn][port])
283285
}
284286
}
285287
}
286-
if !reflect.DeepEqual(actualPodAllocate, test.podAllocate) {
287-
t.Errorf("podAllocate expect %v, but actully got %v", test.podAllocate, actualPodAllocate)
288+
if !reflect.DeepEqual(test.n.podAllocate, test.podAllocate) {
289+
t.Errorf("podAllocate expect %v, but actully got %v", test.podAllocate, test.n.podAllocate)
288290
}
289291
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.19
55
require (
66
github.com/BurntSushi/toml v1.2.1
77
github.com/aws-controllers-k8s/elbv2-controller v0.0.9
8-
github.com/aws-controllers-k8s/runtime v0.34.0
98
github.com/davecgh/go-spew v1.1.1
109
github.com/kr/pretty v0.3.1
1110
github.com/onsi/ginkgo v1.16.5
@@ -32,6 +31,7 @@ require (
3231
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
3332
github.com/Azure/go-autorest/logger v0.2.1 // indirect
3433
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
34+
github.com/aws-controllers-k8s/runtime v0.34.0 // indirect
3535
github.com/aws/aws-sdk-go v1.50.20 // indirect
3636
github.com/beorn7/perks v1.0.1 // indirect
3737
github.com/cespare/xxhash/v2 v2.2.0 // indirect

0 commit comments

Comments
 (0)