Skip to content

Commit

Permalink
Skip the TLS of the PD dashboard when the TiDB version < v4.0.0 (ping…
Browse files Browse the repository at this point in the history
  • Loading branch information
weekface authored and sre-bot committed May 8, 2020
1 parent 3568f36 commit 39267f2
Show file tree
Hide file tree
Showing 4 changed files with 340 additions and 3 deletions.
11 changes: 11 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 (
"encoding/json"
"fmt"
"strings"

"github.com/pingcap/advanced-statefulset/pkg/apis/apps/v1/helper"
"github.com/pingcap/tidb-operator/pkg/label"
Expand Down Expand Up @@ -55,6 +56,16 @@ func (tc *TidbCluster) PDImage() string {
return image
}

func (tc *TidbCluster) PDVersion() string {
image := tc.PDImage()
colonIdx := strings.LastIndexByte(image, ':')
if colonIdx >= 0 {
return image[colonIdx+1:]
}

return "latest"
}

func (tc *TidbCluster) TiKVImage() string {
image := tc.Spec.TiKV.Image
baseImage := tc.Spec.TiKV.BaseImage
Expand Down
41 changes: 41 additions & 0 deletions pkg/apis/pingcap/v1alpha1/tidbcluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,47 @@ func TestHelperImagePullPolicy(t *testing.T) {
}
}

func TestPDVersion(t *testing.T) {
g := NewGomegaWithT(t)

type testcase struct {
name string
update func(*TidbCluster)
expectFn func(*GomegaWithT, *TidbCluster)
}
testFn := func(test *testcase, t *testing.T) {
t.Log(test.name)

tc := newTidbCluster()
test.update(tc)
test.expectFn(g, tc)
}
tests := []testcase{
{
name: "has tag",
update: func(tc *TidbCluster) {
tc.Spec.PD.Image = "pingcap/pd:v3.1.0"
},
expectFn: func(g *GomegaWithT, tc *TidbCluster) {
g.Expect(tc.PDVersion()).To(Equal("v3.1.0"))
},
},
{
name: "don't have tag",
update: func(tc *TidbCluster) {
tc.Spec.PD.Image = "pingcap/pd"
},
expectFn: func(g *GomegaWithT, tc *TidbCluster) {
g.Expect(tc.PDVersion()).To(Equal("latest"))
},
},
}

for i := range tests {
testFn(&tests[i], t)
}
}

func newTidbCluster() *TidbCluster {
return &TidbCluster{
TypeMeta: metav1.TypeMeta{
Expand Down
27 changes: 24 additions & 3 deletions pkg/manager/member/pd_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"strconv"
"strings"

"github.com/Masterminds/semver"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/controller"
"github.com/pingcap/tidb-operator/pkg/label"
Expand Down Expand Up @@ -537,6 +538,11 @@ func getNewPDSetForTidbCluster(tc *v1alpha1.TidbCluster, cm *corev1.ConfigMap) (
pdConfigMap = cm.Name
}

clusterVersionGE4, err := clusterVersionGreaterThanOrEqualTo4(tc.PDVersion())
if err != nil {
klog.Warningf("cluster version: %s is not semantic versioning compatible", tc.PDVersion())
}

annMount, annVolume := annotationsMountVolume()
volMounts := []corev1.VolumeMount{
annMount,
Expand All @@ -549,7 +555,7 @@ func getNewPDSetForTidbCluster(tc *v1alpha1.TidbCluster, cm *corev1.ConfigMap) (
Name: "pd-tls", ReadOnly: true, MountPath: "/var/lib/pd-tls",
})
}
if tc.Spec.TiDB.IsTLSClientEnabled() && !tc.SkipTLSWhenConnectTiDB() {
if tc.Spec.TiDB.IsTLSClientEnabled() && !tc.SkipTLSWhenConnectTiDB() && clusterVersionGE4 {
volMounts = append(volMounts, corev1.VolumeMount{
Name: "tidb-client-tls", ReadOnly: true, MountPath: tidbClientCertPath,
})
Expand Down Expand Up @@ -587,7 +593,7 @@ func getNewPDSetForTidbCluster(tc *v1alpha1.TidbCluster, cm *corev1.ConfigMap) (
},
})
}
if tc.Spec.TiDB.IsTLSClientEnabled() && !tc.SkipTLSWhenConnectTiDB() {
if tc.Spec.TiDB.IsTLSClientEnabled() && !tc.SkipTLSWhenConnectTiDB() && clusterVersionGE4 {
vols = append(vols, corev1.Volume{
Name: "tidb-client-tls", VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
Expand Down Expand Up @@ -724,6 +730,11 @@ func getPDConfigMap(tc *v1alpha1.TidbCluster) (*corev1.ConfigMap, error) {
return nil, nil
}

clusterVersionGE4, err := clusterVersionGreaterThanOrEqualTo4(tc.PDVersion())
if err != nil {
klog.Warningf("cluster version: %s is not semantic versioning compatible", tc.PDVersion())
}

// override CA if tls enabled
if tc.IsTLSClusterEnabled() {
if config.Security == nil {
Expand All @@ -733,7 +744,8 @@ func getPDConfigMap(tc *v1alpha1.TidbCluster) (*corev1.ConfigMap, error) {
config.Security.CertPath = path.Join(pdClusterCertPath, corev1.TLSCertKey)
config.Security.KeyPath = path.Join(pdClusterCertPath, corev1.TLSPrivateKeyKey)
}
if tc.Spec.TiDB.IsTLSClientEnabled() && !tc.SkipTLSWhenConnectTiDB() {
// Versions below v4.0 do not support Dashboard
if tc.Spec.TiDB.IsTLSClientEnabled() && !tc.SkipTLSWhenConnectTiDB() && clusterVersionGE4 {
if config.Dashboard == nil {
config.Dashboard = &v1alpha1.DashboardConfig{}
}
Expand Down Expand Up @@ -773,6 +785,15 @@ func getPDConfigMap(tc *v1alpha1.TidbCluster) (*corev1.ConfigMap, error) {
return cm, nil
}

func clusterVersionGreaterThanOrEqualTo4(version string) (bool, error) {
v, err := semver.NewVersion(version)
if err != nil {
return true, err
}

return v.Major() >= 4, nil
}

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 {
Expand Down
Loading

0 comments on commit 39267f2

Please sign in to comment.