Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed pdbMinAvailableGreaterThanHPAMinReplicas and added validation for pdbMinAvailableEqualToHPAMinReplicas. #1073

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ polaris
*.test

# Output of the go coverage tool
coverage.txt

/*.out

Tiltfile
Expand Down
1 change: 1 addition & 0 deletions docs/checks/reliability.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ key | default | description
`hpaMaxAvailability` | `warning` | Fails when `maxAvailable` lesser or equal than `minAvailable` (if defined) for a HorizontalPodAutoscaler
`hpaMinAvailability` | `warning` | Fails when `minAvailable` (if defined) lesser or equal to one for a HorizontalPodAutoscaler
`pdbMinAvailableGreaterThanHPAMinReplicas` | `warning` | Fails when PDB `minAvailable` is greater than HPA `minReplicas`
`pdbMinAvailableEqualToHPAMinReplicas` | `warning` | Fails when PDB `minAvailable` is equal to HPA `minReplicas`

## Background

Expand Down
1 change: 1 addition & 0 deletions pkg/config/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ var (
"hpaMaxAvailability",
"hpaMinAvailability",
"pdbMinAvailableGreaterThanHPAMinReplicas",
"pdbMinAvailableEqualToHPAMinReplicas",
}

// BuiltInChecks contains the checks that come pre-installed w/ Polaris
Expand Down
7 changes: 7 additions & 0 deletions pkg/config/checks/pdbMinAvailableEqualToHPAMinReplicas.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
successMessage: PDB and HPA are correctly configured
failureMessage: PDB minAvailable is equal to HPA minReplicas
category: Reliability
target: Controller
controllers:
include:
- Deployment
1 change: 1 addition & 0 deletions pkg/config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ checks:
hpaMaxAvailability: warning
hpaMinAvailability: warning
pdbMinAvailableGreaterThanHPAMinReplicas: warning
pdbMinAvailableEqualToHPAMinReplicas: warning

# efficiency
cpuRequestsMissing: warning
Expand Down
1 change: 1 addition & 0 deletions pkg/config/examples/config-full.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ checks:
hpaMaxAvailability: warning
hpaMinAvailability: warning
pdbMinAvailableGreaterThanHPAMinReplicas: warning
pdbMinAvailableEqualToHPAMinReplicas: warning

# efficiency
cpuRequestsMissing: warning
Expand Down
69 changes: 67 additions & 2 deletions pkg/validator/pdb_hpa_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

func init() {
registerCustomChecks("pdbMinAvailableGreaterThanHPAMinReplicas", pdbMinAvailableGreaterThanHPAMinReplicas)
registerCustomChecks("pdbMinAvailableEqualToHPAMinReplicas", pdbMinAvailableEqualToHPAMinReplicas)
}

func pdbMinAvailableGreaterThanHPAMinReplicas(test schemaTestCase) (bool, []jsonschema.ValError, error) {
Expand Down Expand Up @@ -69,12 +70,76 @@ func pdbMinAvailableGreaterThanHPAMinReplicas(test schemaTestCase) (bool, []json
}
}

if attachedHPA.Spec.MinReplicas != nil && pdbMinAvailable >= int(*attachedHPA.Spec.MinReplicas) {
if attachedHPA.Spec.MinReplicas != nil && pdbMinAvailable > int(*attachedHPA.Spec.MinReplicas) {
return false, []jsonschema.ValError{
{
PropertyPath: "spec.minAvailable",
InvalidValue: pdbMinAvailable,
Message: fmt.Sprintf("The minAvailable value in the PodDisruptionBudget(%s) is %d, which is greater or equal than the minReplicas value in the HorizontalPodAutoscaler(%s) (%d)", attachedPDB.Name, pdbMinAvailable, attachedHPA.Name, *attachedHPA.Spec.MinReplicas),
Message: fmt.Sprintf("The minAvailable value in the PodDisruptionBudget(%s) is %d, which is greater than the minReplicas value in the HorizontalPodAutoscaler(%s) (%d)", attachedPDB.Name, pdbMinAvailable, attachedHPA.Name, *attachedHPA.Spec.MinReplicas),
},
}, nil
}
}

return true, nil, nil
}

func pdbMinAvailableEqualToHPAMinReplicas(test schemaTestCase) (bool, []jsonschema.ValError, error) {
if test.ResourceProvider == nil {
return true, nil, nil
}

deployment := &appsv1.Deployment{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(test.Resource.Resource.Object, deployment)
if err != nil {
logrus.Warnf("error converting unstructured to Deployment: %v", err)
return true, nil, nil
}

attachedPDB, err := hasPDBAttached(*deployment, test.ResourceProvider.Resources["policy/PodDisruptionBudget"])
if err != nil {
logrus.Warnf("error getting PodDisruptionBudget: %v", err)
return true, nil, nil
}

attachedHPA, err := hasHPAAttached(*deployment, test.ResourceProvider.Resources["autoscaling/HorizontalPodAutoscaler"])
if err != nil {
logrus.Warnf("error getting HorizontalPodAutoscaler: %v", err)
return true, nil, nil
}

if attachedPDB != nil && attachedHPA != nil {
logrus.Debugf("both PDB and HPA are attached to deployment %s", deployment.Name)

if attachedPDB.Spec.MinAvailable == nil {
return true, nil, nil
}

pdbMinAvailable, isPercent, err := getIntOrPercentValueSafely(attachedPDB.Spec.MinAvailable)
if err != nil {
logrus.Warnf("error getting getIntOrPercentValueSafely: %v", err)
return true, nil, nil
}

if isPercent {
// if the value is a percentage, we need to calculate the actual value
if attachedHPA.Spec.MinReplicas == nil {
return true, nil, nil
}

pdbMinAvailable, err = intstr.GetScaledValueFromIntOrPercent(attachedPDB.Spec.MinAvailable, int(*attachedHPA.Spec.MinReplicas), true)
if err != nil {
logrus.Warnf("error getting minAvailable value from PodDisruptionBudget: %v", err)
return true, nil, nil
}
}

if attachedHPA.Spec.MinReplicas != nil && pdbMinAvailable == int(*attachedHPA.Spec.MinReplicas) {
return false, []jsonschema.ValError{
{
PropertyPath: "spec.minAvailable",
InvalidValue: pdbMinAvailable,
Message: fmt.Sprintf("The minAvailable value in the PodDisruptionBudget(%s) is %d, which is equal to the minReplicas value in the HorizontalPodAutoscaler(%s) (%d)", attachedPDB.Name, pdbMinAvailable, attachedHPA.Name, *attachedHPA.Spec.MinReplicas),
},
}, nil
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: zookeeper
spec:
replicas: 10
template:
metadata:
labels:
app.kubernetes.io/name: zookeeper
foo: bar
spec:
containers:
- name: zookeeper
image: zookeeper
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: zookeeper-pdb
spec:
minAvailable: 150% # 1.5 * 10 = 15
selector:
matchLabels:
app.kubernetes.io/name: zookeeper
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: zookeeper-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: zookeeper
minReplicas: 10
maxReplicas: 15
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: zookeeper
spec:
template:
metadata:
labels:
app.kubernetes.io/name: zookeeper
foo: bar
spec:
containers:
- name: zookeeper
image: zookeeper
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: zookeeper-pdb
spec:
minAvailable: 10
selector:
matchLabels:
app.kubernetes.io/name: zookeeper
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: zookeeper-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: zookeeper
minReplicas: 5
maxReplicas: 7
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: zookeeper
spec:
replicas: 10
template:
metadata:
labels:
app.kubernetes.io/name: zookeeper
foo: bar
spec:
containers:
- name: zookeeper
image: zookeeper
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: zookeeper-pdb
spec:
minAvailable: 50% # 0.5 * 10 = 5
selector:
matchLabels:
app.kubernetes.io/name: zookeeper
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: zookeeper-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: zookeeper
minReplicas: 10
maxReplicas: 15
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: zookeeper
spec:
template:
metadata:
labels:
app.kubernetes.io/name: zookeeper
foo: bar
spec:
containers:
- name: zookeeper
image: zookeeper
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: zookeeper-pdb
spec:
maxUnavailable: 5
selector:
matchLabels:
app.kubernetes.io/name: zookeeper
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: zookeeper-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: zookeeper
minReplicas: 5
maxReplicas: 7
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: zookeeper
spec:
template:
metadata:
labels:
app.kubernetes.io/name: zookeeper
foo: bar
spec:
containers:
- name: zookeeper
image: zookeeper
---
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: zookeeper-pdb
spec:
minAvailable: 5
selector:
matchLabels:
app.kubernetes.io/name: zookeeper
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: zookeeper-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: no-match
minReplicas: 5
maxReplicas: 7
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
Loading