From f23dec06fb103d01d8ccdd534ca421b272b596a2 Mon Sep 17 00:00:00 2001 From: Claudio Netto Date: Tue, 25 Jan 2022 16:41:07 -0300 Subject: [PATCH] fix(controller/pdb): skip PDB creation when flag isn't provided --- controllers/controller.go | 18 +++++++++++------- controllers/controller_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/controllers/controller.go b/controllers/controller.go index ba403783a..17854172c 100644 --- a/controllers/controller.go +++ b/controllers/controller.go @@ -630,16 +630,20 @@ func (r *RpaasInstanceReconciler) reconcilePDB(ctx context.Context, instance *v1 var existingPDB policyv1beta1.PodDisruptionBudget err = r.Get(ctx, client.ObjectKey{Name: pdb.Name, Namespace: pdb.Namespace}, &existingPDB) - if err == nil && (instance.Spec.EnablePodDisruptionBudget == nil || !*instance.Spec.EnablePodDisruptionBudget) { - return r.Delete(ctx, &existingPDB) - } + if err != nil { + if !k8serrors.IsNotFound(err) { + return err + } - if k8serrors.IsNotFound(err) { - return r.Create(ctx, pdb) + if instance.Spec.EnablePodDisruptionBudget != nil && *instance.Spec.EnablePodDisruptionBudget { + return r.Create(ctx, pdb) + } + + return nil } - if err != nil { - return err + if instance.Spec.EnablePodDisruptionBudget == nil || (instance.Spec.EnablePodDisruptionBudget != nil && !*instance.Spec.EnablePodDisruptionBudget) { + return r.Delete(ctx, &existingPDB) } if equality.Semantic.DeepDerivative(existingPDB.Spec, pdb.Spec) { diff --git a/controllers/controller_test.go b/controllers/controller_test.go index 2e2ea25f2..3433c0c04 100644 --- a/controllers/controller_test.go +++ b/controllers/controller_test.go @@ -1310,6 +1310,34 @@ func Test_reconcilePDB(t *testing.T) { }, pdb) }, }, + + "skip PDB creation when instance disables PDB feature": { + instance: &v1alpha1.RpaasInstance{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance", + Namespace: "rpaasv2", + }, + Spec: v1alpha1.RpaasInstanceSpec{ + EnablePodDisruptionBudget: func(b bool) *bool { return &b }(false), + Replicas: func(n int32) *int32 { return &n }(10), + }, + }, + nginx: &nginxv1alpha1.Nginx{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance", + Namespace: "rpaasv2", + }, + Status: nginxv1alpha1.NginxStatus{ + PodSelector: "nginx.tsuru.io/resource-name=my-instance", + }, + }, + assert: func(t *testing.T, c client.Client) { + var pdb policyv1beta1.PodDisruptionBudget + err := c.Get(context.TODO(), client.ObjectKey{Name: "my-instance", Namespace: "rpaasv2"}, &pdb) + require.Error(t, err) + assert.True(t, k8serrors.IsNotFound(err)) + }, + }, } for name, tt := range tests {