@@ -150,6 +150,20 @@ func validateHomeInitContainer(c corev1.Container) error {
150150 return nil
151151}
152152
153+ // ensureHomeInitContainerFields ensures that an init-persistent-home container has
154+ // the correct Command, Args, and VolumeMounts.
155+ func ensureHomeInitContainerFields (c * corev1.Container ) error {
156+ c .Command = []string {"/bin/sh" , "-c" }
157+ if len (c .Args ) != 1 {
158+ return fmt .Errorf ("args must contain exactly one script string for %s" , constants .HomeInitComponentName )
159+ }
160+ c .VolumeMounts = []corev1.VolumeMount {{
161+ Name : constants .HomeVolumeName ,
162+ MountPath : constants .HomeUserDirectory ,
163+ }}
164+ return nil
165+ }
166+
153167// defaultAndValidateHomeInitContainer applies defaults and validation for a custom
154168// DWOC-provided init container named init-persistent-home. It ensures a shell execution
155169// model, a single script arg, injects the persistent-home mount at /home/user/, and
@@ -165,10 +179,9 @@ func defaultAndValidateHomeInitContainer(c corev1.Container, workspace *common.D
165179 return c , err
166180 }
167181
168- c .VolumeMounts = []corev1.VolumeMount {{
169- Name : constants .HomeVolumeName ,
170- MountPath : constants .HomeUserDirectory ,
171- }}
182+ if err = ensureHomeInitContainerFields (& c ); err != nil {
183+ return c , err
184+ }
172185
173186 return c , nil
174187}
@@ -512,17 +525,9 @@ func (r *DevWorkspaceReconciler) Reconcile(ctx context.Context, req ctrl.Request
512525 // Ensure init-persistent-home container have correct fields after merge
513526 for i := range merged {
514527 if merged [i ].Name == constants .HomeInitComponentName {
515- // Ensure Command is correct (should be set by defaultAndValidateHomeInitContainer, but enforce after merge)
516- merged [i ].Command = []string {"/bin/sh" , "-c" }
517- // Args should be set by patch validation, but ensure it has exactly one element
518- if len (merged [i ].Args ) != 1 {
519- return r .failWorkspace (workspace , fmt .Sprintf ("Invalid %s container: args must contain exactly one script string" , constants .HomeInitComponentName ), metrics .ReasonBadRequest , reqLogger , & reconcileStatus ), nil
528+ if err := ensureHomeInitContainerFields (& merged [i ]); err != nil {
529+ return r .failWorkspace (workspace , fmt .Sprintf ("Invalid %s container: %s" , constants .HomeInitComponentName , err ), metrics .ReasonBadRequest , reqLogger , & reconcileStatus ), nil
520530 }
521- // Ensure VolumeMounts are correct
522- merged [i ].VolumeMounts = []corev1.VolumeMount {{
523- Name : constants .HomeVolumeName ,
524- MountPath : constants .HomeUserDirectory ,
525- }}
526531 }
527532 }
528533
0 commit comments