Skip to content

Commit

Permalink
cherry pick pingcap#3438 to release-1.1
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
july2993 authored and ti-srebot committed Oct 30, 2020
1 parent b1d0b7c commit 6b4eac5
Show file tree
Hide file tree
Showing 8 changed files with 1,208 additions and 36 deletions.
1,003 changes: 1,003 additions & 0 deletions docs/api-references/docs.md

Large diffs are not rendered by default.

31 changes: 0 additions & 31 deletions go.sum

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions manifests/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6506,6 +6506,11 @@ spec:
type: object
priorityClassName:
type: string
readinessProbe:
properties:
type:
type: string
type: object
replicas:
format: int32
type: integer
Expand Down
36 changes: 36 additions & 0 deletions pkg/apis/pingcap/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions pkg/apis/pingcap/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,29 @@ type TiDBSpec struct {
// until the action is complete, unless the container process fails, in which case the handler is aborted.
// +optional
Lifecycle *corev1.Lifecycle `json:"lifecycle,omitempty"`
// ReadinessProbe describes actions that probe the tidb's readiness.
// the default behavior is like setting type as "tcp"
// +optional
ReadinessProbe *TiDBProbe `json:"readinessProbe,omitempty"`
}

const (
TCPProbeType string = "tcp"
CommandProbeType string = "command"
)

// +k8s:openapi-gen=true
// TiDBProbe contains details of probing tidb.
// default probe by TCPPort on 4000.
type TiDBProbe struct {
// "tcp" will use TCP socket to connetct port 4000
//
// "command" will probe the status api of tidb.
// This will use curl command to request tidb, before v4.0.9 there is no curl in the image,
// So do not use this before v4.0.9.
// +kubebuilder:validation:Enum=tcp,command
// +optional
Type *string `json:"type,omitempty"` // tcp or command
}

// +k8s:openapi-gen=true
Expand Down
26 changes: 26 additions & 0 deletions pkg/apis/pingcap/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 48 additions & 5 deletions pkg/manager/member/tidb_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,11 +694,7 @@ func getNewTiDBSetForTidbCluster(tc *v1alpha1.TidbCluster, cm *corev1.ConfigMap)
Resources: controller.ContainerResource(tc.Spec.TiDB.ResourceRequirements),
Env: util.AppendEnv(envs, baseTiDBSpec.Env()),
ReadinessProbe: &corev1.Probe{
Handler: corev1.Handler{
TCPSocket: &corev1.TCPSocketAction{
Port: intstr.FromInt(4000),
},
},
Handler: buildTiDBReadinessProbHandler(tc),
InitialDelaySeconds: int32(10),
},
}
Expand Down Expand Up @@ -851,6 +847,53 @@ func tidbStatefulSetIsUpgrading(podLister corelisters.PodLister, set *apps.State
return false, nil
}

func buildTiDBReadinessProbHandler(tc *v1alpha1.TidbCluster) corev1.Handler {
if tc.Spec.TiDB.ReadinessProbe != nil {
if tp := tc.Spec.TiDB.ReadinessProbe.Type; tp != nil {
if *tp == v1alpha1.CommandProbeType {
command := buildTiDBProbeCommand(tc)
return corev1.Handler{
Exec: &corev1.ExecAction{
Command: command,
},
}
}
}
}

// fall to default case v1alpha1.TCPProbeType
return corev1.Handler{
TCPSocket: &corev1.TCPSocketAction{
Port: intstr.FromInt(4000),
},
}
}

func buildTiDBProbeCommand(tc *v1alpha1.TidbCluster) (command []string) {
host := "127.0.0.1"

readinessURL := fmt.Sprintf("%s://%s:10080/status", tc.Scheme(), host)
command = append(command, "curl")
command = append(command, readinessURL)

// Fail silently (no output at all) on server errors
// without this if the server return 500, the exist code will be 0
// and probe is success.
command = append(command, "--fail")
// follow 301 or 302 redirect
command = append(command, "--location")

if tc.IsTLSClusterEnabled() {
cacert := path.Join(clusterCertPath, tlsSecretRootCAKey)
cert := path.Join(clusterCertPath, corev1.TLSCertKey)
key := path.Join(clusterCertPath, corev1.TLSPrivateKeyKey)
command = append(command, "--cacert", cacert)
command = append(command, "--cert", cert)
command = append(command, "--key", key)
}
return
}

func tlsClientSecretName(tc *v1alpha1.TidbCluster) string {
return fmt.Sprintf("%s-server-secret", controller.TiDBMemberName(tc.Name))
}
Expand Down
67 changes: 67 additions & 0 deletions pkg/manager/member/tidb_member_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package member
import (
"context"
"fmt"
"path"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -2173,6 +2174,72 @@ func TestTiDBShouldRecover(t *testing.T) {
}
}

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

defaultHandler := corev1.Handler{
TCPSocket: &corev1.TCPSocketAction{
Port: intstr.FromInt(4000),
},
}

execHandler := corev1.Handler{
Exec: &corev1.ExecAction{
Command: []string{
"curl",
"http://127.0.0.1:10080/status",
"--fail",
"--location",
},
},
}

sslExecHandler := corev1.Handler{
Exec: &corev1.ExecAction{
Command: []string{
"curl",
"https://127.0.0.1:10080/status",
"--fail",
"--location",
"--cacert", path.Join(clusterCertPath, tlsSecretRootCAKey),
"--cert", path.Join(clusterCertPath, corev1.TLSCertKey),
"--key", path.Join(clusterCertPath, corev1.TLSPrivateKeyKey),
},
},
}

tc := &v1alpha1.TidbCluster{
Spec: v1alpha1.TidbClusterSpec{
TiDB: &v1alpha1.TiDBSpec{},
},
}

// test default
get := buildTiDBReadinessProbHandler(tc)
g.Expect(get).Should(Equal(defaultHandler))

// test set command type & not tls
tc.Spec.TiDB.ReadinessProbe = &v1alpha1.TiDBProbe{
Type: pointer.StringPtr(v1alpha1.CommandProbeType),
}
get = buildTiDBReadinessProbHandler(tc)
g.Expect(get).Should(Equal(execHandler))

// test command type and tls
tc.Spec.TLSCluster = &v1alpha1.TLSCluster{
Enabled: true,
}
get = buildTiDBReadinessProbHandler(tc)
g.Expect(get).Should(Equal(sslExecHandler))

// test tcp type
tc.Spec.TiDB.ReadinessProbe = &v1alpha1.TiDBProbe{
Type: pointer.StringPtr(v1alpha1.TCPProbeType),
}
get = buildTiDBReadinessProbHandler(tc)
g.Expect(get).Should(Equal(defaultHandler))
}

func mustConfig(x interface{}) *v1alpha1.TiDBConfigWraper {
data, err := MarshalTOML(x)
if err != nil {
Expand Down

0 comments on commit 6b4eac5

Please sign in to comment.