Skip to content

Commit

Permalink
fix(controller/pdb): set min available to zero replicas at least
Browse files Browse the repository at this point in the history
  • Loading branch information
nettoclaudio committed Jan 25, 2022
1 parent f2cfeb1 commit 886009a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ func newPDB(instance *v1alpha1.RpaasInstance, nginx *nginxv1alpha1.Nginx) (*poli
}
}

if minAvailable.IntValue() < 0 {
minAvailable = intstr.FromInt(0)
}

return &policyv1beta1.PodDisruptionBudget{
TypeMeta: metav1.TypeMeta{
APIVersion: "policy/v1beta1",
Expand Down
57 changes: 57 additions & 0 deletions controllers/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1253,6 +1253,63 @@ func Test_reconcilePDB(t *testing.T) {
assert.True(t, k8serrors.IsNotFound(err))
},
},

"creating PDB when instance has 0 replicas": {
instance: &v1alpha1.RpaasInstance{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
Namespace: "rpaasv2",
},
Spec: v1alpha1.RpaasInstanceSpec{
EnablePodDisruptionBudget: func(b bool) *bool { return &b }(true),
Replicas: func(n int32) *int32 { return &n }(0),
},
},
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.NoError(t, err)
assert.Equal(t, policyv1beta1.PodDisruptionBudget{
TypeMeta: metav1.TypeMeta{
APIVersion: "policy/v1beta1",
Kind: "PodDisruptionBudget",
},
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
Namespace: "rpaasv2",
Labels: map[string]string{
"rpaas.extensions.tsuru.io/instance-name": "my-instance",
"rpaas.extensions.tsuru.io/plan-name": "",
},
ResourceVersion: "1",
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "extensions.tsuru.io/v1alpha1",
Kind: "RpaasInstance",
Name: "my-instance",
Controller: func(b bool) *bool { return &b }(true),
BlockOwnerDeletion: func(b bool) *bool { return &b }(true),
},
},
},
Spec: policyv1beta1.PodDisruptionBudgetSpec{
MinAvailable: func(n intstr.IntOrString) *intstr.IntOrString { return &n }(intstr.FromInt(0)),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"nginx.tsuru.io/resource-name": "my-instance"},
},
},
}, pdb)
},
},
}

for name, tt := range tests {
Expand Down

0 comments on commit 886009a

Please sign in to comment.