Returns a modified copy of the given DevWorkspace's Template Spec which contains an additional Devfile volume 'persistentHome' that is mounted to `/home/user/` for every container component defined in the DevWorkspace. An error is returned if the addition of the 'persistentHome' volume would result
(workspace *common.DevWorkspaceWithConfig)
| 50 | // An error is returned if the addition of the 'persistentHome' volume would result |
| 51 | // in an invalid DevWorkspace. |
| 52 | func AddPersistentHomeVolume(workspace *common.DevWorkspaceWithConfig) (*v1alpha2.DevWorkspaceTemplateSpec, error) { |
| 53 | dwTemplateSpecCopy := workspace.Spec.Template.DeepCopy() |
| 54 | homeVolume := v1alpha2.Component{ |
| 55 | Name: constants.HomeVolumeName, |
| 56 | ComponentUnion: v1alpha2.ComponentUnion{ |
| 57 | Volume: &v1alpha2.VolumeComponent{}, |
| 58 | }, |
| 59 | } |
| 60 | homeVolumeMount := v1alpha2.VolumeMount{ |
| 61 | Name: constants.HomeVolumeName, |
| 62 | Path: constants.HomeUserDirectory, |
| 63 | } |
| 64 | |
| 65 | // Add default init container only if not disabled and no custom init is configured |
| 66 | if workspace.Config.Workspace.PersistUserHome.DisableInitContainer == nil || !*workspace.Config.Workspace.PersistUserHome.DisableInitContainer { |
| 67 | err := addInitContainer(dwTemplateSpecCopy) |
| 68 | if err != nil { |
| 69 | return nil, fmt.Errorf("failed to add init container for home persistence setup: %w", err) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | dwTemplateSpecCopy.Components = append(dwTemplateSpecCopy.Components, homeVolume) |
| 74 | for _, component := range dwTemplateSpecCopy.Components { |
| 75 | if component.Container == nil { |
| 76 | continue |
| 77 | } |
| 78 | component.Container.VolumeMounts = append(component.Container.VolumeMounts, homeVolumeMount) |
| 79 | } |
| 80 | |
| 81 | err := devfilevalidation.ValidateComponents(dwTemplateSpecCopy.Components) |
| 82 | if err != nil { |
| 83 | return nil, fmt.Errorf("addition of %s volume would render DevWorkspace invalid: %w", constants.HomeVolumeName, err) |
| 84 | } |
| 85 | |
| 86 | return dwTemplateSpecCopy, nil |
| 87 | } |
| 88 | |
| 89 | // Returns true if persistUserHome is enabled, the workspace has at least one container component, |
| 90 | // and no container already mounts a volume to /home/user/. |