Skip to content

Commit fe06ebc

Browse files
CaesarTYRainbowMango
authored andcommitted
refactor: replace klog with controller-runtime logger in certificate controller
Signed-off-by: yteng35 <[email protected]>
1 parent efca312 commit fe06ebc

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

pkg/controllers/certificate/approver/agent_csr_approving.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,21 @@ type AgentCSRApprovingController struct {
5757
// The Controller will requeue the Request to be processed again if an error is non-nil or
5858
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
5959
func (a *AgentCSRApprovingController) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
60-
klog.V(4).Infof("Reconciling for CertificateSigningRequest %s", req.Name)
60+
klog.V(4).InfoS("Reconciling for CertificateSigningRequest", "csr", req.Name)
6161

6262
// 1. get latest CertificateSigningRequest
6363
var csr *certificatesv1.CertificateSigningRequest
6464
var err error
6565
if csr, err = a.Client.CertificatesV1().CertificateSigningRequests().Get(ctx, req.Name, metav1.GetOptions{}); err != nil {
6666
if apierrors.IsNotFound(err) {
67-
klog.Infof("no need to reconcile CertificateSigningRequest %s for it not found", req.Name)
67+
klog.InfoS("No need to reconcile CertificateSigningRequest because it was not found", "csr", req.Name)
6868
return controllerruntime.Result{}, nil
6969
}
7070
return controllerruntime.Result{}, err
7171
}
7272

7373
if csr.DeletionTimestamp != nil {
74-
klog.Infof("no need to reconcile CertificateSigningRequest %s for it has been deleted", csr.Name)
74+
klog.InfoS("No need to reconcile CertificateSigningRequest because it has been deleted", "csr", csr.Name)
7575
return controllerruntime.Result{}, nil
7676
}
7777

@@ -119,7 +119,8 @@ func (a *AgentCSRApprovingController) handleCertificateSigningRequest(ctx contex
119119
}
120120

121121
if len(tried) != 0 {
122-
klog.Warningf("recognized csr %q as %v but subject access review was not approved", csr.Name, tried)
122+
err := fmt.Errorf("subject access review was not approved")
123+
klog.ErrorS(err, "Recognized CSR but SAR was not approved", "csr", csr.Name, "subresources", tried)
123124
}
124125

125126
return nil

pkg/controllers/certificate/cert_rotation_controller.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ type CertRotationController struct {
8787
// The Controller will requeue the Request to be processed again if an error is non-nil or
8888
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
8989
func (c *CertRotationController) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
90-
klog.V(4).Infof("Rotating the certificate of karmada-agent for the member cluster: %s", req.NamespacedName.Name)
90+
klog.V(4).InfoS("Rotating the certificate of karmada-agent for the member cluster", "cluster", req.NamespacedName.String())
9191

9292
var err error
9393

@@ -108,18 +108,18 @@ func (c *CertRotationController) Reconcile(ctx context.Context, req controllerru
108108
// create a ClusterClient for the given member cluster
109109
c.ClusterClient, err = c.ClusterClientSetFunc(cluster.Name, c.Client, c.ClusterClientOption)
110110
if err != nil {
111-
klog.Errorf("Failed to create a ClusterClient for the given member cluster: %s, err is: %v", cluster.Name, err)
111+
klog.ErrorS(err, "Failed to create a ClusterClient for the given member cluster", "cluster", cluster.Name)
112112
return controllerruntime.Result{}, err
113113
}
114114

115115
secret, err := c.ClusterClient.KubeClient.CoreV1().Secrets(c.KarmadaKubeconfigNamespace).Get(ctx, KarmadaKubeconfigName, metav1.GetOptions{})
116116
if err != nil {
117-
klog.Errorf("failed to get karmada kubeconfig secret: %v", err)
117+
klog.ErrorS(err, "failed to get karmada kubeconfig secret")
118118
return controllerruntime.Result{}, err
119119
}
120120

121121
if err = c.syncCertRotation(ctx, secret); err != nil {
122-
klog.Errorf("Failed to rotate the certificate of karmada-agent for the given member cluster: %s, err is: %v", cluster.Name, err)
122+
klog.ErrorS(err, "Failed to rotate the certificate of karmada-agent for the given member cluster", "cluster", cluster.Name)
123123
return controllerruntime.Result{}, err
124124
}
125125

@@ -181,20 +181,20 @@ func (c *CertRotationController) syncCertRotation(ctx context.Context, secret *c
181181
}
182182

183183
var newCertData []byte
184-
klog.V(1).Infof("Waiting for the client certificate to be issued")
184+
klog.V(1).InfoS("Waiting for the client certificate to be issued")
185185
err = wait.PollUntilContextTimeout(ctx, 1*time.Second, 5*time.Minute, false, func(context.Context) (done bool, err error) {
186186
csr, err := c.KubeClient.CertificatesV1().CertificateSigningRequests().Get(ctx, csr, metav1.GetOptions{})
187187
if err != nil {
188188
return false, fmt.Errorf("failed to get the cluster csr %s. err: %v", clusterName, err)
189189
}
190190

191191
if csr.Status.Certificate != nil {
192-
klog.V(1).Infof("Signing certificate successfully")
192+
klog.V(1).InfoS("Signing certificate successfully")
193193
newCertData = csr.Status.Certificate
194194
return true, nil
195195
}
196196

197-
klog.V(1).Infof("Waiting for the client certificate to be issued")
197+
klog.V(1).InfoS("Waiting for the client certificate to be issued")
198198
return false, nil
199199
})
200200
if err != nil {
@@ -217,11 +217,11 @@ func (c *CertRotationController) syncCertRotation(ctx context.Context, secret *c
217217

218218
newCert, err := certutil.ParseCertsPEM(newCertData)
219219
if err != nil {
220-
klog.Errorf("Unable to parse new certificate: %v", err)
220+
klog.ErrorS(err, "Unable to parse new certificate")
221221
return err
222222
}
223223

224-
klog.V(4).Infof("The certificate has been rotated successfully, new expiration time is %v", newCert[0].NotAfter)
224+
klog.V(4).InfoS("The certificate has been rotated successfully", "new expiration time", newCert[0].NotAfter)
225225

226226
return nil
227227
}
@@ -287,15 +287,15 @@ func (c *CertRotationController) shouldRotateCert(certData []byte) (bool, error)
287287

288288
total := notAfter.Sub(*notBefore)
289289
remaining := time.Until(*notAfter)
290-
klog.V(4).Infof("The certificate of karmada-agent: time total=%v, remaining=%v, remaining/total=%v", total, remaining, remaining.Seconds()/total.Seconds())
290+
klog.V(4).InfoS("The certificate of karmada-agent", "time total", total, "remaining", remaining, "remaining/total", remaining.Seconds()/total.Seconds())
291291

292292
if remaining.Seconds()/total.Seconds() > c.CertRotationRemainingTimeThreshold {
293293
// Do nothing if the certificate of karmada-agent is valid and has more than a valid threshold of its life remaining
294-
klog.V(4).Infof("The certificate of karmada-agent is valid and has more than %.2f%% of its life remaining", c.CertRotationRemainingTimeThreshold*100)
294+
klog.V(4).InfoS("The certificate of karmada-agent is valid and has more than the valid threshold of its life remaining", "threshold", c.CertRotationRemainingTimeThreshold*100)
295295
return false, nil
296296
}
297297

298-
klog.V(4).Infof("The certificate of karmada-agent has less than or equal %.2f%% of its life remaining and need to be rotated", c.CertRotationRemainingTimeThreshold*100)
298+
klog.V(4).InfoS("The certificate of karmada-agent has less than or equal the valid threshold of its life remaining and need to be rotated", "threshold", c.CertRotationRemainingTimeThreshold*100)
299299
return true, nil
300300
}
301301

0 commit comments

Comments
 (0)