Skip to content

feat: add ability to configure custom init containers in DevWorkspace pods#20

Open
akurinnoy wants to merge 12 commits into
mainfrom
supervisor/8da68333-69ad-4eed-b62a-11388869e460
Open

feat: add ability to configure custom init containers in DevWorkspace pods#20
akurinnoy wants to merge 12 commits into
mainfrom
supervisor/8da68333-69ad-4eed-b62a-11388869e460

Conversation

@akurinnoy

Copy link
Copy Markdown
Owner

Summary

Implements the ability for platform administrators to configure custom init containers via the DevWorkspaceOperatorConfig (DWOC) API field config.workspace.initContainers (CRW-9373, CRW-9367).

DWOC API Field

Administrators set config.workspace.initContainers in the DWOC CR. Each listed container is injected as an init container in every workspace pod, running in the specified order.

init-persistent-home Override

If a container in initContainers has name: init-persistent-home, it overrides the default operator-generated home init container:

  • The default stow-based home initialization is replaced by the custom container
  • The custom container inherits the same volume mount (persistent-home) automatically
  • Only args and optionally command need to be specified; other fields are inherited

Validation: If the custom init-persistent-home container specifies a command other than ["/bin/sh", "-c"], the workspace fails with:

Invalid init-persistent-home container: command must be exactly [/bin/sh, -c]

Backward Compatibility

When config.workspace.initContainers is nil or empty, existing workspace behavior is completely unchanged — the default stow-based home initialization runs as before.

Changes

File Change
pkg/library/home/persistentHome.go Added isValidInitPersistentHomeCommand validation; fixed EnsureHomeInitContainerFields to use append-if-absent for VolumeMounts (not destructive replacement)
pkg/library/initcontainers/merge.go MergeInitContainers — strategic merge of DWOC init containers with operator-generated ones
controllers/workspace/devworkspace_controller.go DWOC init container injection block; nil-guard for PersistUserHome before dereferencing DisableInitContainer
pkg/library/home/custom_init_test.go Unit tests: TestEnsureHomeInitContainerFields, TestDWOCMultipleInitContainersFullFlow
pkg/library/home/testdata/persistent-home/*.yaml Three new fixture files for TestPersistentHomeVolume
pkg/library/initcontainers/merge_test.go Additional test cases: nil base, patch order preservation
controllers/workspace/devworkspace_controller_test.go Controller-level test for DWOC init container injection

Closes CRW-9373, CRW-9367

akurinnoy and others added 12 commits June 29, 2026 16:31
Adds a new 'DWOC init containers' Context block to devworkspace_controller_test.go
with three It() cases: basic custom init injection, init-persistent-home override
when persistent home is enabled, and skipping init-persistent-home when disabled.
Adds a minimal supporting fixture YAML for the new tests.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
The comment claimed the default init container was skipped when custom
init containers were present, but the code only checks DisableInitContainer.
Update the comment to accurately reflect the actual condition.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…itContainerFields

- Add isValidInitPersistentHomeCommand helper that returns true only when
  cmd equals ["/bin/sh", "-c"]
- Fix EnsureHomeInitContainerFields to validate non-empty Command via
  isValidInitPersistentHomeCommand, returning an error for invalid commands
- Fix EnsureHomeInitContainerFields to use append-if-absent pattern for
  VolumeMounts instead of replacing the entire slice, preserving
  pre-existing mounts with other names

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…ner in reconcile loop

Add nil check for workspace.Config.Workspace.PersistUserHome before calling
pointer.BoolDeref on PersistUserHome.DisableInitContainer to prevent a nil
pointer panic when PersistUserHome is not configured. Default disableHomeInit
to constants.DefaultDisableHomeInitContainer and only override inside the
nil-guarded block.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Add 4 new test cases to TestMergeInitContainers covering:
- nil base returns patches as result (no panic)
- patch overrides image while preserving args (strategic merge)
- new-named patch containers appended after base containers in patch order
- mixed case: base [A,B] + patches [B-override,C] = [A,B-merged,C]

Total test count: 9 (5 existing + 4 new). All pass with go vet clean.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
…ontainersFullFlow

Implements table-driven tests for EnsureHomeInitContainerFields covering:
- valid command [/bin/sh, -c]: no error, volumeMount added
- invalid command [/bin/bash, -c]: returns error with exact message
- empty command: set to [/bin/sh, -c], volumeMount added
- idempotency test: container with existing persistent-home AND my-secret
  VolumeMount — asserts no duplicate persistent-home AND my-secret preserved

Also implements TestDWOCMultipleInitContainersFullFlow which constructs a
DevWorkspaceWithConfig with multiple DWOC initContainers (one
init-persistent-home override + one extra-init), calls MergeInitContainers
and EnsureHomeInitContainerFields, and verifies correct ordering, persistent-home
VolumeMount presence, and custom container preservation.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Add three new fixture files for TestPersistentHomeVolume covering:
- DWOC with init-persistent-home initContainers (non-ephemeral, default init added)
- Ephemeral workspace with init-persistent-home in DWOC (NeedsPersistentHomeDirectory=true)
- No DWOC initContainers (backward compat, default stow behavior preserved)

Test count increases from 12 to 15.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant