Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KO-286 Allow loadbalancer spec update. #263

Merged
merged 4 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions api/v1/aerospikecluster_validating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 && !reflect.DeepEqual(oldLBSpec, newLBSpec) {
return fmt.Errorf("cannot update existing LoadBalancer Service")
}

return nil
}

func validateSecurityConfigUpdate(
newVersion, oldVersion string, newSpec, oldSpec *AerospikeConfigSpec,
) error {
Expand Down
53 changes: 40 additions & 13 deletions controllers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,49 @@ func (r *SingleClusterReconciler) createOrUpdateSTSLoadBalancerSvc() error {
r.Log.Info("LoadBalancer service already exist for cluster, checking for update",
"name", utils.NamespacedName(service.Namespace, service.Name))

if len(service.Spec.Ports) == 1 && service.Spec.Ports[0].Port == servicePort.Port &&
service.Spec.Ports[0].TargetPort == servicePort.TargetPort {
r.Log.Info("LoadBalancer service update not required, skipping",
"name", utils.NamespacedName(service.Namespace, service.Name))
return nil
return r.updateLBService(service, &servicePort, loadBalancer)
}

func (r *SingleClusterReconciler) updateLBService(service *corev1.Service, servicePort *corev1.ServicePort,
loadBalancer *asdbv1.LoadBalancerSpec) error {
updateLBService := false

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,
}
}

service.Spec.Ports = []corev1.ServicePort{
servicePort,
if !reflect.DeepEqual(service.ObjectMeta.Annotations, loadBalancer.Annotations) {
service.ObjectMeta.Annotations = loadBalancer.Annotations
updateLBService = true
}
if err := r.Client.Update(
context.TODO(), service, updateOption,
); err != nil {
return fmt.Errorf(
"failed to update service %s: %v", serviceName, err,
)

if !reflect.DeepEqual(service.Spec.LoadBalancerSourceRanges, loadBalancer.LoadBalancerSourceRanges) {
service.Spec.LoadBalancerSourceRanges = loadBalancer.LoadBalancerSourceRanges
updateLBService = true
}

if !reflect.DeepEqual(service.Spec.ExternalTrafficPolicy, loadBalancer.ExternalTrafficPolicy) {
service.Spec.ExternalTrafficPolicy = loadBalancer.ExternalTrafficPolicy
updateLBService = true
}

if updateLBService {
if err := r.Client.Update(
context.TODO(), service, updateOption,
); err != nil {
return fmt.Errorf(
"failed to update service %s: %v", service.Name, err,
)
}
} else {
r.Log.Info("LoadBalancer service update not required, skipping",
"name", utils.NamespacedName(service.Namespace, service.Name))
return nil
}

r.Log.Info("LoadBalancer service updated",
Expand Down
4 changes: 2 additions & 2 deletions test/services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var _ = Describe(
)

It(
"Validate LB can not be updated", func() {
"Validate LB can be updated", func() {
By("DeployCluster")
clusterNamespacedName := getNamespacedName(
"load-balancer-invalid", namespace,
Expand All @@ -76,7 +76,7 @@ var _ = Describe(
Expect(err).ToNot(HaveOccurred())
aeroCluster.Spec.SeedsFinderServices.LoadBalancer.ExternalTrafficPolicy = "Cluster"
err = updateCluster(k8sClient, ctx, aeroCluster)
Expect(err).To(HaveOccurred())
Expect(err).ToNot(HaveOccurred())

err = deleteCluster(k8sClient, ctx, aeroCluster)
Expect(err).ToNot(HaveOccurred())
Expand Down