Skip to content

Commit

Permalink
Retain last applied pod template to keep backward-compatibility (#1489)
Browse files Browse the repository at this point in the history
Signed-off-by: Aylei <rayingecho@gmail.com>
  • Loading branch information
aylei authored Jan 6, 2020
1 parent fd4d5ee commit d314b25
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 70 deletions.
22 changes: 2 additions & 20 deletions pkg/manager/member/pd_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
22 changes: 2 additions & 20 deletions pkg/manager/member/pump_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 1 addition & 15 deletions pkg/manager/member/tidb_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 1 addition & 15 deletions pkg/manager/member/tikv_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions pkg/manager/member/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}

0 comments on commit d314b25

Please sign in to comment.