@@ -47,26 +47,23 @@ const (
47
47
// BucketAccessListener manages Bucket objects
48
48
type BucketAccessListener struct {
49
49
provisionerClient cosi.ProvisionerClient
50
- provisionerName string
50
+ driverName string
51
51
52
52
kubeClient kube.Interface
53
53
bucketClient buckets.Interface
54
54
kubeVersion * utilversion.Version
55
-
56
- namespace string
57
55
}
58
56
59
57
// NewBucketAccessListener returns a resource handler for BucketAccess objects
60
- func NewBucketAccessListener (provisionerName string , client cosi.ProvisionerClient ) (* BucketAccessListener , error ) {
58
+ func NewBucketAccessListener (driverName string , client cosi.ProvisionerClient ) (* BucketAccessListener , error ) {
61
59
ns := os .Getenv ("POD_NAMESPACE" )
62
60
if ns == "" {
63
61
return nil , errors .New ("POD_NAMESPACE env var cannot be empty" )
64
62
}
65
63
66
64
return & BucketAccessListener {
67
- provisionerName : provisionerName ,
65
+ driverName : driverName ,
68
66
provisionerClient : client ,
69
- namespace : ns ,
70
67
}, nil
71
68
}
72
69
@@ -77,60 +74,96 @@ func NewBucketAccessListener(provisionerName string, client cosi.ProvisionerClie
77
74
func (bal * BucketAccessListener ) Add (ctx context.Context , inputBucketAccess * v1alpha1.BucketAccess ) error {
78
75
bucketAccess := inputBucketAccess .DeepCopy ()
79
76
80
- bucketName := bucketAccess .Spec .BucketName
77
+ bucketClaimName := bucketAccess .Spec .BucketClaimName
81
78
klog .V (3 ).InfoS ("Add BucketAccess" ,
82
79
"name" , bucketAccess .Name ,
83
- "bucket " , bucketName ,
80
+ "bucketClaim " , bucketClaimName ,
84
81
)
85
82
86
- if bucketAccess .Status .MintedSecret != nil {
87
- klog .V (5 ).InfoS ("AccessAlreadyGranted" ,
83
+ bucketAccessClassName := bucketAccess .Spec .BucketAccessClassName
84
+ klog .V (3 ).InfoS ("Add BucketAccess" ,
85
+ "name" , bucketAccess .Name ,
86
+ "BucketAccessClassName" , bucketAccessClassName ,
87
+ )
88
+
89
+ secretCredName := bucketAccess .Spec .CredentialsSecretName
90
+ if secretCredName == nil {
91
+ return errors .New ("CredentialsSecretName not defined in the BucketAccess" )
92
+ }
93
+
94
+ authType := cosi .AuthenticationType_UnknownAuthenticationType
95
+ if bucketAccess .Spec .AuthenticationType == v1alpha1 .AuthenticationTypeKey {
96
+ authType = cosi .AuthenticationType_Key
97
+ } else if bucketAccess .Spec .AuthenticationType == v1alpha1 .AuthenticationTypeIAM {
98
+ authType = cosi .AuthenticationType_IAM
99
+ }
100
+
101
+ if authType == cosi .AuthenticationType_IAM && bucketAccess .Spec .ServiceAccountName == "" {
102
+ return errors .New ("Must define ServiceAccountName when AuthenticationType is IAM" )
103
+ }
104
+
105
+ namespace := bucketAccess .Namespace
106
+ bucketClaim , err := bal .BucketClaims (namespace ).Get (ctx , bucketClaimName , metav1.GetOptions {})
107
+ if err != nil {
108
+ klog .ErrorS (err , "Failed to fetch bucketClaim" , "bucketClaim" , bucketClaimName )
109
+ return errors .Wrap (err , "Failed to fetch bucketClaim" )
110
+ }
111
+
112
+
113
+ if bucketClaim .Status .BucketName == "" || bucketClaim .Status .BucketReady != true {
114
+ err := errors .New ("BucketName cannot be empty or BucketNotReady" )
115
+ klog .ErrorS (err ,
116
+ "Invalid arguments" ,
117
+ "bucketClaim" , bucketClaim .Name ,
88
118
"bucketAccess" , bucketAccess .Name ,
89
- "bucket" , bucketName ,
90
119
)
91
- return nil
120
+ return errors . Wrap ( err , "Invalid arguments" )
92
121
}
93
122
94
- bucket , err := bal .Buckets ().Get (ctx , bucketName , metav1.GetOptions {})
123
+ bucketAccessClass , err := bal .BucketAccessClasses ().Get (ctx , bucketAccessClassName , metav1.GetOptions {})
95
124
if err != nil {
96
- klog .ErrorS (err , "Failed to fetch bucket " , "bucket " , bucketName )
97
- return errors .Wrap (err , "Failed to fetch bucket " )
125
+ klog .ErrorS (err , "Failed to fetch bucketAccessClass " , "bucketAccessClass " , bucketAccessClassName )
126
+ return errors .Wrap (err , "Failed to fetch BucketAccessClass " )
98
127
}
99
128
100
- if ! strings .EqualFold (bucket . Spec . Provisioner , bal .provisionerName ) {
129
+ if ! strings .EqualFold (bucketAccessClass . DriverName , bal .driverName ) {
101
130
klog .V (5 ).InfoS ("Skipping bucketaccess for provisiner" ,
102
131
"bucketAccess" , bucketAccess .Name ,
103
- "provisioner " , bucket . Spec . Provisioner ,
132
+ "driver " , bucketAccessClass . DriverName ,
104
133
)
105
134
return nil
106
135
}
107
136
108
- if bucketAccess .Status .AccessGranted {
137
+
138
+ if bucketAccess .Status .AccessGranted == true {
109
139
klog .V (5 ).InfoS ("AccessAlreadyGranted" ,
110
- "bucketaccess " , bucketAccess .Name ,
111
- "bucket " , bucket . Name ,
140
+ "bucketAccess " , bucketAccess .Name ,
141
+ "bucketClaim " , bucketClaimName ,
112
142
)
113
143
return nil
114
144
}
115
145
116
- if bucket .Status .BucketID == "" {
117
- err := errors .New ("BucketID cannot be empty" )
118
- klog .ErrorS (err ,
119
- "Invalid arguments" ,
120
- "bucket" , bucket .Name ,
121
- "bucketAccess" , bucketAccess .Name ,
122
- )
123
- return errors .Wrap (err , "Invalid arguments" )
146
+ bucket , err := bal .Buckets ().Get (ctx , bucketClaim .Status .BucketName , metav1.GetOptions {})
147
+ if err != nil {
148
+ klog .ErrorS (err , "Failed to fetch bucket" , "bucket" , bucketClaim .Status .BucketName )
149
+ return errors .Wrap (err , "Failed to fetch bucket" )
124
150
}
125
151
126
- req := & cosi.ProvisionerGrantBucketAccessRequest {
152
+ if bucket .Status .BucketStatus != true || bucket .Status .BucketID == "" {
153
+ return errors .New ("BucketAccess can't be granted to bucket not in Ready state and without a bucketID" )
154
+ }
155
+
156
+ accountName := "ba-" + bucketAccess .UID
157
+
158
+ req := & cosi.DriverGrantBucketAccessRequest {
127
159
BucketId : bucket .Status .BucketID ,
128
- AccountName : bucketAccess .Name ,
129
- AccessPolicy : bucketAccess .Spec .PolicyActionsConfigMapData ,
160
+ AccountName : accountName ,
161
+ AuthenticationType : authType ,
162
+ Parameters : bucketAccessClass .Parameters ,
130
163
}
131
164
132
165
// This needs to be idempotent
133
- rsp , err := bal .provisionerClient .ProvisionerGrantBucketAccess (ctx , req )
166
+ rsp , err := bal .provisionerClient .DriverGrantBucketAccess (ctx , req )
134
167
if err != nil {
135
168
if status .Code (err ) != codes .AlreadyExists {
136
169
klog .ErrorS (err ,
@@ -142,9 +175,8 @@ func (bal *BucketAccessListener) Add(ctx context.Context, inputBucketAccess *v1a
142
175
}
143
176
144
177
}
145
- ns := bal .namespace
146
- mintedSecretName := "ba-" + string (bucketAccess .UID )
147
- if _ , err := bal .Secrets (ns ).Get (ctx , mintedSecretName , metav1.GetOptions {}); err != nil {
178
+
179
+ if _ , err := bal .Secrets (namespace ).Get (ctx , secretCredName , metav1.GetOptions {}); err != nil {
148
180
if ! kubeerrors .IsNotFound (err ) {
149
181
klog .ErrorS (err ,
150
182
"Failed to create secrets" ,
@@ -156,10 +188,10 @@ func (bal *BucketAccessListener) Add(ctx context.Context, inputBucketAccess *v1a
156
188
// if secret doesn't exist, create it
157
189
credentials := rsp .Credentials
158
190
159
- if _ , err := bal .Secrets (ns ).Create (ctx , & corev1.Secret {
191
+ if _ , err := bal .Secrets (namespace ).Create (ctx , & corev1.Secret {
160
192
ObjectMeta : metav1.ObjectMeta {
161
- Name : mintedSecretName ,
162
- Namespace : ns ,
193
+ Name : secretCredName ,
194
+ Namespace : namespace ,
163
195
},
164
196
StringData : map [string ]string {
165
197
Credentials : credentials ,
@@ -179,7 +211,7 @@ func (bal *BucketAccessListener) Add(ctx context.Context, inputBucketAccess *v1a
179
211
bucketAccess .Status .AccountID = rsp .AccountId
180
212
bucketAccess .Status .MintedSecret = & corev1.SecretReference {
181
213
Namespace : bal .namespace ,
182
- Name : mintedSecretName ,
214
+ Name : secretCredName ,
183
215
}
184
216
bucketAccess .Status .AccessGranted = true
185
217
@@ -263,6 +295,20 @@ func (b *BucketAccessListener) Buckets() bucketapi.BucketInterface {
263
295
panic ("uninitialized listener" )
264
296
}
265
297
298
+ func (b * BucketAccessListener ) BucketClaims (namespace string ) bucketapi.BucketClaimInterface {
299
+ if b .bucketClient != nil {
300
+ return b .bucketClient .ObjectstorageV1alpha1 ().BucketClaims (namespace )
301
+ }
302
+ panic ("uninitialized listener" )
303
+ }
304
+
305
+ func (b * BucketAccessListener ) BucketAccessClasses () bucketapi.BucketClaimInterface {
306
+ if b .bucketClient != nil {
307
+ return b .bucketClient .ObjectstorageV1alpha1 ().BucketAccessClasses ()
308
+ }
309
+ panic ("uninitialized listener" )
310
+ }
311
+
266
312
// InitializeKubeClient initializes the kubernetes client
267
313
func (b * BucketAccessListener ) InitializeKubeClient (k kube.Interface ) {
268
314
b .kubeClient = k
0 commit comments