Skip to content

Commit

Permalink
logserver - use a Service with a pod-name selector
Browse files Browse the repository at this point in the history
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
  • Loading branch information
morucci committed Nov 22, 2023
1 parent e9deeae commit fe88eb6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
7 changes: 4 additions & 3 deletions controllers/libs/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions controllers/logserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down
29 changes: 29 additions & 0 deletions controllers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"fmt"
"os"
"reflect"
"sort"
"strconv"
"strings"
"time"

"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -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, &current) {
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(&current)
}
}
}

// ensureRoute ensures the Route exist
// The Route is updated if needed
// The function returns false when the resource is just created/updated
Expand Down

0 comments on commit fe88eb6

Please sign in to comment.