From 2aa2834b61e9c38dc7524b97775ba0c8403f2e4e Mon Sep 17 00:00:00 2001 From: Tanmay Jain Date: Wed, 13 Dec 2023 16:52:40 +0530 Subject: [PATCH] overwrite user specified port in lb service --- api/v1/aerospikecluster_validating_webhook.go | 20 ----------- controllers/service.go | 34 +++---------------- 2 files changed, 5 insertions(+), 49 deletions(-) diff --git a/api/v1/aerospikecluster_validating_webhook.go b/api/v1/aerospikecluster_validating_webhook.go index 4c3f9d7be..707d4a054 100644 --- a/api/v1/aerospikecluster_validating_webhook.go +++ b/api/v1/aerospikecluster_validating_webhook.go @@ -122,14 +122,6 @@ func (c *AerospikeCluster) ValidateUpdate(oldObj runtime.Object) (admission.Warn return nil, err } - // Validate Load Balancer update - if err := validateLoadBalancerUpdate( - aslog, c.Spec.SeedsFinderServices.LoadBalancer, - old.Spec.SeedsFinderServices.LoadBalancer, - ); err != nil { - return nil, err - } - // Validate RackConfig update return nil, c.validateRackUpdate(aslog, old) } @@ -1273,18 +1265,6 @@ func getNamespaceReplicationFactor(nsConf map[string]interface{}) (int, error) { return rf, nil } -func validateLoadBalancerUpdate( - aslog logr.Logger, newLBSpec, oldLBSpec *LoadBalancerSpec, -) error { - aslog.Info("Validate LoadBalancer update") - - if oldLBSpec != nil && oldLBSpec.PortName != newLBSpec.PortName { - return fmt.Errorf("cannot update existing LoadBalancer Service name") - } - - return nil -} - func validateSecurityConfigUpdate( newVersion, oldVersion string, newSpec, oldSpec *AerospikeConfigSpec, ) error { diff --git a/controllers/service.go b/controllers/service.go index 9e75d735f..b95a68974 100644 --- a/controllers/service.go +++ b/controllers/service.go @@ -164,30 +164,14 @@ func (r *SingleClusterReconciler) createOrUpdateSTSLoadBalancerSvc() error { func (r *SingleClusterReconciler) updateLBService(service *corev1.Service, servicePort *corev1.ServicePort, loadBalancer *asdbv1.LoadBalancerSpec) error { updateLBService := false - aerospikePortNameFound := false - for idx := range service.Spec.Ports { - if service.Spec.Ports[idx].Name == servicePort.Name { - aerospikePortNameFound = true - - if service.Spec.Ports[idx].Port != servicePort.Port || - service.Spec.Ports[idx].TargetPort != servicePort.TargetPort { - service.Spec.Ports[idx].Port = servicePort.Port - service.Spec.Ports[idx].TargetPort = servicePort.TargetPort - updateLBService = true - } - - break - } - } - - // If there is no port name set for in port added by operator, then set it and overwrite ports set by user. - if !aerospikePortNameFound { + if len(service.Spec.Ports) != 1 || service.Spec.Ports[0].Port != servicePort.Port || + service.Spec.Ports[0].TargetPort != servicePort.TargetPort || + service.Spec.Ports[0].Name != servicePort.Name { + updateLBService = true service.Spec.Ports = []corev1.ServicePort{ *servicePort, } - - updateLBService = true } if !reflect.DeepEqual(service.ObjectMeta.Annotations, loadBalancer.Annotations) { @@ -394,17 +378,9 @@ func (r *SingleClusterReconciler) getLBServicePort(loadBalancer *asdbv1.LoadBala port = targetPort } - var portName string - if loadBalancer.PortName == "" { - // If port is specified in CR. - portName = "aerospikeport" - } else { - portName = loadBalancer.PortName - } - return corev1.ServicePort{ Port: port, - Name: portName, + Name: loadBalancer.PortName, TargetPort: intstr.FromInt(int(targetPort)), } }