From f26a0876ebd5b5b6b0d7259120a1460cbbc56c98 Mon Sep 17 00:00:00 2001 From: xueshengjie Date: Fri, 25 Feb 2022 11:23:09 +0800 Subject: [PATCH] optimize dedupCurHistories func in ReconcileDaemonSet Signed-off-by: LastNight1997 <934104625@qq.com> --- pkg/controller/daemonset/daemonset_history.go | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/pkg/controller/daemonset/daemonset_history.go b/pkg/controller/daemonset/daemonset_history.go index 78122d66ec..b1606e2f9a 100644 --- a/pkg/controller/daemonset/daemonset_history.go +++ b/pkg/controller/daemonset/daemonset_history.go @@ -242,29 +242,29 @@ func (dsc *ReconcileDaemonSet) dedupCurHistories(ds *appsv1alpha1.DaemonSet, cur maxRevision = cur.Revision } } + // Relabel pods before dedup + pods, err := dsc.getDaemonPods(ds) + if err != nil { + return nil, err + } + for _, pod := range pods { + if pod.Labels[apps.DefaultDaemonSetUniqueLabelKey] != keepCur.Labels[apps.DefaultDaemonSetUniqueLabelKey] { + toUpdate := pod.DeepCopy() + if toUpdate.Labels == nil { + toUpdate.Labels = make(map[string]string) + } + toUpdate.Labels[apps.DefaultDaemonSetUniqueLabelKey] = keepCur.Labels[apps.DefaultDaemonSetUniqueLabelKey] + _, err = dsc.kubeClient.CoreV1().Pods(ds.Namespace).Update(context.TODO(), toUpdate, metav1.UpdateOptions{}) + if err != nil { + return nil, err + } + } + } // Clean up duplicates and relabel pods for _, cur := range curHistories { if cur.Name == keepCur.Name { continue } - // Relabel pods before dedup - pods, err := dsc.getDaemonPods(ds) - if err != nil { - return nil, err - } - for _, pod := range pods { - if pod.Labels[apps.DefaultDaemonSetUniqueLabelKey] != keepCur.Labels[apps.DefaultDaemonSetUniqueLabelKey] { - toUpdate := pod.DeepCopy() - if toUpdate.Labels == nil { - toUpdate.Labels = make(map[string]string) - } - toUpdate.Labels[apps.DefaultDaemonSetUniqueLabelKey] = keepCur.Labels[apps.DefaultDaemonSetUniqueLabelKey] - _, err = dsc.kubeClient.CoreV1().Pods(ds.Namespace).Update(context.TODO(), toUpdate, metav1.UpdateOptions{}) - if err != nil { - return nil, err - } - } - } // Remove duplicates err = dsc.kubeClient.AppsV1().ControllerRevisions(ds.Namespace).Delete(context.TODO(), cur.Name, metav1.DeleteOptions{}) if err != nil {