Skip to content

Commit

Permalink
fix(controller/pdb): skip PDB creation when flag isn't provided
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Jan 25, 2022
1 parent 886009a commit f23dec0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
18 changes: 11 additions & 7 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 28 additions & 0 deletions controllers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f23dec0

Please sign in to comment.