forked from devfile/devworkspace-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployment.go
365 lines (327 loc) · 12.5 KB
/
deployment.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
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
//
// Copyright (c) 2019-2021 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package provision
import (
"context"
"errors"
"fmt"
"strings"
maputils "github.com/devfile/devworkspace-operator/internal/map"
"github.com/devfile/devworkspace-operator/pkg/common"
devworkspace "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/devworkspace-operator/apis/controller/v1alpha1"
"github.com/devfile/devworkspace-operator/controllers/workspace/env"
"github.com/devfile/devworkspace-operator/pkg/config"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
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"
runtimeClient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
type DeploymentProvisioningStatus struct {
ProvisioningStatus
}
var deploymentDiffOpts = cmp.Options{
cmpopts.IgnoreFields(appsv1.Deployment{}, "TypeMeta", "ObjectMeta", "Status"),
cmpopts.IgnoreFields(appsv1.DeploymentSpec{}, "RevisionHistoryLimit", "ProgressDeadlineSeconds"),
cmpopts.IgnoreFields(corev1.PodSpec{}, "DNSPolicy", "SchedulerName", "DeprecatedServiceAccount"),
cmpopts.IgnoreFields(corev1.Container{}, "TerminationMessagePath", "TerminationMessagePolicy", "ImagePullPolicy"),
cmpopts.SortSlices(func(a, b corev1.Container) bool {
return strings.Compare(a.Name, b.Name) > 0
}),
cmpopts.SortSlices(func(a, b corev1.Volume) bool {
return strings.Compare(a.Name, b.Name) > 0
}),
}
func SyncDeploymentToCluster(
workspace *devworkspace.DevWorkspace,
podAdditions []v1alpha1.PodAdditions,
saName string,
clusterAPI ClusterAPI) DeploymentProvisioningStatus {
// [design] we have to pass components and routing pod additions separately because we need mountsources from each
// component.
specDeployment, err := getSpecDeployment(workspace, podAdditions, saName, clusterAPI.Scheme)
if err != nil {
return DeploymentProvisioningStatus{
ProvisioningStatus: ProvisioningStatus{
Err: err,
FailStartup: true,
},
}
}
clusterDeployment, err := getClusterDeployment(specDeployment.Name, workspace.Namespace, clusterAPI.Client)
if err != nil {
return DeploymentProvisioningStatus{
ProvisioningStatus: ProvisioningStatus{Err: err},
}
}
if clusterDeployment == nil {
clusterAPI.Logger.Info("Creating deployment...")
err := clusterAPI.Client.Create(context.TODO(), specDeployment)
return DeploymentProvisioningStatus{
ProvisioningStatus: ProvisioningStatus{
Requeue: true,
Err: err,
},
}
}
if !cmp.Equal(specDeployment, clusterDeployment, deploymentDiffOpts) {
clusterAPI.Logger.Info("Updating deployment...")
clusterDeployment.Spec = specDeployment.Spec
err := clusterAPI.Client.Update(context.TODO(), clusterDeployment)
if err != nil {
if apierrors.IsConflict(err) {
return DeploymentProvisioningStatus{ProvisioningStatus: ProvisioningStatus{Requeue: true}}
}
return DeploymentProvisioningStatus{ProvisioningStatus{Err: err}}
}
return DeploymentProvisioningStatus{
ProvisioningStatus: ProvisioningStatus{Requeue: true},
}
}
deploymentReady := checkDeploymentStatus(clusterDeployment)
if deploymentReady {
return DeploymentProvisioningStatus{
ProvisioningStatus: ProvisioningStatus{
Continue: true,
},
}
}
return DeploymentProvisioningStatus{}
}
// DeleteWorkspaceDeployment deletes the deployment for the DevWorkspace
func DeleteWorkspaceDeployment(ctx context.Context, workspace *devworkspace.DevWorkspace, client runtimeClient.Client) (wait bool, err error) {
clusterDeployment, err := getClusterDeployment(common.DeploymentName(workspace.Status.WorkspaceId), workspace.Namespace, client)
if err != nil {
return false, err
}
if clusterDeployment == nil {
return false, nil
}
err = client.Delete(ctx, clusterDeployment)
if err != nil {
if k8sErrors.IsNotFound(err) {
return false, nil
}
return false, err
}
return true, nil
}
func GetDevWorkspaceSecurityContext() *corev1.PodSecurityContext {
if !config.ControllerCfg.IsOpenShift() {
uID := int64(1234)
rootGID := int64(0)
nonRoot := true
return &corev1.PodSecurityContext{
RunAsUser: &uID,
RunAsGroup: &rootGID,
RunAsNonRoot: &nonRoot,
}
}
return &corev1.PodSecurityContext{}
}
func checkDeploymentStatus(deployment *appsv1.Deployment) (ready bool) {
return deployment.Status.ReadyReplicas > 0
}
func getSpecDeployment(
workspace *devworkspace.DevWorkspace,
podAdditionsList []v1alpha1.PodAdditions,
saName string,
scheme *runtime.Scheme) (*appsv1.Deployment, error) {
replicas := int32(1)
terminationGracePeriod := int64(1)
podAdditions, err := mergePodAdditions(podAdditionsList)
if err != nil {
return nil, err
}
creator := workspace.Labels[config.WorkspaceCreatorLabel]
commonEnv := env.CommonEnvironmentVariables(workspace.Name, workspace.Status.WorkspaceId, workspace.Namespace, creator)
for idx := range podAdditions.Containers {
podAdditions.Containers[idx].Env = append(podAdditions.Containers[idx].Env, commonEnv...)
podAdditions.Containers[idx].VolumeMounts = append(podAdditions.Containers[idx].VolumeMounts, podAdditions.VolumeMounts...)
}
for idx := range podAdditions.InitContainers {
podAdditions.InitContainers[idx].Env = append(podAdditions.InitContainers[idx].Env, commonEnv...)
podAdditions.InitContainers[idx].VolumeMounts = append(podAdditions.InitContainers[idx].VolumeMounts, podAdditions.VolumeMounts...)
}
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: common.DeploymentName(workspace.Status.WorkspaceId),
Namespace: workspace.Namespace,
Labels: map[string]string{
config.WorkspaceIDLabel: workspace.Status.WorkspaceId,
},
},
Spec: appsv1.DeploymentSpec{
Replicas: &replicas,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
config.WorkspaceIDLabel: workspace.Status.WorkspaceId,
config.WorkspaceNameLabel: workspace.Name,
},
},
Strategy: appsv1.DeploymentStrategy{
Type: appsv1.RecreateDeploymentStrategyType,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Name: workspace.Status.WorkspaceId,
Namespace: workspace.Namespace,
Labels: map[string]string{
config.WorkspaceIDLabel: workspace.Status.WorkspaceId,
config.WorkspaceNameLabel: workspace.Name,
},
},
Spec: corev1.PodSpec{
InitContainers: podAdditions.InitContainers,
Containers: podAdditions.Containers,
ImagePullSecrets: podAdditions.PullSecrets,
Volumes: podAdditions.Volumes,
RestartPolicy: "Always",
TerminationGracePeriodSeconds: &terminationGracePeriod,
SecurityContext: GetDevWorkspaceSecurityContext(),
ServiceAccountName: saName,
AutomountServiceAccountToken: nil,
},
},
},
}
if needsPVCWorkaround(podAdditions) {
// 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.WorkspaceId)}, 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.WorkspaceId)}, volumeMounts...)
deployment.Spec.Template.Spec.Containers[0].VolumeMounts = volumeMounts
}
}
workspaceCreator, present := workspace.Labels[config.WorkspaceCreatorLabel]
if present {
deployment.Labels[config.WorkspaceCreatorLabel] = workspaceCreator
deployment.Spec.Template.Labels[config.WorkspaceCreatorLabel] = workspaceCreator
} else {
if config.ControllerCfg.GetWebhooksEnabled() == "true" {
return nil, errors.New("workspace must have creator specified to be run. Recreate it to fix an issue")
}
}
restrictedAccess, present := workspace.Annotations[config.WorkspaceRestrictedAccessAnnotation]
if present {
if config.ControllerCfg.GetWebhooksEnabled() == "false" {
return nil, errors.New("workspace is configured to have restricted access but webhooks are not enabled")
}
deployment.Annotations = maputils.Append(deployment.Annotations, config.WorkspaceRestrictedAccessAnnotation, restrictedAccess)
deployment.Spec.Template.Annotations = maputils.Append(deployment.Spec.Template.Annotations, config.WorkspaceRestrictedAccessAnnotation, restrictedAccess)
}
err = controllerutil.SetControllerReference(workspace, deployment, scheme)
if err != nil {
return nil, err
}
return deployment, nil
}
func getClusterDeployment(name string, namespace string, client runtimeClient.Client) (*appsv1.Deployment, error) {
deployment := &appsv1.Deployment{}
namespacedName := types.NamespacedName{
Namespace: namespace,
Name: name,
}
err := client.Get(context.TODO(), namespacedName, deployment)
if err != nil {
if apierrors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
return deployment, 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 {
podAdditions.Annotations[annotKey] = annotVal
}
for labelKey, labelVal := range additions.Labels {
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 string) corev1.VolumeMount {
volumeName := config.ControllerCfg.GetWorkspacePVCName()
workspaceVolumeMount := corev1.VolumeMount{
Name: volumeName,
MountPath: "/tmp/workspace-storage",
SubPath: workspaceId,
}
return workspaceVolumeMount
}
func needsPVCWorkaround(podAdditions *v1alpha1.PodAdditions) bool {
commonPVCName := config.ControllerCfg.GetWorkspacePVCName()
for _, vol := range podAdditions.Volumes {
if vol.Name == commonPVCName {
return true
}
}
return false
}