4
4
"context"
5
5
"crypto/sha256"
6
6
"fmt"
7
+ "strings"
7
8
"time"
8
9
9
10
"github.com/operator-framework/operator-registry/pkg/api"
@@ -37,7 +38,7 @@ const (
37
38
// and overrides the default specified by the --bundle-unpack-timeout flag
38
39
// The time duration should be in the same format as accepted by time.ParseDuration()
39
40
// e.g 1m30s
40
- BundleUnpackTimeoutAnnotationKey = "bundle-unpack-timeout"
41
+ BundleUnpackTimeoutAnnotationKey = "operatorframework.io/ bundle-unpack-timeout"
41
42
)
42
43
43
44
type BundleUnpackResult struct {
@@ -220,7 +221,7 @@ func (c *ConfigMapUnpacker) job(cmRef *corev1.ObjectReference, bundlePath string
220
221
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Unpacker
221
222
222
223
type Unpacker interface {
223
- UnpackBundle (lookup * operatorsv1alpha1.BundleLookup , annotationUnpackTimeout time.Duration ) (result * BundleUnpackResult , err error )
224
+ UnpackBundle (lookup * operatorsv1alpha1.BundleLookup , timeout time.Duration ) (result * BundleUnpackResult , err error )
224
225
}
225
226
226
227
type ConfigMapUnpacker struct {
@@ -367,7 +368,7 @@ const (
367
368
NotUnpackedMessage = "bundle contents have not yet been persisted to installplan status"
368
369
)
369
370
370
- func (c * ConfigMapUnpacker ) UnpackBundle (lookup * operatorsv1alpha1.BundleLookup , annotationUnpackTimeout time.Duration ) (result * BundleUnpackResult , err error ) {
371
+ func (c * ConfigMapUnpacker ) UnpackBundle (lookup * operatorsv1alpha1.BundleLookup , timeout time.Duration ) (result * BundleUnpackResult , err error ) {
371
372
372
373
result = newBundleUnpackResult (lookup )
373
374
@@ -377,7 +378,7 @@ func (c *ConfigMapUnpacker) UnpackBundle(lookup *operatorsv1alpha1.BundleLookup,
377
378
return result , nil
378
379
}
379
380
380
- // if pending condition is not true then bundle has already been unpacked(unknown) or failed(false)
381
+ // if pending condition is not true then bundle has already been unpacked(unknown)
381
382
pendingCond := result .GetCondition (operatorsv1alpha1 .BundleLookupPending )
382
383
if pendingCond .Status != corev1 .ConditionTrue {
383
384
return result , nil
@@ -430,33 +431,25 @@ func (c *ConfigMapUnpacker) UnpackBundle(lookup *operatorsv1alpha1.BundleLookup,
430
431
secrets = append (secrets , corev1.LocalObjectReference {Name : secretName })
431
432
}
432
433
var job * batchv1.Job
433
- job , err = c .ensureJob (cmRef , result .Path , secrets , annotationUnpackTimeout )
434
+ job , err = c .ensureJob (cmRef , result .Path , secrets , timeout )
434
435
if err != nil {
435
436
return
436
437
}
437
438
438
439
// Check if bundle unpack job has failed due a timeout
439
440
// Return a BundleJobError so we can mark the InstallPlan as Failed
440
- isFailed , jobCond := jobConditionTrue (job , batchv1 .JobFailed )
441
- if isFailed {
441
+ if jobCond , isFailed := getCondition (job , batchv1 .JobFailed ); isFailed {
442
442
// Add the BundleLookupFailed condition with the message and reason from the job failure
443
443
failedCond .Status = corev1 .ConditionTrue
444
444
failedCond .Reason = jobCond .Reason
445
445
failedCond .Message = jobCond .Message
446
446
failedCond .LastTransitionTime = & now
447
447
result .SetCondition (failedCond )
448
448
449
- // BundleLookupPending is false with reason being job failed
450
- pendingCond .Status = corev1 .ConditionFalse
451
- pendingCond .Reason = JobFailedReason
452
- pendingCond .Message = JobFailedMessage
453
- pendingCond .LastTransitionTime = & now
454
- result .SetCondition (pendingCond )
455
-
456
449
return
457
450
}
458
451
459
- if isComplete , _ := jobConditionTrue (job , batchv1 .JobComplete ); ! isComplete {
452
+ if _ , isComplete := getCondition (job , batchv1 .JobComplete ); ! isComplete {
460
453
// In the case of an image pull failure for a non-existent image the bundle unpack job
461
454
// can stay pending until the ActiveDeadlineSeconds timeout ~10m
462
455
// To indicate why it's pending we inspect the container statuses of the
@@ -508,12 +501,12 @@ func (c *ConfigMapUnpacker) UnpackBundle(lookup *operatorsv1alpha1.BundleLookup,
508
501
}
509
502
510
503
func (c * ConfigMapUnpacker ) pendingContainerStatusMessages (job * batchv1.Job ) (string , error ) {
511
- containerStatusMessages := ""
504
+ containerStatusMessages := [] string {}
512
505
// List pods for unpack job
513
506
podLabel := map [string ]string {"job-name" : job .GetName ()}
514
507
pods , listErr := c .podLister .Pods (job .GetNamespace ()).List (k8slabels .SelectorFromSet (podLabel ))
515
508
if listErr != nil {
516
- return containerStatusMessages , fmt .Errorf ("Failed to list pods for job(%s): %v" , job .GetName (), listErr )
509
+ return "" , fmt .Errorf ("Failed to list pods for job(%s): %v" , job .GetName (), listErr )
517
510
}
518
511
519
512
// Ideally there should be just 1 pod running but inspect all pods in the pending phase
@@ -531,13 +524,13 @@ func (c *ConfigMapUnpacker) pendingContainerStatusMessages(job *batchv1.Job) (st
531
524
}
532
525
533
526
// Aggregate the wait reasons for all pending containers
534
- containerStatusMessages = containerStatusMessages +
527
+ containerStatusMessages = append ( containerStatusMessages ,
535
528
fmt .Sprintf ("Unpack pod(%s/%s) container(%s) is pending. Reason: %s, Message: %s | " ,
536
- pod .Namespace , pod .Name , ic .Name , ic .State .Waiting .Reason , ic .State .Waiting .Message )
529
+ pod .Namespace , pod .Name , ic .Name , ic .State .Waiting .Reason , ic .State .Waiting .Message ))
537
530
}
538
531
}
539
532
540
- return containerStatusMessages , nil
533
+ return strings . Join ( containerStatusMessages , " | " ) , nil
541
534
}
542
535
543
536
func (c * ConfigMapUnpacker ) ensureConfigmap (csRef * corev1.ObjectReference , name string ) (cm * corev1.ConfigMap , err error ) {
@@ -554,8 +547,8 @@ func (c *ConfigMapUnpacker) ensureConfigmap(csRef *corev1.ObjectReference, name
554
547
return
555
548
}
556
549
557
- func (c * ConfigMapUnpacker ) ensureJob (cmRef * corev1.ObjectReference , bundlePath string , secrets []corev1.LocalObjectReference , annotationUnpackTimeout time.Duration ) (job * batchv1.Job , err error ) {
558
- fresh := c .job (cmRef , bundlePath , secrets , annotationUnpackTimeout )
550
+ func (c * ConfigMapUnpacker ) ensureJob (cmRef * corev1.ObjectReference , bundlePath string , secrets []corev1.LocalObjectReference , timeout time.Duration ) (job * batchv1.Job , err error ) {
551
+ fresh := c .job (cmRef , bundlePath , secrets , timeout )
559
552
job , err = c .jobLister .Jobs (fresh .GetNamespace ()).Get (fresh .GetName ())
560
553
if err != nil {
561
554
if apierrors .IsNotFound (err ) {
@@ -686,17 +679,19 @@ func ownerRef(ref *corev1.ObjectReference) metav1.OwnerReference {
686
679
}
687
680
}
688
681
689
- // jobConditionTrue returns true if the given job has the given condition with the given condition type true, and returns false otherwise.
682
+ // getCondition returns true if the given job has the given condition with the given condition type true, and returns false otherwise.
690
683
// Also returns the condition if true
691
- func jobConditionTrue (job * batchv1.Job , conditionType batchv1.JobConditionType ) (bool , * batchv1.JobCondition ) {
684
+ func getCondition (job * batchv1.Job , conditionType batchv1.JobConditionType ) (condition * batchv1.JobCondition , isTrue bool ) {
692
685
if job == nil {
693
- return false , nil
686
+ return
694
687
}
695
688
696
689
for _ , cond := range job .Status .Conditions {
697
690
if cond .Type == conditionType && cond .Status == corev1 .ConditionTrue {
698
- return true , & cond
691
+ condition = & cond
692
+ isTrue = true
693
+ return
699
694
}
700
695
}
701
- return false , nil
696
+ return
702
697
}
0 commit comments