@@ -219,15 +219,37 @@ func getDedicatedCLBListenerPodFinalizerName(lis *networkingv1alpha1.DedicatedCL
219
219
return "dedicatedclblistener.networking.cloud.tencent.com/" + lis .Name
220
220
}
221
221
222
+ const podNameAnnotation = "dedicatedclblistener.networking.cloud.tencent.com/pod-name"
223
+
222
224
func (r * DedicatedCLBListenerReconciler ) ensureBackendPod (ctx context.Context , log logr.Logger , lis * networkingv1alpha1.DedicatedCLBListener ) error {
223
225
if lis .Status .ListenerId == "" {
224
226
return nil
225
227
}
226
228
log = log .WithValues ("listenerId" , lis .Status .ListenerId )
227
229
// 没配置后端 pod,确保后端rs全被解绑,并且状态为 Available
228
230
targetPod := lis .Spec .TargetPod
231
+ podFinalizerName := getDedicatedCLBListenerPodFinalizerName (lis )
232
+
229
233
if targetPod == nil {
230
234
if lis .Status .State == networkingv1alpha1 .DedicatedCLBListenerStateBound { // 但监听器状态是已占用,需要解绑
235
+ if podName := lis .GetAnnotations ()[podNameAnnotation ]; podName != "" {
236
+ pod := & corev1.Pod {}
237
+ err := r .Get (
238
+ ctx ,
239
+ client.ObjectKey {
240
+ Namespace : lis .Namespace ,
241
+ Name : podName ,
242
+ },
243
+ pod ,
244
+ )
245
+ if err == nil {
246
+ if err = kube .RemovePodFinalizer (ctx , pod , podFinalizerName ); err != nil {
247
+ return err
248
+ }
249
+ } else {
250
+ r .Recorder .Event (lis , corev1 .EventTypeWarning , "Deregister" , fmt .Sprintf ("faild to find pod before deregister: %s" , err .Error ()))
251
+ }
252
+ }
231
253
r .Recorder .Event (lis , corev1 .EventTypeNormal , "PodChangeToNil" , "no backend pod configured, try to deregister all targets" )
232
254
// 解绑所有后端
233
255
if err := clb .DeregisterAllTargets (ctx , lis .Spec .LbRegion , lis .Spec .LbId , lis .Status .ListenerId ); err != nil {
@@ -246,6 +268,17 @@ func (r *DedicatedCLBListenerReconciler) ensureBackendPod(ctx context.Context, l
246
268
// 有配置后端 pod,对pod对账
247
269
log = log .WithValues ("pod" , targetPod .PodName , "port" , targetPod .TargetPort )
248
270
log .V (6 ).Info ("ensure backend pod" )
271
+ // 确保记录当前 target pod 到注解以便 targetPod 置为 nil 后(不删除listener)可找到pod以删除pod finalizer
272
+ if lis .Annotations == nil {
273
+ lis .Annotations = make (map [string ]string )
274
+ }
275
+ if lis .Annotations [podNameAnnotation ] != targetPod .PodName {
276
+ log .V (6 ).Info ("set pod name annotation" )
277
+ lis .Annotations [podNameAnnotation ] = targetPod .PodName
278
+ if err := r .Update (ctx , lis ); err != nil {
279
+ return err
280
+ }
281
+ }
249
282
pod := & corev1.Pod {}
250
283
err := r .Get (
251
284
ctx ,
@@ -272,8 +305,6 @@ func (r *DedicatedCLBListenerReconciler) ensureBackendPod(ctx context.Context, l
272
305
return err
273
306
}
274
307
275
- podFinalizerName := getDedicatedCLBListenerPodFinalizerName (lis )
276
-
277
308
if ! pod .DeletionTimestamp .IsZero () { // pod 正在删除
278
309
return r .syncPodDelete (ctx , log , lis , podFinalizerName , pod )
279
310
}
0 commit comments