Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make controller logic independable with certain tidbcluster label #1419

Merged
merged 3 commits into from
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/apis/pingcap/v1alpha1/tidbcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package v1alpha1
import (
"fmt"

"github.com/pingcap/tidb-operator/pkg/label"
corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -403,3 +404,14 @@ func (tc *TidbCluster) Timezone() string {
}
return tz
}

func (tc *TidbCluster) GetInstanceName() string {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, we could define a GetLabels() method and shadow the one of ObjectMeta so that the controller logic don't have to be modified, but that way we have to copy a map every time this method get called.

labels := tc.ObjectMeta.GetLabels()
// Keep backward compatibility for helm.
// This introduce a hidden danger that change this label will trigger rolling-update of most of the components
// TODO(aylei): disallow mutation of this label or adding this label with value other than the cluster name in ValidateUpdate()
if inst, ok := labels[label.InstanceLabelKey]; ok {
return inst
}
return tc.Name
}
2 changes: 1 addition & 1 deletion pkg/manager/member/orphan_pods_cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (opc *orphanPodsCleaner) Clean(tc *v1alpha1.TidbCluster) (map[string]string
// for unit test
skipReason := map[string]string{}

selector, err := label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).Selector()
selector, err := label.New().Instance(tc.GetInstanceName()).Selector()
if err != nil {
return skipReason, err
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/manager/member/orphan_pods_cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestOrphanPodsCleanerClean(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: metav1.NamespaceDefault,
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).TiDB().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).TiDB().Labels(),
},
},
},
Expand All @@ -104,7 +104,7 @@ func TestOrphanPodsCleanerClean(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: metav1.NamespaceDefault,
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).PD().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).PD().Labels(),
},
Status: corev1.PodStatus{
Phase: corev1.PodPending,
Expand All @@ -125,7 +125,7 @@ func TestOrphanPodsCleanerClean(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: metav1.NamespaceDefault,
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).PD().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).PD().Labels(),
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestOrphanPodsCleanerClean(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: metav1.NamespaceDefault,
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).PD().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).PD().Labels(),
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestOrphanPodsCleanerClean(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: metav1.NamespaceDefault,
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).PD().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).PD().Labels(),
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
Expand Down Expand Up @@ -233,7 +233,7 @@ func TestOrphanPodsCleanerClean(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: metav1.NamespaceDefault,
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).PD().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).PD().Labels(),
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestOrphanPodsCleanerClean(t *testing.T) {
UID: "pod-1-uid",
ResourceVersion: "1",
Namespace: metav1.NamespaceDefault,
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).PD().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).PD().Labels(),
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
Expand All @@ -294,7 +294,7 @@ func TestOrphanPodsCleanerClean(t *testing.T) {
UID: "pod-1-uid",
ResourceVersion: "2",
Namespace: metav1.NamespaceDefault,
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).PD().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).PD().Labels(),
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
Expand Down Expand Up @@ -327,7 +327,7 @@ func TestOrphanPodsCleanerClean(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: metav1.NamespaceDefault,
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).PD().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).PD().Labels(),
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
Expand Down Expand Up @@ -361,7 +361,7 @@ func TestOrphanPodsCleanerClean(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "pod-1",
Namespace: metav1.NamespaceDefault,
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).PD().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).PD().Labels(),
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
Expand All @@ -383,7 +383,7 @@ func TestOrphanPodsCleanerClean(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "pod-2",
Namespace: metav1.NamespaceDefault,
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).TiKV().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).TiKV().Labels(),
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
Expand All @@ -405,7 +405,7 @@ func TestOrphanPodsCleanerClean(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "pod-3",
Namespace: metav1.NamespaceDefault,
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).TiKV().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).TiKV().Labels(),
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
Expand All @@ -427,7 +427,7 @@ func TestOrphanPodsCleanerClean(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "pod-4",
Namespace: metav1.NamespaceDefault,
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).TiDB().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).TiDB().Labels(),
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
Expand Down
10 changes: 5 additions & 5 deletions pkg/manager/member/pd_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func (pmm *pdMemberManager) getNewPDServiceForTidbCluster(tc *v1alpha1.TidbClust
ns := tc.Namespace
tcName := tc.Name
svcName := controller.PDMemberName(tcName)
instanceName := tc.GetLabels()[label.InstanceLabelKey]
instanceName := tc.GetInstanceName()
pdLabel := label.New().Instance(instanceName).PD().Labels()

return &corev1.Service{
Expand Down Expand Up @@ -467,7 +467,7 @@ func getNewPDHeadlessServiceForTidbCluster(tc *v1alpha1.TidbCluster) *corev1.Ser
ns := tc.Namespace
tcName := tc.Name
svcName := controller.PDPeerMemberName(tcName)
instanceName := tc.GetLabels()[label.InstanceLabelKey]
instanceName := tc.GetInstanceName()
pdLabel := label.New().Instance(instanceName).PD().Labels()

return &corev1.Service{
Expand Down Expand Up @@ -498,7 +498,7 @@ func (pmm *pdMemberManager) pdStatefulSetIsUpgrading(set *apps.StatefulSet, tc *
return true, nil
}
selector, err := label.New().
Instance(tc.GetLabels()[label.InstanceLabelKey]).
Instance(tc.GetInstanceName()).
PD().
Selector()
if err != nil {
Expand All @@ -523,7 +523,7 @@ func (pmm *pdMemberManager) pdStatefulSetIsUpgrading(set *apps.StatefulSet, tc *
func getNewPDSetForTidbCluster(tc *v1alpha1.TidbCluster, cm *corev1.ConfigMap) (*apps.StatefulSet, error) {
ns := tc.Namespace
tcName := tc.Name
instanceName := tc.GetLabels()[label.InstanceLabelKey]
instanceName := tc.GetInstanceName()
pdConfigMap := controller.MemberConfigMapName(tc, v1alpha1.PDMemberType)
if cm != nil {
pdConfigMap = cm.Name
Expand Down Expand Up @@ -728,7 +728,7 @@ func getPDConfigMap(tc *v1alpha1.TidbCluster) (*corev1.ConfigMap, error) {
return nil, err
}

instanceName := tc.GetLabels()[label.InstanceLabelKey]
instanceName := tc.GetInstanceName()
pdLabel := label.New().Instance(instanceName).PD().Labels()
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Expand Down
8 changes: 4 additions & 4 deletions pkg/manager/member/pd_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ func TestPDMemberManagerPdStatefulSetIsUpgrading(t *testing.T) {
Name: ordinalPodName(v1alpha1.PDMemberType, tc.GetName(), 0),
Namespace: metav1.NamespaceDefault,
Annotations: map[string]string{},
Labels: label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).PD().Labels(),
Labels: label.New().Instance(tc.GetInstanceName()).PD().Labels(),
},
}
if test.updatePod != nil {
Expand Down Expand Up @@ -849,7 +849,7 @@ func TestGetNewPDHeadlessServiceForTidbCluster(t *testing.T) {
Labels: map[string]string{
"app.kubernetes.io/name": "tidb-cluster",
"app.kubernetes.io/managed-by": "tidb-operator",
"app.kubernetes.io/instance": "",
"app.kubernetes.io/instance": "foo",
"app.kubernetes.io/component": "pd",
},
OwnerReferences: []metav1.OwnerReference{
Expand Down Expand Up @@ -880,7 +880,7 @@ func TestGetNewPDHeadlessServiceForTidbCluster(t *testing.T) {
Selector: map[string]string{
"app.kubernetes.io/name": "tidb-cluster",
"app.kubernetes.io/managed-by": "tidb-operator",
"app.kubernetes.io/instance": "",
"app.kubernetes.io/instance": "foo",
"app.kubernetes.io/component": "pd",
},
PublishNotReadyAddresses: true,
Expand Down Expand Up @@ -1088,7 +1088,7 @@ func TestGetPDConfigMap(t *testing.T) {
Labels: map[string]string{
"app.kubernetes.io/name": "tidb-cluster",
"app.kubernetes.io/managed-by": "tidb-operator",
"app.kubernetes.io/instance": "",
"app.kubernetes.io/instance": "foo",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use const vars in label package to replace these keys?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think both make sense, by not using the const var, the correctness of the labelKey itself is also tested, it just depends on the purpose of the UTs

"app.kubernetes.io/component": "pd",
},
OwnerReferences: []metav1.OwnerReference{
Expand Down
4 changes: 2 additions & 2 deletions pkg/manager/member/pump_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func getNewPumpStatefulSet(tc *v1alpha1.TidbCluster, cm *corev1.ConfigMap) (*app
}

func getPumpMeta(tc *v1alpha1.TidbCluster, nameFunc func(string) string) (metav1.ObjectMeta, label.Label) {
instanceName := tc.GetLabels()[label.InstanceLabelKey]
instanceName := tc.GetInstanceName()
pumpLabel := label.New().Instance(instanceName).Pump()

objMeta := metav1.ObjectMeta{
Expand Down Expand Up @@ -453,7 +453,7 @@ func (pmm *pumpMemberManager) pumpStatefulSetIsUpgrading(set *apps.StatefulSet,
return true, nil
}
selector, err := label.New().
Instance(tc.GetLabels()[label.InstanceLabelKey]).
Instance(tc.GetInstanceName()).
Pump().
Selector()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/manager/member/pump_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ func TestGetNewPumpHeadlessService(t *testing.T) {
Labels: map[string]string{
"app.kubernetes.io/name": "tidb-cluster",
"app.kubernetes.io/managed-by": "tidb-operator",
"app.kubernetes.io/instance": "",
"app.kubernetes.io/instance": "foo",
"app.kubernetes.io/component": "pump",
},
OwnerReferences: []metav1.OwnerReference{
Expand Down Expand Up @@ -579,7 +579,7 @@ func TestGetNewPumpHeadlessService(t *testing.T) {
Selector: map[string]string{
"app.kubernetes.io/name": "tidb-cluster",
"app.kubernetes.io/managed-by": "tidb-operator",
"app.kubernetes.io/instance": "",
"app.kubernetes.io/instance": "foo",
"app.kubernetes.io/component": "pump",
},
PublishNotReadyAddresses: true,
Expand Down Expand Up @@ -627,7 +627,7 @@ func TestGetNewPumpConfigMap(t *testing.T) {
Labels: map[string]string{
"app.kubernetes.io/name": "tidb-cluster",
"app.kubernetes.io/managed-by": "tidb-operator",
"app.kubernetes.io/instance": "",
"app.kubernetes.io/instance": "foo",
"app.kubernetes.io/component": "pump",
},
OwnerReferences: []metav1.OwnerReference{
Expand Down Expand Up @@ -676,7 +676,7 @@ func TestGetNewPumpConfigMap(t *testing.T) {
Labels: map[string]string{
"app.kubernetes.io/name": "tidb-cluster",
"app.kubernetes.io/managed-by": "tidb-operator",
"app.kubernetes.io/instance": "",
"app.kubernetes.io/instance": "foo",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

"app.kubernetes.io/component": "pump",
},
OwnerReferences: []metav1.OwnerReference{
Expand Down
2 changes: 1 addition & 1 deletion pkg/manager/member/pvc_cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (rpc *realPVCCleaner) listAllPVCs(tc *v1alpha1.TidbCluster) ([]*corev1.Pers
ns := tc.GetNamespace()
tcName := tc.GetName()

selector, err := label.New().Instance(tc.GetLabels()[label.InstanceLabelKey]).Selector()
selector, err := label.New().Instance(tc.GetInstanceName()).Selector()
if err != nil {
return nil, fmt.Errorf("cluster %s/%s assemble label selector failed, err: %v", ns, tcName, err)
}
Expand Down
Loading