@@ -24,10 +24,10 @@ import (
2424	kube "k8s.io/client-go/kubernetes" 
2525	"k8s.io/klog/v2" 
2626
27- 	"sigs.k8s.io/container-object-storage-interface-api/apis/objectstorage.k8s.io /v1alpha1" 
28- 	buckets "sigs.k8s.io/container-object-storage-interface-api/clientset" 
29- 	bucketapi "sigs.k8s.io/container-object-storage-interface-api/clientset/typed/objectstorage.k8s.io /v1alpha1" 
30- 	"sigs.k8s.io/container-object-storage-interface-provisioner-sidecar/pkg/const " 
27+ 	"sigs.k8s.io/container-object-storage-interface-api/apis/objectstorage/v1alpha1" 
28+ 	buckets "sigs.k8s.io/container-object-storage-interface-api/client/ clientset/versioned " 
29+ 	bucketapi "sigs.k8s.io/container-object-storage-interface-api/client/ clientset/versioned/ typed/objectstorage/v1alpha1" 
30+ 	"sigs.k8s.io/container-object-storage-interface-provisioner-sidecar/pkg/consts " 
3131	cosi "sigs.k8s.io/container-object-storage-interface-spec" 
3232	"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" 
3333
@@ -63,6 +63,8 @@ func NewBucketListener(driverName string, client cosi.ProvisionerClient) *Bucket
6363func  (b  * BucketListener ) Add (ctx  context.Context , inputBucket  * v1alpha1.Bucket ) error  {
6464	bucket  :=  inputBucket .DeepCopy ()
6565
66+ 	var  err  error 
67+ 
6668	klog .V (3 ).InfoS ("Add Bucket" ,
6769		"name" , bucket .ObjectMeta .Name ,
6870		"bucketclass" , bucket .Spec .BucketClassName ,
@@ -85,9 +87,10 @@ func (b *BucketListener) Add(ctx context.Context, inputBucket *v1alpha1.Bucket)
8587		return  nil 
8688	}
8789
88- 	if  bucket .Spec .ExistingBucketID  !=  nil  {
90+ 	if  bucket .Spec .ExistingBucketID  !=  ""  {
8991		bucket .Status .BucketReady  =  true 
9092		bucket .Status .BucketID  =  bucket .Spec .ExistingBucketID 
93+ 
9194	} else  {
9295		req  :=  & cosi.DriverCreateBucketRequest {
9396			Parameters : bucket .Spec .Parameters ,
@@ -122,20 +125,26 @@ func (b *BucketListener) Add(ctx context.Context, inputBucket *v1alpha1.Bucket)
122125		// Now we update the BucketReady status of BucketClaim 
123126		if  bucket .Spec .BucketClaim  !=  nil  {
124127			ref  :=  bucket .Spec .BucketClaim 
125- 			bucketClaim , err  :=  b .BucketClaims (ref .Namespace ).Get (ctx , ref .Name , metav1.GetOptions {})
128+ 			bucketClaim , err  :=  b .bucketClaims (ref .Namespace ).Get (ctx , ref .Name , metav1.GetOptions {})
126129			if  err  !=  nil  {
127130				return  err 
128131			}
129132
130133			bucketClaim .Status .BucketReady  =  true 
131- 			if  _ , err  =  b .BucketClaims (bucketClaim .Namespace ).Update (ctx , bucketClaim , metav1.UpdateOptions {}); err  !=  nil  {
134+ 			if  _ , err  =  b .bucketClaims (bucketClaim .Namespace ).Update (ctx , bucketClaim , metav1.UpdateOptions {}); err  !=  nil  {
132135				return  err 
133136			}
134137		}
135138	}
136139
140+ 	controllerutil .AddFinalizer (bucket , consts .BucketFinalizer )
141+ 	if  _ , err  =  b .buckets ().Update (ctx , bucket , metav1.UpdateOptions {}); err  !=  nil  {
142+ 		klog .ErrorS (err , "Failed to update bucket finalizers" , "bucket" , bucket .ObjectMeta .Name )
143+ 		return  errors .Wrap (err , "Failed to update bucket finalizers" )
144+ 	}
145+ 
137146	// if this step fails, then controller will retry with backoff 
138- 	if  _ , err  =  b .Buckets ().UpdateStatus (ctx , bucket , metav1.UpdateOptions {}); err  !=  nil  {
147+ 	if  _ , err  =  b .buckets ().UpdateStatus (ctx , bucket , metav1.UpdateOptions {}); err  !=  nil  {
139148		klog .ErrorS (err , "Failed to update bucket status" ,
140149			"bucket" , bucket .ObjectMeta .Name )
141150		return  errors .Wrap (err , "Failed to update bucket status" )
@@ -152,20 +161,33 @@ func (b *BucketListener) Update(ctx context.Context, old, new *v1alpha1.Bucket)
152161	klog .V (3 ).InfoS ("Update Bucket" ,
153162		"name" , old .Name )
154163
155- 	if  ! new .GetDeletionTimestamp ().IsZero () {
156- 		if  len (new .ObjectMeta .Finalizers ) >  0  {
157- 			bucketClaimNs  :=  new .Spec .BucketClaim .Namespace 
158- 			bucketClaimName  :=  new .Spec .BucketClaim .Name 
159- 			bucketAccessList , err  :=  b .BucketAccesses (bucketClaimNs ).List (ctx , ListOptions {})
164+ 	bucket  :=  new .DeepCopy ()
165+ 
166+ 	var  err  error 
167+ 
168+ 	if  ! bucket .GetDeletionTimestamp ().IsZero () {
169+ 		if  controllerutil .ContainsFinalizer (bucket , consts .BABucketFinalizer ) {
170+ 			bucketClaimNs  :=  bucket .Spec .BucketClaim .Namespace 
171+ 			bucketClaimName  :=  bucket .Spec .BucketClaim .Name 
172+ 			bucketAccessList , err  :=  b .bucketAccesses (bucketClaimNs ).List (ctx , metav1.ListOptions {})
160173
161174			for  _ , bucketAccess  :=  range  bucketAccessList .Items  {
162175				if  strings .EqualFold (bucketAccess .Spec .BucketClaimName , bucketClaimName ) {
163- 					err  =  b .BucketAccesses (bucketClaimNs ).Delete (ctx , bucketAccess .Name , metav1.DeleteOptions {})
176+ 					err  =  b .bucketAccesses (bucketClaimNs ).Delete (ctx , bucketAccess .Name , metav1.DeleteOptions {})
164177					if  err  !=  nil  {
165178						return  err 
166179					}
167180				}
168181			}
182+ 
183+ 			controllerutil .RemoveFinalizer (bucket , consts .BABucketFinalizer )
184+ 		}
185+ 
186+ 		if  controllerutil .ContainsFinalizer (bucket , consts .BucketFinalizer ) {
187+ 			err  =  b .deleteBucketOp (ctx , bucket )
188+ 			if  err  !=  nil  {
189+ 				return  err 
190+ 			}
169191		}
170192	}
171193
@@ -177,13 +199,33 @@ func (b *BucketListener) Update(ctx context.Context, old, new *v1alpha1.Bucket)
177199//    nil - Bucket successfully deleted 
178200//    non-nil err - Internal error                                [requeue'd with exponential backoff] 
179201func  (b  * BucketListener ) Delete (ctx  context.Context , inputBucket  * v1alpha1.Bucket ) error  {
180- 	bucket  :=  inputBucket .DeepCopy ()
181- 
182202	klog .V (3 ).InfoS ("Delete Bucket" ,
183- 		"name" , bucket .ObjectMeta .Name ,
184- 		"bucketclass" , bucket .Spec .BucketClassName ,
203+ 		"name" , inputBucket .ObjectMeta .Name ,
204+ 		"bucketclass" , inputBucket .Spec .BucketClassName ,
185205	)
186206
207+ 	return  nil 
208+ 
209+ }
210+ 
211+ // InitializeKubeClient initializes the kubernetes client 
212+ func  (b  * BucketListener ) InitializeKubeClient (k  kube.Interface ) {
213+ 	b .kubeClient  =  k 
214+ 
215+ 	serverVersion , err  :=  k .Discovery ().ServerVersion ()
216+ 	if  err  !=  nil  {
217+ 		klog .ErrorS (err , "Cannot determine server version" )
218+ 	} else  {
219+ 		b .kubeVersion  =  utilversion .MustParseSemantic (serverVersion .GitVersion )
220+ 	}
221+ }
222+ 
223+ // InitializeBucketClient initializes the object storage bucket client 
224+ func  (b  * BucketListener ) InitializeBucketClient (bc  buckets.Interface ) {
225+ 	b .bucketClient  =  bc 
226+ }
227+ 
228+ func  (b  * BucketListener ) deleteBucketOp (ctx  context.Context , bucket  * v1alpha1.Bucket ) error  {
187229	if  ! strings .EqualFold (bucket .Spec .DriverName , b .driverName ) {
188230		klog .V (5 ).InfoS ("Skipping bucket for provisiner" ,
189231			"bucket" , bucket .ObjectMeta .Name ,
@@ -194,7 +236,7 @@ func (b *BucketListener) Delete(ctx context.Context, inputBucket *v1alpha1.Bucke
194236
195237	// We ask the driver to clean up the bucket from the storage provider 
196238	// only when the retain policy is set to Delete 
197- 	if  bucket .Spec .DeletionPolicy  ==  bucketapi .DeletionPolicyDelete  {
239+ 	if  bucket .Spec .DeletionPolicy  ==  v1alpha1 .DeletionPolicyDelete  {
198240		req  :=  & cosi.DriverDeleteBucketRequest {
199241			BucketId : bucket .Status .BucketID ,
200242		}
@@ -211,13 +253,13 @@ func (b *BucketListener) Delete(ctx context.Context, inputBucket *v1alpha1.Bucke
211253
212254	if  bucket .Spec .BucketClaim  !=  nil  {
213255		ref  :=  bucket .Spec .BucketClaim 
214- 		bucketClaim , err  :=  b .BucketClaims (ref .Namespace ).Get (ctx , ref .Name , metav1.GetOptions {})
256+ 		bucketClaim , err  :=  b .bucketClaims (ref .ObjectMeta . Namespace ).Get (ctx , ref . ObjectMeta .Name , metav1.GetOptions {})
215257		if  err  !=  nil  {
216258			return  err 
217259		}
218260
219- 		if  controllerutil .RemoveFinalizer (bucketClaim , const . BcFinalizer ) {
220- 			if  _ , err  :=  b .BucketClaims (bucketClaim .Namespace ).Update (ctx , bucketClaim , metav1.UpdateOptions {}); err  !=  nil  {
261+ 		if  controllerutil .RemoveFinalizer (bucketClaim , consts . BCFinalizer ) {
262+ 			if  _ , err  :=  b .bucketClaims (bucketClaim . ObjectMeta .Namespace ).Update (ctx , bucketClaim , metav1.UpdateOptions {}); err  !=  nil  {
221263				return  err 
222264			}
223265		}
@@ -226,14 +268,14 @@ func (b *BucketListener) Delete(ctx context.Context, inputBucket *v1alpha1.Bucke
226268	return  nil 
227269}
228270
229- func  (b  * BucketListener ) Buckets () bucketapi.BucketInterface  {
271+ func  (b  * BucketListener ) buckets () bucketapi.BucketInterface  {
230272	if  b .bucketClient  !=  nil  {
231273		return  b .bucketClient .ObjectstorageV1alpha1 ().Buckets ()
232274	}
233275	panic ("uninitialized listener" )
234276}
235277
236- func  (b  * BucketListener ) BucketClaims (namespace  string ) bucketapi.BucketClaimInterface  {
278+ func  (b  * BucketListener ) bucketClaims (namespace  string ) bucketapi.BucketClaimInterface  {
237279	if  b .bucketClient  !=  nil  {
238280		return  b .bucketClient .ObjectstorageV1alpha1 ().BucketClaims (namespace )
239281	}
@@ -242,26 +284,10 @@ func (b *BucketListener) BucketClaims(namespace string) bucketapi.BucketClaimInt
242284}
243285
244286
245- func  (b  * BucketListener ) BucketAccesses (namespace  string ) bucketapi.BucketAccessInterface  {
287+ func  (b  * BucketListener ) bucketAccesses (namespace  string ) bucketapi.BucketAccessInterface  {
246288	if  b .bucketClient  !=  nil  {
247289		return  b .bucketClient .ObjectstorageV1alpha1 ().BucketAccesses (namespace )
248290	}
249291	panic ("uninitialized listener" )
250292}
251293
252- // InitializeKubeClient initializes the kubernetes client 
253- func  (b  * BucketListener ) InitializeKubeClient (k  kube.Interface ) {
254- 	b .kubeClient  =  k 
255- 
256- 	serverVersion , err  :=  k .Discovery ().ServerVersion ()
257- 	if  err  !=  nil  {
258- 		klog .ErrorS (err , "Cannot determine server version" )
259- 	} else  {
260- 		b .kubeVersion  =  utilversion .MustParseSemantic (serverVersion .GitVersion )
261- 	}
262- }
263- 
264- // InitializeBucketClient initializes the object storage bucket client 
265- func  (b  * BucketListener ) InitializeBucketClient (bc  buckets.Interface ) {
266- 	b .bucketClient  =  bc 
267- }
0 commit comments