8
8
9
9
"github.com/go-logr/logr"
10
10
"sigs.k8s.io/cluster-api/util/patch"
11
+ "sigs.k8s.io/controller-runtime/pkg/client"
12
+ "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
11
13
12
14
infrav1alpha2 "github.com/linode/cluster-api-provider-linode/api/v1alpha2"
13
15
"github.com/linode/cluster-api-provider-linode/clients"
@@ -77,6 +79,16 @@ func NewObjectStorageBucketScope(ctx context.Context, linodeClientConfig ClientC
77
79
}, nil
78
80
}
79
81
82
+ // AddFinalizer adds a finalizer if not present and immediately patches the
83
+ // object to avoid any race conditions.
84
+ func (s * ObjectStorageBucketScope ) AddFinalizer (ctx context.Context ) error {
85
+ if controllerutil .AddFinalizer (s .Bucket , infrav1alpha2 .BucketFinalizer ) {
86
+ return s .Close (ctx )
87
+ }
88
+
89
+ return nil
90
+ }
91
+
80
92
// PatchObject persists the object storage bucket configuration and status.
81
93
func (s * ObjectStorageBucketScope ) PatchObject (ctx context.Context ) error {
82
94
return s .PatchHelper .Patch (ctx , s .Bucket )
@@ -86,3 +98,56 @@ func (s *ObjectStorageBucketScope) PatchObject(ctx context.Context) error {
86
98
func (s * ObjectStorageBucketScope ) Close (ctx context.Context ) error {
87
99
return s .PatchObject (ctx )
88
100
}
101
+
102
+ // AddAccessKeyRefFinalizer adds a finalizer to the linodeobjectstoragekey referenced in spec.AccessKeyRef.
103
+ func (s * ObjectStorageBucketScope ) AddAccessKeyRefFinalizer (ctx context.Context , finalizer string ) error {
104
+ obj , err := s .getAccessKey (ctx )
105
+ if err != nil {
106
+ return err
107
+ }
108
+
109
+ controllerutil .AddFinalizer (obj , finalizer )
110
+ if err := s .Client .Update (ctx , obj ); err != nil {
111
+ return fmt .Errorf ("add linodeobjectstoragekey finalizer %s/%s: %w" , s .Bucket .Spec .AccessKeyRef .Namespace , s .Bucket .Spec .AccessKeyRef .Name , err )
112
+ }
113
+
114
+ return nil
115
+ }
116
+
117
+ // RemoveAccessKeyRefFinalizer removes a finalizer from the linodeobjectstoragekey referenced in spec.AccessKeyRef.
118
+ func (s * ObjectStorageBucketScope ) RemoveAccessKeyRefFinalizer (ctx context.Context , finalizer string ) error {
119
+ obj , err := s .getAccessKey (ctx )
120
+ if err != nil {
121
+ return err
122
+ }
123
+
124
+ controllerutil .RemoveFinalizer (obj , finalizer )
125
+ if err := s .Client .Update (ctx , obj ); err != nil {
126
+ return fmt .Errorf ("remove linodeobjectstoragekey finalizer %s/%s: %w" , s .Bucket .Spec .AccessKeyRef .Namespace , s .Bucket .Spec .AccessKeyRef .Name , err )
127
+ }
128
+
129
+ return nil
130
+ }
131
+
132
+ func (s * ObjectStorageBucketScope ) getAccessKey (ctx context.Context ) (* infrav1alpha2.LinodeObjectStorageKey , error ) {
133
+ if s .Bucket .Spec .AccessKeyRef == nil {
134
+ return nil , fmt .Errorf ("accessKeyRef is nil for bucket %s" , s .Bucket .Name )
135
+ }
136
+
137
+ objKeyNamespace := s .Bucket .Spec .AccessKeyRef .Namespace
138
+ if s .Bucket .Spec .AccessKeyRef .Namespace == "" {
139
+ objKeyNamespace = s .Bucket .Namespace
140
+ }
141
+
142
+ objKey := client.ObjectKey {
143
+ Name : s .Bucket .Spec .AccessKeyRef .Name ,
144
+ Namespace : objKeyNamespace ,
145
+ }
146
+
147
+ objStorageKey := & infrav1alpha2.LinodeObjectStorageKey {}
148
+ if err := s .Client .Get (ctx , objKey , objStorageKey ); err != nil {
149
+ return nil , fmt .Errorf ("get linodeobjectstoragekey %s: %w" , s .Bucket .Spec .AccessKeyRef .Name , err )
150
+ }
151
+
152
+ return objStorageKey , nil
153
+ }
0 commit comments