Skip to content

Commit 67b254c

Browse files
harsh maheshwariammolitor
harsh maheshwari
authored andcommitted
refactoring code
1 parent b54372e commit 67b254c

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

controllers/add_services.go

+23-21
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,7 @@ func (a addServices) reconcile(ctx context.Context, r *FoundationDBClusterReconc
4343
existingService := &corev1.Service{}
4444
err := r.Get(ctx, client.ObjectKey{Namespace: cluster.Namespace, Name: cluster.Name}, existingService)
4545
if err == nil {
46-
if cluster.IsPodIPFamily6() && (existingService.Spec.IPFamilies == nil || existingService.Spec.IPFamilies[0] != corev1.IPv6Protocol) {
47-
logger.V(1).Info("Recreating service", "name", service.Name)
48-
err = r.Delete(ctx, existingService)
49-
if err != nil {
50-
return &requeue{curError: err}
51-
}
52-
err = r.Create(ctx, service)
53-
} else {
54-
err = updateService(ctx, logger, r, existingService, service)
55-
}
46+
err = updateService(ctx, logger, cluster, r, existingService, service)
5647
if err != nil {
5748
return &requeue{curError: err}
5849
}
@@ -90,16 +81,7 @@ func (a addServices) reconcile(ctx context.Context, r *FoundationDBClusterReconc
9081
existingService := &corev1.Service{}
9182
err = r.Get(ctx, client.ObjectKey{Namespace: cluster.Namespace, Name: serviceName}, existingService)
9283
if err == nil {
93-
if cluster.IsPodIPFamily6() && (existingService.Spec.IPFamilies == nil || existingService.Spec.IPFamilies[0] != corev1.IPv6Protocol) {
94-
logger.V(1).Info("Recreating service", "name", service.Name)
95-
err = r.Delete(ctx, existingService)
96-
if err != nil {
97-
return &requeue{curError: err}
98-
}
99-
err = r.Create(ctx, service)
100-
} else {
101-
err = updateService(ctx, logger, r, existingService, service)
102-
}
84+
err = updateService(ctx, logger, cluster, r, existingService, service)
10385
if err != nil {
10486
return &requeue{curError: err}
10587
}
@@ -122,9 +104,29 @@ func (a addServices) reconcile(ctx context.Context, r *FoundationDBClusterReconc
122104
return nil
123105
}
124106

107+
// requiresRecreation returns true if the cluster supports podIPFamily as IPv6 and the existing service does not have
108+
// IPv6 in the IPFamilies.
109+
func requiresRecreation(cluster *fdbv1beta2.FoundationDBCluster, existingService *corev1.Service) bool {
110+
return cluster.IsPodIPFamily6() && (existingService.Spec.IPFamilies == nil || existingService.Spec.IPFamilies[0] != corev1.IPv6Protocol)
111+
}
112+
113+
// recreateService removes the existing service and create a new service.
114+
func recreateService(ctx context.Context, r *FoundationDBClusterReconciler, currentService *corev1.Service, newService *corev1.Service, logger logr.Logger) error {
115+
logger.V(1).Info("Recreating service", "name", newService.Name)
116+
err := r.Delete(ctx, currentService)
117+
if err != nil {
118+
return err
119+
}
120+
err = r.Create(ctx, newService)
121+
return err
122+
}
123+
125124
// updateServices updates selected safe fields on a service based on a new
126125
// service definition.
127-
func updateService(ctx context.Context, logger logr.Logger, r *FoundationDBClusterReconciler, currentService *corev1.Service, newService *corev1.Service) error {
126+
func updateService(ctx context.Context, logger logr.Logger, cluster *fdbv1beta2.FoundationDBCluster, r *FoundationDBClusterReconciler, currentService *corev1.Service, newService *corev1.Service) error {
127+
if requiresRecreation(cluster, currentService) {
128+
return recreateService(ctx, r, currentService, newService, logger)
129+
}
128130
originalSpec := currentService.Spec.DeepCopy()
129131

130132
currentService.Spec.Selector = newService.Spec.Selector

0 commit comments

Comments
 (0)