@@ -22,6 +22,9 @@ import (
22
22
"strings"
23
23
"time"
24
24
25
+ "github.com/pkg/errors"
26
+ "google.golang.org/grpc/codes"
27
+ "google.golang.org/grpc/status"
25
28
v1 "k8s.io/api/core/v1"
26
29
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
27
30
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -31,18 +34,14 @@ import (
31
34
kubecorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
32
35
"k8s.io/client-go/tools/record"
33
36
"k8s.io/klog/v2"
34
-
35
37
cosiapi "sigs.k8s.io/container-object-storage-interface-api/apis"
36
38
"sigs.k8s.io/container-object-storage-interface-api/apis/objectstorage/v1alpha1"
37
39
buckets "sigs.k8s.io/container-object-storage-interface-api/client/clientset/versioned"
38
40
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"
39
42
"sigs.k8s.io/container-object-storage-interface-provisioner-sidecar/pkg/consts"
40
43
cosi "sigs.k8s.io/container-object-storage-interface-spec"
41
44
"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"
46
45
)
47
46
48
47
// BucketAccessListener manages Bucket objects
@@ -67,16 +66,9 @@ func NewBucketAccessListener(driverName string, client cosi.ProvisionerClient) (
67
66
68
67
// Add attempts to provision credentials to access a given bucket. This function must be idempotent
69
68
//
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
- //
76
69
// 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]
80
72
func (bal * BucketAccessListener ) Add (ctx context.Context , inputBucketAccess * v1alpha1.BucketAccess ) error {
81
73
bucketAccess := inputBucketAccess .DeepCopy ()
82
74
@@ -104,7 +96,7 @@ func (bal *BucketAccessListener) Add(ctx context.Context, inputBucketAccess *v1a
104
96
105
97
bucketAccessClass , err := bal .bucketAccessClasses ().Get (ctx , bucketAccessClassName , metav1.GetOptions {})
106
98
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" )
108
100
return err
109
101
} else if err != nil {
110
102
klog .ErrorS (err , "Failed to fetch bucketAccessClass" , "bucketAccessClass" , bucketAccessClassName )
@@ -144,7 +136,7 @@ func (bal *BucketAccessListener) Add(ctx context.Context, inputBucketAccess *v1a
144
136
}
145
137
146
138
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" )
148
140
return errors .New ("Must define ServiceAccountName when AuthenticationType is IAM" )
149
141
}
150
142
@@ -162,10 +154,8 @@ func (bal *BucketAccessListener) Add(ctx context.Context, inputBucketAccess *v1a
162
154
return errors .Wrap (err , "Failed to fetch bucket" )
163
155
}
164
156
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
- }
168
157
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" )
169
159
return errors .New ("BucketAccess can't be granted to bucket not in Ready state and without a bucketID" )
170
160
}
171
161
@@ -182,7 +172,7 @@ func (bal *BucketAccessListener) Add(ctx context.Context, inputBucketAccess *v1a
182
172
rsp , err := bal .provisionerClient .DriverGrantBucketAccess (ctx , req )
183
173
if err != nil {
184
174
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" )
186
176
return errors .Wrap (err , "failed to grant access" )
187
177
}
188
178
@@ -306,9 +296,8 @@ func (bal *BucketAccessListener) Add(ctx context.Context, inputBucketAccess *v1a
306
296
307
297
// Update attempts to reconcile changes to a given bucketAccess. This function must be idempotent
308
298
// 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]
312
301
func (bal * BucketAccessListener ) Update (ctx context.Context , old , new * v1alpha1.BucketAccess ) error {
313
302
klog .V (3 ).InfoS ("Update BucketAccess" ,
314
303
"name" , old .ObjectMeta .Name )
@@ -328,9 +317,8 @@ func (bal *BucketAccessListener) Update(ctx context.Context, old, new *v1alpha1.
328
317
329
318
// Delete attemps to delete a bucketAccess. This function must be idempotent
330
319
// 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]
334
322
func (bal * BucketAccessListener ) Delete (ctx context.Context , bucketAccess * v1alpha1.BucketAccess ) error {
335
323
klog .V (3 ).InfoS ("Delete BucketAccess" ,
336
324
"name" , bucketAccess .ObjectMeta .Name ,
@@ -363,7 +351,7 @@ func (bal *BucketAccessListener) deleteBucketAccessOp(ctx context.Context, bucke
363
351
364
352
// First we revoke the bucketAccess from the driver
365
353
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" )
367
355
return errors .Wrap (err , "failed to revoke access" )
368
356
}
369
357
0 commit comments