-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathdeployment.go
More file actions
398 lines (354 loc) · 14.8 KB
/
deployment.go
File metadata and controls
398 lines (354 loc) · 14.8 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//
// Copyright (c) 2019-2025 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 workspace
import (
"context"
"errors"
"fmt"
"math"
"time"
"github.com/devfile/devworkspace-operator/pkg/dwerrors"
"github.com/devfile/devworkspace-operator/pkg/library/overrides"
"github.com/devfile/devworkspace-operator/pkg/library/status"
nsconfig "github.com/devfile/devworkspace-operator/pkg/provision/config"
"github.com/devfile/devworkspace-operator/pkg/provision/sync"
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
maputils "github.com/devfile/devworkspace-operator/internal/map"
"github.com/devfile/devworkspace-operator/pkg/common"
"github.com/devfile/devworkspace-operator/pkg/constants"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
k8sErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
func SyncDeploymentToCluster(
workspace *common.DevWorkspaceWithConfig,
podAdditions []v1alpha1.PodAdditions,
saName string,
clusterAPI sync.ClusterAPI) error {
podTolerations, nodeSelector, err := nsconfig.GetNamespacePodTolerationsAndNodeSelector(workspace.Namespace, clusterAPI)
if err != nil {
return &dwerrors.FailError{Message: "Failed to read pod tolerations and node selector from namespace", Err: err}
}
// [design] we have to pass components and routing pod additions separately because we need mountsources from each
// component.
specDeployment, err := getSpecDeployment(workspace, podAdditions, saName, podTolerations, nodeSelector, clusterAPI.Scheme)
if err != nil {
return &dwerrors.FailError{Message: "Error while creating workspace deployment", Err: err}
}
if len(specDeployment.Spec.Template.Spec.Containers) == 0 {
// DevWorkspace defines no container components, cannot create a deployment
return nil
}
clusterObj, err := sync.SyncObjectWithCluster(specDeployment, clusterAPI)
if err != nil {
return dwerrors.WrapSyncError(err)
}
clusterDeployment := clusterObj.(*appsv1.Deployment)
deploymentReady := status.CheckDeploymentStatus(clusterDeployment, workspace)
if !deploymentReady {
deploymentHealthy, deploymentErrMsg := status.CheckDeploymentConditions(clusterDeployment)
if !deploymentHealthy {
return &dwerrors.FailError{Message: deploymentErrMsg}
}
workspaceIDLabel := k8sclient.MatchingLabels{constants.DevWorkspaceIDLabel: workspace.Status.DevWorkspaceId}
ignoredEvents := workspace.Config.Workspace.IgnoredUnrecoverableEvents
failureMsg, checkErr := status.CheckPodsState(workspace.Status.DevWorkspaceId, workspace.Namespace, workspaceIDLabel, ignoredEvents, clusterAPI)
if checkErr != nil {
return err
}
if failureMsg != "" {
return &dwerrors.FailError{Message: failureMsg}
}
return &dwerrors.RetryError{Message: "Deployment is not ready"}
}
return nil
}
// DeleteWorkspaceDeployment deletes the deployment for the DevWorkspace
func DeleteWorkspaceDeployment(ctx context.Context, workspace *common.DevWorkspaceWithConfig, client k8sclient.Client) (wait bool, err error) {
err = client.Delete(ctx, &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: workspace.Namespace,
Name: common.DeploymentName(workspace.Status.DevWorkspaceId),
},
})
if err != nil {
if k8sErrors.IsNotFound(err) {
return false, nil
}
return false, err
}
return true, nil
}
// ScaleDeploymentToZero scales the cluster deployment to zero
func ScaleDeploymentToZero(ctx context.Context, workspace *common.DevWorkspaceWithConfig, client k8sclient.Client) error {
patch := []byte(`{"spec":{"replicas": 0}}`)
err := client.Patch(ctx, &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Namespace: workspace.Namespace,
Name: common.DeploymentName(workspace.Status.DevWorkspaceId),
},
}, k8sclient.RawPatch(types.StrategicMergePatchType, patch))
if err != nil && !k8sErrors.IsNotFound(err) {
return err
}
return nil
}
func getSpecDeployment(
workspace *common.DevWorkspaceWithConfig,
podAdditionsList []v1alpha1.PodAdditions,
saName string,
podTolerations []corev1.Toleration,
nodeSelector map[string]string,
scheme *runtime.Scheme) (*appsv1.Deployment, error) {
replicas := int32(1)
terminationGracePeriod := int64(10)
podAdditions, err := mergePodAdditions(podAdditionsList)
if err != nil {
return nil, err
}
for idx := range podAdditions.Containers {
podAdditions.Containers[idx].VolumeMounts = append(podAdditions.Containers[idx].VolumeMounts, podAdditions.VolumeMounts...)
}
for idx := range podAdditions.InitContainers {
podAdditions.InitContainers[idx].VolumeMounts = append(podAdditions.InitContainers[idx].VolumeMounts, podAdditions.VolumeMounts...)
}
labels := map[string]string{}
labels[constants.DevWorkspaceIDLabel] = workspace.Status.DevWorkspaceId
labels[constants.DevWorkspaceNameLabel] = workspace.Name
annotations, err := getAdditionalDeploymentAnnotations(workspace)
if err != nil {
return nil, err
}
deploymentStrategy := appsv1.DeploymentStrategy{
Type: workspace.Config.Workspace.DeploymentStrategy,
}
if workspace.Config.Workspace.DeploymentStrategy == appsv1.RollingUpdateDeploymentStrategyType {
deploymentStrategy.RollingUpdate = &appsv1.RollingUpdateDeployment{
MaxUnavailable: &constants.RollingUpdateMaxUnavailable,
MaxSurge: &constants.RollingUpdateMaximumSurge,
}
}
progressDeadlineSeconds, err := getProgressDeadlineSeconds(workspace.Config)
if err != nil {
return nil, err
}
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: common.DeploymentName(workspace.Status.DevWorkspaceId),
Namespace: workspace.Namespace,
Labels: labels,
Annotations: annotations,
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
constants.DevWorkspaceIDLabel: workspace.Status.DevWorkspaceId,
},
},
Strategy: deploymentStrategy,
ProgressDeadlineSeconds: &progressDeadlineSeconds,
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: workspace.Status.DevWorkspaceId,
Namespace: workspace.Namespace,
Labels: map[string]string{
constants.DevWorkspaceIDLabel: workspace.Status.DevWorkspaceId,
constants.DevWorkspaceNameLabel: workspace.Name,
},
Annotations: podAdditions.Annotations,
},
Spec: corev1.PodSpec{
InitContainers: podAdditions.InitContainers,
Containers: podAdditions.Containers,
ImagePullSecrets: podAdditions.PullSecrets,
Volumes: podAdditions.Volumes,
RestartPolicy: "Always",
TerminationGracePeriodSeconds: &terminationGracePeriod,
SchedulerName: workspace.Config.Workspace.SchedulerName,
SecurityContext: workspace.Config.Workspace.PodSecurityContext,
ServiceAccountName: saName,
AutomountServiceAccountToken: nil,
RuntimeClassName: workspace.Config.Workspace.RuntimeClassName,
HostUsers: workspace.Config.Workspace.HostUsers,
},
},
},
}
if overrides.NeedsPodOverrides(workspace) {
patchedDeployment, err := overrides.ApplyPodOverrides(workspace, deployment)
if err != nil {
return nil, err
}
deployment = patchedDeployment
}
if len(podTolerations) > 0 {
deployment.Spec.Template.Spec.Tolerations = podTolerations
}
if len(nodeSelector) > 0 {
deployment.Spec.Template.Spec.NodeSelector = nodeSelector
}
if workspace.Spec.Template.Attributes.Exists(constants.RuntimeClassNameAttribute) {
runtimeClassName := workspace.Spec.Template.Attributes.GetString(constants.RuntimeClassNameAttribute, nil)
if runtimeClassName != "" {
deployment.Spec.Template.Spec.RuntimeClassName = &runtimeClassName
}
}
if needPVC, pvcName := needsPVCWorkaround(podAdditions, workspace.Config.Workspace.PVCName); needPVC {
// Kubernetes creates directories in a PVC to support subpaths such that only the leaf directory has g+rwx permissions.
// This means that mounting the subpath e.g. <workspace-id>/plugins will result in the <workspace-id> directory being
// created with 755 permissions, requiring the root UID to remove it.
// To avoid this issue, we need to ensure that the first volumeMount encountered is for the <workspace-id> subpath.
if len(deployment.Spec.Template.Spec.InitContainers) > 0 {
volumeMounts := deployment.Spec.Template.Spec.InitContainers[0].VolumeMounts
volumeMounts = append([]corev1.VolumeMount{getWorkspaceSubpathVolumeMount(workspace.Status.DevWorkspaceId, pvcName)}, volumeMounts...)
deployment.Spec.Template.Spec.InitContainers[0].VolumeMounts = volumeMounts
} else {
volumeMounts := deployment.Spec.Template.Spec.Containers[0].VolumeMounts
volumeMounts = append([]corev1.VolumeMount{getWorkspaceSubpathVolumeMount(workspace.Status.DevWorkspaceId, pvcName)}, volumeMounts...)
deployment.Spec.Template.Spec.Containers[0].VolumeMounts = volumeMounts
}
}
workspaceCreator, present := workspace.Labels[constants.DevWorkspaceCreatorLabel]
if present {
deployment.Labels[constants.DevWorkspaceCreatorLabel] = workspaceCreator
deployment.Spec.Template.Labels[constants.DevWorkspaceCreatorLabel] = workspaceCreator
} else {
return nil, errors.New("workspace must have creator specified to be run. Recreate it to fix an issue")
}
restrictedAccess, present := workspace.Annotations[constants.DevWorkspaceRestrictedAccessAnnotation]
if present {
deployment.Annotations = maputils.Append(deployment.Annotations, constants.DevWorkspaceRestrictedAccessAnnotation, restrictedAccess)
deployment.Spec.Template.Annotations = maputils.Append(deployment.Spec.Template.Annotations, constants.DevWorkspaceRestrictedAccessAnnotation, restrictedAccess)
}
err = controllerutil.SetControllerReference(workspace.DevWorkspace, deployment, scheme)
if err != nil {
return nil, err
}
return deployment, nil
}
// Returns the ProgressDeadlineSeconds to use for workspace deployments as an int32.
// The ProgressDeadLineSeconds returned is the same length of time as DWOC's workspace.ProgressTimeout.
// Returns an error if ProgressTimeout could not be properly parsed,
// or if the ProgressTimeout would exceed the maximum capacity of an int32
func getProgressDeadlineSeconds(config *v1alpha1.OperatorConfiguration) (int32, error) {
timeout, err := time.ParseDuration(config.Workspace.ProgressTimeout)
if err != nil {
return 0, fmt.Errorf("invalid duration specified for workspace progress timeout: %w", err)
}
// Prevent overflow
if timeout.Seconds() > math.MaxInt32 {
return 0, fmt.Errorf("duration specified for workspace progress timeout is too long: %s", config.Workspace.ProgressTimeout)
}
return int32(timeout.Seconds()), nil
}
func mergePodAdditions(toMerge []v1alpha1.PodAdditions) (*v1alpha1.PodAdditions, error) {
podAdditions := &v1alpha1.PodAdditions{}
// "Set"s to store k8s object names and detect duplicates
containerNames := map[string]bool{}
initContainerNames := map[string]bool{}
volumeNames := map[string]bool{}
volumeMountNames := map[string]bool{}
pullSecretNames := map[string]bool{}
for _, additions := range toMerge {
for annotKey, annotVal := range additions.Annotations {
if podAdditions.Annotations == nil {
podAdditions.Annotations = map[string]string{}
}
podAdditions.Annotations[annotKey] = annotVal
}
for labelKey, labelVal := range additions.Labels {
if podAdditions.Labels == nil {
podAdditions.Labels = map[string]string{}
}
podAdditions.Labels[labelKey] = labelVal
}
for _, container := range additions.Containers {
if containerNames[container.Name] {
return nil, fmt.Errorf("duplicate containers in the workspace definition: %s", container.Name)
}
containerNames[container.Name] = true
podAdditions.Containers = append(podAdditions.Containers, container)
}
for _, container := range additions.InitContainers {
if initContainerNames[container.Name] {
return nil, fmt.Errorf("duplicate init containers in the workspace definition: %s", container.Name)
}
initContainerNames[container.Name] = true
podAdditions.InitContainers = append(podAdditions.InitContainers, container)
}
for _, volume := range additions.Volumes {
if volumeNames[volume.Name] {
return nil, fmt.Errorf("duplicate volumes in the workspace definition: %s", volume.Name)
}
volumeNames[volume.Name] = true
podAdditions.Volumes = append(podAdditions.Volumes, volume)
}
for _, volumeMount := range additions.VolumeMounts {
if volumeMountNames[volumeMount.Name] {
return nil, fmt.Errorf("duplicated volumeMounts in workspace definition: %s", volumeMount.Name)
}
volumeMountNames[volumeMount.Name] = true
podAdditions.VolumeMounts = append(podAdditions.VolumeMounts, volumeMount)
}
for _, pullSecret := range additions.PullSecrets {
if pullSecretNames[pullSecret.Name] {
continue
}
pullSecretNames[pullSecret.Name] = true
podAdditions.PullSecrets = append(podAdditions.PullSecrets, pullSecret)
}
}
return podAdditions, nil
}
func getWorkspaceSubpathVolumeMount(workspaceId, pvcName string) corev1.VolumeMount {
workspaceVolumeMount := corev1.VolumeMount{
Name: pvcName,
MountPath: "/tmp/workspace-storage",
SubPath: workspaceId,
}
return workspaceVolumeMount
}
func needsPVCWorkaround(podAdditions *v1alpha1.PodAdditions, pvcName string) (needs bool, workaroundPvcName string) {
for _, vol := range podAdditions.Volumes {
if vol.Name == pvcName {
return true, pvcName
}
if vol.Name == constants.CheCommonPVCName {
return true, constants.CheCommonPVCName
}
}
return false, ""
}
func getAdditionalDeploymentAnnotations(workspace *common.DevWorkspaceWithConfig) (map[string]string, error) {
annotations := map[string]string{}
for _, component := range workspace.Spec.Template.Components {
if component.Container == nil || component.Container.Annotation == nil || component.Container.Annotation.Deployment == nil {
continue
}
for k, v := range component.Container.Annotation.Deployment {
if currValue, exists := annotations[k]; exists && v != currValue {
return nil, fmt.Errorf("conflicting annotations found on container components for key %s", k)
}
annotations[k] = v
}
}
return annotations, nil
}