Skip to content

Commit e3a0292

Browse files
author
Peng Zhou
committed
support for pathbased routing
1 parent 519579a commit e3a0292

File tree

1 file changed

+55
-34
lines changed

1 file changed

+55
-34
lines changed

pkg/k8sutil/haProxyHelper.go

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package k8sutil
33
import (
44
"bytes"
55
"fmt"
6+
"strings"
67
"text/template"
78

89
marklogicv1 "github.com/marklogic/marklogic-operator-kubernetes/api/v1"
@@ -25,6 +26,7 @@ type HAProxyTemplate struct {
2526
}
2627

2728
type HAProxyConfig struct {
29+
IsPathBased bool
2830
FrontEndConfigMap map[string]FrontEndConfig
2931
BackendConfigMap map[string][]BackendConfig
3032
}
@@ -39,6 +41,7 @@ type FrontEndConfig struct {
3941
}
4042

4143
type BackendConfig struct {
44+
IsPathBased bool
4245
BackendName string
4346
GroupName string
4447
Port int
@@ -53,11 +56,23 @@ func generateHAProxyConfig(cr *marklogicv1.MarklogicCluster) *HAProxyConfig {
5356
backendMap := make(map[string][]BackendConfig)
5457
defaultAppServer := cr.Spec.HAProxy.AppServers
5558
groups := cr.Spec.MarkLogicGroups
59+
config.IsPathBased = *cr.Spec.HAProxy.PathBasedRouting
5660
for _, group := range groups {
5761
if group.HAProxy != nil && !group.HAProxy.Enabled {
5862
continue
63+
} else {
64+
// setting is path-based
65+
if !config.IsPathBased && *group.HAProxy.PathBasedRouting == true {
66+
config.IsPathBased = true
67+
}
5968
}
6069
appServers := group.HAProxy.AppServers
70+
var groupPathBased bool
71+
if group.HAProxy.PathBasedRouting != nil {
72+
groupPathBased = *group.HAProxy.PathBasedRouting
73+
} else {
74+
groupPathBased = *cr.Spec.HAProxy.PathBasedRouting
75+
}
6176
if len(appServers) == 0 {
6277
appServers = defaultAppServer
6378
}
@@ -67,24 +82,31 @@ func generateHAProxyConfig(cr *marklogicv1.MarklogicCluster) *HAProxyConfig {
6782
targetPort = int(appServer.Port)
6883
}
6984
var key string
70-
if int(appServer.Port) == targetPort {
71-
key = fmt.Sprintf("%d", appServer.Port)
85+
if groupPathBased {
86+
if int(appServer.Port) == targetPort {
87+
key = fmt.Sprintf("%d", appServer.Port)
88+
} else {
89+
key = fmt.Sprintf("%d-%d", appServer.Port, targetPort)
90+
}
7291
} else {
73-
key = fmt.Sprintf("%d-%d", appServer.Port, targetPort)
92+
pathWithoutSlashes := strings.ReplaceAll(appServer.Path, "/", "")
93+
key = fmt.Sprintf("%d-%s-path", appServer.Port, pathWithoutSlashes)
7494
}
7595

76-
frontendName := "marklogic-" + key + "-frontend"
7796
backendName := "marklogic-" + key + "-backend"
78-
79-
if _, exists := frontendMap[key]; !exists {
80-
frontend := FrontEndConfig{
81-
FrontendName: frontendName,
82-
PathBasedRouting: *cr.Spec.HAProxy.PathBasedRouting,
83-
Port: int(appServer.Port),
84-
TargetPort: targetPort,
85-
BackendName: backendName,
97+
// only add frontend when pathBasedRouting is set to false for the group
98+
if !groupPathBased {
99+
frontendName := "marklogic-" + key + "-frontend"
100+
if _, exists := frontendMap[key]; !exists {
101+
frontend := FrontEndConfig{
102+
FrontendName: frontendName,
103+
PathBasedRouting: *cr.Spec.HAProxy.PathBasedRouting,
104+
Port: int(appServer.Port),
105+
TargetPort: targetPort,
106+
BackendName: backendName,
107+
}
108+
frontendMap[key] = frontend
86109
}
87-
frontendMap[key] = frontend
88110
}
89111
backend := BackendConfig{
90112
BackendName: backendName,
@@ -93,6 +115,7 @@ func generateHAProxyConfig(cr *marklogicv1.MarklogicCluster) *HAProxyConfig {
93115
TargetPort: targetPort,
94116
Path: appServer.Path,
95117
Replicas: int(*group.Replicas),
118+
IsPathBased: groupPathBased,
96119
}
97120
backendMap[key] = append(backendMap[key], backend)
98121
}
@@ -110,9 +133,7 @@ func generateFrontendConfig(cr *marklogicv1.MarklogicCluster, config *HAProxyCon
110133
var frontEndDef string
111134
var data *HAProxyTemplate
112135
var result string
113-
pathBasedRouting := cr.Spec.HAProxy.PathBasedRouting
114-
appServers := cr.Spec.HAProxy.AppServers
115-
if *pathBasedRouting {
136+
if config.IsPathBased {
116137
// front end configuration for path based routing
117138
frontEndDef = `
118139
frontend marklogic-pathbased-frontend
@@ -126,32 +147,32 @@ frontend marklogic-pathbased-frontend
126147
SslCert: getSSLConfig(cr.Spec.HAProxy.Tls),
127148
}
128149
result = parseTemplateToString(frontEndDef, data)
129-
for _, appServer := range appServers {
150+
for _, backends := range config.BackendConfigMap {
130151
data = &HAProxyTemplate{
131-
PortNumber: int(appServer.Port),
132-
TargetPortNumber: int(appServer.TargetPort),
133-
Path: appServer.Path,
152+
PortNumber: int(backends[0].Port),
153+
TargetPortNumber: int(backends[0].TargetPort),
154+
Path: backends[0].Path,
155+
BackendName: backends[0].BackendName,
134156
}
135157
result += getFrontendForPathbased(data)
136158
}
137-
} else {
138-
// front end configuration for non-path based routing
139-
frontEndDef = `
159+
}
160+
// front end configuration for non-path based routing
161+
frontEndDef = `
140162
frontend {{ .FrontendName }}
141163
mode http
142164
bind :{{ .PortNumber }} {{ .SslCert }}
143165
log-format "%ci:%cp [%tr] %ft %b/%s %TR/%Tw/%Tc/%Tr/%Ta %ST %B %CC %CS %tsc %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"
144166
default_backend {{ .BackendName }}`
145-
for _, frontend := range frontEndConfigs {
146-
data = &HAProxyTemplate{
147-
FrontendName: frontend.FrontendName,
148-
BackendName: frontend.BackendName,
149-
PortNumber: int(frontend.Port),
150-
TargetPortNumber: int(frontend.TargetPort),
151-
SslCert: getSSLConfig(cr.Spec.HAProxy.Tls),
152-
}
153-
result += parseTemplateToString(frontEndDef, data) + "\n"
167+
for _, frontend := range frontEndConfigs {
168+
data = &HAProxyTemplate{
169+
FrontendName: frontend.FrontendName,
170+
BackendName: frontend.BackendName,
171+
PortNumber: int(frontend.Port),
172+
TargetPortNumber: int(frontend.TargetPort),
173+
SslCert: getSSLConfig(cr.Spec.HAProxy.Tls),
154174
}
175+
result += parseTemplateToString(frontEndDef, data) + "\n"
155176
}
156177
return result
157178
}
@@ -191,7 +212,7 @@ backend {{ .BackendName }}
191212
groupReplicas := backend.Replicas
192213
for i := 0; i < groupReplicas; i++ {
193214
data := &HAProxyTemplate{
194-
PortNumber: backend.Port,
215+
PortNumber: backend.TargetPort,
195216
PodName: name,
196217
Path: backend.Path,
197218
Index: i,
@@ -221,7 +242,7 @@ func getBackendServerConfigs(data *HAProxyTemplate) string {
221242

222243
func getFrontendForPathbased(data *HAProxyTemplate) string {
223244
frontend := `
224-
use_backend marklogic-{{.PortNumber}}-backend if { path {{.Path}} } || { path_beg {{.Path}}/ }`
245+
use_backend {{.BackendName}} if { path {{.Path}} } || { path_beg {{.Path}}/ }`
225246
if data.PortNumber == 8000 || data.TargetPortNumber == 8000 {
226247
frontend += `
227248
http-request set-header X-ML-QC-Path {{.Path}}`

0 commit comments

Comments
 (0)