Skip to content

Commit

Permalink
Allow loadbalancer spec update. (#263)
Browse files Browse the repository at this point in the history
* Allow loadbalancer spec update.
  • Loading branch information
tanmayja authored Jan 24, 2024
1 parent 71443db commit 4522505
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 36 deletions.
20 changes: 0 additions & 20 deletions api/v1/aerospikecluster_validating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,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 @@ -1274,18 +1266,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
3 changes: 2 additions & 1 deletion test/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ func clusterWithMaxIgnorablePod(ctx goctx.Context) {
err = k8sClient.Get(ctx, types.NamespacedName{Name: ignorePodName,
Namespace: clusterNamespacedName.Namespace}, pod)

return *pod.Status.ContainerStatuses[0].Started && pod.Status.ContainerStatuses[0].Ready
return len(pod.Status.ContainerStatuses) != 0 && *pod.Status.ContainerStatuses[0].Started &&
pod.Status.ContainerStatuses[0].Ready
}, 1*time.Minute).Should(BeTrue())

Eventually(func() error {
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

0 comments on commit 4522505

Please sign in to comment.