Skip to content

Commit b00ae36

Browse files
fix(helm): preserve zero PDB budgets and validate minAvailable/maxUnavailable (#106)
* feat(helm): add deploymentAnnotations and podDisruptionBudget Support custom deployment metadata annotations and configurable PodDisruptionBudget for kagent-tools. Assisted-by: Gemini 3.7 Flash Signed-off-by: Anthony Hausman <anthonyhaussmann@gmail.com> * fix(helm): preserve zero PDB budgets and validate minAvailable/maxUnavailable Use hasKey so minAvailable/maxUnavailable of 0 are rendered, fail when both or neither are set, and add unit tests for these cases. Signed-off-by: Dmytro Rashko <dimetron@me.com> * feat(helm): combine kagent and istio PDB patterns - Add kagent-tools.pdb.isSet/validate helpers: null and "" are unset, 0 and percentages are kept; both-set and neither-set fail with actionable messages (mirrors kagent's controller PDB). - Default to maxUnavailable: 1 when enabled. - Only render the PDB when replicaCount > 1 (istio pattern), still validating config, and warn in NOTES when it is skipped. - Expand unit tests. Signed-off-by: Dmytro Rashko <dimetron@me.com> --------- Signed-off-by: Anthony Hausman <anthonyhaussmann@gmail.com> Signed-off-by: Dmytro Rashko <dimetron@me.com> Co-authored-by: Anthony Hausman <anthonyhaussmann@gmail.com>
1 parent b8c6431 commit b00ae36

7 files changed

Lines changed: 275 additions & 2 deletions

File tree

‎helm/kagent-tools/templates/NOTES.txt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
#
88
# 2. Then visit http://127.0.0.1:{{.Values.service.ports.tools.port}}/mcp to use MCP
99
#
10-
###############################################################################################
10+
###############################################################################################{{- if and (.Values.podDisruptionBudget).enabled (not (include "kagent-tools.pdb.shouldRender" .)) }}
11+
12+
WARNING: podDisruptionBudget.enabled is true but replicaCount is {{ .Values.replicaCount }}.
13+
The PodDisruptionBudget was not created; set replicaCount > 1 to enable it.
14+
{{- end }}

‎helm/kagent-tools/templates/_helpers.tpl‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,45 @@ over it would brick an install the value was never about.
120120
{{- end -}}
121121
{{- end -}}
122122

123+
{{/*
124+
Prints "1" when a PDB budget value is set. null and "" mean unset; 0 and
125+
percentage strings are valid budgets and must be kept.
126+
*/}}
127+
{{- define "kagent-tools.pdb.isSet" -}}
128+
{{- if not (kindIs "invalid" .) -}}
129+
{{- if ne (toString .) "" -}}1{{- end -}}
130+
{{- end -}}
131+
{{- end -}}
132+
133+
{{/*
134+
Kubernetes rejects a PodDisruptionBudget that sets both minAvailable and
135+
maxUnavailable, and one that sets neither is meaningless, so both fail at
136+
template time. Runs whenever the PDB is enabled, even if the replica guard
137+
skips rendering, so misconfiguration surfaces before replicas are scaled up.
138+
*/}}
139+
{{- define "kagent-tools.pdb.validate" -}}
140+
{{- $pdb := .Values.podDisruptionBudget | default dict -}}
141+
{{- if $pdb.enabled -}}
142+
{{- $hasMinAvailable := include "kagent-tools.pdb.isSet" $pdb.minAvailable -}}
143+
{{- $hasMaxUnavailable := include "kagent-tools.pdb.isSet" $pdb.maxUnavailable -}}
144+
{{- if and $hasMinAvailable $hasMaxUnavailable -}}
145+
{{- fail "podDisruptionBudget: minAvailable and maxUnavailable are mutually exclusive. Set exactly one (to use minAvailable, set podDisruptionBudget.maxUnavailable=null)." -}}
146+
{{- end -}}
147+
{{- if not (or $hasMinAvailable $hasMaxUnavailable) -}}
148+
{{- fail "podDisruptionBudget is enabled but neither minAvailable nor maxUnavailable is set. Set exactly one." -}}
149+
{{- end -}}
150+
{{- end -}}
151+
{{- end -}}
152+
153+
{{/*
154+
A PodDisruptionBudget is only rendered for more than one replica: with a single
155+
replica it either protects nothing or blocks node drains entirely.
156+
*/}}
157+
{{- define "kagent-tools.pdb.shouldRender" -}}
158+
{{- $pdb := .Values.podDisruptionBudget | default dict -}}
159+
{{- if and $pdb.enabled (gt (int .Values.replicaCount) 1) -}}1{{- end -}}
160+
{{- end -}}
161+
123162
{{/*
124163
Pull secrets for the pod: the chart's own list merged (union) with
125164
global.imagePullSecrets. Renders nothing when both are empty.

‎helm/kagent-tools/templates/deployment.yaml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ metadata:
55
namespace: {{ include "kagent-tools.namespace" . }}
66
labels:
77
{{- include "kagent-tools.labels" . | nindent 4 }}
8+
{{- with .Values.deploymentAnnotations }}
9+
annotations:
10+
{{- toYaml . | nindent 4 }}
11+
{{- end }}
812
spec:
913
replicas: {{ .Values.replicaCount }}
1014
selector:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{{- include "kagent-tools.pdb.validate" . }}
2+
{{- if include "kagent-tools.pdb.shouldRender" . }}
3+
{{- $pdb := .Values.podDisruptionBudget }}
4+
apiVersion: policy/v1
5+
kind: PodDisruptionBudget
6+
metadata:
7+
name: {{ include "kagent-tools.fullname" . }}
8+
namespace: {{ include "kagent-tools.namespace" . }}
9+
labels:
10+
{{- include "kagent-tools.labels" . | nindent 4 }}
11+
{{- with $pdb.labels }}
12+
{{- toYaml . | nindent 4 }}
13+
{{- end }}
14+
{{- with $pdb.annotations }}
15+
annotations:
16+
{{- toYaml . | nindent 4 }}
17+
{{- end }}
18+
spec:
19+
{{- if include "kagent-tools.pdb.isSet" $pdb.minAvailable }}
20+
minAvailable: {{ $pdb.minAvailable }}
21+
{{- else }}
22+
maxUnavailable: {{ $pdb.maxUnavailable }}
23+
{{- end }}
24+
{{- with $pdb.unhealthyPodEvictionPolicy }}
25+
unhealthyPodEvictionPolicy: {{ . }}
26+
{{- end }}
27+
selector:
28+
matchLabels:
29+
{{- /* Same helper as the Deployment's spec.selector, so the budget cannot drift from its pods. */}}
30+
{{- include "kagent-tools.selectorLabels" . | nindent 6 }}
31+
{{- end }}

‎helm/kagent-tools/tests/deployment_test.yaml‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,23 @@ tests:
173173
value:
174174
app.kubernetes.io/name: RELEASE-NAME
175175
app.kubernetes.io/instance: RELEASE-NAME
176+
177+
- it: should not have deployment annotations by default
178+
template: deployment.yaml
179+
asserts:
180+
- isNull:
181+
path: metadata.annotations
182+
183+
- it: should render custom deployment annotations
184+
template: deployment.yaml
185+
set:
186+
deploymentAnnotations:
187+
keel.sh/policy: "force"
188+
example.com/annotation: "test-value"
189+
asserts:
190+
- equal:
191+
path: metadata.annotations["keel.sh/policy"]
192+
value: "force"
193+
- equal:
194+
path: metadata.annotations["example.com/annotation"]
195+
value: "test-value"
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
suite: test pod disruption budget
2+
templates:
3+
- pdb.yaml
4+
tests:
5+
- it: should not render pdb by default
6+
asserts:
7+
- hasDocuments:
8+
count: 0
9+
10+
- it: should render default maxUnavailable of 1 when enabled with multiple replicas
11+
set:
12+
replicaCount: 2
13+
podDisruptionBudget:
14+
enabled: true
15+
asserts:
16+
- isKind:
17+
of: PodDisruptionBudget
18+
- equal:
19+
path: metadata.name
20+
value: RELEASE-NAME
21+
- equal:
22+
path: spec.maxUnavailable
23+
value: 1
24+
- notExists:
25+
path: spec.minAvailable
26+
- notExists:
27+
path: spec.unhealthyPodEvictionPolicy
28+
- equal:
29+
path: spec.selector.matchLabels
30+
value:
31+
app.kubernetes.io/name: RELEASE-NAME
32+
app.kubernetes.io/instance: RELEASE-NAME
33+
34+
- it: should not render pdb with a single replica
35+
set:
36+
replicaCount: 1
37+
podDisruptionBudget:
38+
enabled: true
39+
asserts:
40+
- hasDocuments:
41+
count: 0
42+
43+
- it: should still validate pdb settings with a single replica
44+
set:
45+
replicaCount: 1
46+
podDisruptionBudget:
47+
enabled: true
48+
minAvailable: 1
49+
asserts:
50+
- failedTemplate:
51+
errorMessage: "podDisruptionBudget: minAvailable and maxUnavailable are mutually exclusive. Set exactly one (to use minAvailable, set podDisruptionBudget.maxUnavailable=null)."
52+
53+
- it: should render minAvailable when maxUnavailable is nulled
54+
set:
55+
replicaCount: 3
56+
podDisruptionBudget:
57+
enabled: true
58+
minAvailable: "50%"
59+
maxUnavailable: null
60+
asserts:
61+
- equal:
62+
path: spec.minAvailable
63+
value: "50%"
64+
- notExists:
65+
path: spec.maxUnavailable
66+
67+
- it: should render minAvailable of zero
68+
set:
69+
replicaCount: 2
70+
podDisruptionBudget:
71+
enabled: true
72+
minAvailable: 0
73+
maxUnavailable: null
74+
asserts:
75+
- equal:
76+
path: spec.minAvailable
77+
value: 0
78+
- notExists:
79+
path: spec.maxUnavailable
80+
81+
- it: should render maxUnavailable of zero
82+
set:
83+
replicaCount: 2
84+
podDisruptionBudget:
85+
enabled: true
86+
maxUnavailable: 0
87+
asserts:
88+
- equal:
89+
path: spec.maxUnavailable
90+
value: 0
91+
- notExists:
92+
path: spec.minAvailable
93+
94+
- it: should treat empty string budgets as unset
95+
set:
96+
replicaCount: 2
97+
podDisruptionBudget:
98+
enabled: true
99+
minAvailable: ""
100+
maxUnavailable: "25%"
101+
asserts:
102+
- equal:
103+
path: spec.maxUnavailable
104+
value: "25%"
105+
- notExists:
106+
path: spec.minAvailable
107+
108+
- it: should render eviction policy, labels and annotations
109+
set:
110+
replicaCount: 2
111+
podDisruptionBudget:
112+
enabled: true
113+
unhealthyPodEvictionPolicy: AlwaysAllow
114+
labels:
115+
custom-label: pdb-label
116+
annotations:
117+
custom-annotation: pdb-annotation
118+
asserts:
119+
- equal:
120+
path: spec.unhealthyPodEvictionPolicy
121+
value: AlwaysAllow
122+
- equal:
123+
path: metadata.labels["custom-label"]
124+
value: pdb-label
125+
- equal:
126+
path: metadata.labels["app.kubernetes.io/name"]
127+
value: RELEASE-NAME
128+
- equal:
129+
path: metadata.annotations["custom-annotation"]
130+
value: pdb-annotation
131+
132+
- it: should fail when both minAvailable and maxUnavailable are set
133+
set:
134+
replicaCount: 2
135+
podDisruptionBudget:
136+
enabled: true
137+
minAvailable: 1
138+
maxUnavailable: 1
139+
asserts:
140+
- failedTemplate:
141+
errorMessage: "podDisruptionBudget: minAvailable and maxUnavailable are mutually exclusive. Set exactly one (to use minAvailable, set podDisruptionBudget.maxUnavailable=null)."
142+
143+
- it: should fail when neither minAvailable nor maxUnavailable is set
144+
set:
145+
replicaCount: 2
146+
podDisruptionBudget:
147+
enabled: true
148+
maxUnavailable: null
149+
asserts:
150+
- failedTemplate:
151+
errorMessage: "podDisruptionBudget is enabled but neither minAvailable nor maxUnavailable is set. Set exactly one."

‎helm/kagent-tools/values.yaml‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ imagePullSecrets: []
7878
nameOverride: ""
7979
fullnameOverride: ""
8080

81+
# Annotations to add to the deployment
82+
deploymentAnnotations: {}
83+
8184
podAnnotations: {}
8285

8386
podSecurityContext: {}
@@ -90,7 +93,28 @@ securityContext: {}
9093
# readOnlyRootFilesystem: true
9194
# runAsNonRoot: true
9295
# runAsUser: 1000
93-
96+
97+
# PodDisruptionBudget for the tools Deployment.
98+
# Only rendered when replicaCount > 1: with a single replica a PDB either
99+
# protects nothing or blocks node drains entirely.
100+
podDisruptionBudget:
101+
# -- Set to true to create the PodDisruptionBudget.
102+
enabled: false
103+
# -- Minimum number of pods that must remain available. Int or percentage
104+
# string (e.g. `1` or `"50%"`). Mutually exclusive with `maxUnavailable`;
105+
# to use it, also set `maxUnavailable: null`.
106+
minAvailable: null
107+
# -- Maximum number of pods that may be unavailable. Int or percentage
108+
# string (e.g. `1` or `"50%"`). Mutually exclusive with `minAvailable`.
109+
maxUnavailable: 1
110+
# -- `spec.unhealthyPodEvictionPolicy`: `IfHealthyBudget` (Kubernetes
111+
# default) or `AlwaysAllow`. Unset leaves the Kubernetes default.
112+
unhealthyPodEvictionPolicy: ""
113+
# -- Extra labels for the PodDisruptionBudget.
114+
labels: {}
115+
# -- Annotations for the PodDisruptionBudget.
116+
annotations: {}
117+
94118
# Node taints which will be tolerated for Pod.
95119
tolerations: []
96120

0 commit comments

Comments
 (0)