Skip to content

Commit 423621f

Browse files
liubog2008ti-chi-bot
authored andcommitted
This is an automated cherry-pick of #6781
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
1 parent eac867b commit 423621f

34 files changed

+902
-70
lines changed

cmd/discovery/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func main() {
9393
if tlsEnabled == strconv.FormatBool(true) {
9494
tcTls = true
9595
}
96+
discoveryMTLS := os.Getenv("DISCOVERY_MTLS_ENABLED") == strconv.FormatBool(true)
9697
// informers
9798
options := []kubeinformers.SharedInformerOption{
9899
kubeinformers.WithNamespace(os.Getenv("MY_POD_NAMESPACE")),
@@ -113,7 +114,16 @@ func main() {
113114
klog.Infof("starting TiDB Discovery server, listening on %s", addr)
114115
lister := kubeInformerFactory.Core().V1().Secrets().Lister()
115116
discoveryServer := server.NewServer(pdapi.NewDefaultPDControl(lister), dmapi.NewDefaultMasterControl(lister), cli, kubeCli)
116-
discoveryServer.ListenAndServe(addr)
117+
if discoveryMTLS {
118+
klog.Infof("mTLS enabled for discovery server")
119+
discoveryServer.ListenAndServeTLS(addr,
120+
"/var/lib/discovery-tls/tls.crt",
121+
"/var/lib/discovery-tls/tls.key",
122+
"/var/lib/discovery-tls/ca.crt",
123+
)
124+
} else {
125+
discoveryServer.ListenAndServe(addr)
126+
}
117127
}, 5*time.Second)
118128
go wait.Forever(func() {
119129
addr := fmt.Sprintf("0.0.0.0:%d", proxyPort)

docs/api-references/docs.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16908,6 +16908,23 @@ For Client: kubectl create secret generic <clusterName>-cluster-client-secret &n
1690816908
Same for other components.</p>
1690916909
</td>
1691016910
</tr>
16911+
<tr>
16912+
<td>
16913+
<code>enableDiscoveryMTLS</code></br>
16914+
<em>
16915+
bool
16916+
</em>
16917+
</td>
16918+
<td>
16919+
<em>(Optional)</em>
16920+
<p>EnableDiscoveryMTLS indicates whether to enable mutual TLS on the discovery server (port 10261).
16921+
When enabled, the discovery server presents its own certificate, and all components must present
16922+
a client certificate when calling the discovery service.
16923+
A single secret named <clusterName>-discovery-cluster-secret must be created containing ca.crt,
16924+
tls.crt and tls.key, and will be mounted to both the discovery server pod and all component pods.
16925+
This field only takes effect when Enabled is true.</p>
16926+
</td>
16927+
</tr>
1691116928
</tbody>
1691216929
</table>
1691316930
<h3 id="tlsconfig">TLSConfig</h3>

manifests/crd.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18996,6 +18996,8 @@ spec:
1899618996
type: array
1899718997
tlsCluster:
1899818998
properties:
18999+
enableDiscoveryMTLS:
19000+
type: boolean
1899919001
enabled:
1900019002
type: boolean
1900119003
type: object
@@ -51793,6 +51795,8 @@ spec:
5179351795
type: object
5179451796
tlsCluster:
5179551797
properties:
51798+
enableDiscoveryMTLS:
51799+
type: boolean
5179651800
enabled:
5179751801
type: boolean
5179851802
type: object

manifests/crd/v1/pingcap.com_dmclusters.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6199,6 +6199,8 @@ spec:
61996199
type: array
62006200
tlsCluster:
62016201
properties:
6202+
enableDiscoveryMTLS:
6203+
type: boolean
62026204
enabled:
62036205
type: boolean
62046206
type: object

manifests/crd/v1/pingcap.com_tidbclusters.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26322,6 +26322,8 @@ spec:
2632226322
type: object
2632326323
tlsCluster:
2632426324
properties:
26325+
enableDiscoveryMTLS:
26326+
type: boolean
2632526327
enabled:
2632626328
type: boolean
2632726329
type: object

pkg/apis/pingcap/v1alpha1/tidbcluster.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,10 @@ func (tc *TidbCluster) IsTLSClusterEnabled() bool {
10351035
return tc.Spec.TLSCluster != nil && tc.Spec.TLSCluster.Enabled
10361036
}
10371037

1038+
func (tc *TidbCluster) IsDiscoveryMTLSEnabled() bool {
1039+
return tc.IsTLSClusterEnabled() && tc.Spec.TLSCluster.EnableDiscoveryMTLS
1040+
}
1041+
10381042
func (tc *TidbCluster) IsRecoveryMode() bool {
10391043
return tc.Spec.RecoveryMode
10401044
}

pkg/apis/pingcap/v1alpha1/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,6 +1947,15 @@ type TLSCluster struct {
19471947
// Same for other components.
19481948
// +optional
19491949
Enabled bool `json:"enabled,omitempty"`
1950+
1951+
// EnableDiscoveryMTLS indicates whether to enable mutual TLS on the discovery server (port 10261).
1952+
// When enabled, the discovery server presents its own certificate, and all components must present
1953+
// a client certificate when calling the discovery service.
1954+
// A single secret named <clusterName>-discovery-cluster-secret must be created containing ca.crt,
1955+
// tls.crt and tls.key, and will be mounted to both the discovery server pod and all component pods.
1956+
// This field only takes effect when Enabled is true.
1957+
// +optional
1958+
EnableDiscoveryMTLS bool `json:"enableDiscoveryMTLS,omitempty"`
19501959
}
19511960

19521961
// +genclient

pkg/controller/tidbcluster/pod_control_test.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,9 @@ func TestTiKVPodSyncForEviction(t *testing.T) {
9393
})
9494

9595
stop := make(chan struct{})
96-
go func() {
97-
deps.KubeInformerFactory.Start(stop)
98-
}()
96+
deps.KubeInformerFactory.Start(stop)
9997
deps.KubeInformerFactory.WaitForCacheSync(stop)
100-
go func() {
101-
deps.InformerFactory.Start(stop)
102-
}()
98+
deps.InformerFactory.Start(stop)
10399
deps.InformerFactory.WaitForCacheSync(stop)
104100

105101
defer close(stop)
@@ -577,13 +573,9 @@ func TestPDPodSyncForLeaderTransfer(t *testing.T) {
577573
podController.testPDClient = pdClient
578574

579575
stop := make(chan struct{})
580-
go func() {
581-
deps.KubeInformerFactory.Start(stop)
582-
}()
576+
deps.KubeInformerFactory.Start(stop)
583577
deps.KubeInformerFactory.WaitForCacheSync(stop)
584-
go func() {
585-
deps.InformerFactory.Start(stop)
586-
}()
578+
deps.InformerFactory.Start(stop)
587579
deps.InformerFactory.WaitForCacheSync(stop)
588580

589581
defer close(stop)
@@ -753,13 +745,9 @@ func TestTiDBPodSyncForGracefulShutdown(t *testing.T) {
753745
podController := NewPodController(deps)
754746

755747
stop := make(chan struct{})
756-
go func() {
757-
deps.KubeInformerFactory.Start(stop)
758-
}()
748+
deps.KubeInformerFactory.Start(stop)
759749
deps.KubeInformerFactory.WaitForCacheSync(stop)
760-
go func() {
761-
deps.InformerFactory.Start(stop)
762-
}()
750+
deps.InformerFactory.Start(stop)
763751
deps.InformerFactory.WaitForCacheSync(stop)
764752

765753
defer close(stop)

pkg/discovery/server/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ package server
1515

1616
type Server interface {
1717
ListenAndServe(addr string)
18+
ListenAndServeTLS(addr, certFile, keyFile, caFile string)
1819
}

pkg/discovery/server/proxy.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,7 @@ func (p *proxyServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
101101
func (p *proxyServer) ListenAndServe(addr string) {
102102
klog.Fatal(http.ListenAndServe(addr, p))
103103
}
104+
105+
func (p *proxyServer) ListenAndServeTLS(addr, certFile, keyFile, caFile string) {
106+
klog.Fatal("proxy server does not support mTLS")
107+
}

0 commit comments

Comments
 (0)