@@ -23,108 +23,74 @@ import (
2323 "net/http"
2424 "sort"
2525
26- corev1 "k8s.io/api/core/v1"
2726 apierrors "k8s.io/apimachinery/pkg/api/errors"
2827 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2928
30- "knative.dev/pkg/controller"
31- "knative.dev/pkg/reconciler"
32-
3329 "github.com/aws/aws-sdk-go/aws"
3430 "github.com/aws/aws-sdk-go/aws/awserr"
3531 "github.com/aws/aws-sdk-go/aws/credentials"
3632 "github.com/aws/aws-sdk-go/service/s3"
3733 "github.com/aws/aws-sdk-go/service/s3/s3iface"
3834 "github.com/aws/aws-sdk-go/service/sqs"
3935
40- commonv1alpha1 "github.com/triggermesh/triggermesh/pkg/apis/common/v1alpha1"
41- "github.com/triggermesh/triggermesh/pkg/apis/sources/v1alpha1"
42- "github.com/triggermesh/triggermesh/pkg/reconciler/event"
36+ "github.com/triggermesh/scoby-hook-triggermesh/pkg/apis/sources/v1alpha1"
4337)
4438
4539// EnsureNotificationsEnabled ensures that event notifications are enabled in
4640// the S3 bucket.
47- func EnsureNotificationsEnabled (ctx context.Context , cli s3iface.S3API , queueARN string ) error {
48- src := commonv1alpha1 .ReconcilableFromContext (ctx )
49- typedSrc := src .(* v1alpha1.AWSS3Source )
50-
51- status := & typedSrc .Status
52-
53- bucketARN := typedSrc .Spec .ARN
41+ func EnsureNotificationsEnabled (ctx context.Context , src * v1alpha1.AWSS3Source , cli s3iface.S3API , queueARN string ) error {
42+ bucketARN := src .Spec .ARN
5443
5544 notifCfg , err := getNotificationsConfig (ctx , cli , bucketARN .Resource )
5645 switch {
5746 case isNotFound (err ):
58- status .MarkNotSubscribed (v1alpha1 .AWSS3ReasonNoBucket , "Bucket does not exist" )
59- return controller .NewPermanentError (reconciler .NewEvent (corev1 .EventTypeWarning , ReasonFailedSubscribe ,
60- "The bucket does not exist: %s" , toErrMsg (err )))
47+ return fmt .Errorf ("The bucket does not exist: %v" , toErrMsg (err ))
6148 case isAWSError (err ):
6249 // All documented API errors require some user intervention and
6350 // are not to be retried.
6451 // https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html
65- status .MarkNotSubscribed (v1alpha1 .AWSS3ReasonAPIError , "Request to S3 API got rejected" )
66- return controller .NewPermanentError (reconciler .NewEvent (corev1 .EventTypeWarning , ReasonFailedSubscribe ,
67- "Failed to synchronize bucket configuration: %s" , toErrMsg (err )))
52+ return fmt .Errorf ("Failed to synchronize bucket configuration: %v" , toErrMsg (err ))
6853 case err != nil :
69- status .MarkNotSubscribed (v1alpha1 .AWSS3ReasonAPIError , "Cannot obtain current bucket configuration" )
70- // wrap any other error to fail the reconciliation
71- return fmt .Errorf ("%w" , reconciler .NewEvent (corev1 .EventTypeWarning , ReasonFailedSubscribe ,
72- "Error reading current event notifications configuration: %s" , toErrMsg (err )))
54+ return fmt .Errorf ("Cannot obtain current bucket configuration: %v" , toErrMsg (err ))
7355 }
7456
75- desiredQueueCfg := makeQueueConfiguration (typedSrc , queueARN )
57+ desiredQueueCfg := makeQueueConfiguration (src , queueARN )
7658
7759 notifCfg , hasUpdates := setQueueConfiguration (notifCfg , desiredQueueCfg )
7860
7961 if hasUpdates {
8062 if err := configureNotifications (ctx , cli , bucketARN .Resource , notifCfg ); err != nil {
81- status .MarkNotSubscribed (v1alpha1 .AWSS3ReasonAPIError , "Cannot configure event notifications" )
82- return fmt .Errorf ("%w" , reconciler .NewEvent (corev1 .EventTypeWarning , ReasonFailedSubscribe ,
83- "Error configuring event notifications: %s" , toErrMsg (err )))
63+ return fmt .Errorf ("Cannot configure event notifications: %v" , toErrMsg (err ))
8464 }
8565 }
8666
87- if ! status .GetCondition (v1alpha1 .AWSS3ConditionSubscribed ).IsTrue () {
88- event .Normal (ctx , ReasonSubscribed , "Configured event notifications for S3 bucket %q" , bucketARN )
89- }
90- status .MarkSubscribed ()
91-
9267 return nil
9368}
9469
9570// EnsureNotificationsDisabled ensures that event notifications are disabled in
9671// the S3 bucket.
97- func EnsureNotificationsDisabled (ctx context.Context , cli s3iface.S3API ) error {
98- src := commonv1alpha1 .ReconcilableFromContext (ctx )
99- typedSrc := src .(* v1alpha1.AWSS3Source )
100-
101- bucketARN := typedSrc .Spec .ARN
72+ func EnsureNotificationsDisabled (ctx context.Context , src * v1alpha1.AWSS3Source , cli s3iface.S3API ) error {
73+ bucketARN := src .Spec .ARN
10274
10375 notifCfg , err := getNotificationsConfig (ctx , cli , bucketARN .Resource )
10476 switch {
10577 case isNotFound (err ):
106- return reconciler .NewEvent (corev1 .EventTypeNormal , ReasonUnsubscribed ,
107- "Bucket not found, skipping finalization" )
78+ return fmt .Errorf ("Bucket not found: %v" , toErrMsg (err ))
10879 case isDenied (err ):
10980 // it is unlikely that we recover from auth errors in the
11081 // finalizer, so we simply record a warning event and return
111- event .Warn (ctx , ReasonFailedUnsubscribe ,
112- "Authorization error getting bucket configuration. Ignoring: %s" , toErrMsg (err ))
113- return nil
82+ return fmt .Errorf ("Authorization error getting bucket configuration: %v" , toErrMsg (err ))
11483 case err != nil :
115- return reconciler .NewEvent (corev1 .EventTypeWarning , ReasonFailedUnsubscribe ,
116- "Error reading current event notifications configuration: %s" , toErrMsg (err ))
84+ return fmt .Errorf ("Error reading current event notifications configuration: %v" , toErrMsg (err ))
11785 }
11886
11987 notifCfg = removeQueueConfiguration (notifCfg , sourceID (src ))
12088
12189 if err := configureNotifications (ctx , cli , bucketARN .Resource , notifCfg ); err != nil {
122- return fmt .Errorf ("%w" , reconciler .NewEvent (corev1 .EventTypeWarning , ReasonFailedUnsubscribe ,
123- "Error configuring event notifications: %s" , toErrMsg (err )))
90+ return fmt .Errorf ("Error configuring event notifications: %v" , toErrMsg (err ))
12491 }
12592
126- return reconciler .NewEvent (corev1 .EventTypeNormal , ReasonUnsubscribed ,
127- "Disabled event notifications for S3 bucket %q" , bucketARN )
93+ return nil
12894}
12995
13096// getNotificationsConfig reads the current event notifications configuration
0 commit comments