@@ -24,6 +24,7 @@ import (
24
24
"sigs.k8s.io/controller-runtime/pkg/reconcile"
25
25
)
26
26
27
+ //nolint:revive
27
28
type ServiceAccountReconciler struct {
28
29
proxyURL string
29
30
proxyCA string
@@ -86,6 +87,7 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, request ctrl.R
86
87
87
88
return reconcile.Result {}, nil
88
89
}
90
+
89
91
r .Log .Error (err , "Error reading the object" )
90
92
91
93
return ctrl.Result {}, err
@@ -105,16 +107,15 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, request ctrl.R
105
107
if err != nil {
106
108
return reconcile.Result {}, errors .Wrap (err , "error getting token of the service account" )
107
109
}
110
+
108
111
if tokenSecret .Data == nil {
109
112
r .Log .Info ("ServiceAccount token data is missing. Requeueing." )
113
+
110
114
return reconcile.Result {Requeue : true }, nil
111
115
}
112
116
113
117
// Build the kubeConfig for the ServiceAccount Tenant Owner.
114
- config , err := r .buildKubeconfig (r .proxyURL , string (tokenSecret .Data [corev1 .ServiceAccountTokenKey ]))
115
- if err != nil {
116
- return reconcile.Result {}, errors .Wrap (err , "error building the tenant owner config" )
117
- }
118
+ config := r .buildKubeconfig (r .proxyURL , string (tokenSecret .Data [corev1 .ServiceAccountTokenKey ]))
118
119
119
120
configRaw , err := clientcmd .Write (* config )
120
121
if err != nil {
@@ -145,10 +146,12 @@ func (r *ServiceAccountReconciler) Reconcile(ctx context.Context, request ctrl.R
145
146
if sa .GetAnnotations ()[ServiceAccountGlobalAnnotationKey ] == ServiceAccountGlobalAnnotationValue {
146
147
// Get the Tenant owned by the ServiceAccount.
147
148
ownerName := fmt .Sprintf ("system:serviceaccount:%s:%s" , sa .GetNamespace (), sa .GetName ())
149
+
148
150
tenantList , err := r .listTenantsOwned (ctx , string (capsulev1beta2 .ServiceAccountOwner ), ownerName )
149
151
if err != nil {
150
152
return reconcile.Result {}, errors .Wrap (err , "error listing Tenants for owner" )
151
153
}
154
+
152
155
if tenantList .Items == nil {
153
156
return reconcile.Result {}, errors .New ("Tenant list for owner is empty" )
154
157
}
@@ -179,6 +182,7 @@ func (r *ServiceAccountReconciler) forOption(ctx context.Context) builder.ForOpt
179
182
predicate .NewPredicateFuncs (func (object client.Object ) bool {
180
183
ownerName := fmt .Sprintf ("system:serviceaccount:%s:%s" , object .GetNamespace (), object .GetName ())
181
184
tntList , err := r .listTenantsOwned (ctx , string (capsulev1beta2 .ServiceAccountOwner ), ownerName )
185
+
182
186
return err == nil && tntList .Items != nil && len (tntList .Items ) != 0
183
187
}),
184
188
),
@@ -199,7 +203,7 @@ func (r *ServiceAccountReconciler) listTenantsOwned(ctx context.Context, ownerKi
199
203
200
204
// buildKubeconfig returns a client-go/clientcmd/api.Config with a token and server URL specified as arguments.
201
205
// The server set is be the proxy configured at ServiceAccountReconciler-level.
202
- func (r * ServiceAccountReconciler ) buildKubeconfig (server , token string ) ( * clientcmdapi.Config , error ) {
206
+ func (r * ServiceAccountReconciler ) buildKubeconfig (server , token string ) * clientcmdapi.Config {
203
207
// Build the client API Config.
204
208
config := clientcmdapi .NewConfig ()
205
209
config .APIVersion = clientcmdlatest .Version
@@ -231,5 +235,5 @@ func (r *ServiceAccountReconciler) buildKubeconfig(server, token string) (*clien
231
235
config .Contexts = contexts
232
236
config .CurrentContext = KubeconfigContextName
233
237
234
- return config , nil
238
+ return config
235
239
}
0 commit comments