Skip to content

Commit 0c70f55

Browse files
authored
Merge pull request #77 from PDOK/wr/pdb-fix
Added check and tests that PDB gets removed/not created if replicas == 1
2 parents 5663dd7 + b4fc88c commit 0c70f55

File tree

2 files changed

+81
-5
lines changed

2 files changed

+81
-5
lines changed

internal/controller/shared_controller.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"strconv"
77
"time"
88

9+
"sigs.k8s.io/controller-runtime/pkg/client"
10+
911
"github.com/pdok/smooth-operator/model"
1012

1113
"github.com/pdok/mapserver-operator/internal/controller/constants"
@@ -105,12 +107,9 @@ func createOrUpdateAllForWMSWFS[R Reconciler, O pdoknlv3.WMSWFS](ctx context.Con
105107

106108
// region PodDisruptionBudget
107109
{
108-
podDisruptionBudget := getBarePodDisruptionBudget(obj)
109-
operationResults[smoothoperatorutils.GetObjectFullName(reconcilerClient, podDisruptionBudget)], err = controllerutil.CreateOrUpdate(ctx, reconcilerClient, podDisruptionBudget, func() error {
110-
return mutatePodDisruptionBudget(r, obj, podDisruptionBudget)
111-
})
110+
err = createOrUpdateOrDeletePodDisruptionBudget(ctx, r, obj, operationResults)
112111
if err != nil {
113-
return operationResults, fmt.Errorf("unable to create/update resource %s: %w", smoothoperatorutils.GetObjectFullName(reconcilerClient, podDisruptionBudget), err)
112+
return operationResults, err
114113
}
115114
}
116115
// end region PodDisruptionBudget
@@ -227,3 +226,26 @@ func createOrUpdateConfigMap[O pdoknlv3.WMSWFS, R Reconciler](ctx context.Contex
227226
}
228227
return cm, &or, nil
229228
}
229+
230+
func createOrUpdateOrDeletePodDisruptionBudget[O pdoknlv3.WMSWFS, R Reconciler](ctx context.Context, reconciler R, obj O, operationResults map[string]controllerutil.OperationResult) (err error) {
231+
reconcilerClient := getReconcilerClient(reconciler)
232+
podDisruptionBudget := getBarePodDisruptionBudget(obj)
233+
if obj.HorizontalPodAutoscalerPatch().MinReplicas != nil && obj.HorizontalPodAutoscalerPatch().MaxReplicas != nil &&
234+
*obj.HorizontalPodAutoscalerPatch().MinReplicas == 1 && *obj.HorizontalPodAutoscalerPatch().MaxReplicas == 1 {
235+
err = reconcilerClient.Delete(ctx, podDisruptionBudget)
236+
if err == nil {
237+
operationResults[smoothoperatorutils.GetObjectFullName(reconcilerClient, podDisruptionBudget)] = "deleted"
238+
}
239+
if client.IgnoreNotFound(err) != nil {
240+
return fmt.Errorf("unable to delete resource %s: %w", smoothoperatorutils.GetObjectFullName(reconcilerClient, podDisruptionBudget), err)
241+
}
242+
} else {
243+
operationResults[smoothoperatorutils.GetObjectFullName(reconcilerClient, podDisruptionBudget)], err = controllerutil.CreateOrUpdate(ctx, reconcilerClient, podDisruptionBudget, func() error {
244+
return mutatePodDisruptionBudget(reconciler, obj, podDisruptionBudget)
245+
})
246+
if err != nil {
247+
return fmt.Errorf("unable to create/update resource %s: %w", smoothoperatorutils.GetObjectFullName(reconcilerClient, podDisruptionBudget), err)
248+
}
249+
}
250+
return nil
251+
}

internal/controller/wfs_controller_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
"github.com/pdok/smooth-operator/model"
3434
smoothoperatorutils "github.com/pdok/smooth-operator/pkg/util"
3535
k8stypes "k8s.io/apimachinery/pkg/types"
36+
"k8s.io/utils/ptr"
37+
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3638

3739
. "github.com/onsi/ginkgo/v2" //nolint:revive // ginkgo bdd
3840
. "github.com/onsi/gomega" //nolint:revive // ginkgo bdd
@@ -179,6 +181,58 @@ var _ = Describe("Testing WFS Controller", func() {
179181
}, "10s", "1s").Should(BeTrue())
180182
})
181183

184+
It("Should delete PodDisruptionBudget if Min and Max replicas == 1 ", func() {
185+
controllerReconciler := getWFSReconciler()
186+
187+
By("Setting Min and Max replicas to 1")
188+
189+
Expect(k8sClient.Get(ctx, objectKeyWfs, clusterWfs)).To(Succeed())
190+
191+
resource := clusterWfs.DeepCopy()
192+
193+
resource.Spec.HorizontalPodAutoscalerPatch.MinReplicas = ptr.To(int32(1))
194+
resource.Spec.HorizontalPodAutoscalerPatch.MaxReplicas = ptr.To(int32(1))
195+
196+
Expect(k8sClient.Update(ctx, resource)).To(Succeed())
197+
198+
podDisruptionBudget := getBarePodDisruptionBudget(resource)
199+
200+
By("Reconciling the WFS")
201+
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: objectKeyWfs})
202+
Expect(err).NotTo(HaveOccurred())
203+
204+
By("Getting the PodDisruptionBudget")
205+
err = k8sClient.Get(ctx, client.ObjectKeyFromObject(podDisruptionBudget), podDisruptionBudget)
206+
Expect(apierrors.IsNotFound(err)).To(BeTrue())
207+
208+
Expect(k8sClient.Get(ctx, objectKeyWfs, clusterWfs)).To(Succeed())
209+
Expect(clusterWfs.Status.OperationResults[smoothoperatorutils.GetObjectFullName(k8sClient, podDisruptionBudget)]).To(Equal(controllerutil.OperationResult("deleted")))
210+
})
211+
212+
It("Should not Create PodDisruptionBudget if Min and Max replicas == 1 ", func() {
213+
controllerReconciler := getWFSReconciler()
214+
215+
By("Getting Cluster WFS Min and Max replicas to 1")
216+
Expect(k8sClient.Get(ctx, objectKeyWfs, clusterWfs)).To(Succeed())
217+
218+
Expect(clusterWfs.HorizontalPodAutoscalerPatch().MaxReplicas).To(Equal(ptr.To(int32(1))))
219+
Expect(clusterWfs.HorizontalPodAutoscalerPatch().MinReplicas).To(Equal(ptr.To(int32(1))))
220+
221+
podDisruptionBudget := getBarePodDisruptionBudget(clusterWfs)
222+
223+
By("Reconciling the WFS")
224+
_, err := controllerReconciler.Reconcile(ctx, reconcile.Request{NamespacedName: objectKeyWfs})
225+
Expect(err).NotTo(HaveOccurred())
226+
227+
By("Getting the PodDisruptionBudget")
228+
err = k8sClient.Get(ctx, client.ObjectKeyFromObject(podDisruptionBudget), podDisruptionBudget)
229+
Expect(apierrors.IsNotFound(err)).To(BeTrue())
230+
231+
Expect(k8sClient.Get(ctx, objectKeyWfs, clusterWfs)).To(Succeed())
232+
_, ok := clusterWfs.Status.OperationResults[smoothoperatorutils.GetObjectFullName(k8sClient, podDisruptionBudget)]
233+
Expect(ok).To(BeFalse())
234+
})
235+
182236
It("Respects the TTL of the WFS", func() {
183237
By("Creating a new resource for the Kind WFS")
184238

0 commit comments

Comments
 (0)