Skip to content

Commit 76382e0

Browse files
authored
fix: correct NLB service prewarming when ReserveNlbNum=0 (#333)
* fix: correct NLB service prewarming when ReserveNlbNum=0 - Re-query NLB list after creating new NLBs in ensurePrewarming - Use NLBPoolIndexLabel instead of array index in prewarmServices - Add unit tests for reserve=0 prewarming scenarios - Add proposal document for the fix * test: add error path coverage for prewarmServices and ensurePrewarming
1 parent 35eec22 commit 76382e0

3 files changed

Lines changed: 625 additions & 6 deletions

File tree

cloudprovider/alibabacloud/auto_nlbs_v2.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,23 @@ func (a *AutoNLBsV2Plugin) ensurePrewarming(ctx context.Context, c client.Client
676676
}
677677
}
678678

679+
// 如果有新创建的 NLB,重新查询列表以包含新创建的 NLB
680+
if existingCount < expectNLBNum {
681+
nlbList = &nlbv1.NLBList{}
682+
err = c.List(ctx, nlbList, &client.ListOptions{
683+
Namespace: namespace,
684+
LabelSelector: labels.SelectorFromSet(map[string]string{
685+
NLBPoolGssLabel: gssName,
686+
NLBPoolEipIspTypeLabel: eipIspType,
687+
}),
688+
})
689+
if err != nil {
690+
log.Errorf("[%s] Failed to re-list NLB CRs after creation: %v", AutoNLBsV2Network, err)
691+
return err
692+
}
693+
log.Infof("[%s] Re-listed NLBs after creation: %d NLBs found", AutoNLBsV2Network, len(nlbList.Items))
694+
}
695+
679696
// 预热 Service(为每个 NLB 预创建所有可能的 Service)
680697
if err := a.prewarmServices(ctx, c, namespace, gssName, eipIspType, nlbList.Items, config, gss); err != nil {
681698
log.Errorf("[%s] Failed to prewarm services: %v", AutoNLBsV2Network, err)
@@ -725,22 +742,35 @@ func (a *AutoNLBsV2Plugin) prewarmServices(ctx context.Context, c client.Client,
725742
skippedCount := 0
726743
skippedNLBCount := 0
727744

728-
for nlbIdx, nlb := range nlbs {
745+
for _, nlb := range nlbs {
729746
// 如果 NLB 还没有 LoadBalancerId,跳过(等待 NLB Operator 创建完成)
730747
if nlb.Status.LoadBalancerId == "" {
731-
log.Infof("[%s] NLB %s (index=%d) not ready yet, skip prewarming services",
732-
AutoNLBsV2Network, nlb.Name, nlbIdx)
748+
log.Infof("[%s] NLB %s not ready yet, skip prewarming services",
749+
AutoNLBsV2Network, nlb.Name)
733750
skippedNLBCount++
734751
continue
735752
}
736753

737754
nlbId := nlb.Status.LoadBalancerId
738-
log.Infof("[%s] Prewarming services for NLB %s (nlbId=%s)", AutoNLBsV2Network, nlb.Name, nlbId)
755+
756+
// 从 Label 获取真实的 NLB pool index
757+
nlbPoolIndexStr, ok := nlb.Labels[NLBPoolIndexLabel]
758+
if !ok {
759+
log.Errorf("[%s] NLB %s missing %s label, skip prewarming", AutoNLBsV2Network, nlb.Name, NLBPoolIndexLabel)
760+
continue
761+
}
762+
nlbPoolIndex, err := strconv.Atoi(nlbPoolIndexStr)
763+
if err != nil {
764+
log.Errorf("[%s] NLB %s has invalid %s label: %s", AutoNLBsV2Network, nlb.Name, NLBPoolIndexLabel, nlbPoolIndexStr)
765+
continue
766+
}
767+
768+
log.Infof("[%s] Prewarming services for NLB %s (nlbId=%s, poolIndex=%d)", AutoNLBsV2Network, nlb.Name, nlbId, nlbPoolIndex)
739769

740770
// 为这个 NLB 预创建所有可能的 Service
741771
for podIdx := 0; podIdx < podsPerNLB; podIdx++ {
742-
// 计算全局 Service 索引(对应未来可能的 Pod 索引)
743-
globalServiceIndex := nlbIdx*podsPerNLB + podIdx
772+
// 使用真实的 pool index 计算全局 Service 索引
773+
globalServiceIndex := nlbPoolIndex*podsPerNLB + podIdx
744774

745775
// Service 命名:podName-eipIspType(如:gss-0-bgp, gss-0-bgp-pro)
746776
basePodName := gssName + "-" + strconv.Itoa(globalServiceIndex)

0 commit comments

Comments
 (0)