Skip to content

Commit df2958c

Browse files
pengzhoumlPeng Zhou
and
Peng Zhou
authored
MLE-19786 Fix Bug for HAProxy Service port is not updated for non pathbased routing (#58)
* MLE-19786 refactor haproxy service for update * remove hard coded ports * update port for stat dynamically --------- Co-authored-by: Peng Zhou <[email protected]>
1 parent 62fa922 commit df2958c

File tree

1 file changed

+65
-23
lines changed

1 file changed

+65
-23
lines changed

pkg/k8sutil/haProxy.go

+65-23
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ func (cc *ClusterContext) ReconcileHAProxy() result.ReconcileResult {
3131
configMapName := "marklogic-haproxy"
3232
objectMeta := generateObjectMeta(configMapName, cr.Namespace, labels, annotations)
3333
nsName := types.NamespacedName{Name: objectMeta.Name, Namespace: objectMeta.Namespace}
34+
svcName := types.NamespacedName{Name: "marklogic-haproxy", Namespace: cr.Namespace}
3435
configmap := &corev1.ConfigMap{}
36+
haproxyService := &corev1.Service{}
3537
err := client.Get(cc.Ctx, nsName, configmap)
3638
data := generateHAProxyConfigMapData(cc.MarklogicCluster)
3739
configMapDef := generateHAProxyConfigMap(objectMeta, marklogicClusterAsOwner(cr), data)
40+
haproxyServiceDef := cc.generateHaproxyServiceDef()
3841
configmapHash := calculateHash(configMapDef.Data)
3942
if err != nil {
4043
if errors.IsNotFound(err) {
@@ -50,8 +53,7 @@ func (cc *ClusterContext) ReconcileHAProxy() result.ReconcileResult {
5053
logger.Info("HAProxy Deployment creation is failed")
5154
return result.Error(err)
5255
}
53-
// createHAProxyService(service corev1.Service)
54-
err = cc.createHAProxyService()
56+
err = cc.createHAProxyService(haproxyServiceDef)
5557
if err != nil {
5658
logger.Info("HAProxy Service creation is failed")
5759
return result.Error(err)
@@ -67,9 +69,8 @@ func (cc *ClusterContext) ReconcileHAProxy() result.ReconcileResult {
6769
patch.IgnoreStatusFields(),
6870
patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus(),
6971
patch.IgnoreField("kind"))
70-
7172
if err != nil {
72-
logger.Error(err, "Error calculating patch")
73+
logger.Error(err, "Error calculating patch for HAProxy configmap")
7374
return result.Error(err)
7475
}
7576
if !patchDiff.IsEmpty() {
@@ -82,6 +83,30 @@ func (cc *ClusterContext) ReconcileHAProxy() result.ReconcileResult {
8283
return result.Error(err)
8384
}
8485
}
86+
err = client.Get(cc.Ctx, svcName, haproxyService)
87+
if err != nil {
88+
logger.Error(err, "Failed to get HAProxy service")
89+
return result.Error(err)
90+
}
91+
patchDiff, err = patch.DefaultPatchMaker.Calculate(haproxyService, haproxyServiceDef,
92+
patch.IgnoreStatusFields(),
93+
patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus(),
94+
patch.IgnoreField("kind"))
95+
if err != nil {
96+
logger.Error(err, "Error calculating patch for HAProxy service")
97+
return result.Error(err)
98+
}
99+
if !patchDiff.IsEmpty() {
100+
logger.Info("HAProxy Service spec is different from the MarkLogicGroup spec, updating the haproxy service")
101+
logger.Info(patchDiff.String())
102+
haproxyService.Spec = haproxyServiceDef.Spec
103+
err := cc.Client.Update(cc.Ctx, haproxyService)
104+
if err != nil {
105+
logger.Error(err, "Error updating HAProxy service")
106+
return result.Error(err)
107+
}
108+
}
109+
85110
haproxyDeployment := &appsv1.Deployment{}
86111
err = client.Get(cc.Ctx, types.NamespacedName{Name: "marklogic-haproxy", Namespace: cr.Namespace}, haproxyDeployment)
87112
if err != nil {
@@ -272,19 +297,9 @@ func (cc *ClusterContext) createHAProxyDeployment() error {
272297
return nil
273298
}
274299

275-
func (cc *ClusterContext) createHAProxyService() error {
276-
logger := cc.ReqLogger
277-
logger.Info("Creating HAProxy Service")
300+
func (cc *ClusterContext) generateHaproxyServiceDef() *corev1.Service {
278301
cr := cc.MarklogicCluster
279-
ownerDef := marklogicClusterAsOwner(cr)
280-
client := cc.Client
281-
servicePort := []corev1.ServicePort{
282-
{
283-
Name: "stat",
284-
Port: 1024,
285-
TargetPort: intstr.FromInt(int(1024)),
286-
Protocol: corev1.ProtocolTCP,
287-
},
302+
defaultPort := []corev1.ServicePort{
288303
{
289304
Name: "qconsole",
290305
Port: 8000,
@@ -304,21 +319,40 @@ func (cc *ClusterContext) createHAProxyService() error {
304319
Protocol: corev1.ProtocolTCP,
305320
},
306321
}
322+
servicePort := []corev1.ServicePort{}
323+
307324
if *cr.Spec.HAProxy.PathBasedRouting {
308325
servicePort = []corev1.ServicePort{
309-
{
310-
Name: "stat",
311-
Port: 1024,
312-
TargetPort: intstr.FromInt(int(1024)),
313-
Protocol: corev1.ProtocolTCP,
314-
},
315326
{
316327
Name: "frontend",
317328
Port: cr.Spec.HAProxy.FrontendPort,
318329
TargetPort: intstr.FromInt(int(cr.Spec.HAProxy.FrontendPort)),
319330
Protocol: corev1.ProtocolTCP,
320331
},
321332
}
333+
} else {
334+
if len(cr.Spec.HAProxy.AppServers) == 0 {
335+
servicePort = append(servicePort, defaultPort...)
336+
} else {
337+
for _, appServer := range cr.Spec.HAProxy.AppServers {
338+
port := corev1.ServicePort{
339+
Name: appServer.Name,
340+
Port: appServer.Port,
341+
}
342+
if appServer.TargetPort != 0 {
343+
port.TargetPort = intstr.FromInt(int(appServer.TargetPort))
344+
} else {
345+
port.TargetPort = intstr.FromInt(int(appServer.Port))
346+
}
347+
servicePort = append(servicePort, port)
348+
}
349+
}
350+
}
351+
if cr.Spec.HAProxy.Stats.Enabled {
352+
servicePort = append(servicePort, corev1.ServicePort{
353+
Name: "stats",
354+
Port: cr.Spec.HAProxy.Stats.Port,
355+
})
322356
}
323357
serviceDef := &corev1.Service{
324358
ObjectMeta: metav1.ObjectMeta{
@@ -338,7 +372,15 @@ func (cc *ClusterContext) createHAProxyService() error {
338372
Type: corev1.ServiceTypeClusterIP,
339373
},
340374
}
341-
logger.Info("===== HAProxy Service ==== ", "service:", serviceDef)
375+
return serviceDef
376+
}
377+
378+
func (cc *ClusterContext) createHAProxyService(serviceDef *corev1.Service) error {
379+
logger := cc.ReqLogger
380+
logger.Info("Creating HAProxy Service")
381+
cr := cc.MarklogicCluster
382+
ownerDef := marklogicClusterAsOwner(cr)
383+
client := cc.Client
342384
AddOwnerRefToObject(serviceDef, ownerDef)
343385
err := client.Create(cc.Ctx, serviceDef)
344386
if err != nil {

0 commit comments

Comments
 (0)