From fe88eb666606fa750191ba3bc3785cdc050b3a14 Mon Sep 17 00:00:00 2001 From: Fabien Boucher Date: Thu, 16 Nov 2023 13:32:29 +0000 Subject: [PATCH] logserver - use a Service with a pod-name selector As the logserver is not scalable we want to make sure that in case of scale up the Serice will still proxy traffic to logserver-0. This change also brings a `EnsureService` function to ease updating a Service resource if needed. Change-Id: Ic8fa95507288f6c8f40af2454175442c68a49f6e --- controllers/libs/base/base.go | 7 ++++--- controllers/logserver_controller.go | 8 ++++---- controllers/utils.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/controllers/libs/base/base.go b/controllers/libs/base/base.go index 66e67c14..b81e23de 100644 --- a/controllers/libs/base/base.go +++ b/controllers/libs/base/base.go @@ -213,9 +213,10 @@ func mkServicePorts(ports []int32, portName string) []apiv1.ServicePort { servicePorts = append( servicePorts, apiv1.ServicePort{ - Name: fmt.Sprintf("%s-%d", portName, p), - Protocol: apiv1.ProtocolTCP, - Port: p, + Name: fmt.Sprintf("%s-%d", portName, p), + Protocol: apiv1.ProtocolTCP, + Port: p, + TargetPort: intstr.FromInt(int(p)), }) } return servicePorts diff --git a/controllers/logserver_controller.go b/controllers/logserver_controller.go index 2dc8acdb..e80d6004 100644 --- a/controllers/logserver_controller.go +++ b/controllers/logserver_controller.go @@ -234,10 +234,10 @@ func (r *LogServerController) DeployLogserver() sfv1.LogServerStatus { r.EnsureConfigMap(logserverIdent, cmData) // Create service exposed by logserver - servicePorts := []int32{httpdPort, sshdPort, sfmonitoring.NodeExporterPort} - svc := base.MkService( - logserverIdent, r.ns, logserverIdent, servicePorts, logserverIdent) - r.GetOrCreate(&svc) + svc := base.MkServicePod( + logserverIdent, r.ns, logserverIdent+"-0", + []int32{httpdPort, sshdPort, sfmonitoring.NodeExporterPort}, logserverIdent) + r.EnsureService(&svc) volumeMounts := []apiv1.VolumeMount{ { diff --git a/controllers/utils.go b/controllers/utils.go index b557dcd8..8b663244 100644 --- a/controllers/utils.go +++ b/controllers/utils.go @@ -11,6 +11,9 @@ import ( "fmt" "os" "reflect" + "sort" + "strconv" + "strings" "time" "sigs.k8s.io/controller-runtime/pkg/client" @@ -245,6 +248,32 @@ func (r *SFUtilContext) EnsureSSHKeySecret(name string) { } } +// EnsureService ensures a Service exists +// The Service is updated if needed +func (r *SFUtilContext) EnsureService(service *apiv1.Service) { + var current apiv1.Service + spsAsString := func(sps []apiv1.ServicePort) string { + s := []string{} + for _, p := range current.Spec.Ports { + s = append(s, []string{strconv.Itoa(int(p.Port)), p.Name, p.TargetPort.String(), string(p.Protocol)}...) + } + sort.Strings(s) + return strings.Join(s[:], "") + } + name := service.GetName() + if !r.GetM(name, ¤t) { + r.log.V(1).Info("Creating service", "name", name) + r.CreateR(service) + } else { + if !reflect.DeepEqual(current.Spec.Selector, service.Spec.Selector) || + spsAsString(current.Spec.Ports) != spsAsString(service.Spec.Ports) { + r.log.V(1).Info("Updating service", "name", name) + current.Spec = *service.Spec.DeepCopy() + r.UpdateR(¤t) + } + } +} + // ensureRoute ensures the Route exist // The Route is updated if needed // The function returns false when the resource is just created/updated