forked from konflux-ci/integration-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildpipeline_adapter.go
More file actions
1468 lines (1310 loc) · 69.8 KB
/
Copy pathbuildpipeline_adapter.go
File metadata and controls
1468 lines (1310 loc) · 69.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
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 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 buildpipeline
import (
"context"
"crypto/rand"
"encoding/json"
"fmt"
"math/big"
"strconv"
"time"
"k8s.io/client-go/util/retry"
"k8s.io/utils/strings/slices"
applicationapiv1alpha1 "github.com/konflux-ci/application-api/api/v1alpha1"
"github.com/konflux-ci/integration-service/api/v1beta2"
"github.com/konflux-ci/integration-service/gitops"
h "github.com/konflux-ci/integration-service/helpers"
"github.com/konflux-ci/integration-service/loader"
intgteststat "github.com/konflux-ci/integration-service/pkg/integrationteststatus"
"github.com/konflux-ci/integration-service/snapshot"
"github.com/konflux-ci/integration-service/status"
"github.com/konflux-ci/integration-service/tekton"
tektonconsts "github.com/konflux-ci/integration-service/tekton/consts"
"github.com/konflux-ci/operator-toolkit/controller"
"github.com/konflux-ci/operator-toolkit/metadata"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// Adapter holds the objects needed to reconcile a build PipelineRun.
type Adapter struct {
pipelineRun *tektonv1.PipelineRun
component *applicationapiv1alpha1.Component
application *applicationapiv1alpha1.Application
componentGroups *[]v1beta2.ComponentGroup
loader loader.ObjectLoader
logger h.IntegrationLogger
client client.Client
context context.Context
status status.StatusInterface
}
// NewAdapterWithApplication creates and returns an Adapter instance when the component belongs to an Application
func NewAdapterWithApplication(context context.Context, pipelineRun *tektonv1.PipelineRun, component *applicationapiv1alpha1.Component, application *applicationapiv1alpha1.Application,
logger h.IntegrationLogger, loader loader.ObjectLoader, client client.Client,
) *Adapter {
return &Adapter{
pipelineRun: pipelineRun,
component: component,
application: application,
componentGroups: nil,
logger: logger,
loader: loader,
client: client,
context: context,
status: status.NewStatus(logger.Logger, client),
}
}
// NewAdapter creates and returns an Adapter instance
func NewAdapter(context context.Context, pipelineRun *tektonv1.PipelineRun, component *applicationapiv1alpha1.Component, componentGroups *[]v1beta2.ComponentGroup,
logger h.IntegrationLogger, loader loader.ObjectLoader, client client.Client,
) *Adapter {
return &Adapter{
pipelineRun: pipelineRun,
component: component,
application: nil,
componentGroups: componentGroups,
logger: logger,
loader: loader,
client: client,
context: context,
status: status.NewStatus(logger.Logger, client),
}
}
// EnsureGlobalCandidateImageUpdated is an operation that ensure the ContainerImage in the Global Candidate List
// being updated when the build pipelinerun from push event succeeds and is signed.
func (a *Adapter) EnsureGlobalCandidateImageUpdated() (controller.OperationResult, error) {
if !a.shouldUpdateGlobalCandidateList() {
return controller.ContinueProcessing()
}
var addedToGlobalCandidateListStatus gitops.AddedToGlobalCandidateListStatus
// TODO: remove branch when we remove old application-specific code
var err error
if a.application != nil {
err = a.updateGCLForBuildPLR()
} else { // ComponentGroup behavior
err = snapshot.UpdateGCLForBuildPLR(a.context, a.client, a.loader, a.componentGroups, a.pipelineRun, a.component.Name)
}
if err != nil {
// TODO: remove HandleLoaderError when we remove application-specific code
// We no longer need to use the loader to update the GCL
_, loaderError := h.HandleLoaderError(a.logger, err, fmt.Sprintf("Component or '%s' label", tektonconsts.ComponentNameLabel), "Snapshot")
if loaderError != nil {
return controller.RequeueWithError(err)
}
addedToGlobalCandidateListStatus = gitops.AddedToGlobalCandidateListStatus{
Result: false,
Reason: fmt.Sprintf("Failed to set Global Candidate List for component %s due to error %s", a.component.Name, err.Error()),
LastUpdatedTime: time.Now().Format(time.RFC3339),
}
} else {
a.logger.Info("Global Candidate List has been updated for component", "component.Namespace", a.component.Namespace, "component.Name", a.component.Name)
addedToGlobalCandidateListStatus = gitops.AddedToGlobalCandidateListStatus{
Result: true,
Reason: gitops.Success,
LastUpdatedTime: time.Now().Format(time.RFC3339),
}
}
annotationJson, err := json.Marshal(addedToGlobalCandidateListStatus)
if err != nil {
return controller.RequeueWithError(err)
}
// Mark the build PLR as already added to global candidate list to prevent it from getting added again when the Snapshot
// gets reconciled at a later time
err = tekton.MarkBuildPLRAsAddedToGlobalCandidateList(a.context, a.client, a.pipelineRun, string(annotationJson))
if err != nil {
a.logger.Error(err, "Failed to update the build pipelinerun's annotation to AddedToGlobalCandidateList")
return controller.RequeueWithError(err)
}
return controller.ContinueProcessing()
}
// EnsureSnapshotExists is an operation that will ensure that a pipeline Snapshot associated
// to the build PipelineRun being processed exists. Otherwise, it will create a new pipeline Snapshot.
func (a *Adapter) EnsureSnapshotExists() (result controller.OperationResult, err error) {
// If we are not using ComponentGroups, skip this method
// TODO: remove check after migration
if a.application != nil {
return controller.ContinueProcessing()
}
// a marker if we should remove finalizer from build PLR
var canRemoveFinalizer bool
defer func() {
// Don't write a failure annotation for transient Chains-not-signed errors
annotationErr := err
if h.IsChainsNotSignedError(err) {
annotationErr = nil
}
updateErr := a.updateBuildPipelineRunWithFinalInfo(canRemoveFinalizer, annotationErr)
if updateErr != nil {
if errors.IsNotFound(updateErr) {
result, err = controller.ContinueProcessing()
} else {
a.logger.Error(updateErr, "Failed to update build pipelineRun")
result, err = controller.RequeueWithError(updateErr)
}
}
}()
if !h.HasPipelineRunSucceeded(a.pipelineRun) {
a.handleUnsuccessfulPipelineRun(&canRemoveFinalizer)
return controller.ContinueProcessing()
}
if _, found := a.pipelineRun.Annotations[tektonconsts.PipelineRunChainsSignedAnnotation]; !found {
completionTime := a.pipelineRun.Status.CompletionTime
var referenceTime time.Time
if completionTime != nil {
referenceTime = completionTime.Time
} else {
referenceTime = a.pipelineRun.CreationTimestamp.Time
}
if time.Since(referenceTime) > h.ChainsSignedCheckTimeout {
err := fmt.Errorf("not processing the pipelineRun because it's not yet signed with Chains after %s", h.ChainsSignedCheckTimeout)
a.logger.Error(err, "Exceeded timeout waiting for Chains signing")
return controller.RequeueOnErrorOrContinue(err)
}
a.logger.Info("PipelineRun not yet signed by Chains, will requeue",
"elapsedSinceCompletion", time.Since(referenceTime).String(),
"timeout", h.ChainsSignedCheckTimeout.String())
return controller.RequeueOnErrorOrContinue(&h.ChainsNotSignedError{
Message: "PipelineRun not yet signed by Chains, requeueing",
})
}
if _, found := a.pipelineRun.Annotations[tektonconsts.SnapshotNamesLabel]; found {
a.logger.Info("The build pipelineRun is already associated with existing Snapshot via annotation",
"snapshot.Name", a.pipelineRun.Annotations[tektonconsts.SnapshotNamesLabel])
canRemoveFinalizer = true
return controller.ContinueProcessing()
}
componentGroupNames := h.GetComponentGroupNames(a.componentGroups)
existingSnapshots, err := a.loader.GetAllSnapshotsForBuildPipelineRun(a.context, a.client, a.pipelineRun, componentGroupNames)
if err != nil {
a.logger.Error(err, "Failed to fetch Snapshots for the build pipelineRun")
return controller.RequeueWithError(err)
}
if allComponentGroupsHaveSnapshots(existingSnapshots) {
return a.checkForOneSnapshotPerComponentGroup(&canRemoveFinalizer, existingSnapshots)
}
for _, componentGroup := range *a.componentGroups {
expectedSnapshot, err := snapshot.PrepareSnapshotForPipelineRun(a.context, a.client, a.pipelineRun, a.component.Name, &componentGroup, a.loader)
if err != nil {
return a.updatePipelineRunWithCustomizedError(&canRemoveFinalizer, err, a.context, a.pipelineRun, a.client, a.logger)
}
// Try to create snapshot, retry with suffix on collision
err = snapshot.CreateSnapshotWithCollisionHandling(a.context, a.client, a.pipelineRun, expectedSnapshot, componentGroup, a.logger)
if err != nil {
result, err = a.handleSnapshotCreationFailure(&canRemoveFinalizer, err)
return result, err
}
a.logger.LogAuditEvent("Created new Snapshot", expectedSnapshot, h.LogActionAdd,
"snapshot.Name", expectedSnapshot.Name,
"snapshot.Spec.Components", expectedSnapshot.Spec.Components)
err = a.annotateBuildPipelineRunWithSnapshot(expectedSnapshot)
if err != nil {
a.logger.Error(err, "Failed to update the build pipelineRun with new annotations",
"pipelineRun.Name", a.pipelineRun.Name)
return controller.RequeueWithError(err)
}
}
canRemoveFinalizer = true
return controller.ContinueProcessing()
}
// EnsureSnapshotExistsApplication is an operation that will ensure that a pipeline Snapshot associated
// to the build PipelineRun being processed exists. Otherwise, it will create a new pipeline Snapshot.
// NOTE: This method is for the old (component/application) model. PLRS associated with the new model
// will run EnsureSnapshotExists() instead.
// TODO: remove after migration
func (a *Adapter) EnsureSnapshotExistsApplication() (result controller.OperationResult, err error) {
// If we are using ComponentGroups, skip this method
if a.application == nil {
return controller.ContinueProcessing()
}
// a marker if we should remove finalizer from build PLR
var canRemoveFinalizer bool
defer func() {
// Don't write a failure annotation for transient Chains-not-signed errors
annotationErr := err
if h.IsChainsNotSignedError(err) {
annotationErr = nil
}
updateErr := a.updateBuildPipelineRunWithFinalInfo(canRemoveFinalizer, annotationErr)
if updateErr != nil {
if errors.IsNotFound(updateErr) {
result, err = controller.ContinueProcessing()
} else {
a.logger.Error(updateErr, "Failed to update build pipelineRun")
result, err = controller.RequeueWithError(updateErr)
}
}
}()
if !h.HasPipelineRunSucceeded(a.pipelineRun) {
a.handleUnsuccessfulPipelineRun(&canRemoveFinalizer)
return controller.ContinueProcessing()
}
if _, found := a.pipelineRun.Annotations[tektonconsts.PipelineRunChainsSignedAnnotation]; !found {
completionTime := a.pipelineRun.Status.CompletionTime
var referenceTime time.Time
if completionTime != nil {
referenceTime = completionTime.Time
} else {
referenceTime = a.pipelineRun.CreationTimestamp.Time
}
if time.Since(referenceTime) > h.ChainsSignedCheckTimeout {
err := fmt.Errorf("not processing the pipelineRun because it's not yet signed with Chains after %s", h.ChainsSignedCheckTimeout)
a.logger.Error(err, "Exceeded timeout waiting for Chains signing")
return controller.RequeueOnErrorOrContinue(err)
}
a.logger.Info("PipelineRun not yet signed by Chains, will requeue",
"elapsedSinceCompletion", time.Since(referenceTime).String(),
"timeout", h.ChainsSignedCheckTimeout.String())
return controller.RequeueOnErrorOrContinue(&h.ChainsNotSignedError{
Message: "PipelineRun not yet signed by Chains, requeueing",
})
}
if _, found := a.pipelineRun.Annotations[tektonconsts.SnapshotNameLabel]; found {
a.logger.Info("The build pipelineRun is already associated with existing Snapshot via annotation",
"snapshot.Name", a.pipelineRun.Annotations[tektonconsts.SnapshotNameLabel])
canRemoveFinalizer = true
return controller.ContinueProcessing()
}
existingSnapshots, err := a.loader.GetAllSnapshotsForBuildPipelineRunApplication(a.context, a.client, a.pipelineRun)
if err != nil {
a.logger.Error(err, "Failed to fetch Snapshots for the build pipelineRun")
return controller.RequeueWithError(err)
}
if len(*existingSnapshots) > 0 {
result, err = a.ensureBuildPLRSWithSnapshotAnnotation(&canRemoveFinalizer, existingSnapshots, a.application.Name)
return result, err
}
expectedSnapshot, err := a.prepareSnapshotForPipelineRun(a.pipelineRun, a.component, a.application)
if err != nil {
return a.updatePipelineRunWithCustomizedError(&canRemoveFinalizer, err, a.context, a.pipelineRun, a.client, a.logger)
}
// Try to create snapshot, retry with suffix on collision
err = a.createSnapshotWithCollisionHandling(expectedSnapshot)
if err != nil {
result, err = a.handleSnapshotCreationFailure(&canRemoveFinalizer, err)
return result, err
}
a.logger.LogAuditEvent("Created new Snapshot", expectedSnapshot, h.LogActionAdd,
"snapshot.Name", expectedSnapshot.Name,
"snapshot.Spec.Components", expectedSnapshot.Spec.Components)
err = a.annotateBuildPipelineRunWithSnapshot(expectedSnapshot)
if err != nil {
a.logger.Error(err, "Failed to update the build pipelineRun with new annotations",
"pipelineRun.Name", a.pipelineRun.Name)
return controller.RequeueWithError(err)
}
canRemoveFinalizer = true
return controller.ContinueProcessing()
}
func (a *Adapter) EnsurePipelineIsFinalized() (controller.OperationResult, error) {
if h.HasPipelineRunFinished(a.pipelineRun) || controllerutil.ContainsFinalizer(a.pipelineRun, h.IntegrationPipelineRunFinalizer) {
return controller.ContinueProcessing()
}
// if pipelinerun has been deleted, do not add finalizer
if a.pipelineRun.GetDeletionTimestamp() != nil {
return controller.ContinueProcessing()
}
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
var err error
// Refetch the PipelineRun to get the latest version before patching
a.pipelineRun, err = a.loader.GetPipelineRun(a.context, a.client, a.pipelineRun.Name, a.pipelineRun.Namespace)
if err != nil {
return err
}
// if pipelinerun has been deleted, do not add finalizer
if a.pipelineRun.GetDeletionTimestamp() != nil {
return nil
}
// Check again if finalizer was already added (might have been added by another reconcile)
if controllerutil.ContainsFinalizer(a.pipelineRun, h.IntegrationPipelineRunFinalizer) {
return nil
}
err = h.AddFinalizerToPipelineRun(a.context, a.client, a.logger, a.pipelineRun, h.IntegrationPipelineRunFinalizer)
return err
})
if err != nil {
// if IsNotFound error, do not log error or requeue
if errors.IsNotFound(err) {
a.logger.Info(fmt.Sprintf("Could not add finalizer %s to build pipeline %s. Build pipeline could not be found.", h.IntegrationPipelineRunFinalizer, a.pipelineRun.Name))
return controller.ContinueProcessing()
}
a.logger.Error(err, fmt.Sprintf("Could not add finalizer %s to build pipeline %s", h.IntegrationPipelineRunFinalizer, a.pipelineRun.Name))
return controller.RequeueWithError(err)
}
return controller.ContinueProcessing()
}
// EnsurePRGroupAnnotated is an operation that will ensure that the pr group info
// is added to build pipelineRun metadata label and annotation once it is created,
// then these label and annotation will be copied to component snapshot when it is created
func (a *Adapter) EnsurePRGroupAnnotated() (controller.OperationResult, error) {
if tekton.IsPLRCreatedByPACPushEvent(a.pipelineRun) {
a.logger.Info("build pipelineRun is not created by pull/merge request, no need to annotate")
return controller.ContinueProcessing()
}
if metadata.HasLabel(a.pipelineRun, gitops.PRGroupHashLabel) && metadata.HasAnnotation(a.pipelineRun, gitops.PRGroupAnnotation) {
// If the pipeline failed, attempt to notify all component Snapshots in the group about the failure
if !h.HasPipelineRunSucceeded(a.pipelineRun) && h.HasPipelineRunFinished(a.pipelineRun) {
prGroupName := a.pipelineRun.Annotations[gitops.PRGroupAnnotation]
buildPLRFailureMsg := fmt.Sprintf("build PLR %s failed for component %s so it can't be added to the group Snapshot for PR group %s", a.pipelineRun.Name, a.component.Name, prGroupName)
err := a.notifySnapshotsInGroupAboutBuild(a.pipelineRun, buildPLRFailureMsg)
if err != nil {
return controller.RequeueWithError(err)
}
}
a.logger.Info("build pipelineRun has had pr group info in metadata, no need to update")
return controller.ContinueProcessing()
}
var err error
// We have to reassign pipelineRun here because of the retryOnConflict within addPRGroupToBuildPLRMetadata sets the
// previous version of the pipelineRun so we don't get the updated pr group metadata
a.pipelineRun, err = a.addPRGroupToBuildPLRMetadata(a.pipelineRun)
if err != nil {
if errors.IsNotFound(err) {
a.logger.Error(err, "failed to add pr group info to build pipelineRun metadata due to notfound pipelineRun")
return controller.StopProcessing()
} else {
a.logger.Error(err, "failed to add pr group info to build pipelineRun metadata")
return controller.RequeueWithError(err)
}
}
a.logger.LogAuditEvent("pr group info is updated to build pipelineRun metadata", a.pipelineRun, h.LogActionUpdate,
"pipelineRun.Name", a.pipelineRun.Name, "PR group", a.pipelineRun.Annotations[gitops.PRGroupAnnotation])
// Notify the group Snapshot and other build PLRs in the group about the incoming new build
if !h.HasPipelineRunFinished(a.pipelineRun) && metadata.HasAnnotation(a.pipelineRun, gitops.PRGroupAnnotation) {
prGroupName := a.pipelineRun.Annotations[gitops.PRGroupAnnotation]
buildPLRIncomingMsg := fmt.Sprintf("a new build PLR %s is running for component %s, waiting for it to create a new group Snapshot for PR group %s", a.pipelineRun.Name, a.component.Name, prGroupName)
err := a.notifySnapshotsInGroupAboutBuild(a.pipelineRun, buildPLRIncomingMsg)
if err != nil {
return controller.RequeueWithError(err)
}
}
return controller.ContinueProcessing()
}
// EnsureIntegrationTestReportedToGitProvider is an operation that will ensure that the integration test status is initialized as pending
// state when build PLR is triggered/retriggered or cancelled/failed when failing to create snapshot
// to prevent PR/MR from being automerged unexpectedly and also show the snapshot creation failure on PR/MR
func (a *Adapter) EnsureIntegrationTestReportedToGitProvider() (controller.OperationResult, error) {
if tekton.IsPLRCreatedByPACPushEvent(a.pipelineRun) {
a.logger.Info("build pipelineRun is not created by pull/merge request, no need to set integration test status in git provider")
return controller.ContinueProcessing()
}
if !metadata.HasLabel(a.pipelineRun, gitops.PRGroupHashLabel) || !metadata.HasAnnotation(a.pipelineRun, gitops.PRGroupAnnotation) {
a.logger.Info("pr group info has not been added to build pipelineRun metadata, skipping reporting tests for the build pipelineRun")
return controller.ContinueProcessing()
}
isLatestBuildPlr, err := a.IsLatestBuildPipelineRunInComponentWithPRGroupHash(a.pipelineRun)
if err != nil {
a.logger.Error(err, "failed to check if build pipelineRun is the latest one")
}
if !isLatestBuildPlr {
a.logger.Info("build plr %s/%s is not the latest one, skipp reporting integration test status to git provider", a.pipelineRun.Namespace, a.pipelineRun.Name)
return controller.ContinueProcessing()
}
if metadata.HasAnnotation(a.pipelineRun, tektonconsts.SnapshotNamesLabel) {
SnapshotCreated := "SnapshotCreated"
a.logger.Info("snapshot has been created for build pipelineRun, no need to report integration status from build pipelinerun status")
if !metadata.HasAnnotationWithValue(a.pipelineRun, h.SnapshotCreationReportAnnotation, SnapshotCreated) {
if err := tekton.AnnotateBuildPipelineRun(a.context, a.pipelineRun, h.SnapshotCreationReportAnnotation, SnapshotCreated, a.client); err != nil {
a.logger.Error(err, "failed to annotate build pipelineRun")
return controller.RequeueWithError(err)
}
}
return controller.ContinueProcessing()
}
integrationTestStatus := getIntegrationTestStatusFromBuildPLR(a.pipelineRun)
if integrationTestStatus == intgteststat.IntegrationTestStatus(0) {
a.logger.Info("integration test has been set correctly or is being processed, no need to set integration test status from build pipelinerun")
return controller.ContinueProcessing()
}
var numComponentSnapshotScenarios, numGroupSnapshotScenarios int
// TODO: remove application section after migration
if a.application != nil {
numComponentSnapshotScenarios, numGroupSnapshotScenarios, err = a.reportIntegrationStatusAndHandleGroupsForApplication(&integrationTestStatus)
if err != nil {
a.logger.Error(err, "failed to report status and handle groups")
return controller.RequeueWithError(err)
}
} else {
numComponentSnapshotScenarios, numGroupSnapshotScenarios, err = a.reportIntegrationStatusAndHandleGroups(&integrationTestStatus)
if err != nil {
a.logger.Error(err, "failed to report status and handle groups")
return controller.RequeueWithError(err)
}
}
if numComponentSnapshotScenarios > 0 || numGroupSnapshotScenarios > 0 {
if err = tekton.AnnotateBuildPipelineRun(a.context, a.pipelineRun, h.SnapshotCreationReportAnnotation, integrationTestStatus.String(), a.client); err != nil {
a.logger.Error(err, fmt.Sprintf("failed to write build plr annotation %s", h.SnapshotCreationReportAnnotation))
return controller.RequeueWithError(fmt.Errorf("failed to write snapshot report status metadata for annotation %s: %w", h.SnapshotCreationReportAnnotation, err))
}
}
return controller.ContinueProcessing()
}
func (a *Adapter) reportIntegrationStatusAndHandleGroupsForApplication(integrationTestStatus *intgteststat.IntegrationTestStatus) (int, int, error) {
numComponentSnapshotScenarios := 0
numGroupSnapshotScenarios := 0
var tempComponentSnapshot *applicationapiv1alpha1.Snapshot
allIntegrationTestScenarios, err := a.loader.GetAllIntegrationTestScenariosForApplication(a.context, a.client, a.application)
if err != nil {
a.logger.Error(err, "Failed to get integration test scenarios for the following application",
"Application.Namespace", a.application.Namespace, "Application.Name", a.application.Name)
return 0, 0, err
}
if allIntegrationTestScenarios == nil {
return 0, 0, nil
}
a.logger.Info(fmt.Sprintf("try to set integration test status according to the build PLR status %s", integrationTestStatus.String()))
tempComponentSnapshot = a.prepareTempComponentSnapshot(a.pipelineRun, &a.application.ObjectMeta, true)
numComponentSnapshotScenarios, err = a.reportStatusForExpectedSnapshot(a.pipelineRun, tempComponentSnapshot, allIntegrationTestScenarios, *integrationTestStatus, a.component.Name)
if err != nil {
return 0, 0, fmt.Errorf("failed to report status for expected group Snapshot: %w", err)
}
a.logger.Info("try to check if group snapshot is expected for build PLR")
isGroupSnapshotExpected, err := a.isGroupSnapshotExpectedForBuildPLR(a.pipelineRun, tempComponentSnapshot, a.application.Name, gitops.ApplicationNameLabel)
if err != nil {
a.logger.Error(err, "failed to check if group snapshot is expected")
return 0, 0, fmt.Errorf("failed to check if group snapshot is expected: %w", err)
}
if isGroupSnapshotExpected {
a.logger.Info("group snapshot is expected to be created for build pipelinerun, group integration test should be set for found context scenario", "pipelineRun.Name", a.pipelineRun.Name)
tempGroupSnapshot := a.prepareTempGroupSnapshot(a.pipelineRun, &a.application.ObjectMeta, true)
numGroupSnapshotScenarios, err = a.reportStatusForExpectedSnapshot(a.pipelineRun, tempGroupSnapshot, allIntegrationTestScenarios, *integrationTestStatus, gitops.ComponentNameForGroupSnapshot)
if err != nil {
return 0, 0, fmt.Errorf("failed to report status for expected group Snapshot: %w", err)
}
}
return numComponentSnapshotScenarios, numGroupSnapshotScenarios, nil
}
func (a *Adapter) reportIntegrationStatusAndHandleGroups(integrationTestStatus *intgteststat.IntegrationTestStatus) (int, int, error) {
numComponentSnapshotScenarios := 0
numGroupSnapshotScenarios := 0
for _, componentGroup := range *a.componentGroups {
integrationTestScenariosForGroup, err := a.loader.GetAllIntegrationTestScenariosForComponentGroup(a.context, a.client, &componentGroup)
if err != nil {
return 0, 0, fmt.Errorf("failed to get integration test scenarios for the componentGroup %s: %w", componentGroup.Name, err)
}
if integrationTestScenariosForGroup == nil || len(*integrationTestScenariosForGroup) == 0 {
continue
}
a.logger.Info(fmt.Sprintf("try to set integration test status according to the build PLR status %s", integrationTestStatus.String()))
tempComponentSnapshot := a.prepareTempComponentSnapshot(a.pipelineRun, &componentGroup.ObjectMeta, false)
num, err := a.reportStatusForExpectedSnapshot(a.pipelineRun, tempComponentSnapshot, integrationTestScenariosForGroup, *integrationTestStatus, a.component.Name)
if err != nil {
return 0, 0, fmt.Errorf("failed to report status for expected group Snapshot: %w", err)
}
numComponentSnapshotScenarios += num
a.logger.Info("try to check if group snapshot is expected for build PLR")
isGroupSnapshotExpected, err := a.isGroupSnapshotExpectedForBuildPLR(a.pipelineRun, tempComponentSnapshot, componentGroup.Name, gitops.ComponentGroupNameLabel)
if err != nil {
return 0, 0, fmt.Errorf("failed to check if group snapshot is expected: %w", err)
}
if isGroupSnapshotExpected {
a.logger.Info("group snapshot is expected to be created for build pipelinerun, group integration test should be set for found context scenario", "pipelineRun.Name", a.pipelineRun.Name)
tempGroupSnapshot := a.prepareTempGroupSnapshot(a.pipelineRun, &componentGroup.ObjectMeta, false)
groupName := fmt.Sprintf("%s %s", gitops.ComponentNameForGroupSnapshot, componentGroup.Name)
numGroup, err := a.reportStatusForExpectedSnapshot(a.pipelineRun, tempGroupSnapshot, integrationTestScenariosForGroup, *integrationTestStatus, groupName)
if err != nil {
a.logger.Error(err, "failed to report status for expected group Snapshot")
return 0, 0, fmt.Errorf("failed to report status for expected group Snapshot: %w", err)
}
numGroupSnapshotScenarios += numGroup
}
}
return numComponentSnapshotScenarios, numGroupSnapshotScenarios, nil
}
// EnsureComponentSnapshotAnnotatedForMergedPR annotates all component snapshots when push build PLR is triggered for the same PR since the PR/MR are merged
func (a *Adapter) EnsurePRSnapshotAnnotatedForMergedPR() (controller.OperationResult, error) {
if !tekton.IsPLRCreatedByPACPushEvent(a.pipelineRun) {
a.logger.Info("build pipelineRun is not created by pull/merge request, no need to annotate PR component snapshot")
return controller.ContinueProcessing()
}
if !metadata.HasLabel(a.pipelineRun, tektonconsts.PipelineAsCodePullRequestLabel) {
a.logger.Info("build pipelineRun has no pull request label, no need to annotate PR component snapshots")
return controller.ContinueProcessing()
}
if metadata.HasAnnotationWithValue(a.pipelineRun, gitops.PRStatusAnnotation, gitops.PRStatusMerged) {
a.logger.Info("build pipelineRun PR status is annotated as merged, we consider component snapshots have been annotated as well")
return controller.ContinueProcessing()
}
var prComponentSnapshots *[]applicationapiv1alpha1.Snapshot
var err error
// TODO: remove application-specific code
if a.application != nil {
prComponentSnapshots, err = a.loader.GetPRComponentSnapshotsForComponentApplication(a.context, a.client, a.pipelineRun.Namespace, a.application.Name, a.component.Name, a.pipelineRun.Labels[tektonconsts.PipelineAsCodePullRequestLabel])
} else {
componentGroupNames := h.GetComponentGroupNames(a.componentGroups)
prComponentSnapshots, err = a.loader.GetPRComponentSnapshotsForComponent(a.context, a.client, componentGroupNames, a.pipelineRun.Namespace, a.component.Name, a.pipelineRun.Labels[tektonconsts.PipelineAsCodePullRequestLabel])
}
if err != nil {
a.logger.Error(err, "failed to get all pull request component snapshots for PR", "pr.Number", a.pipelineRun.Labels[tektonconsts.PipelineAsCodePullRequestLabel])
return controller.RequeueWithError(err)
}
for _, snapshot := range *prComponentSnapshots {
if err = gitops.AnnotateSnapshot(a.context, &snapshot, gitops.PRStatusAnnotation, gitops.PRStatusMerged, a.client); err != nil {
a.logger.Error(err, "failed to annotate snapshot with PR status annotation", "snapshot.Name", snapshot.Name)
return controller.RequeueWithError(fmt.Errorf("failed to annotate snapshot %s with annotation %s: %w", snapshot.Name, gitops.PRStatusAnnotation, err))
}
}
// annotate build PLR to avoid re-checking build plr and re-annotating component snapshot again
if err = tekton.AnnotateBuildPipelineRun(a.context, a.pipelineRun, gitops.PRStatusAnnotation, gitops.PRStatusMerged, a.client); err != nil {
a.logger.Error(err, "failed to annotate build pipelineRun with PR status annotation")
return controller.RequeueWithError(fmt.Errorf("failed to annotate build pipelineRun %s with annotation %s: %w", a.pipelineRun.Name, gitops.PRStatusAnnotation, err))
}
a.logger.Info("all component snapshots for the PR have been annotated with merged status")
return controller.ContinueProcessing()
}
// reportStatusForGroupSnapshot reports the initial integration test statuses for the expected Snapshot
func (a *Adapter) reportStatusForExpectedSnapshot(pipelineRun *tektonv1.PipelineRun, snapshot *applicationapiv1alpha1.Snapshot, integrationTestScenarios *[]v1beta2.IntegrationTestScenario,
integrationTestStatus intgteststat.IntegrationTestStatus, componentNameOrPrGroup string) (int, error) {
integrationTestScenariosForSnapshot := gitops.FilterIntegrationTestScenariosWithContext(integrationTestScenarios, snapshot)
numIntegrationTestScenarios := len(*integrationTestScenariosForSnapshot)
if numIntegrationTestScenarios > 0 {
isErrorRecoverable, err := a.ReportIntegrationTestStatusAccordingToBuildPLR(pipelineRun, snapshot, integrationTestScenariosForSnapshot, integrationTestStatus, componentNameOrPrGroup)
if err != nil {
a.logger.Error(err, "failed to initialize integration test status or report snapshot creation status to git provider from build pipelineRun",
"pipelineRun.Namespace", a.pipelineRun.Namespace, "pipelineRun.Name", a.pipelineRun.Name, "isErrorRecoverable", isErrorRecoverable)
if isErrorRecoverable {
return numIntegrationTestScenarios, err
} else {
a.logger.Error(err, "meeting unrecoverable error, stop reporting build pipelinerun status to git provider integration test scenario")
}
}
}
return numIntegrationTestScenarios, nil
}
// EnsureSupercededSnapshotsCanceled checks for currently running snapshots from the same PR as
// the build PipelineRun and marks them as canceled. This will trigger the Snapshot controller
// to cancel any running pipelines for the canceled snapshot
func (a *Adapter) EnsureSupercededSnapshotsCanceled() (result controller.OperationResult, err error) {
if h.HasPipelineRunFinished(a.pipelineRun) {
a.logger.Info(fmt.Sprintf("PipelineRun %s has finished running", a.pipelineRun.Name))
return controller.ContinueProcessing()
}
if tekton.IsPLRCreatedByPACPushEvent(a.pipelineRun) {
a.logger.Info(fmt.Sprintf("PipelineRun %s is not associated with a pull request", a.pipelineRun.Name))
return controller.ContinueProcessing()
}
// Get Snapshots with matching PR annotation that are not finished
pr := a.pipelineRun.Labels[tektonconsts.PipelineAsCodePullRequestLabel]
// TODO: remove branch after migration to new model
var snapshots *[]applicationapiv1alpha1.Snapshot
if a.application != nil {
snapshots, err = a.loader.GetAllPullSnapshotsForPR(a.context, a.client, a.application.ObjectMeta, a.component.Name, pr)
} else {
// We only have to pass one ComponentGroup here. The componentGroup is only used to get
// the namespace to search. Since all ComponentGroups have to belong to the same NS, we
// don't need to search with each ComponentGroup
snapshots, err = a.loader.GetAllPullSnapshotsForPR(a.context, a.client, (*a.componentGroups)[0].ObjectMeta, a.component.Name, pr)
}
if err != nil {
return controller.RequeueWithError(fmt.Errorf("failed to get running snapshots for PR %s: %w", pr, err))
}
// Mark snapshots as canceled
for _, snapshot := range *snapshots {
err := retry.OnError(retry.DefaultRetry, func(e error) bool { return true }, func() error {
var e error
if !gitops.HaveAppStudioTestsFinished(&snapshot) && !gitops.IgnoreSupersession(snapshot.ObjectMeta) {
a.logger.Info(fmt.Sprintf("Snapshot %s has been superseded by build PLR %s. Canceling snapshot and its pipelineRuns", snapshot.Name, a.pipelineRun.Name))
e = a.cancelAllPipelineRunsForSnapshot(&snapshot)
if e != nil {
return e
}
}
e = gitops.MarkSnapshotAsCanceled(a.context, a.client, &snapshot, "Canceled - Superseded by new build")
return e
})
if err != nil {
a.logger.Error(err, fmt.Sprintf("Could not cancel test pipelines for snapshot %s", snapshot.Name))
}
}
return controller.ContinueProcessing()
}
// notifySnapshotsInGroupAboutBuild tries to find the latest group Snapshot and notify it about the failed build.
// If the group Snapshot can't be found, find the component Snapshots that would belong to the same group and notify them instead.
func (a *Adapter) notifySnapshotsInGroupAboutBuild(pipelineRun *tektonv1.PipelineRun, message string) error {
prGroupHash := pipelineRun.Labels[gitops.PRGroupHashLabel]
var allComponentSnapshotsInGroup *[]applicationapiv1alpha1.Snapshot
var buildPipelineRuns *[]tektonv1.PipelineRun
if a.application != nil {
var err error
buildPipelineRuns, err = a.loader.GetPipelineRunsWithPRGroupHashForApplication(a.context, a.client, a.pipelineRun.Namespace, prGroupHash, a.application.Name)
if err != nil {
return fmt.Errorf("failed to get build pipelineRuns for given pr group hash %s: %w", prGroupHash, err)
}
// Don't do anything if the build pipelineRun isn't the latest for its component
if !tekton.IsLatestBuildPipelineRunInComponent(pipelineRun, buildPipelineRuns) {
a.logger.Info("not the latest pipelineRun, skipping notifying the group about the failure")
return nil
}
applicationComponents, err := a.loader.GetAllApplicationComponents(a.context, a.client, a.application)
if err != nil {
return fmt.Errorf("failed to get all application components for application %s: %w", a.application.Name, err)
}
// Annotate all latest component Snapshots that are part of the PR group
for _, applicationComponent := range *applicationComponents {
applicationComponent := applicationComponent // G601
allComponentSnapshotsInGroup, err = a.loader.GetMatchingComponentSnapshotsForComponentAndPRGroupHash(a.context, a.client,
a.pipelineRun.Namespace, applicationComponent.Name, prGroupHash, a.application.Name, gitops.ApplicationNameLabel)
if err != nil {
return fmt.Errorf("failed to fetch Snapshots for component %s: %w", applicationComponent.Name, err)
}
if allComponentSnapshotsInGroup != nil && len(*allComponentSnapshotsInGroup) > 0 {
latestSnapshot := gitops.SortSnapshots(*allComponentSnapshotsInGroup)[0]
err = gitops.AnnotateSnapshot(a.context, &latestSnapshot, gitops.PRGroupCreationAnnotation,
message, a.client)
if err != nil {
return fmt.Errorf("failed to annotate latest snapshot %s of component %s: %w", latestSnapshot.Name, applicationComponent.Name, err)
}
}
}
} else {
allBuildPipelineRunsWithPRGroupHash, err := a.loader.GetPipelineRunsWithPRGroupHash(a.context, a.client, a.pipelineRun.Namespace, prGroupHash)
if err != nil {
return fmt.Errorf("failed to get build pipelineRuns for given pr group hash %s: %w", prGroupHash, err)
}
buildPipelineRuns = a.filterPipelineRunsForComponentGroups(allBuildPipelineRunsWithPRGroupHash, a.componentGroups)
// Don't do anything if the build pipelineRun isn't the latest for its component
if !tekton.IsLatestBuildPipelineRunInComponent(pipelineRun, buildPipelineRuns) {
a.logger.Info("not the latest pipelineRun, skipping notifying the group about the failure")
return nil
}
for _, componentGroup := range *a.componentGroups {
componentGroup := componentGroup
for _, groupComponent := range componentGroup.Spec.Components {
groupComponent := groupComponent
allComponentSnapshotsInGroup, err = a.loader.GetMatchingComponentSnapshotsForComponentAndPRGroupHash(a.context, a.client,
a.pipelineRun.Namespace, groupComponent.Name, prGroupHash, componentGroup.Name, gitops.ComponentGroupNameLabel)
if err != nil {
return fmt.Errorf("failed to fetch Snapshots for component %s and pr group hash %s: %w", groupComponent.Name, prGroupHash, err)
}
if allComponentSnapshotsInGroup != nil && len(*allComponentSnapshotsInGroup) > 0 {
latestSnapshot := gitops.SortSnapshots(*allComponentSnapshotsInGroup)[0]
err = gitops.AnnotateSnapshot(a.context, &latestSnapshot, gitops.PRGroupCreationAnnotation,
message, a.client)
if err != nil {
return fmt.Errorf("failed to annotate latest snapshot %s of component %s with PR group creation annotation: %w",
latestSnapshot.Name, groupComponent.Name, err)
}
}
}
}
}
// In case there are in-flight build pipelineRuns, we want to also annotate them to make sure that the failure is propagated
// to future Snapshots in the group
for _, buildPipelineRun := range *buildPipelineRuns {
buildPipelineRun := buildPipelineRun
// check if build PLR finished
if !h.HasPipelineRunFinished(&buildPipelineRun) && buildPipelineRun.Labels[tektonconsts.ComponentNameLabel] != a.component.Name {
err := tekton.AnnotateBuildPipelineRun(a.context, &buildPipelineRun, gitops.PRGroupCreationAnnotation, message, a.client)
if err != nil {
return fmt.Errorf("failed to annotate build pipelineRun %s with PR group creation annotation: %w", buildPipelineRun.Name, err)
}
}
}
a.logger.Info("notified all component snapshots and build pipelines in the pr group about the build pipeline status",
"prGroup", pipelineRun.Annotations[gitops.PRGroupAnnotation], "prGroupHash", prGroupHash)
return nil
}
func (a *Adapter) filterPipelineRunsForComponentGroups(allBuildPipelineRuns *[]tektonv1.PipelineRun, componentGroups *[]v1beta2.ComponentGroup) *[]tektonv1.PipelineRun {
var filteredBuildPipelineRuns []tektonv1.PipelineRun
for _, buildPipelineRun := range *allBuildPipelineRuns {
if builtComponentName, found := buildPipelineRun.Labels[tektonconsts.PipelineRunComponentLabel]; found {
for _, componentGroup := range *componentGroups {
componentNames := h.GetComponentNamesFromComponentGroup(&componentGroup)
if slices.Contains(componentNames, builtComponentName) {
filteredBuildPipelineRuns = append(filteredBuildPipelineRuns, buildPipelineRun)
break
}
}
}
}
return &filteredBuildPipelineRuns
}
// prepareSnapshotForPipelineRun prepares the Snapshot for a given PipelineRun,
// component and application. In case the Snapshot can't be created, an error will be returned.
func (a *Adapter) prepareSnapshotForPipelineRun(pipelineRun *tektonv1.PipelineRun, component *applicationapiv1alpha1.Component, application *applicationapiv1alpha1.Application) (*applicationapiv1alpha1.Snapshot, error) {
newContainerImage, err := tekton.GetImagePullSpecFromPipelineRun(pipelineRun)
if err != nil {
return nil, err
}
componentSource, err := tekton.GetComponentSourceFromPipelineRun(pipelineRun)
if err != nil {
return nil, err
}
componentVersion := ""
if v, verr := tekton.GetComponentVersionFromPipelineRun(pipelineRun); verr == nil {
componentVersion = v
}
gitops.EnrichBuiltComponentSourceGitContext(componentSource, component, componentVersion)
applicationComponents, err := a.loader.GetAllApplicationComponents(a.context, a.client, application)
if err != nil {
return nil, err
}
snapshot, err := gitops.PrepareSnapshot(a.context, a.client, application, applicationComponents, component, newContainerImage, componentSource)
if err != nil {
return nil, err
}
prefixes := []string{gitops.BuildPipelineRunPrefix, gitops.TestLabelPrefix, gitops.CustomLabelPrefix, gitops.ReleaseLabelPrefix}
gitops.CopySnapshotLabelsAndAnnotations(&application.ObjectMeta, snapshot, a.component.Name, &pipelineRun.ObjectMeta, prefixes, true)
snapshot.Labels[gitops.BuildPipelineRunNameLabel] = pipelineRun.Name
if pipelineRun.Status.CompletionTime != nil {
snapshot.Labels[gitops.BuildPipelineRunFinishTimeLabel] = strconv.FormatInt(pipelineRun.Status.CompletionTime.Unix(), 10)
} else {
snapshot.Labels[gitops.BuildPipelineRunFinishTimeLabel] = strconv.FormatInt(time.Now().Unix(), 10)
}
if gitops.IsSnapshotCreatedByPACMergeQueueEvent(snapshot) {
pullRequestNumber := gitops.ExtractPullRequestNumberFromMergeQueueSnapshot(snapshot)
if pullRequestNumber != "" {
snapshot.Labels[gitops.PipelineAsCodePullRequestAnnotation] = pullRequestNumber
snapshot.Annotations[gitops.PipelineAsCodePullRequestAnnotation] = pullRequestNumber
}
}
// Set BuildPipelineRunStartTime annotation with millisecond precision and override snapshot name
var timestampMillis int64
// Get the time
if pipelineRun.Status.StartTime != nil {
timestampMillis = pipelineRun.Status.StartTime.UnixMilli()
} else {
timestampMillis = time.Now().UnixMilli()
}
// Naming once, at the end
snapshot.Annotations[gitops.BuildPipelineRunStartTime] = strconv.FormatInt(timestampMillis, 10)
snapshot.Name = gitops.GenerateSnapshotNameWithTimestamp(application.Name, timestampMillis)
// Copy all build PipelineRun results as Snapshot annotations
gitops.CopyBuildPipelineRunResultsToSnapshot(pipelineRun, snapshot)
// Set the integration workflow annotation based on the PipelineRun type
if tekton.IsPLRCreatedByPACPushEvent(pipelineRun) {
snapshot.Annotations[gitops.IntegrationWorkflowAnnotation] = gitops.IntegrationWorkflowPushValue
} else {
snapshot.Annotations[gitops.IntegrationWorkflowAnnotation] = gitops.IntegrationWorkflowPullRequestValue
}
return snapshot, nil
}
func (a *Adapter) annotateBuildPipelineRunWithSnapshot(snapshot *applicationapiv1alpha1.Snapshot) error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
var err error
a.pipelineRun, err = a.loader.GetPipelineRun(a.context, a.client, a.pipelineRun.Name, a.pipelineRun.Namespace)
if err != nil {
return err
}
// TODO: remove after we migrate away from application model
// We are switching from `appstudio.openshift.io/snapshot` to `appstudio.openshift.io/snapshots` and need to support both for the old model so the UI has time to migrate
if a.application != nil {
err = tekton.AnnotateBuildPipelineRun(a.context, a.pipelineRun, tektonconsts.SnapshotNameLabel, snapshot.Name, a.client)
if err == nil {
a.logger.LogAuditEvent("Updated build pipelineRun snapshot annotation", a.pipelineRun, h.LogActionUpdate,
"snapshot.Name", snapshot.Name)
} else {
return err
}
}
err = tekton.AppendBuildPipelineRunAnnotation(a.context, a.pipelineRun, tektonconsts.SnapshotNamesLabel, snapshot.Name, a.client)
if err == nil {
a.logger.LogAuditEvent("Updated build pipelineRun snapshots annotation", a.pipelineRun, h.LogActionUpdate,
"snapshot.Name", snapshot.Name)
}
return err
})
}
// updateBuildPipelineRunWithFinalInfo adds the final pieces of information to the pipelineRun in order to ensure
// that anything that happened during the reconciliation is reflected in the CR
func (a *Adapter) updateBuildPipelineRunWithFinalInfo(canRemoveFinalizer bool, cerr error) error {
return retry.RetryOnConflict(retry.DefaultRetry, func() error {
var err error
a.pipelineRun, err = a.loader.GetPipelineRun(a.context, a.client, a.pipelineRun.Name, a.pipelineRun.Namespace)
if err != nil {
return err
}
if a.pipelineRun.GetDeletionTimestamp() == nil {
err = tekton.AnnotateBuildPipelineRunWithCreateSnapshotAnnotation(a.context, a.pipelineRun, a.client, cerr)
if err != nil {
a.logger.Error(err, "Failed to annotate build pipelinerun with the snapshot creation status")
return err
}
a.logger.LogAuditEvent("Updated build pipelineRun", a.pipelineRun, h.LogActionUpdate,
h.CreateSnapshotAnnotationName, a.pipelineRun.Annotations[h.CreateSnapshotAnnotationName])
} else {
a.logger.Info("Cannot annotate build pipelinerun annotation with the snapshot creation status since it has been marked as deleted")
}
if canRemoveFinalizer {
err = h.RemoveFinalizerFromPipelineRun(a.context, a.client, a.logger, a.pipelineRun, h.IntegrationPipelineRunFinalizer)
if err != nil {
a.logger.Error(err, "Failed to remove finalizer from build pipelinerun")
return err
}
}
return nil
})
}
func allComponentGroupsHaveSnapshots(existingSnapshots *map[string][]applicationapiv1alpha1.Snapshot) bool {
for componentGroup := range *existingSnapshots {
if len((*existingSnapshots)[componentGroup]) == 0 {
return false
}
}
return true
}
// checkForOneSnapshotPerComponentGroup makes sure that only one snapshot is associated with the build pipelinerun per ComponentGroup
// and annotates the build pipelineRun accordingly
func (a *Adapter) checkForOneSnapshotPerComponentGroup(canRemoveFinalizer *bool, existingSnapshots *map[string][]applicationapiv1alpha1.Snapshot) (result controller.OperationResult, err error) {
for componentGroup, snapshots := range *existingSnapshots {
result, err := a.ensureBuildPLRSWithSnapshotAnnotation(canRemoveFinalizer, &snapshots, componentGroup)
if err != nil {
return result, err
}
}
return controller.ContinueProcessing()
}
// checkForSnapshotsCount makes sure that only one snapshot is associated with build pipelinerun and annotate that PLR with snapshot
// informs about fact when PLR is associated with more existing snapshots
func (a *Adapter) ensureBuildPLRSWithSnapshotAnnotation(canRemoveFinalizer *bool, existingSnapshots *[]applicationapiv1alpha1.Snapshot, componentGroupName string) (result controller.OperationResult, err error) {
if len(*existingSnapshots) == 1 {
existingSnapshot := (*existingSnapshots)[0]
a.logger.Info("There is an existing Snapshot associated with this build pipelineRun, but the pipelineRun is not yet annotated",
"snapshot.Name", existingSnapshot.Name)
err := a.annotateBuildPipelineRunWithSnapshot(&existingSnapshot)
if err != nil {
a.logger.Error(err, "Failed to update the build pipelineRun with snapshot name",
"pipelineRun.Name", a.pipelineRun.Name)
return controller.RequeueWithError(err)
}
} else {
a.logger.Info("The build pipelineRun is already associated with more than one existing Snapshot for the application/componentGroup", componentGroupName)
}
*canRemoveFinalizer = true
return controller.ContinueProcessing()
}
// failedOrDeletedPLR checks for pipelinerun state and proceeds according to it,
// failed or in running state > report this into a logger and set canRemoveFinalizer flag to true
func (a *Adapter) handleUnsuccessfulPipelineRun(canRemoveFinalizer *bool) {
// The build pipelinerun has not finished
if h.HasPipelineRunFinished(a.pipelineRun) || a.pipelineRun.GetDeletionTimestamp() != nil {
// The pipeline run has failed
// OR pipeline has been deleted but it's still in running state (tekton bug/feature?)
a.logger.Info("Finished processing of unsuccessful build PLR",
"statusCondition", a.pipelineRun.GetStatusCondition(),