Skip to content

Commit 182c675

Browse files
committed
make sure important info is updated to status
Signed-off-by: roc <[email protected]>
1 parent 893ad9d commit 182c675

File tree

5 files changed

+31
-4
lines changed

5 files changed

+31
-4
lines changed

internal/clbbinding/clbbinding.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type CLBBinding interface {
1515
GetAssociatedObject(context.Context, client.Client) (Backend, error)
1616
GetObject() client.Object
1717
GetType() string
18+
GetNewest(context.Context, client.Client) (CLBBinding, error)
1819
}
1920

2021
type Backend interface {

internal/clbbinding/clbnodebinding.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ func (b *CLBNodeBinding) GetType() string {
4343
return "CLBNodeBinding"
4444
}
4545

46+
func (b *CLBNodeBinding) GetNewest(ctx context.Context, c client.Client) (CLBBinding, error) {
47+
nbd := &networkingv1alpha1.CLBNodeBinding{}
48+
err := c.Get(ctx, client.ObjectKeyFromObject(b), nbd)
49+
if err == nil {
50+
b.CLBNodeBinding = nbd
51+
}
52+
return WrapCLBNodeBinding(nbd), err
53+
}
54+
4655
type nodeBackend struct {
4756
*corev1.Node
4857
}

internal/clbbinding/clbpodbinding.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ func WrapCLBPodBinding(pb *networkingv1alpha1.CLBPodBinding) *CLBPodBinding {
2525

2626
type CLBPodBinding struct {
2727
*networkingv1alpha1.CLBPodBinding
28-
client.Client
2928
}
3029

3130
func (b *CLBPodBinding) GetSpec() *networkingv1alpha1.CLBBindingSpec {
@@ -44,6 +43,15 @@ func (b *CLBPodBinding) GetType() string {
4443
return "CLBPodBinding"
4544
}
4645

46+
func (b *CLBPodBinding) GetNewest(ctx context.Context, c client.Client) (CLBBinding, error) {
47+
pb := &networkingv1alpha1.CLBPodBinding{}
48+
err := c.Get(ctx, client.ObjectKeyFromObject(b), pb)
49+
if err == nil {
50+
b.CLBPodBinding = pb
51+
}
52+
return WrapCLBPodBinding(pb), err
53+
}
54+
4755
type podBackend struct {
4856
*corev1.Pod
4957
client.Client

internal/controller/clbbinding.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,15 @@ func (r *CLBBindingReconciler[T]) ensureListeners(ctx context.Context, bd clbbin
224224
}
225225
if needUpdate {
226226
status.PortBindings = newBindings
227-
if e := r.Status().Update(ctx, bd.GetObject()); e != nil {
227+
update := func() error {
228+
newBd, err := bd.GetNewest(ctx, r.Client)
229+
if err != nil {
230+
return err
231+
}
232+
newBd.GetStatus().PortBindings = newBindings
233+
return r.Status().Update(ctx, newBd.GetObject())
234+
}
235+
if e := util.RetryIfPossible(update); e != nil {
228236
err = multierr.Append(err, e)
229237
}
230238
}
@@ -419,6 +427,7 @@ func (r *CLBBindingReconciler[T]) ensureListener(ctx context.Context, bd clbbind
419427
log.FromContext(ctx).V(3).Info("create clb listener success", "port", binding.Port, "protocol", binding.Protocol, "listenerId", lisId, "lbPort", binding.LoadbalancerPort, "lbId", binding.LoadbalancerId)
420428
}
421429
}
430+
422431
if binding.ListenerId == "" { // 通常是还未创建监听器,不查询直接尝试创建,以提升扩容场景的速度
423432
createListener()
424433
return

internal/portpool/util/portpool.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ func (p *PortPool) TryCreateLB(ctx context.Context) (portpool.CreateLbResult, er
7979
addLbIdToStatus := func() error {
8080
pp := &networkingv1alpha1.CLBPortPool{}
8181
if err := p.Client.Get(ctx, client.ObjectKeyFromObject(p.CLBPortPool), pp); err != nil {
82-
return errors.WithStack(err)
82+
return err
8383
}
8484
pp.Status.State = networkingv1alpha1.CLBPortPoolStateActive // 创建成功,状态改为 Active,以便再次可分配端口
8585
pp.Status.LoadbalancerStatuses = append(pp.Status.LoadbalancerStatuses, networkingv1alpha1.LoadBalancerStatus{
8686
LoadbalancerID: lbId,
8787
AutoCreated: util.GetPtr(true),
8888
})
8989
if err := p.Client.Status().Update(ctx, pp); err != nil {
90-
return errors.WithStack(err)
90+
return err
9191
}
9292
p.CLBPortPool = pp
9393
return nil

0 commit comments

Comments
 (0)