@@ -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