Skip to content

Commit a7517f6

Browse files
committed
update clb binding status if pool or lb is deleted
Signed-off-by: roc <[email protected]>
1 parent 138d0f6 commit a7517f6

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

internal/controller/clbbinding.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ func (r *CLBBindingReconciler[T]) sync(ctx context.Context, bd T) (result ctrl.R
8282
}
8383

8484
func (r *CLBBindingReconciler[T]) ensureCLBBinding(ctx context.Context, bd clbbinding.CLBBinding) error {
85+
// 确保依赖的端口池和 CLB 都存在,如果已删除则释放端口并更新状态
86+
if err := r.ensurePoolAndCLB(ctx, bd); err != nil {
87+
return err
88+
}
8589
// 确保所有端口都被分配
8690
if err := r.ensurePortAllocated(ctx, bd); err != nil {
8791
return errors.WithStack(err)
@@ -97,6 +101,34 @@ func (r *CLBBindingReconciler[T]) ensureCLBBinding(ctx context.Context, bd clbbi
97101
return nil
98102
}
99103

104+
func (r *CLBBindingReconciler[T]) ensurePoolAndCLB(ctx context.Context, bd clbbinding.CLBBinding) error {
105+
status := bd.GetStatus()
106+
newBindings := []networkingv1alpha1.PortBindingStatus{}
107+
needUpdateStatus := false
108+
for i := range status.PortBindings {
109+
binding := &status.PortBindings[i]
110+
pool := portpool.Allocator.GetPool(binding.Pool)
111+
if pool == nil { // 端口池不存在,将端口绑定状态置为 Failed,并记录事件
112+
r.Recorder.Event(bd.GetObject(), corev1.EventTypeWarning, "PortPoolDeleted", fmt.Sprintf("port pool has been deleted (%s/%s/%d)", binding.Pool, binding.LoadbalancerId, binding.LoadbalancerPort))
113+
needUpdateStatus = true
114+
} else {
115+
if !pool.IsLbExists(binding.LoadbalancerId) {
116+
r.Recorder.Event(bd.GetObject(), corev1.EventTypeWarning, "CLBDeleted", fmt.Sprintf("clb has been deleted (%s/%s/%d)", binding.Pool, binding.LoadbalancerId, binding.LoadbalancerPort))
117+
needUpdateStatus = true
118+
} else {
119+
newBindings = append(newBindings, *binding)
120+
}
121+
}
122+
}
123+
if needUpdateStatus {
124+
status.PortBindings = newBindings
125+
if err := r.Status().Update(ctx, bd.GetObject()); err != nil {
126+
return errors.WithStack(err)
127+
}
128+
}
129+
return nil
130+
}
131+
100132
func (r *CLBBindingReconciler[T]) ensureListeners(ctx context.Context, bd clbbinding.CLBBinding) error {
101133
log.FromContext(ctx).V(10).Info("ensureListeners")
102134
status := bd.GetStatus()

internal/portpool/portpool.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,15 @@ func (pp *PortPool) AllocatePort(ctx context.Context, ports ...ProtocolPort) ([]
101101
return nil, nil
102102
}
103103

104+
// 释放已分配的端口
104105
func (pp *PortPool) ReleasePort(lbId string, port ProtocolPort) {
105106
pp.mu.Lock()
106107
defer pp.mu.Unlock()
107-
delete(pp.cache[lbId], port)
108+
cache := pp.cache[lbId]
109+
if cache == nil {
110+
return
111+
}
112+
delete(cache, port)
108113
}
109114

110115
func (pp *PortPool) EnsureLbIds(lbIds []string) {
@@ -132,3 +137,10 @@ func (pp *PortPool) EnsureLbIds(lbIds []string) {
132137
pp.cache[lbId] = make(map[ProtocolPort]struct{})
133138
}
134139
}
140+
141+
func (pp *PortPool) IsLbExists(lbId string) bool {
142+
pp.mu.Lock()
143+
_, exists := pp.cache[lbId]
144+
defer pp.mu.Unlock()
145+
return exists
146+
}

internal/portpool/util/portpool.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,14 @@ func NewPortPool(pp *networkingv1alpha1.CLBPortPool, c client.Client) *PortPool
5656
Client: c,
5757
}
5858
}
59+
60+
// func ProtocolPortFromBindingStatus(binding *networkingv1alpha1.PortBindingStatus) portpool.ProtocolPort {
61+
// pp := ProtocolPort{
62+
// Port: binding.Port,
63+
// Protocol: binding.Protocol,
64+
// }
65+
// if binding.LoadbalancerEndPort != nil {
66+
// pp.EndPort = *binding.LoadbalancerEndPort
67+
// }
68+
// return pp
69+
// }

0 commit comments

Comments
 (0)