Skip to content

Commit f5a4ba9

Browse files
committed
Structured logging for unified auth controller
Signed-off-by: Joe Nathan Abellard <[email protected]>
1 parent b8f6874 commit f5a4ba9

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

pkg/controllers/unifiedauth/unified_auth_controller.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type Controller struct {
6767

6868
// Reconcile performs a full reconciliation for the object referred to by the Request.
6969
func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
70-
klog.V(4).Infof("Reconciling cluster %s", req.NamespacedName.String())
70+
klog.V(4).InfoS("Reconciling cluster", "cluster", req.Name)
7171

7272
cluster := &clusterv1alpha1.Cluster{}
7373
if err := c.Client.Get(ctx, req.NamespacedName, cluster); err != nil {
@@ -86,13 +86,13 @@ func (c *Controller) Reconcile(ctx context.Context, req controllerruntime.Reques
8686
}
8787

8888
if cluster.Spec.ImpersonatorSecretRef == nil {
89-
klog.Infof("Unified auth is disabled on cluster %s as it does not have an impersonator secret", cluster.Name)
89+
klog.InfoS("Unified auth is disabled: no impersonator secret", "cluster", cluster.Name)
9090
return controllerruntime.Result{}, nil
9191
}
9292

9393
err := c.syncImpersonationConfig(ctx, cluster)
9494
if err != nil {
95-
klog.Errorf("Failed to sync impersonation config for cluster %s. Error: %v.", cluster.Name, err)
95+
klog.ErrorS(err, "Failed to sync impersonation config", "cluster", cluster.Name)
9696
c.EventRecorder.Eventf(cluster, corev1.EventTypeWarning, events.EventReasonSyncImpersonationConfigFailed, err.Error())
9797
return controllerruntime.Result{}, err
9898
}
@@ -113,13 +113,13 @@ func (c *Controller) syncImpersonationConfig(ctx context.Context, cluster *clust
113113

114114
// step3: sync ClusterRole to cluster for impersonation
115115
if err := c.buildImpersonationClusterRole(ctx, cluster, rules); err != nil {
116-
klog.Errorf("Failed to sync impersonate ClusterRole to cluster(%s): %v", cluster.Name, err)
116+
klog.ErrorS(err, "Failed to sync impersonate ClusterRole to cluster", "cluster", cluster.Name)
117117
return err
118118
}
119119

120120
// step4: sync ClusterRoleBinding to cluster for impersonation
121121
if err := c.buildImpersonationClusterRoleBinding(ctx, cluster); err != nil {
122-
klog.Errorf("Failed to sync impersonate ClusterRoleBinding to cluster(%s): %v", cluster.Name, err)
122+
klog.ErrorS(err, "Failed to sync impersonate ClusterRoleBinding to cluster", "cluster", cluster.Name)
123123
return err
124124
}
125125

@@ -129,7 +129,7 @@ func (c *Controller) syncImpersonationConfig(ctx context.Context, cluster *clust
129129
func findRBACSubjectsWithCluster(ctx context.Context, c client.Client, cluster string) ([]rbacv1.Subject, error) {
130130
clusterRoleList := &rbacv1.ClusterRoleList{}
131131
if err := c.List(ctx, clusterRoleList); err != nil {
132-
klog.Errorf("Failed to list ClusterRoles, error: %v", err)
132+
klog.ErrorS(err, "Failed to list ClusterRoles", "cluster", cluster)
133133
return nil, err
134134
}
135135

@@ -158,7 +158,7 @@ func findRBACSubjectsWithCluster(ctx context.Context, c client.Client, cluster s
158158

159159
clusterRoleBindings := &rbacv1.ClusterRoleBindingList{}
160160
if err := c.List(ctx, clusterRoleBindings); err != nil {
161-
klog.Errorf("Failed to list ClusterRoleBindings, error: %v", err)
161+
klog.ErrorS(err, "Failed to list ClusterRoleBindings", "cluster", cluster)
162162
return nil, err
163163
}
164164

@@ -189,7 +189,7 @@ func (c *Controller) buildImpersonationClusterRole(ctx context.Context, cluster
189189

190190
clusterRoleObj, err := helper.ToUnstructured(impersonationClusterRole)
191191
if err != nil {
192-
klog.Errorf("Failed to transform ClusterRole %s. Error: %v", impersonationClusterRole.GetName(), err)
192+
klog.ErrorS(err, "Failed to transform ClusterRole", "name", impersonationClusterRole.GetName(), "cluster", cluster.Name)
193193
return err
194194
}
195195

@@ -224,7 +224,7 @@ func (c *Controller) buildImpersonationClusterRoleBinding(ctx context.Context, c
224224

225225
clusterRoleBindingObj, err := helper.ToUnstructured(impersonatorClusterRoleBinding)
226226
if err != nil {
227-
klog.Errorf("Failed to transform ClusterRoleBinding %s. Error: %v", impersonatorClusterRoleBinding.GetName(), err)
227+
klog.ErrorS(err, "Failed to transform ClusterRoleBinding", "name", impersonatorClusterRoleBinding.GetName(), "cluster", cluster.Name)
228228
return err
229229
}
230230

@@ -283,7 +283,7 @@ func (c *Controller) newClusterRoleBindingMapFunc() handler.MapFunc {
283283

284284
clusterRole := &rbacv1.ClusterRole{}
285285
if err := c.Client.Get(ctx, types.NamespacedName{Name: clusterRoleBinding.RoleRef.Name}, clusterRole); err != nil {
286-
klog.Errorf("Failed to get reference ClusterRole, error: %v", err)
286+
klog.ErrorS(err, "Failed to get reference ClusterRole", "name", clusterRoleBinding.RoleRef.Name)
287287
return nil
288288
}
289289
return c.generateRequestsFromClusterRole(ctx, clusterRole)
@@ -294,7 +294,7 @@ func (c *Controller) newClusterRoleBindingMapFunc() handler.MapFunc {
294294
func (c *Controller) generateRequestsFromClusterRole(ctx context.Context, clusterRole *rbacv1.ClusterRole) []reconcile.Request {
295295
clusterList := &clusterv1alpha1.ClusterList{}
296296
if err := c.Client.List(ctx, clusterList); err != nil {
297-
klog.Errorf("Failed to list existing clusters, error: %v", err)
297+
klog.ErrorS(err, "Failed to list existing clusters")
298298
return nil
299299
}
300300

0 commit comments

Comments
 (0)