Skip to content

Commit 0f0e9c3

Browse files
committed
basic implementation of external opd clusters
Signed-off-by: Yang Keao <yangkeao@chunibyo.icu>
1 parent f1c9aea commit 0f0e9c3

12 files changed

Lines changed: 391 additions & 21 deletions

File tree

api/core/v1alpha1/tiproxy_types.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,37 @@ type TiProxyServer struct {
205205

206206
type TiProxyPorts struct {
207207
// Client defines port for TiProxy's SQL service.
208-
Client *Port `json:"client,omitempty"`
208+
Client *TiProxyPortOrRange `json:"client,omitempty"`
209209
// API defines port for TiProxy API service.
210210
API *Port `json:"api,omitempty"`
211211
// Peer defines port for TiProxy's peer service.
212212
Peer *Port `json:"peer,omitempty"`
213213
}
214214

215+
// TiProxyPortOrRange defines the main SQL port and optional extra listening range.
216+
// The operator continues to use Port for Pod probes and the internal Service.
217+
// Range is only propagated to TiProxy's proxy.port-range config.
218+
// +kubebuilder:validation:XValidation:rule="!has(self.range) || self.range.start <= self.range.end",message="range.start must be less than or equal to range.end"
219+
type TiProxyPortOrRange struct {
220+
// Port defines the main SQL port exposed by the TiProxy Pod and Service.
221+
// +kubebuilder:validation:Minimum=1
222+
// +kubebuilder:validation:Maximum=65535
223+
Port *int32 `json:"port,omitempty"`
224+
225+
// Range defines additional SQL ports listened by TiProxy itself.
226+
Range *TiProxyPortRange `json:"range,omitempty"`
227+
}
228+
229+
type TiProxyPortRange struct {
230+
// +kubebuilder:validation:Minimum=1
231+
// +kubebuilder:validation:Maximum=65535
232+
Start int32 `json:"start"`
233+
234+
// +kubebuilder:validation:Minimum=1
235+
// +kubebuilder:validation:Maximum=65535
236+
End int32 `json:"end"`
237+
}
238+
215239
type TiProxyProbes struct {
216240
// Readiness defines the readiness probe for TiProxy.
217241
// The default handler is a TCP socket on the client port.

api/core/v1alpha1/zz_generated.deepcopy.go

Lines changed: 44 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

manifests/crd/core.pingcap.com_tiproxies.yaml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8538,11 +8538,34 @@ spec:
85388538
description: Client defines port for TiProxy's SQL service.
85398539
properties:
85408540
port:
8541+
description: Port defines the main SQL port exposed by
8542+
the TiProxy Pod and Service.
85418543
format: int32
8544+
maximum: 65535
8545+
minimum: 1
85428546
type: integer
8543-
required:
8544-
- port
8547+
range:
8548+
description: Range defines additional SQL ports listened
8549+
by TiProxy itself.
8550+
properties:
8551+
end:
8552+
format: int32
8553+
maximum: 65535
8554+
minimum: 1
8555+
type: integer
8556+
start:
8557+
format: int32
8558+
maximum: 65535
8559+
minimum: 1
8560+
type: integer
8561+
required:
8562+
- end
8563+
- start
8564+
type: object
85458565
type: object
8566+
x-kubernetes-validations:
8567+
- message: range.start must be less than or equal to range.end
8568+
rule: '!has(self.range) || self.range.start <= self.range.end'
85468569
peer:
85478570
description: Peer defines port for TiProxy's peer service.
85488571
properties:

manifests/crd/core.pingcap.com_tiproxygroups.yaml

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8836,11 +8836,35 @@ spec:
88368836
service.
88378837
properties:
88388838
port:
8839+
description: Port defines the main SQL port exposed
8840+
by the TiProxy Pod and Service.
88398841
format: int32
8842+
maximum: 65535
8843+
minimum: 1
88408844
type: integer
8841-
required:
8842-
- port
8845+
range:
8846+
description: Range defines additional SQL ports
8847+
listened by TiProxy itself.
8848+
properties:
8849+
end:
8850+
format: int32
8851+
maximum: 65535
8852+
minimum: 1
8853+
type: integer
8854+
start:
8855+
format: int32
8856+
maximum: 65535
8857+
minimum: 1
8858+
type: integer
8859+
required:
8860+
- end
8861+
- start
8862+
type: object
88438863
type: object
8864+
x-kubernetes-validations:
8865+
- message: range.start must be less than or equal
8866+
to range.end
8867+
rule: '!has(self.range) || self.range.start <= self.range.end'
88448868
peer:
88458869
description: Peer defines port for TiProxy's peer
88468870
service.

pkg/apiutil/core/v1alpha1/tiproxy.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323

2424
func TiProxyGroupClientPort(proxyg *v1alpha1.TiProxyGroup) int32 {
2525
if proxyg.Spec.Template.Spec.Server.Ports.Client != nil {
26-
return proxyg.Spec.Template.Spec.Server.Ports.Client.Port
26+
if proxyg.Spec.Template.Spec.Server.Ports.Client.Port != nil {
27+
return *proxyg.Spec.Template.Spec.Server.Ports.Client.Port
28+
}
2729
}
2830
return v1alpha1.DefaultTiProxyPortClient
2931
}
@@ -44,11 +46,23 @@ func TiProxyGroupPeerPort(proxyg *v1alpha1.TiProxyGroup) int32 {
4446

4547
func TiProxyClientPort(tiproxy *v1alpha1.TiProxy) int32 {
4648
if tiproxy.Spec.Server.Ports.Client != nil {
47-
return tiproxy.Spec.Server.Ports.Client.Port
49+
if tiproxy.Spec.Server.Ports.Client.Port != nil {
50+
return *tiproxy.Spec.Server.Ports.Client.Port
51+
}
4852
}
4953
return v1alpha1.DefaultTiProxyPortClient
5054
}
5155

56+
func TiProxyClientPortRange(tiproxy *v1alpha1.TiProxy) []int {
57+
if tiproxy.Spec.Server.Ports.Client == nil || tiproxy.Spec.Server.Ports.Client.Range == nil {
58+
return nil
59+
}
60+
return []int{
61+
int(tiproxy.Spec.Server.Ports.Client.Range.Start),
62+
int(tiproxy.Spec.Server.Ports.Client.Range.End),
63+
}
64+
}
65+
5266
func TiProxyAPIPort(tiproxy *v1alpha1.TiProxy) int32 {
5367
if tiproxy.Spec.Server.Ports.API != nil {
5468
return tiproxy.Spec.Server.Ports.API.Port
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2024 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package coreutil
16+
17+
import (
18+
"testing"
19+
20+
"github.com/stretchr/testify/require"
21+
"k8s.io/utils/ptr"
22+
23+
"github.com/pingcap/tidb-operator/api/v2/core/v1alpha1"
24+
)
25+
26+
func TestTiProxyClientPort(t *testing.T) {
27+
tiproxy := &v1alpha1.TiProxy{}
28+
require.Equal(t, int32(v1alpha1.DefaultTiProxyPortClient), TiProxyClientPort(tiproxy))
29+
30+
tiproxy.Spec.Server.Ports.Client = &v1alpha1.TiProxyPortOrRange{
31+
Range: &v1alpha1.TiProxyPortRange{
32+
Start: 10000,
33+
End: 10002,
34+
},
35+
}
36+
require.Equal(t, int32(v1alpha1.DefaultTiProxyPortClient), TiProxyClientPort(tiproxy))
37+
require.Equal(t, []int{10000, 10002}, TiProxyClientPortRange(tiproxy))
38+
39+
tiproxy.Spec.Server.Ports.Client.Port = ptr.To[int32](7000)
40+
require.Equal(t, int32(7000), TiProxyClientPort(tiproxy))
41+
}
42+
43+
func TestTiProxyGroupClientPort(t *testing.T) {
44+
proxyg := &v1alpha1.TiProxyGroup{}
45+
require.Equal(t, int32(v1alpha1.DefaultTiProxyPortClient), TiProxyGroupClientPort(proxyg))
46+
47+
proxyg.Spec.Template.Spec.Server.Ports.Client = &v1alpha1.TiProxyPortOrRange{
48+
Range: &v1alpha1.TiProxyPortRange{
49+
Start: 10000,
50+
End: 10002,
51+
},
52+
}
53+
require.Equal(t, int32(v1alpha1.DefaultTiProxyPortClient), TiProxyGroupClientPort(proxyg))
54+
55+
proxyg.Spec.Template.Spec.Server.Ports.Client.Port = ptr.To[int32](7000)
56+
require.Equal(t, int32(7000), TiProxyGroupClientPort(proxyg))
57+
}

pkg/configs/tiproxy/config.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ import (
2727
)
2828

2929
type Proxy struct {
30-
Address string `toml:"addr"`
31-
AdvertiseAddress string `toml:"advertise-addr"`
32-
PDAddress string `toml:"pd-addrs"`
30+
Address string `toml:"addr,omitempty" json:"addr,omitempty"`
31+
AdvertiseAddress string `toml:"advertise-addr,omitempty" json:"advertise-addr,omitempty"`
32+
PDAddress string `toml:"pd-addrs,omitempty" json:"pd-addrs,omitempty"`
33+
PortRange []int `toml:"port-range,omitempty" json:"port-range,omitempty"`
34+
BackendClusters []BackendCluster `toml:"backend-clusters,omitempty" json:"backend-clusters,omitempty"`
3335
}
3436

3537
type API struct {
36-
Address string `toml:"addr"`
38+
Address string `toml:"addr,omitempty" json:"addr,omitempty"`
3739
}
3840

3941
type TLSConfig struct {
@@ -54,6 +56,12 @@ type Security struct {
5456
SQLTLS TLSConfig `toml:"sql-tls,omitempty"`
5557
}
5658

59+
type BackendCluster struct {
60+
Name string `toml:"name,omitempty" json:"name,omitempty"`
61+
PDAddrs string `toml:"pd-addrs,omitempty" json:"pd-addrs,omitempty"`
62+
NSServers []string `toml:"ns-servers,omitempty" json:"ns-servers,omitempty"`
63+
}
64+
5765
// Config is a subset config of TiProxy.
5866
// Only TiDB Operator managed fields are defined here.
5967
// ref: https://docs.pingcap.com/tidb/stable/tiproxy-configuration/
@@ -73,6 +81,7 @@ func (c *Config) Overlay(cluster *v1alpha1.Cluster, tiproxy *v1alpha1.TiProxy) e
7381
c.Proxy.Address = coreutil.ListenAddress(coreutil.TiProxyClientPort(tiproxy))
7482
c.Proxy.AdvertiseAddress = getAdvertiseAddress(cluster, tiproxy)
7583
c.Proxy.PDAddress = stringutil.RemoveHTTPPrefix(cluster.Status.PD)
84+
c.Proxy.PortRange = coreutil.TiProxyClientPortRange(tiproxy)
7685
c.API.Address = coreutil.ListenAddress(coreutil.TiProxyAPIPort(tiproxy))
7786

7887
if coreutil.IsTLSClusterEnabled(cluster) {
@@ -128,6 +137,10 @@ func (c *Config) Validate() error {
128137
fields = append(fields, "proxy.pd-address")
129138
}
130139

140+
if len(c.Proxy.PortRange) > 0 {
141+
fields = append(fields, "proxy.port-range")
142+
}
143+
131144
if c.API.Address != "" {
132145
fields = append(fields, "api.address")
133146
}

0 commit comments

Comments
 (0)