From d314b251735693a000088d7011f9a5b2932f579f Mon Sep 17 00:00:00 2001 From: Aylei Date: Mon, 6 Jan 2020 14:03:03 +0800 Subject: [PATCH] Retain last applied pod template to keep backward-compatibility (#1489) Signed-off-by: Aylei --- pkg/manager/member/pd_member_manager.go | 22 ++------------- pkg/manager/member/pump_member_manager.go | 22 ++------------- pkg/manager/member/tidb_member_manager.go | 16 +---------- pkg/manager/member/tikv_member_manager.go | 16 +---------- pkg/manager/member/utils.go | 34 +++++++++++++++++++++++ 5 files changed, 40 insertions(+), 70 deletions(-) diff --git a/pkg/manager/member/pd_member_manager.go b/pkg/manager/member/pd_member_manager.go index 60019ad7e2b..90c5d71e12e 100644 --- a/pkg/manager/member/pd_member_manager.go +++ b/pkg/manager/member/pd_member_manager.go @@ -225,7 +225,7 @@ func (pmm *pdMemberManager) syncPDStatefulSetForTidbCluster(tc *v1alpha1.TidbClu if force { tc.Status.PD.Phase = v1alpha1.UpgradePhase setUpgradePartition(newPDSet, 0) - errSTS := pmm.updateStatefulSet(tc, newPDSet, oldPDSet) + errSTS := updateStatefulSet(pmm.setControl, tc, newPDSet, oldPDSet) return controller.RequeueErrorf("tidbcluster: [%s/%s]'s pd needs force upgrade, %v", ns, tcName, errSTS) } } @@ -250,7 +250,7 @@ func (pmm *pdMemberManager) syncPDStatefulSetForTidbCluster(tc *v1alpha1.TidbClu } } - return pmm.updateStatefulSet(tc, newPDSet, oldPDSet) + return updateStatefulSet(pmm.setControl, tc, newPDSet, oldPDSet) } func (pmm *pdMemberManager) syncPDClientCerts(tc *v1alpha1.TidbCluster) error { @@ -309,24 +309,6 @@ func (pmm *pdMemberManager) syncPDServerCerts(tc *v1alpha1.TidbCluster) error { return pmm.certControl.Create(controller.GetOwnerRef(tc), certOpts) } -func (pmm *pdMemberManager) updateStatefulSet(tc *v1alpha1.TidbCluster, newPDSet, oldPDSet *apps.StatefulSet) error { - if !statefulSetEqual(*newPDSet, *oldPDSet) { - set := *oldPDSet - set.Annotations = newPDSet.Annotations - set.Spec.Template = newPDSet.Spec.Template - *set.Spec.Replicas = *newPDSet.Spec.Replicas - set.Spec.UpdateStrategy = newPDSet.Spec.UpdateStrategy - err := SetStatefulSetLastAppliedConfigAnnotation(&set) - if err != nil { - return err - } - _, err = pmm.setControl.UpdateStatefulSet(tc, &set) - return err - } - - return nil -} - func (pmm *pdMemberManager) syncTidbClusterStatus(tc *v1alpha1.TidbCluster, set *apps.StatefulSet) error { ns := tc.GetNamespace() tcName := tc.GetName() diff --git a/pkg/manager/member/pump_member_manager.go b/pkg/manager/member/pump_member_manager.go index 593643bc8be..b03f0697532 100644 --- a/pkg/manager/member/pump_member_manager.go +++ b/pkg/manager/member/pump_member_manager.go @@ -106,30 +106,12 @@ func (pmm *pumpMemberManager) syncPumpStatefulSetForTidbCluster(tc *v1alpha1.Tid return pmm.setControl.CreateStatefulSet(tc, newPumpSet) } - isOrphan := metav1.GetControllerOf(oldPumpSet) == nil - - if !statefulSetEqual(*newPumpSet, *oldPumpSet) || isOrphan { - set := *oldPumpSet - set.Spec.Template = newPumpSet.Spec.Template - *set.Spec.Replicas = *newPumpSet.Spec.Replicas - set.Spec.UpdateStrategy = newPumpSet.Spec.UpdateStrategy - err := SetStatefulSetLastAppliedConfigAnnotation(&set) - if err != nil { - return err - } - if isOrphan { - set.OwnerReferences = newPumpSet.OwnerReferences - set.Labels = newPumpSet.Labels - } - _, err = pmm.setControl.UpdateStatefulSet(tc, &set) - return err - } - if err := pmm.syncTiDBClusterStatus(tc, oldPumpSet); err != nil { glog.Errorf("failed to sync TidbCluster: [%s/%s]'s status, error: %v", tc.Namespace, tc.Name, err) return err } - return nil + + return updateStatefulSet(pmm.setControl, tc, newPumpSet, oldPumpSet) } func (pmm *pumpMemberManager) syncTiDBClusterStatus(tc *v1alpha1.TidbCluster, set *apps.StatefulSet) error { diff --git a/pkg/manager/member/tidb_member_manager.go b/pkg/manager/member/tidb_member_manager.go index 78419f1c71c..d99995b25ef 100644 --- a/pkg/manager/member/tidb_member_manager.go +++ b/pkg/manager/member/tidb_member_manager.go @@ -222,21 +222,7 @@ func (tmm *tidbMemberManager) syncTiDBStatefulSetForTidbCluster(tc *v1alpha1.Tid } } - if !statefulSetEqual(*newTiDBSet, *oldTiDBSet) { - set := *oldTiDBSet - set.Annotations = newTiDBSet.Annotations - set.Spec.Template = newTiDBSet.Spec.Template - *set.Spec.Replicas = *newTiDBSet.Spec.Replicas - set.Spec.UpdateStrategy = newTiDBSet.Spec.UpdateStrategy - err := SetStatefulSetLastAppliedConfigAnnotation(&set) - if err != nil { - return err - } - _, err = tmm.setControl.UpdateStatefulSet(tc, &set) - return err - } - - return nil + return updateStatefulSet(tmm.setControl, tc, newTiDBSet, oldTiDBSet) } // syncTiDBClusterCerts creates the cert pair for TiDB if not exist, the cert diff --git a/pkg/manager/member/tikv_member_manager.go b/pkg/manager/member/tikv_member_manager.go index 2b1f653cc93..92fe48b98b4 100644 --- a/pkg/manager/member/tikv_member_manager.go +++ b/pkg/manager/member/tikv_member_manager.go @@ -225,21 +225,7 @@ func (tkmm *tikvMemberManager) syncStatefulSetForTidbCluster(tc *v1alpha1.TidbCl } } - if !statefulSetEqual(*newSet, *oldSet) { - set := *oldSet - set.Annotations = newSet.Annotations - set.Spec.Template = newSet.Spec.Template - *set.Spec.Replicas = *newSet.Spec.Replicas - set.Spec.UpdateStrategy = newSet.Spec.UpdateStrategy - err := SetStatefulSetLastAppliedConfigAnnotation(&set) - if err != nil { - return err - } - _, err = tkmm.setControl.UpdateStatefulSet(tc, &set) - return err - } - - return nil + return updateStatefulSet(tkmm.setControl, tc, newSet, oldSet) } func (tkmm *tikvMemberManager) syncTiKVServerCerts(tc *v1alpha1.TidbCluster) error { diff --git a/pkg/manager/member/utils.go b/pkg/manager/member/utils.go index 627039ca879..b68197167b2 100644 --- a/pkg/manager/member/utils.go +++ b/pkg/manager/member/utils.go @@ -27,6 +27,7 @@ import ( apps "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" apiequality "k8s.io/apimachinery/pkg/api/equality" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" glog "k8s.io/klog" ) @@ -262,3 +263,36 @@ func MapContainers(podSpec *corev1.PodSpec) map[string]corev1.Container { } return m } + +// updateStatefulSet is a template function to update the statefulset of components +func updateStatefulSet(setCtl controller.StatefulSetControlInterface, tc *v1alpha1.TidbCluster, newSet, oldSet *apps.StatefulSet) error { + isOrphan := metav1.GetControllerOf(oldSet) == nil + if !statefulSetEqual(*newSet, *oldSet) || isOrphan { + set := *oldSet + // Retain the deprecated last applied pod template annotation for backward compatibility + var podConfig string + var hasPodConfig bool + if oldSet.Spec.Template.Annotations != nil { + podConfig, hasPodConfig = oldSet.Spec.Template.Annotations[LastAppliedConfigAnnotation] + } + set.Spec.Template = newSet.Spec.Template + if hasPodConfig { + set.Spec.Template.Annotations[LastAppliedConfigAnnotation] = podConfig + } + set.Annotations = newSet.Annotations + *set.Spec.Replicas = *newSet.Spec.Replicas + set.Spec.UpdateStrategy = newSet.Spec.UpdateStrategy + if isOrphan { + set.OwnerReferences = newSet.OwnerReferences + set.Labels = newSet.Labels + } + err := SetStatefulSetLastAppliedConfigAnnotation(&set) + if err != nil { + return err + } + _, err = setCtl.UpdateStatefulSet(tc, &set) + return err + } + + return nil +}