Repository navigation
fix(webhook.go): Consider 0th index - #34
Merged
Merged
Conversation
Souheil-Yazji
force-pushed
the
sidecar-patch
branch
from
February 23, 2026 19:22
100ee08 to
d0cbbf7
Compare
Collaborator
Author
Collaborator
Author
|
Volumes fixed, merging. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Webhook Bug Fix: Multiple Sidecars Not Being Injected
Problem Description
When deploying with multiple volumes/shares to mount, only the last injected sidecar initContainer appeared in the mutated Pod.
Root Cause
Root Cause A — Repeated “first add” overwrote earlier additions
The original injection logic called helpers like this on every loop iteration:
The helper functions determine “first vs append” like this:
The problem: Each loop iteration passed the original, unchanged
pod.Spec.InitContainersThose slices were never updated during patch construction, solen(target)stayed0every time.That caused patches like:
[ {"op": "add", "path": "/spec/initContainers", "value": [container1]}, {"op": "add", "path": "/spec/initContainers", "value": [container2]}, {"op": "add", "path": "/spec/initContainers", "value": [container3]} ]In JSON Patch,
op: addon an existing field path effectively replaces the value, so only the last one survived.Root Cause B — Appending to an array that doesn’t exist
After switching to append semantics:
{"op":"add","path":"/spec/initContainers/-","value":{...}}Kubernetes rejects this when
/spec/initContainersis missing (not present at all), producing:This happens commonly for Pods created that don’t define
initContainersupfront.Solution
Step 1 — Ensure the arrays exist once (
initContainers)Before appending any entries, the webhook ensures these fields exist:
spec.initContainersis missing, add it as an empty array[]Example patches:
{"op":"add","path":"/spec/initContainers","value":[]}Step 2 — Always append with
/-Once the arrays exist, every injected initContainer is appended using
/-:{"op":"add","path":"/spec/initContainers/-","value":{...}}This removes the need to track “first vs subsequent” across loop iterations and prevents overwrites.
Correct Behavior Now
With
Nshares, the webhook generates:/spec/initContainers(only if missing)Example (simplified):
[ {"op":"add","path":"/spec/initContainers","value":[]}, {"op":"add","path":"/spec/initContainers/-","value":container1}, {"op":"add","path":"/spec/initContainers/-","value":container2}, ]Files Changed
webhook.goAdded “ensure array exists” logic for:
/spec/initContainersUpdated injection to append using
/-consistentlyRemoved reliance on
len(target) == 0for correctness across loop iterationsTesting Recommendations
Create a ConfigMap with multiple shares for a single SVM
Create a ConfigMap with multiple SVMs, each with multiple shares
Verify the mutated pod contains all expected initContainers and volumes
Check webhook logs to confirm:
add /spec/initContainers []appears only when missing/spec/initContainers/-Verify all volumes mount correctly in the running pod
Additional Notes
Each share injects:
Ensuring arrays exist prevents:
/spec/initContainers/-