From 8f190ceb8d0f7891c61536c6be659fc0068ebcb2 Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Sun, 5 Jan 2020 14:53:46 +0800 Subject: [PATCH 1/8] add k8s check when pd node not join cluster --- pkg/apis/pingcap/v1alpha1/types.go | 21 ++- pkg/manager/member/pd_member_manager.go | 53 +++++++ pkg/manager/member/pd_member_manager_test.go | 138 +++++++++++++++++++ 3 files changed, 206 insertions(+), 6 deletions(-) diff --git a/pkg/apis/pingcap/v1alpha1/types.go b/pkg/apis/pingcap/v1alpha1/types.go index 0bc66327037..da4f1acbe8a 100644 --- a/pkg/apis/pingcap/v1alpha1/types.go +++ b/pkg/apis/pingcap/v1alpha1/types.go @@ -499,12 +499,13 @@ type Service struct { // PDStatus is PD status type PDStatus struct { - Synced bool `json:"synced,omitempty"` - Phase MemberPhase `json:"phase,omitempty"` - StatefulSet *apps.StatefulSetStatus `json:"statefulSet,omitempty"` - Members map[string]PDMember `json:"members,omitempty"` - Leader PDMember `json:"leader,omitempty"` - FailureMembers map[string]PDFailureMember `json:"failureMembers,omitempty"` + Synced bool `json:"synced,omitempty"` + Phase MemberPhase `json:"phase,omitempty"` + StatefulSet *apps.StatefulSetStatus `json:"statefulSet,omitempty"` + Members map[string]PDMember `json:"members,omitempty"` + Leader PDMember `json:"leader,omitempty"` + FailureMembers map[string]PDFailureMember `json:"failureMembers,omitempty"` + NotJoinClusterMembers map[string]NotJoinClusterMember `json:"notJoinClusterMembers,omitempty"` } // PDMember is PD member @@ -528,6 +529,14 @@ type PDFailureMember struct { CreatedAt metav1.Time `json:"createdAt,omitempty"` } +// PDFailureMember is the pd failure member information +type NotJoinClusterMember struct { + PodName string `json:"podName,omitempty"` + MemberID string `json:"memberID,omitempty"` + PVCUID types.UID `json:"pvcUID,omitempty"` + CreatedAt metav1.Time `json:"createdAt,omitempty"` +} + // TiDBStatus is TiDB status type TiDBStatus struct { Phase MemberPhase `json:"phase,omitempty"` diff --git a/pkg/manager/member/pd_member_manager.go b/pkg/manager/member/pd_member_manager.go index 60019ad7e2b..71e9125ae4d 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" + "github.com/pingcap/tidb-operator/pkg/util" "strconv" "strings" @@ -407,6 +408,11 @@ func (pmm *pdMemberManager) syncTidbClusterStatus(tc *v1alpha1.TidbCluster, set tc.Status.PD.Members = pdStatus tc.Status.PD.Leader = tc.Status.PD.Members[leader.GetName()] + // k8s check + err = pmm.checkNotJoinClusterNode(tc, set, pdStatus) + if err != nil { + return err + } return nil } @@ -756,6 +762,53 @@ func getPDConfigMap(tc *v1alpha1.TidbCluster) (*corev1.ConfigMap, error) { return cm, nil } +func (pmm *pdMemberManager) checkNotJoinClusterNode(tc *v1alpha1.TidbCluster, set *apps.StatefulSet, pdStatus map[string]v1alpha1.PDMember) error { + podSelector, podSelectErr := metav1.LabelSelectorAsSelector(set.Spec.Selector) + if podSelectErr != nil { + return podSelectErr + } + pods, podErr := pmm.podLister.Pods(tc.Namespace).List(podSelector) + if podErr != nil { + return podErr + } + for _, pod := range pods { + var joined = false + for podName, _ := range pdStatus { + if strings.EqualFold(pod.Name, podName) { + joined = true + } + } + if !joined { + if tc.Status.PD.NotJoinClusterMembers == nil { + tc.Status.PD.NotJoinClusterMembers = map[string]v1alpha1.NotJoinClusterMember{} + } + ordinal, err := util.GetOrdinalFromPodName(pod.Name) + if err != nil { + return err + } + pvcName := ordinalPVCName(v1alpha1.PDMemberType, controller.PDMemberName(tc.Name), ordinal) + pvc, err := pmm.pvcLister.PersistentVolumeClaims(tc.Namespace).Get(pvcName) + if err != nil { + return err + } + tc.Status.PD.NotJoinClusterMembers[pod.Name] = v1alpha1.NotJoinClusterMember{ + PodName: pod.Name, + MemberID: "", + PVCUID: pvc.UID, + CreatedAt: metav1.Now(), + } + } else { + if tc.Status.PD.NotJoinClusterMembers != nil { + if _, ok := tc.Status.PD.NotJoinClusterMembers[pod.Name]; ok { + delete(tc.Status.PD.NotJoinClusterMembers, pod.Name) + } + + } + } + } + return nil +} + type FakePDMemberManager struct { err error } diff --git a/pkg/manager/member/pd_member_manager_test.go b/pkg/manager/member/pd_member_manager_test.go index b94263a983b..0edf582f3f2 100644 --- a/pkg/manager/member/pd_member_manager_test.go +++ b/pkg/manager/member/pd_member_manager_test.go @@ -1408,3 +1408,141 @@ func TestGetNewPdServiceForTidbCluster(t *testing.T) { }) } } + +func TestPDMemberManagerSyncPDStsWhenPdNotJoinCluster(t *testing.T) { + g := NewGomegaWithT(t) + type testcase struct { + name string + modify func(cluster *v1alpha1.TidbCluster, podIndexer cache.Indexer, pvcIndexer cache.Indexer) + pdHealth *pdapi.HealthInfo + tcStatusChange func(cluster *v1alpha1.TidbCluster) + err bool + expectTidbClusterFn func(*GomegaWithT, *v1alpha1.TidbCluster) + } + + testFn := func(test *testcase, t *testing.T) { + tc := newTidbClusterForPD() + ns := tc.Namespace + tcName := tc.Name + + pmm, _, _, fakePDControl, podIndexer, pvcIndexer, _ := newFakePDMemberManager() + pdClient := controller.NewFakePDClient(fakePDControl, tc) + + pdClient.AddReaction(pdapi.GetHealthActionType, func(action *pdapi.Action) (interface{}, error) { + return test.pdHealth, nil + }) + pdClient.AddReaction(pdapi.GetClusterActionType, func(action *pdapi.Action) (interface{}, error) { + return &metapb.Cluster{Id: uint64(1)}, nil + }) + + err := pmm.Sync(tc) + g.Expect(controller.IsRequeueError(err)).To(BeTrue()) + _, err = pmm.svcLister.Services(ns).Get(controller.PDMemberName(tcName)) + g.Expect(err).NotTo(HaveOccurred()) + _, err = pmm.svcLister.Services(ns).Get(controller.PDPeerMemberName(tcName)) + g.Expect(err).NotTo(HaveOccurred()) + _, err = pmm.setLister.StatefulSets(ns).Get(controller.PDMemberName(tcName)) + g.Expect(err).NotTo(HaveOccurred()) + if test.tcStatusChange != nil { + test.tcStatusChange(tc) + } + test.modify(tc, podIndexer, pvcIndexer) + err = pmm.syncPDStatefulSetForTidbCluster(tc) + if test.err { + g.Expect(err).To(HaveOccurred()) + } else { + g.Expect(err).NotTo(HaveOccurred()) + } + if test.expectTidbClusterFn != nil { + test.expectTidbClusterFn(g, tc) + } + } + tests := []testcase{ + { + name: "add not join cluster pod info ", + modify: func(cluster *v1alpha1.TidbCluster, podIndexer cache.Indexer, pvcIndexer cache.Indexer) { + for ordinal := 0; ordinal < 3; ordinal++ { + pod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: ordinalPodName(v1alpha1.PDMemberType, cluster.GetName(), int32(ordinal)), + Namespace: metav1.NamespaceDefault, + Annotations: map[string]string{}, + Labels: label.New().Instance(cluster.GetInstanceName()).PD().Labels(), + }, + } + podIndexer.Add(pod) + } + for ordinal := 0; ordinal < 3; ordinal++ { + pvc := &corev1.PersistentVolumeClaim{ + ObjectMeta: metav1.ObjectMeta{ + Name: ordinalPVCName(v1alpha1.PDMemberType, controller.PDMemberName(cluster.GetName()), int32(ordinal)), + Namespace: metav1.NamespaceDefault, + Annotations: map[string]string{}, + Labels: label.New().Instance(cluster.GetInstanceName()).PD().Labels(), + }, + } + pvcIndexer.Add(pvc) + } + + }, + pdHealth: &pdapi.HealthInfo{Healths: []pdapi.MemberHealth{ + {Name: "test-pd-0", MemberID: uint64(1), ClientUrls: []string{"http://test-pd-0:2379"}, Health: false}, + {Name: "test-pd-1", MemberID: uint64(2), ClientUrls: []string{"http://test-pd-1:2379"}, Health: false}, + }}, + err: false, + expectTidbClusterFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster) { + g.Expect(tc.Status.PD.NotJoinClusterMembers["test-pd-2"]).NotTo(Equal(nil)) + }, + }, + { + name: "clear notJoinClusterMembers when the member join the cluster ", + tcStatusChange: func(cluster *v1alpha1.TidbCluster) { + cluster.Status.PD.NotJoinClusterMembers = map[string]v1alpha1.NotJoinClusterMember{ + "test-pd-0": { + PodName: "test-pd-0", + CreatedAt: metav1.Now(), + }, + } + }, + modify: func(cluster *v1alpha1.TidbCluster, podIndexer cache.Indexer, pvcIndexer cache.Indexer) { + for ordinal := 0; ordinal < 3; ordinal++ { + pod := &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: ordinalPodName(v1alpha1.PDMemberType, cluster.GetName(), int32(ordinal)), + Namespace: metav1.NamespaceDefault, + Annotations: map[string]string{}, + Labels: label.New().Instance(cluster.GetInstanceName()).PD().Labels(), + }, + } + podIndexer.Add(pod) + } + for ordinal := 0; ordinal < 3; ordinal++ { + pvc := &corev1.PersistentVolumeClaim{ + ObjectMeta: metav1.ObjectMeta{ + Name: ordinalPVCName(v1alpha1.PDMemberType, controller.PDMemberName(cluster.GetName()), int32(ordinal)), + Namespace: metav1.NamespaceDefault, + Annotations: map[string]string{}, + Labels: label.New().Instance(cluster.GetInstanceName()).PD().Labels(), + }, + } + pvcIndexer.Add(pvc) + } + + }, + pdHealth: &pdapi.HealthInfo{Healths: []pdapi.MemberHealth{ + {Name: "test-pd-0", MemberID: uint64(1), ClientUrls: []string{"http://test-pd-0:2379"}, Health: false}, + {Name: "test-pd-1", MemberID: uint64(2), ClientUrls: []string{"http://test-pd-1:2379"}, Health: false}, + {Name: "test-pd-2", MemberID: uint64(2), ClientUrls: []string{"http://test-pd-2:2379"}, Health: false}, + }}, + err: false, + expectTidbClusterFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster) { + g.Expect(len(tc.Status.PD.NotJoinClusterMembers)).To(Equal(0)) + }, + }, + } + for i := range tests { + t.Logf("begin: %s", tests[i].name) + testFn(&tests[i], t) + t.Logf("end: %s", tests[i].name) + } +} From da3c1e6d9b0cf2f8fabd31c48c8bf5af540ca5dc Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Sun, 5 Jan 2020 15:10:20 +0800 Subject: [PATCH 2/8] format `PDStatus` class --- pkg/apis/pingcap/v1alpha1/types.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/apis/pingcap/v1alpha1/types.go b/pkg/apis/pingcap/v1alpha1/types.go index da4f1acbe8a..458a6073b5f 100644 --- a/pkg/apis/pingcap/v1alpha1/types.go +++ b/pkg/apis/pingcap/v1alpha1/types.go @@ -499,12 +499,12 @@ type Service struct { // PDStatus is PD status type PDStatus struct { - Synced bool `json:"synced,omitempty"` - Phase MemberPhase `json:"phase,omitempty"` - StatefulSet *apps.StatefulSetStatus `json:"statefulSet,omitempty"` - Members map[string]PDMember `json:"members,omitempty"` - Leader PDMember `json:"leader,omitempty"` - FailureMembers map[string]PDFailureMember `json:"failureMembers,omitempty"` + Synced bool `json:"synced,omitempty"` + Phase MemberPhase `json:"phase,omitempty"` + StatefulSet *apps.StatefulSetStatus `json:"statefulSet,omitempty"` + Members map[string]PDMember `json:"members,omitempty"` + Leader PDMember `json:"leader,omitempty"` + FailureMembers map[string]PDFailureMember `json:"failureMembers,omitempty"` NotJoinClusterMembers map[string]NotJoinClusterMember `json:"notJoinClusterMembers,omitempty"` } From 654698572a523d20690101e24ed343145c6b229a Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Sun, 5 Jan 2020 15:40:41 +0800 Subject: [PATCH 3/8] optimize range method --- pkg/manager/member/pd_member_manager.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/manager/member/pd_member_manager.go b/pkg/manager/member/pd_member_manager.go index 71e9125ae4d..5754bee9eaf 100644 --- a/pkg/manager/member/pd_member_manager.go +++ b/pkg/manager/member/pd_member_manager.go @@ -773,7 +773,7 @@ func (pmm *pdMemberManager) checkNotJoinClusterNode(tc *v1alpha1.TidbCluster, se } for _, pod := range pods { var joined = false - for podName, _ := range pdStatus { + for podName := range pdStatus { if strings.EqualFold(pod.Name, podName) { joined = true } From 78d37c10ebee040fbca5586550645d406aa332c3 Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Sun, 5 Jan 2020 15:52:13 +0800 Subject: [PATCH 4/8] gen code --- .../pingcap/v1alpha1/zz_generated.deepcopy.go | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go index b1976db66fb..69ed65257c2 100644 --- a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go @@ -708,6 +708,23 @@ func (in *MonitorContainer) DeepCopy() *MonitorContainer { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *NotJoinClusterMember) DeepCopyInto(out *NotJoinClusterMember) { + *out = *in + in.CreatedAt.DeepCopyInto(&out.CreatedAt) + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NotJoinClusterMember. +func (in *NotJoinClusterMember) DeepCopy() *NotJoinClusterMember { + if in == nil { + return nil + } + out := new(NotJoinClusterMember) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *OpenTracing) DeepCopyInto(out *OpenTracing) { *out = *in @@ -1381,6 +1398,13 @@ func (in *PDStatus) DeepCopyInto(out *PDStatus) { (*out)[key] = *val.DeepCopy() } } + if in.NotJoinClusterMembers != nil { + in, out := &in.NotJoinClusterMembers, &out.NotJoinClusterMembers + *out = make(map[string]NotJoinClusterMember, len(*in)) + for key, val := range *in { + (*out)[key] = *val.DeepCopy() + } + } return } From 3feb36bc1c8d933cb40dbf740af84223d6137906 Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Sun, 5 Jan 2020 16:01:40 +0800 Subject: [PATCH 5/8] rm useless `notJoinCluster` field --- pkg/apis/pingcap/v1alpha1/types.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/apis/pingcap/v1alpha1/types.go b/pkg/apis/pingcap/v1alpha1/types.go index 458a6073b5f..7fcec698d54 100644 --- a/pkg/apis/pingcap/v1alpha1/types.go +++ b/pkg/apis/pingcap/v1alpha1/types.go @@ -532,7 +532,6 @@ type PDFailureMember struct { // PDFailureMember is the pd failure member information type NotJoinClusterMember struct { PodName string `json:"podName,omitempty"` - MemberID string `json:"memberID,omitempty"` PVCUID types.UID `json:"pvcUID,omitempty"` CreatedAt metav1.Time `json:"createdAt,omitempty"` } From 2a1fd7a77d7f2e835499bd5676144bb1606bea63 Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Sun, 5 Jan 2020 16:08:36 +0800 Subject: [PATCH 6/8] fmt types class --- pkg/apis/pingcap/v1alpha1/types.go | 12 ++++++------ pkg/manager/member/pd_member_manager.go | 1 - 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/pkg/apis/pingcap/v1alpha1/types.go b/pkg/apis/pingcap/v1alpha1/types.go index 7fcec698d54..e1e39a18a92 100644 --- a/pkg/apis/pingcap/v1alpha1/types.go +++ b/pkg/apis/pingcap/v1alpha1/types.go @@ -499,12 +499,12 @@ type Service struct { // PDStatus is PD status type PDStatus struct { - Synced bool `json:"synced,omitempty"` - Phase MemberPhase `json:"phase,omitempty"` - StatefulSet *apps.StatefulSetStatus `json:"statefulSet,omitempty"` - Members map[string]PDMember `json:"members,omitempty"` - Leader PDMember `json:"leader,omitempty"` - FailureMembers map[string]PDFailureMember `json:"failureMembers,omitempty"` + Synced bool `json:"synced,omitempty"` + Phase MemberPhase `json:"phase,omitempty"` + StatefulSet *apps.StatefulSetStatus `json:"statefulSet,omitempty"` + Members map[string]PDMember `json:"members,omitempty"` + Leader PDMember `json:"leader,omitempty"` + FailureMembers map[string]PDFailureMember `json:"failureMembers,omitempty"` NotJoinClusterMembers map[string]NotJoinClusterMember `json:"notJoinClusterMembers,omitempty"` } diff --git a/pkg/manager/member/pd_member_manager.go b/pkg/manager/member/pd_member_manager.go index 5754bee9eaf..b8c0268732d 100644 --- a/pkg/manager/member/pd_member_manager.go +++ b/pkg/manager/member/pd_member_manager.go @@ -793,7 +793,6 @@ func (pmm *pdMemberManager) checkNotJoinClusterNode(tc *v1alpha1.TidbCluster, se } tc.Status.PD.NotJoinClusterMembers[pod.Name] = v1alpha1.NotJoinClusterMember{ PodName: pod.Name, - MemberID: "", PVCUID: pvc.UID, CreatedAt: metav1.Now(), } From 76c223446e56181907f73e090e9b2a0b64265593 Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Mon, 6 Jan 2020 10:39:58 +0800 Subject: [PATCH 7/8] optimize code style --- pkg/apis/pingcap/v1alpha1/types.go | 18 ++++----- .../pingcap/v1alpha1/zz_generated.deepcopy.go | 40 +++++++++---------- pkg/manager/member/pd_member_manager.go | 16 ++++---- pkg/manager/member/pd_member_manager_test.go | 10 ++--- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/pkg/apis/pingcap/v1alpha1/types.go b/pkg/apis/pingcap/v1alpha1/types.go index e1e39a18a92..2a53fa9fb25 100644 --- a/pkg/apis/pingcap/v1alpha1/types.go +++ b/pkg/apis/pingcap/v1alpha1/types.go @@ -499,13 +499,13 @@ type Service struct { // PDStatus is PD status type PDStatus struct { - Synced bool `json:"synced,omitempty"` - Phase MemberPhase `json:"phase,omitempty"` - StatefulSet *apps.StatefulSetStatus `json:"statefulSet,omitempty"` - Members map[string]PDMember `json:"members,omitempty"` - Leader PDMember `json:"leader,omitempty"` - FailureMembers map[string]PDFailureMember `json:"failureMembers,omitempty"` - NotJoinClusterMembers map[string]NotJoinClusterMember `json:"notJoinClusterMembers,omitempty"` + Synced bool `json:"synced,omitempty"` + Phase MemberPhase `json:"phase,omitempty"` + StatefulSet *apps.StatefulSetStatus `json:"statefulSet,omitempty"` + Members map[string]PDMember `json:"members,omitempty"` + Leader PDMember `json:"leader,omitempty"` + FailureMembers map[string]PDFailureMember `json:"failureMembers,omitempty"` + UnjoinedMembers map[string]UnjoinedMember `json:"unJoinedMembers,omitempty"` } // PDMember is PD member @@ -529,8 +529,8 @@ type PDFailureMember struct { CreatedAt metav1.Time `json:"createdAt,omitempty"` } -// PDFailureMember is the pd failure member information -type NotJoinClusterMember struct { +// UnjoinedMember is the pd unjoin cluster member information +type UnjoinedMember struct { PodName string `json:"podName,omitempty"` PVCUID types.UID `json:"pvcUID,omitempty"` CreatedAt metav1.Time `json:"createdAt,omitempty"` diff --git a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go index 69ed65257c2..71833be5c74 100644 --- a/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go @@ -708,23 +708,6 @@ func (in *MonitorContainer) DeepCopy() *MonitorContainer { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *NotJoinClusterMember) DeepCopyInto(out *NotJoinClusterMember) { - *out = *in - in.CreatedAt.DeepCopyInto(&out.CreatedAt) - return -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NotJoinClusterMember. -func (in *NotJoinClusterMember) DeepCopy() *NotJoinClusterMember { - if in == nil { - return nil - } - out := new(NotJoinClusterMember) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *OpenTracing) DeepCopyInto(out *OpenTracing) { *out = *in @@ -1398,9 +1381,9 @@ func (in *PDStatus) DeepCopyInto(out *PDStatus) { (*out)[key] = *val.DeepCopy() } } - if in.NotJoinClusterMembers != nil { - in, out := &in.NotJoinClusterMembers, &out.NotJoinClusterMembers - *out = make(map[string]NotJoinClusterMember, len(*in)) + if in.UnjoinedMembers != nil { + in, out := &in.UnjoinedMembers, &out.UnjoinedMembers + *out = make(map[string]UnjoinedMember, len(*in)) for key, val := range *in { (*out)[key] = *val.DeepCopy() } @@ -4114,3 +4097,20 @@ func (in *TxnLocalLatches) DeepCopy() *TxnLocalLatches { in.DeepCopyInto(out) return out } + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *UnjoinedMember) DeepCopyInto(out *UnjoinedMember) { + *out = *in + in.CreatedAt.DeepCopyInto(&out.CreatedAt) + return +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new UnjoinedMember. +func (in *UnjoinedMember) DeepCopy() *UnjoinedMember { + if in == nil { + return nil + } + out := new(UnjoinedMember) + in.DeepCopyInto(out) + return out +} diff --git a/pkg/manager/member/pd_member_manager.go b/pkg/manager/member/pd_member_manager.go index b8c0268732d..646269166ac 100644 --- a/pkg/manager/member/pd_member_manager.go +++ b/pkg/manager/member/pd_member_manager.go @@ -409,7 +409,7 @@ func (pmm *pdMemberManager) syncTidbClusterStatus(tc *v1alpha1.TidbCluster, set tc.Status.PD.Leader = tc.Status.PD.Members[leader.GetName()] // k8s check - err = pmm.checkNotJoinClusterNode(tc, set, pdStatus) + err = pmm.collectUnjoinedMembers(tc, set, pdStatus) if err != nil { return err } @@ -762,7 +762,7 @@ func getPDConfigMap(tc *v1alpha1.TidbCluster) (*corev1.ConfigMap, error) { return cm, nil } -func (pmm *pdMemberManager) checkNotJoinClusterNode(tc *v1alpha1.TidbCluster, set *apps.StatefulSet, pdStatus map[string]v1alpha1.PDMember) error { +func (pmm *pdMemberManager) collectUnjoinedMembers(tc *v1alpha1.TidbCluster, set *apps.StatefulSet, pdStatus map[string]v1alpha1.PDMember) error { podSelector, podSelectErr := metav1.LabelSelectorAsSelector(set.Spec.Selector) if podSelectErr != nil { return podSelectErr @@ -779,8 +779,8 @@ func (pmm *pdMemberManager) checkNotJoinClusterNode(tc *v1alpha1.TidbCluster, se } } if !joined { - if tc.Status.PD.NotJoinClusterMembers == nil { - tc.Status.PD.NotJoinClusterMembers = map[string]v1alpha1.NotJoinClusterMember{} + if tc.Status.PD.UnjoinedMembers == nil { + tc.Status.PD.UnjoinedMembers = map[string]v1alpha1.UnjoinedMember{} } ordinal, err := util.GetOrdinalFromPodName(pod.Name) if err != nil { @@ -791,15 +791,15 @@ func (pmm *pdMemberManager) checkNotJoinClusterNode(tc *v1alpha1.TidbCluster, se if err != nil { return err } - tc.Status.PD.NotJoinClusterMembers[pod.Name] = v1alpha1.NotJoinClusterMember{ + tc.Status.PD.UnjoinedMembers[pod.Name] = v1alpha1.UnjoinedMember{ PodName: pod.Name, PVCUID: pvc.UID, CreatedAt: metav1.Now(), } } else { - if tc.Status.PD.NotJoinClusterMembers != nil { - if _, ok := tc.Status.PD.NotJoinClusterMembers[pod.Name]; ok { - delete(tc.Status.PD.NotJoinClusterMembers, pod.Name) + if tc.Status.PD.UnjoinedMembers != nil { + if _, ok := tc.Status.PD.UnjoinedMembers[pod.Name]; ok { + delete(tc.Status.PD.UnjoinedMembers, pod.Name) } } diff --git a/pkg/manager/member/pd_member_manager_test.go b/pkg/manager/member/pd_member_manager_test.go index 0edf582f3f2..eeaf9acb5f0 100644 --- a/pkg/manager/member/pd_member_manager_test.go +++ b/pkg/manager/member/pd_member_manager_test.go @@ -1459,7 +1459,7 @@ func TestPDMemberManagerSyncPDStsWhenPdNotJoinCluster(t *testing.T) { } tests := []testcase{ { - name: "add not join cluster pod info ", + name: "add pd unjoin cluster member info ", modify: func(cluster *v1alpha1.TidbCluster, podIndexer cache.Indexer, pvcIndexer cache.Indexer) { for ordinal := 0; ordinal < 3; ordinal++ { pod := &corev1.Pod{ @@ -1491,13 +1491,13 @@ func TestPDMemberManagerSyncPDStsWhenPdNotJoinCluster(t *testing.T) { }}, err: false, expectTidbClusterFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster) { - g.Expect(tc.Status.PD.NotJoinClusterMembers["test-pd-2"]).NotTo(Equal(nil)) + g.Expect(tc.Status.PD.UnjoinedMembers["test-pd-2"]).NotTo(BeNil()) }, }, { - name: "clear notJoinClusterMembers when the member join the cluster ", + name: "clear unjoin cluster member info when the member join the cluster ", tcStatusChange: func(cluster *v1alpha1.TidbCluster) { - cluster.Status.PD.NotJoinClusterMembers = map[string]v1alpha1.NotJoinClusterMember{ + cluster.Status.PD.UnjoinedMembers = map[string]v1alpha1.UnjoinedMember{ "test-pd-0": { PodName: "test-pd-0", CreatedAt: metav1.Now(), @@ -1536,7 +1536,7 @@ func TestPDMemberManagerSyncPDStsWhenPdNotJoinCluster(t *testing.T) { }}, err: false, expectTidbClusterFn: func(g *GomegaWithT, tc *v1alpha1.TidbCluster) { - g.Expect(len(tc.Status.PD.NotJoinClusterMembers)).To(Equal(0)) + g.Expect(tc.Status.PD.UnjoinedMembers).To(BeEmpty()) }, }, } From 9f958ab56ecdbc0311e424b430a5cf190fdeaae5 Mon Sep 17 00:00:00 2001 From: mikechengwei <842725815@qq.com> Date: Mon, 6 Jan 2020 14:09:40 +0800 Subject: [PATCH 8/8] optimize code --- pkg/apis/pingcap/v1alpha1/types.go | 2 +- pkg/manager/member/pd_member_manager.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/apis/pingcap/v1alpha1/types.go b/pkg/apis/pingcap/v1alpha1/types.go index 2a53fa9fb25..d24ef59b910 100644 --- a/pkg/apis/pingcap/v1alpha1/types.go +++ b/pkg/apis/pingcap/v1alpha1/types.go @@ -505,7 +505,7 @@ type PDStatus struct { Members map[string]PDMember `json:"members,omitempty"` Leader PDMember `json:"leader,omitempty"` FailureMembers map[string]PDFailureMember `json:"failureMembers,omitempty"` - UnjoinedMembers map[string]UnjoinedMember `json:"unJoinedMembers,omitempty"` + UnjoinedMembers map[string]UnjoinedMember `json:"unjoinedMembers,omitempty"` } // PDMember is PD member diff --git a/pkg/manager/member/pd_member_manager.go b/pkg/manager/member/pd_member_manager.go index 646269166ac..2d64131328b 100644 --- a/pkg/manager/member/pd_member_manager.go +++ b/pkg/manager/member/pd_member_manager.go @@ -776,6 +776,7 @@ func (pmm *pdMemberManager) collectUnjoinedMembers(tc *v1alpha1.TidbCluster, set for podName := range pdStatus { if strings.EqualFold(pod.Name, podName) { joined = true + break } } if !joined {