@@ -16,7 +16,6 @@ import (
16
16
corev1 "k8s.io/api/core/v1"
17
17
rbacv1 "k8s.io/api/rbac/v1"
18
18
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
19
- utilruntime "k8s.io/apimachinery/pkg/util/runtime"
20
19
"k8s.io/client-go/kubernetes"
21
20
"k8s.io/klog/v2"
22
21
@@ -86,16 +85,18 @@ func (a *CRDTemplateAgentAddon) GetDesiredAddOnTemplate(addon *addonapiv1alpha1.
86
85
return a .getDesiredAddOnTemplateInner (cma .Name , configReferences )
87
86
}
88
87
89
- func (a * CRDTemplateAgentAddon ) TemplateCSRConfigurationsFunc () func ( cluster * clusterv1. ManagedCluster ) []addonapiv1alpha1. RegistrationConfig {
88
+ func (a * CRDTemplateAgentAddon ) TemplateCSRConfigurationsFunc () agent. CSRConfigurationsFunc {
90
89
91
- return func (cluster * clusterv1.ManagedCluster ) []addonapiv1alpha1.RegistrationConfig {
92
- template , err := a .GetDesiredAddOnTemplate (nil , cluster .Name , a .addonName )
90
+ return func (cluster * clusterv1.ManagedCluster , addon * addonapiv1alpha1.ManagedClusterAddOn ,
91
+ ) ([]addonapiv1alpha1.RegistrationConfig , error ) {
92
+ template , err := a .GetDesiredAddOnTemplate (addon , cluster .Name , a .addonName )
93
93
if err != nil {
94
- a . logger . Info ("CSRConfigurations failed to get addon template" , "addonName" , a . addonName , "error" , err )
95
- return nil
94
+ return nil , fmt . Errorf ("CSRConfigurations failed to get addon template for addon %s/%s: %v" ,
95
+ cluster . Name , a . addonName , err )
96
96
}
97
97
if template == nil {
98
- return nil
98
+ return nil , fmt .Errorf ("CSRConfigurations failed to get addon template for addon %s/%s, template is nil" ,
99
+ cluster .Name , a .addonName )
99
100
}
100
101
101
102
contain := func (rcs []addonapiv1alpha1.RegistrationConfig , signerName string ) bool {
@@ -112,7 +113,11 @@ func (a *CRDTemplateAgentAddon) TemplateCSRConfigurationsFunc() func(cluster *cl
112
113
switch registration .Type {
113
114
case addonapiv1alpha1 .RegistrationTypeKubeClient :
114
115
if ! contain (registrationConfigs , certificatesv1 .KubeAPIServerClientSignerName ) {
115
- configs := agent .KubeClientSignerConfigurations (a .addonName , a .agentName )(cluster )
116
+ configs , err := agent .KubeClientSignerConfigurations (a .addonName , a .agentName )(cluster , addon )
117
+ if err != nil {
118
+ return nil , fmt .Errorf ("failed to get kube signer config for %s/%s: %v" ,
119
+ cluster .Name , a .addonName , err )
120
+ }
116
121
registrationConfigs = append (registrationConfigs , configs ... )
117
122
}
118
123
@@ -121,29 +126,34 @@ func (a *CRDTemplateAgentAddon) TemplateCSRConfigurationsFunc() func(cluster *cl
121
126
continue
122
127
}
123
128
if ! contain (registrationConfigs , registration .CustomSigner .SignerName ) {
124
- configs := CustomSignerConfigurations (
129
+ configs , err := CustomSignerConfigurations (
125
130
a .addonName , a .agentName , registration .CustomSigner )(cluster )
131
+ if err != nil {
132
+ return nil , fmt .Errorf ("failed to get custom signer config for %s/%s: %v" ,
133
+ cluster .Name , a .addonName , err )
134
+ }
126
135
registrationConfigs = append (registrationConfigs , configs ... )
127
136
}
128
137
129
138
default :
130
- utilruntime .HandleError (fmt .Errorf ("unsupported registration type %s" , registration .Type ))
139
+ a .logger .Info ("CSRConfigurations unsupported registration type" ,
140
+ "clusterName" , cluster .Name , "addonName" , a .addonName , "type" , registration .Type )
131
141
}
132
142
133
143
}
134
144
135
- return registrationConfigs
145
+ return registrationConfigs , nil
136
146
}
137
147
}
138
148
139
149
// CustomSignerConfigurations returns a func that can generate RegistrationConfig
140
150
// for CustomSigner type registration addon
141
151
func CustomSignerConfigurations (addonName , agentName string ,
142
152
customSignerConfig * addonapiv1alpha1.CustomSignerRegistrationConfig ,
143
- ) func (cluster * clusterv1.ManagedCluster ) []addonapiv1alpha1.RegistrationConfig {
144
- return func (cluster * clusterv1.ManagedCluster ) []addonapiv1alpha1.RegistrationConfig {
153
+ ) func (cluster * clusterv1.ManagedCluster ) ( []addonapiv1alpha1.RegistrationConfig , error ) {
154
+ return func (cluster * clusterv1.ManagedCluster ) ( []addonapiv1alpha1.RegistrationConfig , error ) {
145
155
if customSignerConfig == nil {
146
- utilruntime . HandleError ( fmt .Errorf ("custome signer is nil" ) )
156
+ return nil , fmt .Errorf ("custom signer config is nil" )
147
157
}
148
158
config := addonapiv1alpha1.RegistrationConfig {
149
159
SignerName : customSignerConfig .SignerName ,
@@ -157,7 +167,7 @@ func CustomSignerConfigurations(addonName, agentName string,
157
167
config .Subject = * customSignerConfig .Subject
158
168
}
159
169
160
- return []addonapiv1alpha1.RegistrationConfig {config }
170
+ return []addonapiv1alpha1.RegistrationConfig {config }, nil
161
171
}
162
172
}
163
173
@@ -168,10 +178,13 @@ func (a *CRDTemplateAgentAddon) TemplateCSRApproveCheckFunc() agent.CSRApproveFu
168
178
169
179
template , err := a .GetDesiredAddOnTemplate (addon , cluster .Name , a .addonName )
170
180
if err != nil {
171
- a .logger .Info ("CSRApproveCheck failed to get addon template" , "addonName" , a .addonName , "error" , err )
181
+ a .logger .Info ("CSRApproveCheck failed to get addon template" ,
182
+ "clusterName" , cluster .Name , "addonName" , a .addonName , "error" , err )
172
183
return false
173
184
}
174
185
if template == nil {
186
+ a .logger .Info ("CSRApproveCheck failed to get addon template, template is nil" ,
187
+ "clusterName" , cluster .Name , "addonName" , a .addonName )
175
188
return false
176
189
}
177
190
@@ -192,7 +205,8 @@ func (a *CRDTemplateAgentAddon) TemplateCSRApproveCheckFunc() agent.CSRApproveFu
192
205
}
193
206
194
207
default :
195
- utilruntime .HandleError (fmt .Errorf ("unsupported registration type %s" , registration .Type ))
208
+ a .logger .Info ("CSRApproveCheck unsupported registration type" ,
209
+ "clusterName" , cluster .Name , "addonName" , a .addonName , "type" , registration .Type )
196
210
}
197
211
198
212
}
@@ -232,21 +246,16 @@ func CustomerSignerCSRApprover(logger klog.Logger, agentName string) agent.CSRAp
232
246
233
247
func (a * CRDTemplateAgentAddon ) TemplateCSRSignFunc () agent.CSRSignerFunc {
234
248
235
- return func (csr * certificatesv1.CertificateSigningRequest ) []byte {
236
- // TODO: consider to change the agent.CSRSignerFun to accept parameter addon
237
- getClusterName := func (userName string ) string {
238
- return csr .Labels [clusterv1 .ClusterNameLabelKey ]
239
- }
240
-
241
- clusterName := getClusterName (csr .Spec .Username )
242
- template , err := a .GetDesiredAddOnTemplate (nil , clusterName , a .addonName )
249
+ return func (cluster * clusterv1.ManagedCluster , addon * addonapiv1alpha1.ManagedClusterAddOn ,
250
+ csr * certificatesv1.CertificateSigningRequest ) ([]byte , error ) {
251
+ template , err := a .GetDesiredAddOnTemplate (addon , cluster .Name , a .addonName )
243
252
if err != nil {
244
- utilruntime .HandleError (fmt .Errorf ("failed to get template for addon %s in cluster %s: %v" ,
245
- a .addonName , clusterName , err ))
246
- return nil
253
+ return nil , fmt .Errorf ("CSRSign failed to get template for addon %s/%s: %v" ,
254
+ cluster .Name , a .addonName , err )
247
255
}
248
256
if template == nil {
249
- return nil
257
+ return nil , fmt .Errorf ("CSRSign failed to get addon template for addon %s/%s, template is nil" ,
258
+ cluster .Name , a .addonName )
250
259
}
251
260
252
261
for _ , registration := range template .Spec .Registration {
@@ -259,31 +268,33 @@ func (a *CRDTemplateAgentAddon) TemplateCSRSignFunc() agent.CSRSignerFunc {
259
268
continue
260
269
}
261
270
if csr .Spec .SignerName == registration .CustomSigner .SignerName {
262
- return CustomSignerWithExpiry (a .hubKubeClient , registration .CustomSigner , 24 * time .Hour )(csr )
271
+ return CustomSignerWithExpiry (a .hubKubeClient , registration .CustomSigner , 24 * time .Hour )(cluster , addon , csr )
263
272
}
264
273
265
274
default :
266
- utilruntime .HandleError (fmt .Errorf ("unsupported registration type %s" , registration .Type ))
275
+ a .logger .Info ("CSRSign unsupported registration type" ,
276
+ "clusterName" , cluster .Name , "addonName" , a .addonName , "type" , registration .Type )
267
277
}
268
278
269
279
}
270
280
271
- return nil
281
+ return nil , nil
272
282
}
273
283
}
274
284
275
285
func CustomSignerWithExpiry (
276
286
kubeclient kubernetes.Interface ,
277
287
customSignerConfig * addonapiv1alpha1.CustomSignerRegistrationConfig ,
278
- duration time.Duration ) agent.CSRSignerFunc {
279
- return func (csr * certificatesv1.CertificateSigningRequest ) []byte {
288
+ duration time.Duration ,
289
+ ) agent.CSRSignerFunc {
290
+ return func (cluster * clusterv1.ManagedCluster , addon * addonapiv1alpha1.ManagedClusterAddOn ,
291
+ csr * certificatesv1.CertificateSigningRequest ) ([]byte , error ) {
280
292
if customSignerConfig == nil {
281
- utilruntime .HandleError (fmt .Errorf ("custome signer is nil" ))
282
- return nil
293
+ return nil , fmt .Errorf ("custom signer config is nil" )
283
294
}
284
295
285
296
if csr .Spec .SignerName != customSignerConfig .SignerName {
286
- return nil
297
+ return nil , nil
287
298
}
288
299
289
300
secretNamespace := AddonManagerNamespace ()
@@ -293,18 +304,16 @@ func CustomSignerWithExpiry(
293
304
caSecret , err := kubeclient .CoreV1 ().Secrets (secretNamespace ).Get (
294
305
context .TODO (), customSignerConfig .SigningCA .Name , metav1.GetOptions {})
295
306
if err != nil {
296
- utilruntime .HandleError (fmt .Errorf ("get custome signer ca %s/%s failed, %v" ,
297
- secretNamespace , customSignerConfig .SigningCA .Name , err ))
298
- return nil
307
+ return nil , fmt .Errorf ("get custom signer ca %s/%s failed: %w" ,
308
+ secretNamespace , customSignerConfig .SigningCA .Name , err )
299
309
}
300
310
301
311
caData , caKey , err := extractCAdata (caSecret .Data [corev1 .TLSCertKey ], caSecret .Data [corev1 .TLSPrivateKeyKey ])
302
312
if err != nil {
303
- utilruntime .HandleError (fmt .Errorf ("get ca %s/%s data failed, %v" ,
304
- secretNamespace , customSignerConfig .SigningCA .Name , err ))
305
- return nil
313
+ return nil , fmt .Errorf ("get ca %s/%s data failed: %w" ,
314
+ secretNamespace , customSignerConfig .SigningCA .Name , err )
306
315
}
307
- return utils .DefaultSignerWithExpiry (caKey , caData , duration )(csr )
316
+ return utils .DefaultSignerWithExpiry (caKey , caData , duration )(cluster , addon , csr )
308
317
}
309
318
}
310
319
@@ -348,10 +357,12 @@ func (a *CRDTemplateAgentAddon) TemplatePermissionConfigFunc() agent.PermissionC
348
357
return func (cluster * clusterv1.ManagedCluster , addon * addonapiv1alpha1.ManagedClusterAddOn ) error {
349
358
template , err := a .GetDesiredAddOnTemplate (addon , cluster .Name , a .addonName )
350
359
if err != nil {
351
- return err
360
+ return fmt .Errorf ("PermissionConfig failed to get addon template for addon %s/%s: %v" ,
361
+ cluster .Name , a .addonName , err )
352
362
}
353
363
if template == nil {
354
- return nil
364
+ return fmt .Errorf ("PermissionConfig failed to get addon template for addon %s/%s, template is nil" ,
365
+ cluster .Name , a .addonName )
355
366
}
356
367
357
368
for _ , registration := range template .Spec .Registration {
@@ -371,7 +382,8 @@ func (a *CRDTemplateAgentAddon) TemplatePermissionConfigFunc() agent.PermissionC
371
382
continue
372
383
373
384
default :
374
- utilruntime .HandleError (fmt .Errorf ("unsupported registration type %s" , registration .Type ))
385
+ a .logger .Info ("PermissionConfig unsupported registration type" ,
386
+ "clusterName" , cluster .Name , "addonName" , a .addonName , "type" , registration .Type )
375
387
}
376
388
377
389
}
0 commit comments