diff --git a/pkg/k8sutil/haProxy.go b/pkg/k8sutil/haProxy.go index ace021d..9e9d901 100644 --- a/pkg/k8sutil/haProxy.go +++ b/pkg/k8sutil/haProxy.go @@ -31,10 +31,13 @@ func (cc *ClusterContext) ReconcileHAProxy() result.ReconcileResult { configMapName := "marklogic-haproxy" objectMeta := generateObjectMeta(configMapName, cr.Namespace, labels, annotations) nsName := types.NamespacedName{Name: objectMeta.Name, Namespace: objectMeta.Namespace} + svcName := types.NamespacedName{Name: "marklogic-haproxy", Namespace: cr.Namespace} configmap := &corev1.ConfigMap{} + haproxyService := &corev1.Service{} err := client.Get(cc.Ctx, nsName, configmap) data := generateHAProxyConfigMapData(cc.MarklogicCluster) configMapDef := generateHAProxyConfigMap(objectMeta, marklogicClusterAsOwner(cr), data) + haproxyServiceDef := cc.generateHaproxyServiceDef() configmapHash := calculateHash(configMapDef.Data) if err != nil { if errors.IsNotFound(err) { @@ -50,8 +53,7 @@ func (cc *ClusterContext) ReconcileHAProxy() result.ReconcileResult { logger.Info("HAProxy Deployment creation is failed") return result.Error(err) } - // createHAProxyService(service corev1.Service) - err = cc.createHAProxyService() + err = cc.createHAProxyService(haproxyServiceDef) if err != nil { logger.Info("HAProxy Service creation is failed") return result.Error(err) @@ -67,9 +69,8 @@ func (cc *ClusterContext) ReconcileHAProxy() result.ReconcileResult { patch.IgnoreStatusFields(), patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus(), patch.IgnoreField("kind")) - if err != nil { - logger.Error(err, "Error calculating patch") + logger.Error(err, "Error calculating patch for HAProxy configmap") return result.Error(err) } if !patchDiff.IsEmpty() { @@ -82,6 +83,30 @@ func (cc *ClusterContext) ReconcileHAProxy() result.ReconcileResult { return result.Error(err) } } + err = client.Get(cc.Ctx, svcName, haproxyService) + if err != nil { + logger.Error(err, "Failed to get HAProxy service") + return result.Error(err) + } + patchDiff, err = patch.DefaultPatchMaker.Calculate(haproxyService, haproxyServiceDef, + patch.IgnoreStatusFields(), + patch.IgnoreVolumeClaimTemplateTypeMetaAndStatus(), + patch.IgnoreField("kind")) + if err != nil { + logger.Error(err, "Error calculating patch for HAProxy service") + return result.Error(err) + } + if !patchDiff.IsEmpty() { + logger.Info("HAProxy Service spec is different from the MarkLogicGroup spec, updating the haproxy service") + logger.Info(patchDiff.String()) + haproxyService.Spec = haproxyServiceDef.Spec + err := cc.Client.Update(cc.Ctx, haproxyService) + if err != nil { + logger.Error(err, "Error updating HAProxy service") + return result.Error(err) + } + } + haproxyDeployment := &appsv1.Deployment{} err = client.Get(cc.Ctx, types.NamespacedName{Name: "marklogic-haproxy", Namespace: cr.Namespace}, haproxyDeployment) if err != nil { @@ -272,19 +297,9 @@ func (cc *ClusterContext) createHAProxyDeployment() error { return nil } -func (cc *ClusterContext) createHAProxyService() error { - logger := cc.ReqLogger - logger.Info("Creating HAProxy Service") +func (cc *ClusterContext) generateHaproxyServiceDef() *corev1.Service { cr := cc.MarklogicCluster - ownerDef := marklogicClusterAsOwner(cr) - client := cc.Client - servicePort := []corev1.ServicePort{ - { - Name: "stat", - Port: 1024, - TargetPort: intstr.FromInt(int(1024)), - Protocol: corev1.ProtocolTCP, - }, + defaultPort := []corev1.ServicePort{ { Name: "qconsole", Port: 8000, @@ -304,14 +319,10 @@ func (cc *ClusterContext) createHAProxyService() error { Protocol: corev1.ProtocolTCP, }, } + servicePort := []corev1.ServicePort{} + if *cr.Spec.HAProxy.PathBasedRouting { servicePort = []corev1.ServicePort{ - { - Name: "stat", - Port: 1024, - TargetPort: intstr.FromInt(int(1024)), - Protocol: corev1.ProtocolTCP, - }, { Name: "frontend", Port: cr.Spec.HAProxy.FrontendPort, @@ -319,6 +330,29 @@ func (cc *ClusterContext) createHAProxyService() error { Protocol: corev1.ProtocolTCP, }, } + } else { + if len(cr.Spec.HAProxy.AppServers) == 0 { + servicePort = append(servicePort, defaultPort...) + } else { + for _, appServer := range cr.Spec.HAProxy.AppServers { + port := corev1.ServicePort{ + Name: appServer.Name, + Port: appServer.Port, + } + if appServer.TargetPort != 0 { + port.TargetPort = intstr.FromInt(int(appServer.TargetPort)) + } else { + port.TargetPort = intstr.FromInt(int(appServer.Port)) + } + servicePort = append(servicePort, port) + } + } + } + if cr.Spec.HAProxy.Stats.Enabled { + servicePort = append(servicePort, corev1.ServicePort{ + Name: "stats", + Port: cr.Spec.HAProxy.Stats.Port, + }) } serviceDef := &corev1.Service{ ObjectMeta: metav1.ObjectMeta{ @@ -338,7 +372,15 @@ func (cc *ClusterContext) createHAProxyService() error { Type: corev1.ServiceTypeClusterIP, }, } - logger.Info("===== HAProxy Service ==== ", "service:", serviceDef) + return serviceDef +} + +func (cc *ClusterContext) createHAProxyService(serviceDef *corev1.Service) error { + logger := cc.ReqLogger + logger.Info("Creating HAProxy Service") + cr := cc.MarklogicCluster + ownerDef := marklogicClusterAsOwner(cr) + client := cc.Client AddOwnerRefToObject(serviceDef, ownerDef) err := client.Create(cc.Ctx, serviceDef) if err != nil {