Implement new rules for status for PDBs

This commit is contained in:
Morten Torkildsen
2020-01-08 11:03:55 -08:00
parent ed83b2d8fa
commit cfcf885031
3 changed files with 26 additions and 74 deletions

View File

@@ -395,26 +395,21 @@ func podConditions(u *unstructured.Unstructured) (*Result, error) {
return newInProgressStatus("PodNotReady", message), nil
}
// pdbConditions return standardized Conditions for Deployment
// pdbConditions computes the status for PodDisruptionBudgets. A PDB
// is currently considered Current if the disruption controller has
// observed the latest version of the PDB resource and has computed
// the AllowedDisruptions. PDBs do have ObservedGeneration in the
// Status object, so if this function gets called we know that
// the controller has observed the latest changes.
// The disruption controller does not set any conditions if
// computing the AllowedDisruptions fails (and there are many ways
// it can fail), but there is PR against OSS Kubernetes to address
// this: https://github.com/kubernetes/kubernetes/pull/86929
func pdbConditions(u *unstructured.Unstructured) (*Result, error) {
obj := u.UnstructuredContent()
// replicas
currentHealthy := GetIntField(obj, ".status.currentHealthy", 0)
desiredHealthy := GetIntField(obj, ".status.desiredHealthy", 0)
if desiredHealthy == 0 {
message := "Missing or zero .status.desiredHealthy"
return newInProgressStatus("ZeroDesiredHealthy", message), nil
}
if desiredHealthy > currentHealthy {
message := fmt.Sprintf("Budget not met. healthy replicas: %d/%d", currentHealthy, desiredHealthy)
return newInProgressStatus("BudgetNotMet", message), nil
}
// All ok
return &Result{
Status: CurrentStatus,
Message: fmt.Sprintf("Budget is met. Replicas: %d/%d", currentHealthy, desiredHealthy),
Message: "AllowedDisruptions has been computed.",
Conditions: []Condition{},
}, nil
}

View File

@@ -822,66 +822,44 @@ func TestReplicasetStatus(t *testing.T) {
}
}
var pdbNoStatus = `
apiVersion: policy/v1
var pdbNotObserved = `
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
generation: 1
generation: 2
name: test
namespace: qual
status:
observedGeneration: 1
`
var pdbOK1 = `
apiVersion: policy/v1
var pdbObserved = `
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
generation: 1
name: test
namespace: qual
status:
currentHealthy: 2
desiredHealthy: 2
`
var pdbMoreHealthy = `
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
generation: 1
name: test
namespace: qual
status:
currentHealthy: 4
desiredHealthy: 2
`
var pdbLessHealthy = `
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
generation: 1
name: test
namespace: qual
status:
currentHealthy: 2
desiredHealthy: 4
observedGeneration: 1
`
func TestPDBStatus(t *testing.T) {
testCases := map[string]testSpec{
"pdbNoStatus": {
spec: pdbNoStatus,
"pdbNotObserved": {
spec: pdbNotObserved,
expectedStatus: InProgressStatus,
expectedConditions: []Condition{{
Type: ConditionInProgress,
Status: corev1.ConditionTrue,
Reason: "ZeroDesiredHealthy",
Reason: "LatestGenerationNotObserved",
}},
absentConditionTypes: []ConditionType{
ConditionFailed,
},
},
"pdbOK1": {
spec: pdbOK1,
"pdbObserved": {
spec: pdbObserved,
expectedStatus: CurrentStatus,
expectedConditions: []Condition{},
absentConditionTypes: []ConditionType{
@@ -889,27 +867,6 @@ func TestPDBStatus(t *testing.T) {
ConditionInProgress,
},
},
"pdbMoreHealthy": {
spec: pdbMoreHealthy,
expectedStatus: CurrentStatus,
expectedConditions: []Condition{},
absentConditionTypes: []ConditionType{
ConditionFailed,
ConditionInProgress,
},
},
"pdbLessHealthy": {
spec: pdbLessHealthy,
expectedStatus: InProgressStatus,
expectedConditions: []Condition{{
Type: ConditionInProgress,
Status: corev1.ConditionTrue,
Reason: "BudgetNotMet",
}},
absentConditionTypes: []ConditionType{
ConditionFailed,
},
},
}
for tn, tc := range testCases {