-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathpersistentHome.go
242 lines (209 loc) · 8.09 KB
/
persistentHome.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
// Copyright (c) 2019-2024 Red Hat, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package home
import (
"fmt"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
devfilevalidation "github.com/devfile/api/v2/pkg/validation"
"github.com/devfile/devworkspace-operator/pkg/provision/storage"
"k8s.io/utils/pointer"
"github.com/devfile/devworkspace-operator/pkg/common"
"github.com/devfile/devworkspace-operator/pkg/constants"
)
const initScript = `(echo "Checking for stow command"
STOW_COMPLETE=/home/user/.stow_completed
if command -v stow &> /dev/null; then
if [ ! -f $STOW_COMPLETE ]; then
echo "Running stow command"
stow . -t /home/user/ -d /home/tooling/ --no-folding -v 2 > /home/user/.stow.log 2>&1
cp -n /home/tooling/.viminfo /home/user/.viminfo
cp -n /home/tooling/.bashrc /home/user/.bashrc
cp -n /home/tooling/.bash_profile /home/user/.bash_profile
touch $STOW_COMPLETE
else
echo "Stow command already run. If you wish to re-run it, delete $STOW_COMPLETE from the persistent volume and restart the workspace."
fi
else
echo "Stow command not found"
fi) || true
`
// 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
// in an invalid DevWorkspace.
func AddPersistentHomeVolume(workspace *common.DevWorkspaceWithConfig) (*v1alpha2.DevWorkspaceTemplateSpec, error) {
dwTemplateSpecCopy := workspace.Spec.Template.DeepCopy()
homeVolume := v1alpha2.Component{
Name: constants.HomeVolumeName,
ComponentUnion: v1alpha2.ComponentUnion{
Volume: &v1alpha2.VolumeComponent{},
},
}
homeVolumeMount := v1alpha2.VolumeMount{
Name: constants.HomeVolumeName,
Path: constants.HomeUserDirectory,
}
if workspace.Config.Workspace.PersistUserHome.DisableInitContainer == nil || !*workspace.Config.Workspace.PersistUserHome.DisableInitContainer {
err := addInitContainer(dwTemplateSpecCopy)
if err != nil {
return nil, fmt.Errorf("failed to add init container for home persistence setup: %w", err)
}
}
dwTemplateSpecCopy.Components = append(dwTemplateSpecCopy.Components, homeVolume)
for _, component := range dwTemplateSpecCopy.Components {
if component.Container == nil {
continue
}
component.Container.VolumeMounts = append(component.Container.VolumeMounts, homeVolumeMount)
}
err := devfilevalidation.ValidateComponents(dwTemplateSpecCopy.Components)
if err != nil {
return nil, fmt.Errorf("addition of %s volume would render DevWorkspace invalid: %w", constants.HomeVolumeName, err)
}
return dwTemplateSpecCopy, nil
}
// Returns true if the following criteria is met:
// - `persistUserHome` is enabled in the DevWorkspaceOperatorConfig
// - The storage strategy used by the DevWorkspace supports home persistence
// - None of the container components in the DevWorkspace mount a volume to `/home/user/`.
// - Persistent storage is required for the DevWorkspace
// Returns false otherwise.
func NeedsPersistentHomeDirectory(workspace *common.DevWorkspaceWithConfig) bool {
if !PersistUserHomeEnabled(workspace) || !storageStrategySupportsPersistentHome(workspace) {
return false
}
for _, component := range workspace.Spec.Template.Components {
if component.Container == nil {
continue
}
for _, volumeMount := range component.Container.VolumeMounts {
if volumeMount.Path == constants.HomeUserDirectory {
// If a volume is already being mounted to /home/user/, it takes precedence
// over the DWO-provisioned home directory volume.
return false
}
}
}
return storage.WorkspaceNeedsStorage(&workspace.Spec.Template)
}
func PersistUserHomeEnabled(workspace *common.DevWorkspaceWithConfig) bool {
return pointer.BoolDeref(workspace.Config.Workspace.PersistUserHome.Enabled, false)
}
// Returns true if the workspace's storage strategy supports persisting the user home directory.
// The storage strategies which support home persistence are: per-user/common, per-workspace & async.
// The ephemeral storage strategy does not support home persistence.
func storageStrategySupportsPersistentHome(workspace *common.DevWorkspaceWithConfig) bool {
storageClass := workspace.Spec.Template.Attributes.GetString(constants.DevWorkspaceStorageTypeAttribute, nil)
return storageClass != constants.EphemeralStorageClassType
}
func addInitContainer(dwTemplateSpec *v1alpha2.DevWorkspaceTemplateSpec) error {
if initComponentExists(dwTemplateSpec) {
return fmt.Errorf("component named %s already exists in the devworkspace", constants.HomeInitComponentName)
}
if initCommandExists(dwTemplateSpec) {
return fmt.Errorf("command with id %s already exists in the devworkspace", constants.HomeInitEventId)
}
if initEventExists(dwTemplateSpec) {
return fmt.Errorf("event with id %s already exists in the devworkspace", constants.HomeInitEventId)
}
initContainer := inferInitContainer(dwTemplateSpec)
if initContainer == nil {
return fmt.Errorf("cannot infer initcontainer for home persistence setup")
}
addInitContainerComponent(dwTemplateSpec, initContainer)
if dwTemplateSpec.Commands == nil {
dwTemplateSpec.Commands = []v1alpha2.Command{}
}
if dwTemplateSpec.Events == nil {
dwTemplateSpec.Events = &v1alpha2.Events{}
}
dwTemplateSpec.Commands = append(dwTemplateSpec.Commands, v1alpha2.Command{
Id: constants.HomeInitEventId,
CommandUnion: v1alpha2.CommandUnion{
Apply: &v1alpha2.ApplyCommand{
Component: constants.HomeInitComponentName,
},
},
})
dwTemplateSpec.Events.PreStart = append(dwTemplateSpec.Events.PreStart, constants.HomeInitEventId)
return nil
}
func initComponentExists(dwTemplateSpec *v1alpha2.DevWorkspaceTemplateSpec) bool {
if dwTemplateSpec.Components == nil {
return false
}
for _, component := range dwTemplateSpec.Components {
if component.Name == constants.HomeInitComponentName {
return true
}
}
return false
}
func initCommandExists(dwTemplateSpec *v1alpha2.DevWorkspaceTemplateSpec) bool {
if dwTemplateSpec.Commands == nil {
return false
}
for _, command := range dwTemplateSpec.Commands {
if command.Id == constants.HomeInitEventId {
return true
}
}
return false
}
func initEventExists(dwTemplateSpec *v1alpha2.DevWorkspaceTemplateSpec) bool {
if dwTemplateSpec.Events == nil {
return false
}
for _, event := range dwTemplateSpec.Events.PreStart {
if event == constants.HomeInitEventId {
return true
}
}
return false
}
func addInitContainerComponent(dwTemplateSpec *v1alpha2.DevWorkspaceTemplateSpec, initContainer *v1alpha2.Container) {
initComponent := v1alpha2.Component{
Name: constants.HomeInitComponentName,
ComponentUnion: v1alpha2.ComponentUnion{
Container: &v1alpha2.ContainerComponent{
Container: *initContainer,
},
},
}
dwTemplateSpec.Components = append(dwTemplateSpec.Components, initComponent)
}
func inferInitContainer(dwTemplateSpec *v1alpha2.DevWorkspaceTemplateSpec) *v1alpha2.Container {
var nonImportedComponent v1alpha2.Component
for _, component := range dwTemplateSpec.Components {
if component.Container == nil {
continue
}
pluginSource := component.Attributes.GetString(constants.PluginSourceAttribute, nil)
if pluginSource == "" || pluginSource == "parent" {
// First non-imported container component is selected
nonImportedComponent = component
break
}
}
if nonImportedComponent.Name != "" {
image := nonImportedComponent.Container.Image
return &v1alpha2.Container{
Image: image,
Command: []string{"/bin/sh", "-c"},
Args: []string{initScript},
}
}
return nil
}