diff --git a/pkg/apis/pingcap.com/v1alpha1/tidbcluster.go b/pkg/apis/pingcap.com/v1alpha1/tidbcluster.go index dc15cf2f05..c336b05806 100644 --- a/pkg/apis/pingcap.com/v1alpha1/tidbcluster.go +++ b/pkg/apis/pingcap.com/v1alpha1/tidbcluster.go @@ -150,3 +150,7 @@ func (tc *TidbCluster) TiKVIsAvailable() bool { return true } + +func (tc *TidbCluster) GetClusterID() string { + return tc.Status.ClusterID +} diff --git a/pkg/apis/pingcap.com/v1alpha1/types.go b/pkg/apis/pingcap.com/v1alpha1/types.go index 201733374a..78cc5de6c7 100644 --- a/pkg/apis/pingcap.com/v1alpha1/types.go +++ b/pkg/apis/pingcap.com/v1alpha1/types.go @@ -98,9 +98,10 @@ type TidbClusterSpec struct { // TidbClusterStatus represents the current status of a tidb cluster. type TidbClusterStatus struct { - PD PDStatus `json:"pd,omitempty"` - TiKV TiKVStatus `json:"tikv,omitempty"` - TiDB TiDBStatus `json:"tidb,omitempty"` + ClusterID string `json:"clusterID,omitempty"` + PD PDStatus `json:"pd,omitempty"` + TiKV TiKVStatus `json:"tikv,omitempty"` + TiDB TiDBStatus `json:"tidb,omitempty"` } // PDSpec contains details of PD member diff --git a/pkg/manager/member/pd_member_manager.go b/pkg/manager/member/pd_member_manager.go index 01c6e074e7..d3039a1829 100644 --- a/pkg/manager/member/pd_member_manager.go +++ b/pkg/manager/member/pd_member_manager.go @@ -15,6 +15,7 @@ package member import ( "fmt" + "strconv" "github.com/golang/glog" "github.com/pingcap/tidb-operator/pkg/apis/pingcap.com/v1alpha1" @@ -254,6 +255,14 @@ func (pmm *pdMemberManager) syncTidbClusterStatus(tc *v1alpha1.TidbCluster, set } pdClient := pmm.pdControl.GetPDClient(tc) + + cluster, err := pdClient.GetCluster() + if err != nil { + tc.Status.PD.Synced = false + return err + } + tc.Status.ClusterID = strconv.FormatUint(cluster.Id, 10) + healthInfo, err := pdClient.GetHealth() if err != nil { tc.Status.PD.Synced = false diff --git a/pkg/manager/member/pd_member_manager_test.go b/pkg/manager/member/pd_member_manager_test.go index 990aa45d6e..2653e13335 100644 --- a/pkg/manager/member/pd_member_manager_test.go +++ b/pkg/manager/member/pd_member_manager_test.go @@ -19,6 +19,7 @@ import ( "testing" . "github.com/onsi/gomega" + "github.com/pingcap/kvproto/pkg/metapb" "github.com/pingcap/tidb-operator/pkg/apis/pingcap.com/v1alpha1" "github.com/pingcap/tidb-operator/pkg/client/clientset/versioned/fake" informers "github.com/pingcap/tidb-operator/pkg/client/informers/externalversions" @@ -187,6 +188,7 @@ func TestPDMemberManagerSyncUpdate(t *testing.T) { errWhenUpdateStatefulSet bool errWhenUpdatePDService bool errWhenUpdatePDPeerService bool + errWhenGetCluster bool errWhenGetPDHealth bool statusChange func(*apps.StatefulSet) err bool @@ -214,6 +216,16 @@ func TestPDMemberManagerSyncUpdate(t *testing.T) { }) } + if test.errWhenGetCluster { + pdClient.AddReaction(controller.GetClusterActionType, func(action *controller.Action) (interface{}, error) { + return nil, fmt.Errorf("failed to get cluster info") + }) + } else { + pdClient.AddReaction(controller.GetClusterActionType, func(action *controller.Action) (interface{}, error) { + return &metapb.Cluster{Id: uint64(1)}, nil + }) + } + if test.statusChange == nil { fakeSetControl.SetStatusChange(func(set *apps.StatefulSet) { set.Status.Replicas = *set.Spec.Replicas @@ -302,6 +314,7 @@ func TestPDMemberManagerSyncUpdate(t *testing.T) { // g.Expect(int(*set.Spec.Replicas)).To(Equal(4)) }, expectTidbClusterFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster) { + g.Expect(tc.Status.ClusterID).To(Equal("1")) g.Expect(tc.Status.PD.Phase).To(Equal(v1alpha1.NormalPhase)) g.Expect(*tc.Status.PD.StatefulSet.ObservedGeneration).To(Equal(int64(1))) g.Expect(len(tc.Status.PD.Members)).To(Equal(3)) @@ -388,6 +401,27 @@ func TestPDMemberManagerSyncUpdate(t *testing.T) { g.Expect(tc.Status.PD.Members).To(BeNil()) }, }, + { + name: "error when sync cluster ID", + modify: func(tc *v1alpha1.TidbCluster) { + tc.Spec.PD.Replicas = 5 + }, + errWhenUpdateStatefulSet: false, + errWhenUpdatePDService: false, + errWhenUpdatePDPeerService: false, + errWhenGetCluster: true, + errWhenGetPDHealth: false, + err: false, + expectPDServiceFn: nil, + expectPDPeerServiceFn: nil, + expectStatefulSetFn: func(g *GomegaWithT, set *apps.StatefulSet, err error) { + g.Expect(err).NotTo(HaveOccurred()) + }, + expectTidbClusterFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster) { + g.Expect(tc.Status.PD.Synced).To(BeFalse()) + g.Expect(tc.Status.PD.Members).To(BeNil()) + }, + }, } for i := range tests { @@ -521,6 +555,9 @@ func TestPDMemberManagerUpgrade(t *testing.T) { pdClient.AddReaction(controller.GetHealthActionType, func(action *controller.Action) (interface{}, error) { return test.pdHealth, nil }) + pdClient.AddReaction(controller.GetClusterActionType, func(action *controller.Action) (interface{}, error) { + return &metapb.Cluster{Id: uint64(1)}, nil + }) fakeSetControl.SetStatusChange(test.statusChange) diff --git a/tests/e2e/create.go b/tests/e2e/create.go index 0ef1c6e4e2..ed091e4315 100644 --- a/tests/e2e/create.go +++ b/tests/e2e/create.go @@ -240,6 +240,11 @@ func pdMemberRunning(tc *v1alpha1.TidbCluster) (bool, error) { } } + if tc.Status.ClusterID == "" { + logf("tc.Status.ClusterID is nil") + return false, nil + } + _, err = kubeCli.CoreV1().Services(ns).Get(controller.PDMemberName(tcName), metav1.GetOptions{}) if err != nil { logf(err.Error())