-
Notifications
You must be signed in to change notification settings - Fork 36
/
component_build_controller.go
607 lines (524 loc) · 24.7 KB
/
component_build_controller.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
Copyright 2021-2023 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 controllers
import (
"context"
"encoding/json"
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
appstudiov1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
"github.com/konflux-ci/build-service/pkg/boerrors"
"github.com/konflux-ci/build-service/pkg/bometrics"
"github.com/konflux-ci/build-service/pkg/k8s"
l "github.com/konflux-ci/build-service/pkg/logs"
"github.com/konflux-ci/build-service/pkg/webhook"
)
const (
BuildRequestAnnotationName = "build.appstudio.openshift.io/request"
BuildRequestTriggerSimpleBuildAnnotationValue = "trigger-simple-build"
BuildRequestTriggerPaCBuildAnnotationValue = "trigger-pac-build"
BuildRequestConfigurePaCAnnotationValue = "configure-pac"
BuildRequestUnconfigurePaCAnnotationValue = "unconfigure-pac"
BuildStatusAnnotationName = "build.appstudio.openshift.io/status"
PaCProvisionFinalizer = "pac.component.appstudio.openshift.io/finalizer"
ImageRegistrySecretLinkFinalizer = "image-registry-secret-sa-link.component.appstudio.openshift.io/finalizer"
ApplicationNameLabelName = "appstudio.openshift.io/application"
ComponentNameLabelName = "appstudio.openshift.io/component"
PartOfLabelName = "app.kubernetes.io/part-of"
PartOfAppStudioLabelValue = "appstudio"
gitCommitShaAnnotationName = "build.appstudio.redhat.com/commit_sha"
gitRepoAtShaAnnotationName = "build.appstudio.openshift.io/repo"
gitTargetBranchAnnotationName = "build.appstudio.redhat.com/target_branch"
ImageRepoAnnotationName = "image.redhat.com/image"
ImageRepoGenerateAnnotationName = "image.redhat.com/generate"
buildPipelineServiceAccountName = "appstudio-pipeline"
buildPipelineSelectorResourceName = "build-pipeline-selector"
defaultBuildPipelineAnnotation = "build.appstudio.openshift.io/pipeline"
buildPipelineConfigMapResourceName = "build-pipeline-config"
buildPipelineConfigName = "config.yaml"
)
type BuildStatus struct {
Simple *SimpleBuildStatus `json:"simple,omitempty"`
PaC *PaCBuildStatus `json:"pac,omitempty"`
// Shows build methods agnostic messages, e.g. invalid build request.
Message string `json:"message,omitempty"`
}
// Describes persistent error for build request.
type ErrorInfo struct {
ErrId int `json:"error-id,omitempty"`
ErrMessage string `json:"error-message,omitempty"`
}
type SimpleBuildStatus struct {
// BuildStartTime shows the time when last simple build was submited.
BuildStartTime string `json:"build-start-time,omitempty"`
ErrorInfo
}
type PaCBuildStatus struct {
// State shows if PaC is used.
// Values are: enabled, disabled.
State string `json:"state,omitempty"`
// Contains link to PaC provision / unprovision pull request
MergeUrl string `json:"merge-url,omitempty"`
// Time of the last successful PaC configuration in RFC1123 format
ConfigurationTime string `json:"configuration-time,omitempty"`
ErrorInfo
}
// ComponentBuildReconciler watches AppStudio Component objects in order to
// provision Pipelines as Code configuration for the Component or
// submit initial builds and dependent resources if PaC is not configured.
type ComponentBuildReconciler struct {
Client client.Client
Scheme *runtime.Scheme
EventRecorder record.EventRecorder
CredentialProvider *k8s.GitCredentialProvider
WebhookURLLoader webhook.WebhookURLLoader
}
// SetupWithManager sets up the controller with the Manager.
func (r *ComponentBuildReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&appstudiov1alpha1.Component{}, builder.WithPredicates(predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
return true
},
DeleteFunc: func(e event.DeleteEvent) bool {
return false
},
GenericFunc: func(e event.GenericEvent) bool {
return false
},
})).
Complete(r)
}
func updateMetricsTimes(componentIdForMetrics string, requestedAction string, reconcileStartTime time.Time) {
componentInfo, timeRecorded := bometrics.ComponentTimesForMetrics[componentIdForMetrics]
// first reconcile
if !timeRecorded {
bometrics.ComponentTimesForMetrics[componentIdForMetrics] = bometrics.ComponentMetricsInfo{StartTimestamp: reconcileStartTime, RequestedAction: requestedAction}
} else {
// new different request
if componentInfo.RequestedAction != requestedAction {
bometrics.ComponentTimesForMetrics[componentIdForMetrics] = bometrics.ComponentMetricsInfo{StartTimestamp: reconcileStartTime, RequestedAction: requestedAction}
}
}
}
//+kubebuilder:rbac:groups=appstudio.redhat.com,resources=components,verbs=get;list;watch;update;patch
//+kubebuilder:rbac:groups=appstudio.redhat.com,resources=components/status,verbs=get;list;watch
//+kubebuilder:rbac:groups=appstudio.redhat.com,resources=buildpipelineselectors,verbs=get;list;watch;create;update;patch
//+kubebuilder:rbac:groups=appstudio.redhat.com,resources=releaseplanadmissions,verbs=get;list;watch
//+kubebuilder:rbac:groups=tekton.dev,resources=pipelineruns,verbs=create
//+kubebuilder:rbac:groups=pipelinesascode.tekton.dev,resources=repositories,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch
//+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;patch;update;delete
//+kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch
//+kubebuilder:rbac:groups="",resources=serviceaccounts,verbs=get;list;watch;create;update;patch
//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch
//+kubebuilder:rbac:groups=route.openshift.io,resources=routes,verbs=get;list;watch
func (r *ComponentBuildReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := ctrllog.FromContext(ctx).WithName("ComponentOnboarding")
ctx = ctrllog.IntoContext(ctx, log)
reconcileStartTime := time.Now()
// Fetch the Component instance
var component appstudiov1alpha1.Component
err := r.Client.Get(ctx, req.NamespacedName, &component)
if err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
return ctrl.Result{}, nil
}
// Error reading the object - requeue the request.
return ctrl.Result{}, err
}
if getContainerImageRepositoryForComponent(&component) == "" {
// Container image must be set. It's not possible to proceed without it.
log.Info("Waiting for ContainerImage to be set")
return ctrl.Result{}, nil
}
// Do not run any builds if component doesn't have gitsource
if component.Spec.Source.GitSource == nil || component.Spec.Source.GitSource.URL == "" {
log.Info("Nothing to do for container image component without source")
return ctrl.Result{}, nil
}
log = log.WithValues("ComponentGitSource", component.Spec.Source.GitSource)
ctx = ctrllog.IntoContext(ctx, log)
componentIdForMetrics := fmt.Sprintf("%s=%s", component.Name, component.Namespace)
if !component.ObjectMeta.DeletionTimestamp.IsZero() {
// Deletion of the component is requested
// remove component from metrics map
delete(bometrics.ComponentTimesForMetrics, componentIdForMetrics)
if controllerutil.ContainsFinalizer(&component, ImageRegistrySecretLinkFinalizer) {
pipelineSA := &corev1.ServiceAccount{}
err := r.Client.Get(ctx, types.NamespacedName{Name: buildPipelineServiceAccountName, Namespace: req.Namespace}, pipelineSA)
if err != nil && !errors.IsNotFound(err) {
log.Error(err, fmt.Sprintf("Failed to read service account %s in namespace %s", buildPipelineServiceAccountName, req.Namespace), l.Action, l.ActionView)
return ctrl.Result{}, err
}
if err == nil { // If pipeline service account found, unlink the secret from it
if _, generatedImageRepoSecretName, err := getComponentImageRepoAndSecretNameFromImageAnnotation(&component); err == nil {
if _, err := r.unlinkSecretFromServiceAccount(ctx, generatedImageRepoSecretName, pipelineSA.Name, pipelineSA.Namespace); err != nil {
return ctrl.Result{}, err
}
}
}
if err := r.Client.Get(ctx, req.NamespacedName, &component); err != nil {
log.Error(err, "failed to get Component", l.Action, l.ActionView)
return ctrl.Result{}, err
}
controllerutil.RemoveFinalizer(&component, ImageRegistrySecretLinkFinalizer)
if err := r.Client.Update(ctx, &component); err != nil {
return ctrl.Result{}, err
}
log.Info("Image registry secret link finalizer removed", l.Action, l.ActionDelete)
// A new reconcile will be triggered because of the update above
return ctrl.Result{}, nil
}
if controllerutil.ContainsFinalizer(&component, PaCProvisionFinalizer) {
// In order to not to block the deletion of the Component,
// delete finalizer unconditionally and then try to do clean up ignoring errors.
// Delete Pipelines as Code provision finalizer
controllerutil.RemoveFinalizer(&component, PaCProvisionFinalizer)
if err := r.Client.Update(ctx, &component); err != nil {
return ctrl.Result{}, err
}
log.Info("PaC finalizer removed", l.Action, l.ActionDelete)
// Try to clean up Pipelines as Code configuration
_, _ = r.UndoPaCProvisionForComponent(ctx, &component)
}
return ctrl.Result{}, nil
}
pipelineRef, err := r.GetBuildPipelineFromComponentAnnotation(ctx, &component)
if err != nil {
log.Error(err, fmt.Sprintf("Failed to read %s annotation on component %s", defaultBuildPipelineAnnotation, component.Name), l.Action, l.ActionView)
buildStatus := readBuildStatus(&component)
// when reading pipeline annotation fails, we should end reconcile, unless transient error
if boErr, ok := err.(*boerrors.BuildOpError); ok && boErr.IsPersistent() {
buildStatus.Message = fmt.Sprintf("%d: %s", err.(*boerrors.BuildOpError).GetErrorId(), err.(*boerrors.BuildOpError).ShortError())
} else {
// transient error, retry
return ctrl.Result{}, err
}
writeBuildStatus(&component, buildStatus)
delete(component.Annotations, BuildRequestAnnotationName)
if err := r.Client.Update(ctx, &component); err != nil {
log.Error(err, fmt.Sprintf("failed to update component after wrong %s annotation", defaultBuildPipelineAnnotation))
return ctrl.Result{}, err
}
log.Info(fmt.Sprintf("updated component after wrong %s annotation", defaultBuildPipelineAnnotation))
r.WaitForCacheUpdate(ctx, req.NamespacedName, &component)
return ctrl.Result{}, nil
}
// when we are using already pipeline annotation we don't need anymore devfile
if pipelineRef == nil {
// Ensure devfile model is set
if component.Status.Devfile == "" {
// The Component has been just created.
// Component controller (from Application Service) must set devfile model, wait for it.
log.Info("Waiting for devfile model in component")
// Do not requeue as after model update a new update event will trigger a new reconcile
return ctrl.Result{}, nil
}
} else {
buildPipelineName, buildPipelineBundle, _ := getPipelineNameAndBundle(pipelineRef)
log.Info(fmt.Sprintf("Will use default pipeline from annotation %s : name: %s; bundle: %s", defaultBuildPipelineAnnotation, buildPipelineName, buildPipelineBundle))
}
// Ensure pipeline service account exists
pipelineSA, err := r.ensurePipelineServiceAccount(ctx, component.Namespace)
if err != nil {
return ctrl.Result{}, err
}
// Link auto generated image registry secret in case of auto generated image repository is used.
if !controllerutil.ContainsFinalizer(&component, ImageRegistrySecretLinkFinalizer) {
imageRepoGenerated, imageRepoSecretName, err := getComponentImageRepoAndSecretNameFromImageAnnotation(&component)
if err != nil {
return ctrl.Result{}, err
}
// Check if the generated image is used
if imageRepoGenerated != "" && (component.Spec.ContainerImage == "" || imageRepoGenerated == getContainerImageRepository(component.Spec.ContainerImage)) {
_, err = r.linkSecretToServiceAccount(ctx, imageRepoSecretName, pipelineSA.Name, pipelineSA.Namespace, true)
if err != nil {
return ctrl.Result{}, err
}
// Ensure finalizer exists to clean up image registry secret link on component deletion
if component.ObjectMeta.DeletionTimestamp.IsZero() {
if !controllerutil.ContainsFinalizer(&component, ImageRegistrySecretLinkFinalizer) {
if err := r.Client.Get(ctx, req.NamespacedName, &component); err != nil {
log.Error(err, "failed to get Component", l.Action, l.ActionView)
return ctrl.Result{}, err
}
controllerutil.AddFinalizer(&component, ImageRegistrySecretLinkFinalizer)
if err := r.Client.Update(ctx, &component); err != nil {
return ctrl.Result{}, err
}
log.Info("Image registry secret service account link finalizer added", l.Action, l.ActionUpdate)
}
}
}
}
requestedAction, requestedActionExists := component.Annotations[BuildRequestAnnotationName]
if !requestedActionExists {
if _, statusExists := component.Annotations[BuildStatusAnnotationName]; statusExists {
// Nothing to do
return ctrl.Result{}, nil
}
// Automatically build component after creation
log.Info("automatically requesting initial build for the new component")
requestedAction = BuildRequestTriggerSimpleBuildAnnotationValue
}
switch requestedAction {
case BuildRequestTriggerSimpleBuildAnnotationValue:
updateMetricsTimes(componentIdForMetrics, requestedAction, reconcileStartTime)
// initial build upon component creation (doesn't have either build status)
initialBuild := func() bool {
initialBuildStatus := readBuildStatus(&component)
return initialBuildStatus.PaC == nil && initialBuildStatus.Simple == nil
}()
simpleBuildStatus := &SimpleBuildStatus{}
if err := r.SubmitNewBuild(ctx, &component); err != nil {
if boErr, ok := err.(*boerrors.BuildOpError); ok && boErr.IsPersistent() {
log.Error(err, "simple build submition for the Component failed")
simpleBuildStatus.ErrId = boErr.GetErrorId()
simpleBuildStatus.ErrMessage = boErr.ShortError()
} else {
// transient error, retry
log.Error(err, "simple build submition transient error")
return ctrl.Result{}, err
}
} else {
simpleBuildStatus.BuildStartTime = time.Now().Format(time.RFC1123)
if initialBuild {
bometrics.ComponentOnboardingTimeMetric.Observe(time.Since(bometrics.ComponentTimesForMetrics[componentIdForMetrics].StartTimestamp).Seconds())
} else {
bometrics.SimpleBuildPipelineCreationTimeMetric.Observe(time.Since(bometrics.ComponentTimesForMetrics[componentIdForMetrics].StartTimestamp).Seconds())
}
}
if err := r.Client.Get(ctx, req.NamespacedName, &component); err != nil {
log.Error(err, "failed to get Component", l.Action, l.ActionView)
return ctrl.Result{}, err
}
// Update build status annotation
buildStatus := readBuildStatus(&component)
buildStatus.Simple = simpleBuildStatus
buildStatus.Message = "done"
writeBuildStatus(&component, buildStatus)
case BuildRequestTriggerPaCBuildAnnotationValue:
updateMetricsTimes(componentIdForMetrics, requestedAction, reconcileStartTime)
buildStatus := readBuildStatus(&component)
if !(buildStatus.PaC != nil && buildStatus.PaC.State == "enabled") {
log.Info("Can't rerun push pipeline because Pipelines as Code isn't provisioned for the Component")
return ctrl.Result{}, nil
}
reconcileRequired, err := r.TriggerPaCBuild(ctx, &component)
if err != nil {
if boErr, ok := err.(*boerrors.BuildOpError); ok && boErr.IsPersistent() {
log.Error(err, "Failed to rerun push pipeline for the Component")
buildStatus := readBuildStatus(&component)
buildStatus.PaC.ErrId = boErr.GetErrorId()
buildStatus.PaC.ErrMessage = boErr.ShortError()
writeBuildStatus(&component, buildStatus)
} else {
// transient error, retry
log.Error(err, "Failed to rerun push pipeline for the Component with transient error")
return ctrl.Result{}, err
}
} else {
if reconcileRequired {
return ctrl.Result{Requeue: true}, nil
}
bometrics.PushPipelineRebuildTriggerTimeMetric.Observe(time.Since(bometrics.ComponentTimesForMetrics[componentIdForMetrics].StartTimestamp).Seconds())
}
case BuildRequestConfigurePaCAnnotationValue:
updateMetricsTimes(componentIdForMetrics, requestedAction, reconcileStartTime)
// initial build upon component creation (doesn't have either build status)
initialBuild := func() bool {
initialBuildStatus := readBuildStatus(&component)
return initialBuildStatus.PaC == nil && initialBuildStatus.Simple == nil
}()
pacBuildStatus := &PaCBuildStatus{}
if mergeUrl, err := r.ProvisionPaCForComponent(ctx, &component); err != nil {
if boErr, ok := err.(*boerrors.BuildOpError); ok && boErr.IsPersistent() {
log.Error(err, "Pipelines as Code provision for the Component failed")
pacBuildStatus.State = "error"
pacBuildStatus.ErrId = boErr.GetErrorId()
pacBuildStatus.ErrMessage = boErr.ShortError()
} else {
// transient error, retry
log.Error(err, "Pipelines as Code provision transient error")
return ctrl.Result{}, err
}
} else {
pacBuildStatus.State = "enabled"
pacBuildStatus.MergeUrl = mergeUrl
pacBuildStatus.ConfigurationTime = time.Now().Format(time.RFC1123)
log.Info("Pipelines as Code provision for the Component finished successfully")
// initial PaC provision upon component creation
if initialBuild {
bometrics.ComponentOnboardingTimeMetric.Observe(time.Since(bometrics.ComponentTimesForMetrics[componentIdForMetrics].StartTimestamp).Seconds())
} else {
// if PaC is up to date, don't count request
if mergeUrl != "" {
bometrics.PipelinesAsCodeComponentProvisionTimeMetric.Observe(time.Since(bometrics.ComponentTimesForMetrics[componentIdForMetrics].StartTimestamp).Seconds())
}
}
}
// Update component to reflect Pipeline as Code provision status
if err := r.Client.Get(ctx, req.NamespacedName, &component); err != nil {
log.Error(err, "failed to get Component", l.Action, l.ActionView)
return ctrl.Result{}, err
}
// Add finalizer to clean up Pipelines as Code configuration on component deletion
if component.ObjectMeta.DeletionTimestamp.IsZero() && pacBuildStatus.ErrId == 0 {
if !controllerutil.ContainsFinalizer(&component, PaCProvisionFinalizer) {
controllerutil.AddFinalizer(&component, PaCProvisionFinalizer)
log.Info("adding PaC finalizer")
}
}
// Update build status annotation
buildStatus := readBuildStatus(&component)
buildStatus.PaC = pacBuildStatus
buildStatus.Message = "done"
writeBuildStatus(&component, buildStatus)
// Update PaC annotation
if len(component.Annotations) == 0 {
component.Annotations = make(map[string]string)
}
case BuildRequestUnconfigurePaCAnnotationValue:
updateMetricsTimes(componentIdForMetrics, requestedAction, reconcileStartTime)
// Remove Pipelines as Code configuration finalizer
if controllerutil.ContainsFinalizer(&component, PaCProvisionFinalizer) {
controllerutil.RemoveFinalizer(&component, PaCProvisionFinalizer)
if err := r.Client.Update(ctx, &component); err != nil {
log.Error(err, "failed to remove PaC finalizer to the Component", l.Action, l.ActionUpdate)
return ctrl.Result{}, err
} else {
log.Info("PaC finalizer removed", l.Action, l.ActionUpdate)
}
}
pacBuildStatus := &PaCBuildStatus{}
if mergeUrl, err := r.UndoPaCProvisionForComponent(ctx, &component); err != nil {
if boErr, ok := err.(*boerrors.BuildOpError); ok && boErr.IsPersistent() {
log.Error(err, "Pipelines as Code unprovision for the Component failed")
pacBuildStatus.State = "error"
pacBuildStatus.ErrId = boErr.GetErrorId()
pacBuildStatus.ErrMessage = boErr.ShortError()
} else {
// transient error, retry
log.Error(err, "Pipelines as Code unprovision transient error")
return ctrl.Result{}, err
}
} else {
pacBuildStatus.State = "disabled"
pacBuildStatus.MergeUrl = mergeUrl
log.Info("Pipelines as Code unprovision for the Component finished successfully")
// if PaC doesn't require unprovision, don't count request
if mergeUrl != "" {
bometrics.PipelinesAsCodeComponentUnconfigureTimeMetric.Observe(time.Since(bometrics.ComponentTimesForMetrics[componentIdForMetrics].StartTimestamp).Seconds())
}
}
// Update component to show Pipeline as Code provision is undone
if err := r.Client.Get(ctx, req.NamespacedName, &component); err != nil {
log.Error(err, "failed to get Component", l.Action, l.ActionView)
return ctrl.Result{}, err
}
// Update build status annotation
buildStatus := readBuildStatus(&component)
buildStatus.PaC = pacBuildStatus
buildStatus.Message = "done"
writeBuildStatus(&component, buildStatus)
default:
if requestedAction == "" {
// Do not show error for empty annotation, consider it as noop.
return ctrl.Result{}, nil
}
buildStatus := readBuildStatus(&component)
buildStatus.Message = fmt.Sprintf("unexpected build request: %s", requestedAction)
writeBuildStatus(&component, buildStatus)
}
delete(component.Annotations, BuildRequestAnnotationName)
if err := r.Client.Update(ctx, &component); err != nil {
log.Error(err, fmt.Sprintf("failed to update component after build request: %s", requestedAction), l.Action, l.ActionUpdate, l.Audit, "true")
return ctrl.Result{}, err
}
log.Info(fmt.Sprintf("updated component after build request: %s", requestedAction), l.Action, l.ActionUpdate)
// remove component from metrics map
delete(bometrics.ComponentTimesForMetrics, componentIdForMetrics)
r.WaitForCacheUpdate(ctx, req.NamespacedName, &component)
return ctrl.Result{}, nil
}
func (r *ComponentBuildReconciler) WaitForCacheUpdate(ctx context.Context, namespace types.NamespacedName, component *appstudiov1alpha1.Component) {
log := ctrllog.FromContext(ctx)
// Here we do some trick.
// The problem is that the component update triggers both: a new reconcile and operator cache update.
// In other words we are getting race condition. If a new reconcile is triggered before cache update,
// requested build action will be repeated, because the last update has not yet visible for the operator.
// For example, instead of one initial pipeline run we could get two.
// To resolve the problem above, instead of just ending the reconcile loop here,
// we are waiting for the cache update. This approach prevents next reconciles with outdated cache.
isComponentInCacheUpToDate := false
for i := 0; i < 20; i++ {
if err := r.Client.Get(ctx, namespace, component); err == nil {
_, buildRequestAnnotationExists := component.Annotations[BuildRequestAnnotationName]
_, buildStatusAnnotationExists := component.Annotations[BuildStatusAnnotationName]
if !buildRequestAnnotationExists && buildStatusAnnotationExists {
// Cache contains updated component
isComponentInCacheUpToDate = true
break
}
// Outdated version of the component, wait more.
} else {
if errors.IsNotFound(err) {
// The component was deleted
isComponentInCacheUpToDate = true
break
}
log.Error(err, "failed to get the component", l.Action, l.ActionView)
}
time.Sleep(100 * time.Millisecond)
}
if !isComponentInCacheUpToDate {
log.Info("failed to wait for updated cache. Requested action could be repeated.", l.Audit, "true")
}
}
func readBuildStatus(component *appstudiov1alpha1.Component) *BuildStatus {
if component.Annotations == nil {
return &BuildStatus{}
}
buildStatus := &BuildStatus{}
buildStatusBytes := []byte(component.Annotations[BuildStatusAnnotationName])
if err := json.Unmarshal(buildStatusBytes, buildStatus); err == nil {
return buildStatus
}
return &BuildStatus{}
}
func writeBuildStatus(component *appstudiov1alpha1.Component, buildStatus *BuildStatus) {
if component.Annotations == nil {
component.Annotations = make(map[string]string)
}
buildStatusBytes, _ := json.Marshal(buildStatus)
component.Annotations[BuildStatusAnnotationName] = string(buildStatusBytes)
}