Skip to content

Commit 63a4f19

Browse files
author
Peng Zhou
committed
add TCP implementation
1 parent 71a37d7 commit 63a4f19

File tree

5 files changed

+90
-22
lines changed

5 files changed

+90
-22
lines changed

api/v1/common_types.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ type HAProxy struct {
9999
PathBasedRouting *bool `json:"pathBasedRouting,omitempty"`
100100
Service *corev1.ServiceType `json:"service,omitempty"`
101101
// +kubebuilder:default:={enabled: false}
102-
TcpPorts Tcpports `json:"tcpPorts,omitempty"`
102+
TcpPorts *Tcpports `json:"tcpPorts,omitempty"`
103103
// +kubebuilder:default:={client: 600, connect: 600, server: 600}
104104
Timeout Timeout `json:"timeout,omitempty"`
105105
// +kubebuilder:default:={enabled: false, secretName: "", certFileName: ""}
@@ -138,9 +138,10 @@ type Tcpports struct {
138138
}
139139

140140
type TcpPort struct {
141-
Port int32 `json:"port,omitempty"`
142-
Name string `json:"name,omitempty"`
143-
Type string `json:"type,omitempty"`
141+
Port int32 `json:"port,omitempty"`
142+
TargetPort int32 `json:"targetPort,omitempty"`
143+
Name string `json:"name,omitempty"`
144+
Type string `json:"type,omitempty"`
144145
}
145146

146147
type Timeout struct {

api/v1/zz_generated.deepcopy.go

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/marklogic.progress.com_marklogicclusters.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4544,6 +4544,9 @@ spec:
45444544
port:
45454545
format: int32
45464546
type: integer
4547+
targetPort:
4548+
format: int32
4549+
type: integer
45474550
type:
45484551
type: string
45494552
type: object
@@ -9316,6 +9319,9 @@ spec:
93169319
port:
93179320
format: int32
93189321
type: integer
9322+
targetPort:
9323+
format: int32
9324+
type: integer
93199325
type:
93209326
type: string
93219327
type: object

pkg/k8sutil/haProxy.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,7 @@ resolvers dns
205205
haProxyData["haproxy.cfg"] += generateStatsConfig(cr)
206206
}
207207

208-
if cr.Spec.HAProxy.TcpPorts.Enabled {
209-
haProxyData["haproxy.cfg"] += generateTcpConfig(cr) + "\n"
210-
}
208+
haProxyData["haproxy.cfg"] += generateTcpConfig(cr, haproxyConfig) + "\n"
211209

212210
return haProxyData
213211
}

pkg/k8sutil/haProxyHelper.go

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
type HAProxyTemplate struct {
1313
FrontendName string
1414
BackendName string
15+
TcpName string
1516
TargetPortNumber int
1617
PortNumber int
1718
PortName string
@@ -30,6 +31,7 @@ type HAProxyConfig struct {
3031
IsPathBased bool
3132
FrontEndConfigMap map[string]FrontEndConfig
3233
BackendConfigMap map[string][]BackendConfig
34+
TCPConfigMap map[string][]TCPConfig
3335
}
3436

3537
type FrontEndConfig struct {
@@ -51,10 +53,21 @@ type BackendConfig struct {
5153
Replicas int
5254
}
5355

56+
type TCPConfig struct {
57+
TcpName string
58+
Port int
59+
TargetPort int
60+
PortName string
61+
PodName string
62+
Replicas int
63+
GroupName string
64+
}
65+
5466
func generateHAProxyConfig(cr *marklogicv1.MarklogicCluster) *HAProxyConfig {
5567
config := &HAProxyConfig{}
5668
frontendMap := make(map[string]FrontEndConfig)
5769
backendMap := make(map[string][]BackendConfig)
70+
tcpMap := make(map[string][]TCPConfig)
5871
defaultAppServer := cr.Spec.HAProxy.AppServers
5972
groups := cr.Spec.MarkLogicGroups
6073
config.IsPathBased = *cr.Spec.HAProxy.PathBasedRouting
@@ -67,6 +80,51 @@ func generateHAProxyConfig(cr *marklogicv1.MarklogicCluster) *HAProxyConfig {
6780
config.IsPathBased = true
6881
}
6982
}
83+
// process tcp ports
84+
if cr.Spec.HAProxy.TcpPorts != nil && group.HAProxy.TcpPorts != nil && group.HAProxy.TcpPorts.Enabled {
85+
tcpPorts := cr.Spec.HAProxy.TcpPorts.Ports
86+
if group.HAProxy != nil && group.HAProxy.TcpPorts != nil {
87+
tcpPorts = group.HAProxy.TcpPorts.Ports
88+
}
89+
if len(tcpPorts) == 0 {
90+
tcpPorts = []marklogicv1.TcpPort{}
91+
}
92+
for _, tcpPort := range tcpPorts {
93+
targetPort := int(tcpPort.TargetPort)
94+
if tcpPort.TargetPort == 0 {
95+
targetPort = int(tcpPort.Port)
96+
}
97+
var key string
98+
if int(tcpPort.Port) == targetPort {
99+
key = fmt.Sprintf("%d", tcpPort.Port)
100+
} else {
101+
key = fmt.Sprintf("%d-%d", tcpPort.Port, targetPort)
102+
}
103+
if _, exists := tcpMap[key]; exists {
104+
tcpMap[key] = append(tcpMap[key], TCPConfig{
105+
TcpName: key,
106+
Port: int(tcpPort.Port),
107+
TargetPort: targetPort,
108+
PortName: tcpPort.Name,
109+
PodName: group.Name,
110+
Replicas: int(*group.Replicas),
111+
GroupName: group.Name,
112+
})
113+
} else {
114+
tcpMap[key] = []TCPConfig{{
115+
TcpName: key,
116+
Port: int(tcpPort.Port),
117+
TargetPort: targetPort,
118+
PortName: tcpPort.Name,
119+
PodName: group.Name,
120+
Replicas: int(*group.Replicas),
121+
GroupName: group.Name,
122+
}}
123+
}
124+
}
125+
}
126+
127+
// process http ports with appServers
70128
appServers := group.HAProxy.AppServers
71129
groupPathBased := *cr.Spec.HAProxy.PathBasedRouting
72130
if group.HAProxy.PathBasedRouting != nil {
@@ -121,6 +179,7 @@ func generateHAProxyConfig(cr *marklogicv1.MarklogicCluster) *HAProxyConfig {
121179
}
122180
config.FrontEndConfigMap = frontendMap
123181
config.BackendConfigMap = backendMap
182+
config.TCPConfigMap = tcpMap
124183
return config
125184
}
126185

@@ -293,30 +352,31 @@ frontend stats
293352
}
294353

295354
// generates the tcp config for HAProxy
296-
func generateTcpConfig(cr *marklogicv1.MarklogicCluster) string {
355+
func generateTcpConfig(cr *marklogicv1.MarklogicCluster, config *HAProxyConfig) string {
297356
result := ""
298-
299-
for _, tcpPort := range cr.Spec.HAProxy.TcpPorts.Ports {
357+
tcpConfigs := config.TCPConfigMap
358+
if len(tcpConfigs) == 0 {
359+
return result
360+
}
361+
for _, tcpConfigSlice := range tcpConfigs {
300362
t := `
301-
listen marklogic-TCP-{{.PortNumber}}
363+
listen marklogic-TCP-{{.TcpName }}
302364
bind :{{ .PortNumber }} {{ .SslCert }}
303365
mode tcp
304366
balance leastconn`
305367
data := &HAProxyTemplate{
306-
PortNumber: int(tcpPort.Port),
368+
PortNumber: int(tcpConfigSlice[0].Port),
369+
TcpName: tcpConfigSlice[0].TcpName,
307370
SslCert: getSSLConfig(cr.Spec.HAProxy.Tls),
308371
}
309372
result += parseTemplateToString(t, data)
310-
for _, group := range cr.Spec.MarkLogicGroups {
311-
name := group.Name
312-
groupReplicas := int(*group.Replicas)
313-
if group.HAProxy != nil && !group.HAProxy.Enabled {
314-
continue
315-
}
373+
name := tcpConfigSlice[0].GroupName
374+
groupReplicas := int(tcpConfigSlice[0].Replicas)
375+
for _, tcpConfig := range tcpConfigSlice {
316376
for i := 0; i < groupReplicas; i++ {
317377
data := &HAProxyTemplate{
318-
PortNumber: int(tcpPort.Port),
319-
PodName: name,
378+
PortNumber: int(tcpConfig.TargetPort),
379+
PodName: tcpConfig.PodName,
320380
Index: i,
321381
ServiceName: name,
322382
NSName: cr.ObjectMeta.Namespace,
@@ -326,7 +386,6 @@ listen marklogic-TCP-{{.PortNumber}}
326386
}
327387
}
328388
}
329-
330389
return result
331390
}
332391

0 commit comments

Comments
 (0)