Skip to content

Commit

Permalink
Add ClusterIDLabelKey label to TidbCluster (#279)
Browse files Browse the repository at this point in the history
* add clusterID label to tidbCluster

* change e2e test

* add clusterID field

* remove label
  • Loading branch information
xiaojingchen authored and tennix committed Feb 21, 2019
1 parent d319c50 commit a2a37ab
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pkg/apis/pingcap.com/v1alpha1/tidbcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,7 @@ func (tc *TidbCluster) TiKVIsAvailable() bool {

return true
}

func (tc *TidbCluster) GetClusterID() string {
return tc.Status.ClusterID
}
7 changes: 4 additions & 3 deletions pkg/apis/pingcap.com/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions pkg/manager/member/pd_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package member

import (
"fmt"
"strconv"

"github.com/golang/glog"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap.com/v1alpha1"
Expand Down Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions pkg/manager/member/pd_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit a2a37ab

Please sign in to comment.