Skip to content

Commit

Permalink
support starting tidb-server with -advertise-address parameter (#1859
Browse files Browse the repository at this point in the history
…) (#1868)

* start tidb-server with

* add EnableAdvertiseAddress switch

* fix indent

* address comments

Co-authored-by: Louis <liuzhi@pingcap.com>
  • Loading branch information
cofyc and LinuxGit authored Mar 5, 2020
1 parent bf883e2 commit be5c05b
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 15 deletions.
5 changes: 5 additions & 0 deletions charts/tidb-cluster/templates/scripts/_start_tidb.sh.tpl
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ then
tail -f /dev/null
fi

# Use HOSTNAME if POD_NAME is unset for backward compatibility.
POD_NAME=${POD_NAME:-$HOSTNAME}
ARGS="--store=tikv \
{{- if .Values.tidb.enableAdvertiseAddress | default false }}
--advertise-address=${POD_NAME}.${HEADLESS_SERVICE_NAME}.${NAMESPACE}.svc \
{{- end }}
--host=0.0.0.0 \
--path=${CLUSTER_NAME}-pd:2379 \
--config=/etc/tidb/tidb.toml
Expand Down
4 changes: 4 additions & 0 deletions charts/tidb-cluster/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,10 @@ tidb:
# annotations:
# cloud.google.com/load-balancer-type: Internal
separateSlowLog: true

# Add --advertise-address to TiDB's startup parameters
enableAdvertiseAddress: false

slowLogTailer:
image: busybox:1.26.2
resources:
Expand Down
4 changes: 4 additions & 0 deletions manifests/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3782,6 +3782,10 @@ spec:
cluster-level updateStrategy if present Optional: Defaults to
cluster-level setting'
type: string
enableAdvertiseAddress:
description: 'Add --advertise-address to TiDB''s startup parameters
Optional: Defaults to false'
type: boolean
hostNetwork:
description: 'Whether Hostnetwork of the component is enabled. Override
the cluster-level setting if present Optional: Defaults to cluster-level
Expand Down
7 changes: 7 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.

22 changes: 15 additions & 7 deletions pkg/apis/pingcap/v1alpha1/tidbcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import (

const (
// defaultHelperImage is default image of helper
defaultHelperImage = "busybox:1.26.2"
defaultTimeZone = "UTC"
defaultEnableTLSCluster = false
defaultEnableTLSClient = false
defaultExposeStatus = true
defaultSeparateSlowLog = true
defaultEnablePVReclaim = false
defaultHelperImage = "busybox:1.26.2"
defaultTimeZone = "UTC"
defaultEnableTLSCluster = false
defaultEnableTLSClient = false
defaultExposeStatus = true
defaultSeparateSlowLog = true
defaultEnablePVReclaim = false
defaultEnableTiDBAdvertiseAddress = false
)

var (
Expand Down Expand Up @@ -342,6 +343,13 @@ func (tidb *TiDBSpec) IsTLSClientEnabled() bool {
return tidb.TLSClient != nil && tidb.TLSClient.Enabled
}

func (tidb *TiDBSpec) IsAdvertiseAddressEnabled() bool {
if tidb.EnableAdvertiseAddress == nil {
return defaultEnableTiDBAdvertiseAddress
}
return *tidb.EnableAdvertiseAddress
}

func (tidb *TiDBSpec) IsUserGeneratedCertificate() bool {
return tidb.IsTLSClientEnabled() && tidb.TLSClient.SecretName != ""
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/pingcap/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,11 @@ type TiDBSpec struct {
// +optional
BinlogEnabled *bool `json:"binlogEnabled,omitempty"`

// Add --advertise-address to TiDB's startup parameters
// Optional: Defaults to false
// +optional
EnableAdvertiseAddress *bool `json:"enableAdvertiseAddress,omitempty"`

// MaxFailoverCount limit the max replicas could be added in failover, 0 means unlimited
// Optional: Defaults to 0
// +kubebuilder:validation:Minimum=0
Expand Down
5 changes: 5 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.

14 changes: 10 additions & 4 deletions pkg/manager/member/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ then
tail -f /dev/null
fi
# Use HOSTNAME if POD_NAME is unset for backward compatibility.
POD_NAME=${POD_NAME:-$HOSTNAME}
ARGS="--store=tikv \
{{- if .EnableAdvertiseAddress }}
--advertise-address=${POD_NAME}.${HEADLESS_SERVICE_NAME}.${NAMESPACE}.svc \
{{- end }}
--host=0.0.0.0 \
--path=${CLUSTER_NAME}-pd:2379 \
--config=/etc/tidb/tidb.toml
Expand All @@ -74,10 +79,11 @@ exec /tidb-server ${ARGS}
`))

type TidbStartScriptModel struct {
ClusterName string
EnablePlugin bool
PluginDirectory string
PluginList string
ClusterName string
EnableAdvertiseAddress bool
EnablePlugin bool
PluginDirectory string
PluginList string
}

func RenderTiDBStartScript(model *TidbStartScriptModel) (string, error) {
Expand Down
37 changes: 33 additions & 4 deletions pkg/manager/member/tidb_member_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,11 @@ func getTiDBConfigMap(tc *v1alpha1.TidbCluster) (*corev1.ConfigMap, error) {

plugins := tc.Spec.TiDB.Plugins
startScript, err := RenderTiDBStartScript(&TidbStartScriptModel{
ClusterName: tc.Name,
EnablePlugin: len(plugins) > 0,
PluginDirectory: "/plugins",
PluginList: strings.Join(plugins, ","),
ClusterName: tc.Name,
EnableAdvertiseAddress: tc.Spec.TiDB.IsAdvertiseAddressEnabled(),
EnablePlugin: len(plugins) > 0,
PluginDirectory: "/plugins",
PluginList: strings.Join(plugins, ","),
})
if err != nil {
return nil, err
Expand Down Expand Up @@ -580,6 +581,7 @@ func getNewTiDBHeadlessServiceForTidbCluster(tc *v1alpha1.TidbCluster) *corev1.S
func getNewTiDBSetForTidbCluster(tc *v1alpha1.TidbCluster, cm *corev1.ConfigMap) *apps.StatefulSet {
ns := tc.GetNamespace()
tcName := tc.GetName()
headlessSvcName := controller.TiDBPeerMemberName(tcName)
baseTiDBSpec := tc.BaseTiDBSpec()
instanceName := tc.GetInstanceName()
tidbConfigMap := controller.MemberConfigMapName(tc, v1alpha1.TiDBMemberType)
Expand Down Expand Up @@ -730,10 +732,37 @@ func getNewTiDBSetForTidbCluster(tc *v1alpha1.TidbCluster, cm *corev1.ConfigMap)
},
}

if tc.Spec.TiDB.IsAdvertiseAddressEnabled() {
advertiseEnvs := []corev1.EnvVar{
{
Name: "POD_NAME",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.name",
},
},
},
{
Name: "NAMESPACE",
ValueFrom: &corev1.EnvVarSource{
FieldRef: &corev1.ObjectFieldSelector{
FieldPath: "metadata.namespace",
},
},
},
{
Name: "HEADLESS_SERVICE_NAME",
Value: headlessSvcName,
},
}
envs = append(envs, advertiseEnvs...)
}

scheme := corev1.URISchemeHTTP
if tc.IsTLSClusterEnabled() {
scheme = corev1.URISchemeHTTPS
}

containers = append(containers, corev1.Container{
Name: v1alpha1.TiDBMemberType.String(),
Image: tc.TiDBImage(),
Expand Down

0 comments on commit be5c05b

Please sign in to comment.