-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathupdate.go
405 lines (368 loc) · 13 KB
/
update.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
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
399
400
401
402
403
404
405
//
//
// Copyright Red Hat
//
// 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 controllers
import (
"context"
"fmt"
"strconv"
registryv1alpha1 "github.com/devfile/registry-operator/api/v1alpha1"
"github.com/devfile/registry-operator/pkg/registry"
routev1 "github.com/openshift/api/route/v1"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const viewerContainerName = "registry-viewer"
// updateDeployment ensures that a devfile registry deployment exists on the cluster and is up to date with the custom resource
func (r *DevfileRegistryReconciler) updateDeployment(ctx context.Context, cr *registryv1alpha1.DevfileRegistry, dep *appsv1.Deployment) error {
// Check to see if the existing devfile registry deployment needs to be updated
needsUpdating := false
indexImage := registry.GetDevfileIndexImage(cr)
indexImageContainer := dep.Spec.Template.Spec.Containers[0]
if indexImageContainer.Image != indexImage {
indexImageContainer.Image = indexImage
needsUpdating = true
} else {
//check Telemetry config to see updates are needed
registryName := cr.Spec.Telemetry.RegistryName
registryKey := cr.Spec.Telemetry.Key
if indexImageContainer.Env[0].Value != registryName {
indexImageContainer.Env[0].Value = registryName
needsUpdating = true
}
if indexImageContainer.Env[1].Value != registryKey {
indexImageContainer.Env[1].Value = registryKey
needsUpdating = true
}
if indexImagePullPolicy := registry.GetDevfileIndexImagePullPolicy(cr); indexImageContainer.ImagePullPolicy != indexImagePullPolicy {
indexImageContainer.ImagePullPolicy = indexImagePullPolicy
needsUpdating = true
}
}
ociImage := registry.GetOCIRegistryImage(cr)
ociImageContainer := dep.Spec.Template.Spec.Containers[1]
if ociImageContainer.Image != ociImage {
ociImageContainer.Image = ociImage
needsUpdating = true
} else {
if ociImagePullPolicy := registry.GetOCIRegistryImagePullPolicy(cr); ociImageContainer.ImagePullPolicy != ociImagePullPolicy {
ociImageContainer.ImagePullPolicy = ociImagePullPolicy
needsUpdating = true
}
}
updated, err := r.updateDeploymentForHeadlessChange(cr, dep)
if err != nil {
return err
}
if updated {
needsUpdating = true
}
if registry.IsStorageEnabled(cr) {
if dep.Spec.Template.Spec.Volumes[0].PersistentVolumeClaim == nil {
dep.Spec.Template.Spec.Volumes[0].VolumeSource = registry.GetDevfileRegistryVolumeSource(cr)
needsUpdating = true
}
} else {
if dep.Spec.Template.Spec.Volumes[0].PersistentVolumeClaim != nil {
dep.Spec.Template.Spec.Volumes[0].VolumeSource = registry.GetDevfileRegistryVolumeSource(cr)
needsUpdating = true
}
}
if len(dep.Spec.Template.Spec.Containers) > 2 {
viewerImage := registry.GetRegistryViewerImage(cr)
viewerImageContainer := dep.Spec.Template.Spec.Containers[2]
//determine if the NEXT_PUBLIC_ANALYTICS_WRITE_KEY env needs updating
viewerKey := cr.Spec.Telemetry.RegistryViewerWriteKey
if viewerImageContainer.Env[0].Value != viewerKey {
r.Log.Info("Updating NEXT_PUBLIC_ANALYTICS_WRITE_KEY ", "value", viewerKey)
viewerImageContainer.Env[0].Value = viewerKey
needsUpdating = true
}
//determine if the DEVFILE_REGISTRIES env needs updating. This will only occur on initial deployment since object name is unique
newDRValue := fmt.Sprintf(`[{"name": "%s","url": "http://localhost:8080","fqdn": "%s"}]`, cr.ObjectMeta.Name, cr.Status.URL)
if viewerImageContainer.Env[1].Value != newDRValue {
r.Log.Info("Updating DEVFILE_REGISTRIES ", "value", newDRValue)
viewerImageContainer.Env[1].Value = newDRValue
needsUpdating = true
}
if viewerImageContainer.Image != viewerImage {
viewerImageContainer.Image = viewerImage
needsUpdating = true
} else {
if viewerImagePullPolicy := registry.GetRegistryViewerImagePullPolicy(cr); viewerImageContainer.ImagePullPolicy != viewerImagePullPolicy {
viewerImageContainer.ImagePullPolicy = viewerImagePullPolicy
needsUpdating = true
}
}
}
if needsUpdating {
r.Log.Info("Updating the DevfileRegistry deployment")
return r.Update(ctx, dep)
}
return nil
}
// updateRoute checks to see if any of the fields in an existing devfile index route needs updating
func (r *DevfileRegistryReconciler) updateRoute(ctx context.Context, cr *registryv1alpha1.DevfileRegistry, route *routev1.Route) error {
needsUpdating := false
// Check to see if TLS fields were updated
if registry.IsTLSEnabled(cr) {
if route.Spec.TLS == nil {
route.Spec.TLS = &routev1.TLSConfig{Termination: routev1.TLSTerminationEdge}
needsUpdating = true
}
} else {
if route.Spec.TLS != nil {
route.Spec.TLS = nil
needsUpdating = true
}
}
if needsUpdating {
return r.Update(ctx, route)
}
return nil
}
// updateIngress checks to see if any of the fields in an existing ingress resouorce need to be updated
func (r *DevfileRegistryReconciler) updateIngress(ctx context.Context, cr *registryv1alpha1.DevfileRegistry, hostname string, ingress *networkingv1.Ingress) error {
needsUpdating := false
// Check to see if TLS fields were updated
if registry.IsTLSEnabled(cr) {
if len(ingress.Spec.TLS) == 0 {
// TLS was toggled on, so enable it in the ingress spec
ingress.Spec.TLS = []networkingv1.IngressTLS{
{
Hosts: []string{hostname},
SecretName: cr.Spec.TLS.SecretName,
},
}
needsUpdating = true
}
if ingress.Spec.TLS[0].SecretName != cr.Spec.TLS.SecretName {
// TLS secret name was updated, so update it in the ingress spec
ingress.Spec.TLS[0].SecretName = cr.Spec.TLS.SecretName
needsUpdating = true
}
} else {
if len(ingress.Spec.TLS) > 0 {
// TLS was disabled, so disable it in the ingress spec
ingress.Spec.TLS = []networkingv1.IngressTLS{}
needsUpdating = true
}
}
// Check to see if the ingress domain was updated
if ingress.Spec.Rules[0].Host != hostname {
ingress.Spec.Rules[0].Host = hostname
// If TLS is enabled, need to update the hostname there too
if registry.IsTLSEnabled(cr) {
ingress.Spec.TLS[0].Hosts = []string{hostname}
}
needsUpdating = true
}
if needsUpdating {
return r.Update(ctx, ingress, &client.UpdateOptions{})
}
return nil
}
// deletePVCIfNeeded deletes the PVC for the devfile registry if one exists and if persistent storage was disabled
func (r *DevfileRegistryReconciler) deleteOldPVCIfNeeded(ctx context.Context, cr *registryv1alpha1.DevfileRegistry) error {
// Check to see if a PVC exists, if so, need to clean it up because storage was disabled
if !registry.IsStorageEnabled(cr) {
pvc := &corev1.PersistentVolumeClaim{}
err := r.Get(ctx, types.NamespacedName{Name: registry.PVCName(cr), Namespace: cr.Namespace}, pvc)
if err != nil {
if errors.IsNotFound(err) {
// PVC not found, so there's no old PVC to delete. Just return nil, nothing to do.
return nil
} else {
// Some other error occurred when listing PVCs, so log and return an error
r.Log.Error(err, "Error listing PersistentVolumeClaims")
return err
}
} else {
// PVC found despite storage being disable, so delete it
r.Log.Info("Old PersistentVolumeClaim " + pvc.Name + " found. Deleting it as storage has been disabled.")
err = r.Delete(ctx, pvc)
if err != nil {
r.Log.Error(err, "Error deleting PersistentVolumeClaim", pvc.Name)
return err
}
}
}
return nil
}
// updateRegistryHeadlessEnv updates or adds the REGISTRY_HEADLESS environment variable
func updateRegistryHeadlessEnv(envVars []corev1.EnvVar, headless bool) []corev1.EnvVar {
found := false
for i, env := range envVars {
if env.Name == "REGISTRY_HEADLESS" {
envVars[i].Value = strconv.FormatBool(headless)
found = true
break
}
}
if !found {
envVars = append(envVars, corev1.EnvVar{
Name: "REGISTRY_HEADLESS",
Value: strconv.FormatBool(headless),
})
}
return envVars
}
// removeViewerContainer removes the registry-viewer container from the list of containers
func removeViewerContainer(containers []corev1.Container) []corev1.Container {
var newContainers []corev1.Container
for _, container := range containers {
if container.Name != viewerContainerName {
newContainers = append(newContainers, container)
}
}
return newContainers
}
// updateDeploymentForHeadlessChange updates the deployment based on headless configuration
func (r *DevfileRegistryReconciler) updateDeploymentForHeadlessChange(cr *registryv1alpha1.DevfileRegistry, dep *appsv1.Deployment) (bool, error) {
updated := false
allowPrivilegeEscalation := false
runAsNonRoot := true
localHostname := "localhost"
if !registry.IsHeadlessEnabled(cr) {
// Check if viewer container already exists before adding
viewerExists := false
for _, container := range dep.Spec.Template.Spec.Containers {
if container.Name == viewerContainerName {
viewerExists = true
break
}
}
if !viewerExists {
// Configure StartupProbe
dep.Spec.Template.Spec.Containers[0].StartupProbe = &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/viewer",
Port: intstr.FromInt(registry.RegistryViewerPort),
Scheme: corev1.URISchemeHTTP,
},
},
InitialDelaySeconds: 30,
PeriodSeconds: 10,
TimeoutSeconds: 20,
}
// Append registry-viewer container
dep.Spec.Template.Spec.Containers = append(dep.Spec.Template.Spec.Containers, corev1.Container{
Image: registry.GetRegistryViewerImage(cr),
ImagePullPolicy: registry.GetRegistryViewerImagePullPolicy(cr),
Name: viewerContainerName,
SecurityContext: &corev1.SecurityContext{
AllowPrivilegeEscalation: &allowPrivilegeEscalation,
RunAsNonRoot: &runAsNonRoot,
Capabilities: &corev1.Capabilities{
Drop: []corev1.Capability{"ALL"},
},
SeccompProfile: &corev1.SeccompProfile{
Type: "RuntimeDefault",
},
},
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("250m"),
corev1.ResourceMemory: resource.MustParse("64Mi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("500m"),
corev1.ResourceMemory: resource.MustParse("256Mi"),
},
},
LivenessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/viewer",
Port: intstr.FromInt(registry.RegistryViewerPort),
Scheme: corev1.URISchemeHTTP,
},
},
InitialDelaySeconds: 15,
PeriodSeconds: 10,
TimeoutSeconds: 20,
},
ReadinessProbe: &corev1.Probe{
ProbeHandler: corev1.ProbeHandler{
HTTPGet: &corev1.HTTPGetAction{
Path: "/viewer",
Port: intstr.FromInt(registry.RegistryViewerPort),
Scheme: corev1.URISchemeHTTP,
},
},
InitialDelaySeconds: 15,
PeriodSeconds: 10,
TimeoutSeconds: 20,
},
Env: []corev1.EnvVar{
{
Name: "NEXT_PUBLIC_ANALYTICS_WRITE_KEY",
Value: cr.Spec.Telemetry.RegistryViewerWriteKey,
},
{
Name: "DEVFILE_REGISTRIES",
Value: fmt.Sprintf(`[
{
"name": "%s",
"url": "http://%s",
"fqdn": "%s"
}
]`, cr.ObjectMeta.Name, localHostname, cr.Status.URL),
},
},
})
updated = true
}
} else {
// Check if REGISTRY_HEADLESS env var needs to be updated
headlessEnvNeedsUpdate := true
for _, env := range dep.Spec.Template.Spec.Containers[0].Env {
if env.Name == "REGISTRY_HEADLESS" && env.Value == strconv.FormatBool(true) {
headlessEnvNeedsUpdate = false
break
}
}
// Check if viewer container needs to be removed
viewerExists := false
for _, container := range dep.Spec.Template.Spec.Containers {
if container.Name == viewerContainerName {
viewerExists = true
break
}
}
if headlessEnvNeedsUpdate || viewerExists {
// Set REGISTRY_HEADLESS environment variable
dep.Spec.Template.Spec.Containers[0].Env = updateRegistryHeadlessEnv(
dep.Spec.Template.Spec.Containers[0].Env,
true,
)
// Remove viewer container
dep.Spec.Template.Spec.Containers = removeViewerContainer(
dep.Spec.Template.Spec.Containers,
)
// Clear startup probe
dep.Spec.Template.Spec.Containers[0].StartupProbe = nil
updated = true
}
}
return updated, nil
}