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

Add readinessProbe config for tidb #3438

Merged
merged 6 commits into from
Oct 30, 2020
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
49 changes: 49 additions & 0 deletions docs/api-references/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -12095,6 +12095,40 @@ string
</tr>
</tbody>
</table>
<h3 id="tidbprobe">TiDBProbe</h3>
<p>
(<em>Appears on:</em>
<a href="#tidbspec">TiDBSpec</a>)
</p>
<p>
<p>TiDBProbe contains details of probing tidb.
default probe by TCPPort on 4000.</p>
</p>
<table>
<thead>
<tr>
<th>Field</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>type</code></br>
<em>
string
</em>
</td>
<td>
<em>(Optional)</em>
<p>&ldquo;tcp&rdquo; will use TCP socket to connetct port 4000</p>
<p>&ldquo;command&rdquo; 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.</p>
</td>
</tr>
</tbody>
</table>
<h3 id="tidbservicespec">TiDBServiceSpec</h3>
<p>
(<em>Appears on:</em>
Expand Down Expand Up @@ -12443,6 +12477,21 @@ events. For the PostStart and PreStop lifecycle handlers, management of the cont
until the action is complete, unless the container process fails, in which case the handler is aborted.</p>
</td>
</tr>
<tr>
<td>
<code>readinessProbe</code></br>
<em>
<a href="#tidbprobe">
TiDBProbe
</a>
</em>
</td>
<td>
<em>(Optional)</em>
<p>ReadinessProbe describes actions that probe the tidb&rsquo;s readiness.
the default behavior is like setting type as &ldquo;tcp&rdquo;</p>
</td>
</tr>
</tbody>
</table>
<h3 id="tidbstatus">TiDBStatus</h3>
Expand Down
32 changes: 0 additions & 32 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 @@ -6510,6 +6510,11 @@ spec:
type: object
priorityClassName:
type: string
readinessProbe:
properties:
type:
type: string
type: object
replicas:
format: int32
type: integer
Expand Down
29 changes: 28 additions & 1 deletion 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 @@ -621,6 +621,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 @@ -695,11 +695,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 @@ -2157,6 +2158,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