@@ -22,6 +22,9 @@ import (
2222 "strings"
2323 "time"
2424
25+ "github.com/pkg/errors"
26+ "google.golang.org/grpc/codes"
27+ "google.golang.org/grpc/status"
2528 v1 "k8s.io/api/core/v1"
2629 kubeerrors "k8s.io/apimachinery/pkg/api/errors"
2730 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -31,18 +34,14 @@ import (
3134 kubecorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
3235 "k8s.io/client-go/tools/record"
3336 "k8s.io/klog/v2"
34-
3537 cosiapi "sigs.k8s.io/container-object-storage-interface-api/apis"
3638 "sigs.k8s.io/container-object-storage-interface-api/apis/objectstorage/v1alpha1"
3739 buckets "sigs.k8s.io/container-object-storage-interface-api/client/clientset/versioned"
3840 bucketapi "sigs.k8s.io/container-object-storage-interface-api/client/clientset/versioned/typed/objectstorage/v1alpha1"
41+ "sigs.k8s.io/container-object-storage-interface-api/controller/events"
3942 "sigs.k8s.io/container-object-storage-interface-provisioner-sidecar/pkg/consts"
4043 cosi "sigs.k8s.io/container-object-storage-interface-spec"
4144 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
42-
43- "github.com/pkg/errors"
44- "google.golang.org/grpc/codes"
45- "google.golang.org/grpc/status"
4645)
4746
4847// BucketAccessListener manages Bucket objects
@@ -67,16 +66,9 @@ func NewBucketAccessListener(driverName string, client cosi.ProvisionerClient) (
6766
6867// Add attempts to provision credentials to access a given bucket. This function must be idempotent
6968//
70- // Recorded events
71- //
72- // BucketNotReady - BucketAccess can't be granted to bucket not in Ready state and without a bucketID
73- // MissingServiceAccountName - Must define ServiceAccountName when AuthenticationType is IAM
74- // InvalidBucketAccessClass - BucketAccessClass provided in the BucketAccess does not exist
75- //
7669// Return values
77- //
78- // nil - BucketAccess successfully granted
79- // non-nil err - Internal error [requeue'd with exponential backoff]
70+ // - nil - BucketAccess successfully granted
71+ // - non-nil err - Internal error [requeue'd with exponential backoff]
8072func (bal * BucketAccessListener ) Add (ctx context.Context , inputBucketAccess * v1alpha1.BucketAccess ) error {
8173 bucketAccess := inputBucketAccess .DeepCopy ()
8274
@@ -104,7 +96,7 @@ func (bal *BucketAccessListener) Add(ctx context.Context, inputBucketAccess *v1a
10496
10597 bucketAccessClass , err := bal .bucketAccessClasses ().Get (ctx , bucketAccessClassName , metav1.GetOptions {})
10698 if kubeerrors .IsNotFound (err ) {
107- bal .recordEvent (inputBucketAccess , v1 .EventTypeWarning , "InvalidBucketAccessClass" , "BucketAccessClass provided in the BucketAccess does not exist" )
99+ bal .recordEvent (inputBucketAccess , v1 .EventTypeWarning , events . GrantingAccessFailed , "BucketAccessClass provided in the BucketAccess does not exist" )
108100 return err
109101 } else if err != nil {
110102 klog .ErrorS (err , "Failed to fetch bucketAccessClass" , "bucketAccessClass" , bucketAccessClassName )
@@ -144,7 +136,7 @@ func (bal *BucketAccessListener) Add(ctx context.Context, inputBucketAccess *v1a
144136 }
145137
146138 if authType == cosi .AuthenticationType_IAM && bucketAccess .Spec .ServiceAccountName == "" {
147- bal .recordEvent (inputBucketAccess , v1 .EventTypeWarning , "MissingServiceAccountName" , "Must define ServiceAccountName when AuthenticationType is IAM" )
139+ bal .recordEvent (inputBucketAccess , v1 .EventTypeWarning , events . GrantingAccessFailed , "Must define ServiceAccountName when AuthenticationType is IAM" )
148140 return errors .New ("Must define ServiceAccountName when AuthenticationType is IAM" )
149141 }
150142
@@ -162,10 +154,8 @@ func (bal *BucketAccessListener) Add(ctx context.Context, inputBucketAccess *v1a
162154 return errors .Wrap (err , "Failed to fetch bucket" )
163155 }
164156
165- if bucket .Status .BucketID == "" {
166- bal .recordEvent (inputBucketAccess , v1 .EventTypeWarning , "BucketNotReady" , "BucketAccess can't be granted to bucket not in Ready state and without a bucketID" )
167- }
168157 if bucket .Status .BucketReady != true || bucket .Status .BucketID == "" {
158+ bal .recordEvent (inputBucketAccess , v1 .EventTypeWarning , events .WaitingForBucket , "BucketAccess can't be granted to bucket not in Ready state and without a bucketID" )
169159 return errors .New ("BucketAccess can't be granted to bucket not in Ready state and without a bucketID" )
170160 }
171161
@@ -182,7 +172,7 @@ func (bal *BucketAccessListener) Add(ctx context.Context, inputBucketAccess *v1a
182172 rsp , err := bal .provisionerClient .DriverGrantBucketAccess (ctx , req )
183173 if err != nil {
184174 if status .Code (err ) != codes .AlreadyExists {
185- bal .recordEvent (inputBucketAccess , v1 .EventTypeWarning , status . Code ( err ). String () , "Failed to grant access" )
175+ bal .recordEvent (inputBucketAccess , v1 .EventTypeWarning , events . GrantingAccessFailed , "Failed to grant access" )
186176 return errors .Wrap (err , "failed to grant access" )
187177 }
188178
@@ -306,9 +296,8 @@ func (bal *BucketAccessListener) Add(ctx context.Context, inputBucketAccess *v1a
306296
307297// Update attempts to reconcile changes to a given bucketAccess. This function must be idempotent
308298// Return values
309- //
310- // nil - BucketAccess successfully reconciled
311- // non-nil err - Internal error [requeue'd with exponential backoff]
299+ // - nil - BucketAccess successfully reconciled
300+ // - non-nil err - Internal error [requeue'd with exponential backoff]
312301func (bal * BucketAccessListener ) Update (ctx context.Context , old , new * v1alpha1.BucketAccess ) error {
313302 klog .V (3 ).InfoS ("Update BucketAccess" ,
314303 "name" , old .ObjectMeta .Name )
@@ -328,9 +317,8 @@ func (bal *BucketAccessListener) Update(ctx context.Context, old, new *v1alpha1.
328317
329318// Delete attemps to delete a bucketAccess. This function must be idempotent
330319// Return values
331- //
332- // nil - BucketAccess successfully deleted
333- // non-nil err - Internal error [requeue'd with exponential backoff]
320+ // - nil - BucketAccess successfully deleted
321+ // - non-nil err - Internal error [requeue'd with exponential backoff]
334322func (bal * BucketAccessListener ) Delete (ctx context.Context , bucketAccess * v1alpha1.BucketAccess ) error {
335323 klog .V (3 ).InfoS ("Delete BucketAccess" ,
336324 "name" , bucketAccess .ObjectMeta .Name ,
@@ -363,7 +351,7 @@ func (bal *BucketAccessListener) deleteBucketAccessOp(ctx context.Context, bucke
363351
364352 // First we revoke the bucketAccess from the driver
365353 if _ , err := bal .provisionerClient .DriverRevokeBucketAccess (ctx , req ); err != nil {
366- bal .recordEvent (bucketAccess , v1 .EventTypeWarning , status . Code ( err ). String () , "Failed to revoke bucket access" )
354+ bal .recordEvent (bucketAccess , v1 .EventTypeWarning , events . RevokingAccessFailed , "Failed to revoke bucket access" )
367355 return errors .Wrap (err , "failed to revoke access" )
368356 }
369357
0 commit comments