Skip to content

Commit 948666e

Browse files
akurinnoyrohanKanojiatolushadkwon17
authored
Add ability to configure custom init containers (#1532)
* feat: configure init containers * chore: run make update_devworkspace_api update_devworkspace_crds generate_all * test: unit and e2e tests for init containers Signed-off-by: Oleksii Kurinnyi <[email protected]> Co-authored-by: Rohan Kumar <[email protected]> Co-authored-by: Anatolii Bazko <[email protected]> Co-authored-by: David Kwon <[email protected]>
1 parent 1eebe7b commit 948666e

25 files changed

+10168
-17
lines changed

apis/controller/v1alpha1/devworkspaceoperatorconfig_types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ type WorkspaceConfig struct {
252252
// If the feature is disabled, setting this field may cause an endless workspace start loop.
253253
// +kubebuilder:validation:Optional
254254
HostUsers *bool `json:"hostUsers,omitempty"`
255+
// InitContainers defines a list of Kubernetes init containers that are automatically injected into all workspace pods.
256+
// Typical uses cases include injecting organization tools/configs, initializing persistent home, etc.
257+
InitContainers []corev1.Container `json:"initContainers,omitempty"`
255258
}
256259

257260
type WebhookConfig struct {

apis/controller/v1alpha1/zz_generated.deepcopy.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

controllers/workspace/devworkspace_controller.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strings"
2323
"time"
2424

25+
"github.com/devfile/devworkspace-operator/pkg/library/initcontainers"
2526
"github.com/devfile/devworkspace-operator/pkg/library/ssh"
2627

2728
dw "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
@@ -369,6 +370,48 @@ func (r *DevWorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request
369370
devfilePodAdditions.InitContainers = append([]corev1.Container{*projectClone}, devfilePodAdditions.InitContainers...)
370371
}
371372

373+
// Inject operator-configured init containers
374+
if workspace.Config != nil && workspace.Config.Workspace != nil && len(workspace.Config.Workspace.InitContainers) > 0 {
375+
// Check if init-persistent-home should be disabled
376+
disableHomeInit := pointer.BoolDeref(workspace.Config.Workspace.PersistUserHome.DisableInitContainer, constants.DefaultDisableHomeInitContainer)
377+
378+
// Filter init containers from config based on workspace settings
379+
patches := []corev1.Container{}
380+
for _, container := range workspace.Config.Workspace.InitContainers {
381+
// Special handling for init-persistent-home
382+
if container.Name == constants.HomeInitComponentName {
383+
// Skip if persistent home is disabled
384+
if !home.PersistUserHomeEnabled(workspace) {
385+
reqLogger.Info("Skipping init-persistent-home container: persistent home is disabled")
386+
continue
387+
}
388+
// Skip if init container is explicitly disabled
389+
if disableHomeInit {
390+
reqLogger.Info("Skipping init-persistent-home container: DisableInitContainer is true")
391+
continue
392+
}
393+
}
394+
patches = append(patches, container)
395+
}
396+
397+
// Perform strategic merge
398+
merged, err := initcontainers.MergeInitContainers(devfilePodAdditions.InitContainers, patches)
399+
if err != nil {
400+
return r.failWorkspace(workspace, fmt.Sprintf("Failed to merge init containers: %s", err), metrics.ReasonBadRequest, reqLogger, &reconcileStatus), nil
401+
}
402+
403+
// Ensure init-persistent-home container has correct fields after merge
404+
for i := range merged {
405+
if merged[i].Name == constants.HomeInitComponentName {
406+
if err := home.EnsureHomeInitContainerFields(&merged[i]); err != nil {
407+
return r.failWorkspace(workspace, fmt.Sprintf("Failed to configure %s container: %s", constants.HomeInitComponentName, err), metrics.ReasonBadRequest, reqLogger, &reconcileStatus), nil
408+
}
409+
}
410+
}
411+
412+
devfilePodAdditions.InitContainers = merged
413+
}
414+
372415
// Add ServiceAccount tokens into devfile containers
373416
if err := wsprovision.ProvisionServiceAccountTokensInto(devfilePodAdditions, workspace); err != nil {
374417
return r.failWorkspace(workspace, fmt.Sprintf("Failed to mount ServiceAccount tokens to workspace: %s", err), metrics.ReasonBadRequest, reqLogger, &reconcileStatus), nil

controllers/workspace/devworkspace_controller_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ var _ = Describe("DevWorkspace Controller", func() {
150150
return err == nil
151151
}, timeout, interval).Should(BeTrue(), "DevWorkspace should exist in cluster")
152152

153+
By("Waiting for the first DevWorkspace to have its ID set")
154+
Eventually(func() (string, error) {
155+
if err := k8sClient.Get(ctx, dwNamespacedName, createdDW); err != nil {
156+
return "", err
157+
}
158+
return createdDW.Status.DevWorkspaceId, nil
159+
}, timeout, interval).Should(Equal("test-workspace-id"), "First DevWorkspace should have ID set from override annotation")
160+
153161
By("Creating a DevWorkspace that duplicates the workspace ID of the first")
154162
Expect(k8sClient.Create(ctx, devworkspace2)).Should(Succeed())
155163
defer deleteDevWorkspace(devworkspace2.Name)
@@ -407,6 +415,13 @@ var _ = Describe("DevWorkspace Controller", func() {
407415
createObject(gitCredentials)
408416
defer deleteObject(gitCredentials)
409417

418+
By("Waiting for DevWorkspaceRouting to be created")
419+
dwr := &controllerv1alpha1.DevWorkspaceRouting{}
420+
dwrName := common.DevWorkspaceRoutingName(workspaceID)
421+
Eventually(func() error {
422+
return k8sClient.Get(ctx, namespacedName(dwrName, testNamespace), dwr)
423+
}, timeout, interval).Should(Succeed(), "DevWorkspaceRouting should be created")
424+
410425
By("Manually making Routing ready to continue")
411426
markRoutingReady(testURL, common.DevWorkspaceRoutingName(workspaceID))
412427

0 commit comments

Comments
 (0)