Skip to content

Commit 92d6577

Browse files
committed
修复:解决DedicatedCLBListener的TargetPod置为nil后导致pod finalizer泄露问题
1 parent 486cd2a commit 92d6577

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

internal/controller/dedicatedclblistener_controller.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,37 @@ func getDedicatedCLBListenerPodFinalizerName(lis *networkingv1alpha1.DedicatedCL
219219
return "dedicatedclblistener.networking.cloud.tencent.com/" + lis.Name
220220
}
221221

222+
const podNameAnnotation = "dedicatedclblistener.networking.cloud.tencent.com/pod-name"
223+
222224
func (r *DedicatedCLBListenerReconciler) ensureBackendPod(ctx context.Context, log logr.Logger, lis *networkingv1alpha1.DedicatedCLBListener) error {
223225
if lis.Status.ListenerId == "" {
224226
return nil
225227
}
226228
log = log.WithValues("listenerId", lis.Status.ListenerId)
227229
// 没配置后端 pod,确保后端rs全被解绑,并且状态为 Available
228230
targetPod := lis.Spec.TargetPod
231+
podFinalizerName := getDedicatedCLBListenerPodFinalizerName(lis)
232+
229233
if targetPod == nil {
230234
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+
}
231253
r.Recorder.Event(lis, corev1.EventTypeNormal, "PodChangeToNil", "no backend pod configured, try to deregister all targets")
232254
// 解绑所有后端
233255
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
246268
// 有配置后端 pod,对pod对账
247269
log = log.WithValues("pod", targetPod.PodName, "port", targetPod.TargetPort)
248270
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+
}
249282
pod := &corev1.Pod{}
250283
err := r.Get(
251284
ctx,
@@ -272,8 +305,6 @@ func (r *DedicatedCLBListenerReconciler) ensureBackendPod(ctx context.Context, l
272305
return err
273306
}
274307

275-
podFinalizerName := getDedicatedCLBListenerPodFinalizerName(lis)
276-
277308
if !pod.DeletionTimestamp.IsZero() { // pod 正在删除
278309
return r.syncPodDelete(ctx, log, lis, podFinalizerName, pod)
279310
}

pkg/kube/pod.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import (
88
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
99
)
1010

11+
// func RemovePodAnnotation(ctx context.Context, pod *corev1.Pod, name string) error {
12+
// return update(ctx, pod, func() {
13+
// if pod.Annotations == nil {
14+
// return
15+
// }
16+
// delete(pod.Annotations, name)
17+
// }, false, false)
18+
// }
1119
func SetPodAnnotation(ctx context.Context, pod *corev1.Pod, name, value string) error {
1220
return update(ctx, pod, func() {
1321
if pod.Annotations == nil {

0 commit comments

Comments
 (0)