diff --git a/pkg/controller/periodicity/periodicity_controller.go b/pkg/controller/periodicity/periodicity_controller.go index a42a29383e..2d3a8079a2 100644 --- a/pkg/controller/periodicity/periodicity_controller.go +++ b/pkg/controller/periodicity/periodicity_controller.go @@ -23,7 +23,6 @@ package periodicity import ( - "k8s.io/apimachinery/pkg/util/wait" "time" "github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1" @@ -31,8 +30,10 @@ import ( v1alpha1listers "github.com/pingcap/tidb-operator/pkg/client/listers/pingcap/v1alpha1" "github.com/pingcap/tidb-operator/pkg/controller" "github.com/pingcap/tidb-operator/pkg/label" + "github.com/pingcap/tidb-operator/pkg/util" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/util/errors" + "k8s.io/apimachinery/pkg/util/wait" kubeinformers "k8s.io/client-go/informers" "k8s.io/client-go/kubernetes" eventv1 "k8s.io/client-go/kubernetes/typed/core/v1" @@ -97,21 +98,18 @@ func (c *Controller) syncStatefulSetTimeStamp() error { for _, sts := range stsList { // If there is any error during our sts annotation updating, we just collect the error // and continue to next sts - if sts.Annotations == nil { - sts.Annotations = map[string]string{} - } - if sts.Labels == nil { - sts.Labels = map[string]string{} - } - tcName, ok := sts.Labels[label.InstanceLabelKey] + ok, tcRef := util.IsOwnedByTidbCluster(sts) if !ok { continue } - tc, err := c.tcLister.TidbClusters(sts.Namespace).Get(tcName) + tc, err := c.tcLister.TidbClusters(sts.Namespace).Get(tcRef.Name) if err != nil { errs = append(errs, err) continue } + if sts.Annotations == nil { + sts.Annotations = map[string]string{} + } sts.Annotations[label.AnnStsLastSyncTimestamp] = time.Now().Format(time.RFC3339) newSts, err := c.statefulSetControl.UpdateStatefulSet(tc, sts) if err != nil { diff --git a/pkg/upgrader/upgrader.go b/pkg/upgrader/upgrader.go index b59bec2ddb..89ea06e697 100644 --- a/pkg/upgrader/upgrader.go +++ b/pkg/upgrader/upgrader.go @@ -23,11 +23,11 @@ import ( "github.com/pingcap/tidb-operator/pkg/client/clientset/versioned" "github.com/pingcap/tidb-operator/pkg/features" "github.com/pingcap/tidb-operator/pkg/label" + "github.com/pingcap/tidb-operator/pkg/util" utildiscovery "github.com/pingcap/tidb-operator/pkg/util/discovery" appsv1 "k8s.io/api/apps/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/kubernetes" "k8s.io/klog" ) @@ -56,20 +56,6 @@ type upgrader struct { var _ Interface = &upgrader{} -// isOwnedByTidbCluster checks if the given object is owned by TidbCluster. -// Schema Kind and Group are checked, Version is ignored. -func isOwnedByTidbCluster(obj metav1.Object) (bool, *metav1.OwnerReference) { - ref := metav1.GetControllerOf(obj) - if ref == nil { - return false, nil - } - gv, err := schema.ParseGroupVersion(ref.APIVersion) - if err != nil { - return false, nil - } - return ref.Kind == v1alpha1.TiDBClusterKind && gv.Group == v1alpha1.SchemeGroupVersion.Group, ref -} - func (u *upgrader) Upgrade() error { if features.DefaultFeatureGate.Enabled(features.AdvancedStatefulSet) { klog.Infof("Upgrader: migrating Kubernetes StatefulSets to Advanced StatefulSets") @@ -80,7 +66,7 @@ func (u *upgrader) Upgrade() error { stsToMigrate := make([]appsv1.StatefulSet, 0) tidbClusters := make([]*v1alpha1.TidbCluster, 0) for _, sts := range stsList.Items { - if ok, tcRef := isOwnedByTidbCluster(&sts); ok { + if ok, tcRef := util.IsOwnedByTidbCluster(&sts); ok { stsToMigrate = append(stsToMigrate, sts) tc, err := u.cli.PingcapV1alpha1().TidbClusters(sts.Namespace).Get(tcRef.Name, metav1.GetOptions{}) if err != nil && !apierrors.IsNotFound(err) { @@ -130,7 +116,7 @@ func (u *upgrader) Upgrade() error { } stsToMigrate := make([]asappsv1.StatefulSet, 0) for _, sts := range stsList.Items { - if ok, _ := isOwnedByTidbCluster(&sts); ok { + if ok, _ := util.IsOwnedByTidbCluster(&sts); ok { stsToMigrate = append(stsToMigrate, sts) } } diff --git a/pkg/upgrader/upgrader_test.go b/pkg/upgrader/upgrader_test.go index 91b3625cb0..aedf4e4830 100644 --- a/pkg/upgrader/upgrader_test.go +++ b/pkg/upgrader/upgrader_test.go @@ -25,6 +25,7 @@ import ( versionedfake "github.com/pingcap/tidb-operator/pkg/client/clientset/versioned/fake" "github.com/pingcap/tidb-operator/pkg/features" "github.com/pingcap/tidb-operator/pkg/label" + "github.com/pingcap/tidb-operator/pkg/util" appsv1 "k8s.io/api/apps/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes/fake" @@ -86,7 +87,7 @@ func TestIsOwnedByTidbCluster(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - ok, _ := isOwnedByTidbCluster(&tt.sts) + ok, _ := util.IsOwnedByTidbCluster(&tt.sts) if tt.wantOK != ok { t.Errorf("got %v, want %v", ok, tt.wantOK) } diff --git a/pkg/util/util.go b/pkg/util/util.go index 9d45d44c75..f518be3978 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -25,6 +25,8 @@ import ( "github.com/pingcap/tidb-operator/pkg/label" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/sets" ) @@ -210,3 +212,17 @@ func AppendEnv(a []corev1.EnvVar, b []corev1.EnvVar) []corev1.EnvVar { } return a } + +// IsOwnedByTidbCluster checks if the given object is owned by TidbCluster. +// Schema Kind and Group are checked, Version is ignored. +func IsOwnedByTidbCluster(obj metav1.Object) (bool, *metav1.OwnerReference) { + ref := metav1.GetControllerOf(obj) + if ref == nil { + return false, nil + } + gv, err := schema.ParseGroupVersion(ref.APIVersion) + if err != nil { + return false, nil + } + return ref.Kind == v1alpha1.TiDBClusterKind && gv.Group == v1alpha1.SchemeGroupVersion.Group, ref +}