migrate pkg/controller/volume/pvcprotection.go to structured logs

This commit is contained in:
wangkai1994 2021-02-02 17:42:20 +08:00
parent 3433f099fb
commit ab11816570

View File

@ -107,8 +107,8 @@ func (c *Controller) Run(workers int, stopCh <-chan struct{}) {
defer utilruntime.HandleCrash() defer utilruntime.HandleCrash()
defer c.queue.ShutDown() defer c.queue.ShutDown()
klog.Infof("Starting PVC protection controller") klog.InfoS("Starting PVC protection controller")
defer klog.Infof("Shutting down PVC protection controller") defer klog.InfoS("Shutting down PVC protection controller")
if !cache.WaitForNamedCacheSync("PVC protection", stopCh, c.pvcListerSynced, c.podListerSynced) { if !cache.WaitForNamedCacheSync("PVC protection", stopCh, c.pvcListerSynced, c.podListerSynced) {
return return
@ -153,7 +153,7 @@ func (c *Controller) processNextWorkItem() bool {
} }
func (c *Controller) processPVC(pvcNamespace, pvcName string) error { func (c *Controller) processPVC(pvcNamespace, pvcName string) error {
klog.V(4).Infof("Processing PVC %s/%s", pvcNamespace, pvcName) klog.V(4).InfoS("Processing PVC", pvcNamespace, "/", pvcName)
startTime := time.Now() startTime := time.Now()
defer func() { defer func() {
klog.V(4).Infof("Finished processing PVC %s/%s (%v)", pvcNamespace, pvcName, time.Since(startTime)) klog.V(4).Infof("Finished processing PVC %s/%s (%v)", pvcNamespace, pvcName, time.Since(startTime))
@ -200,10 +200,10 @@ func (c *Controller) addFinalizer(pvc *v1.PersistentVolumeClaim) error {
claimClone.ObjectMeta.Finalizers = append(claimClone.ObjectMeta.Finalizers, volumeutil.PVCProtectionFinalizer) claimClone.ObjectMeta.Finalizers = append(claimClone.ObjectMeta.Finalizers, volumeutil.PVCProtectionFinalizer)
_, err := c.client.CoreV1().PersistentVolumeClaims(claimClone.Namespace).Update(context.TODO(), claimClone, metav1.UpdateOptions{}) _, err := c.client.CoreV1().PersistentVolumeClaims(claimClone.Namespace).Update(context.TODO(), claimClone, metav1.UpdateOptions{})
if err != nil { if err != nil {
klog.V(3).Infof("Error adding protection finalizer to PVC %s/%s: %v", pvc.Namespace, pvc.Name, err) klog.ErrorS(err, "Error adding protection finalizer to PVC", pvc.Namespace, "/", pvc.Name)
return err return err
} }
klog.V(3).Infof("Added protection finalizer to PVC %s/%s", pvc.Namespace, pvc.Name) klog.V(3).InfoS("Added protection finalizer to PVC", pvc.Namespace, "/", pvc.Name)
return nil return nil
} }
@ -212,10 +212,10 @@ func (c *Controller) removeFinalizer(pvc *v1.PersistentVolumeClaim) error {
claimClone.ObjectMeta.Finalizers = slice.RemoveString(claimClone.ObjectMeta.Finalizers, volumeutil.PVCProtectionFinalizer, nil) claimClone.ObjectMeta.Finalizers = slice.RemoveString(claimClone.ObjectMeta.Finalizers, volumeutil.PVCProtectionFinalizer, nil)
_, err := c.client.CoreV1().PersistentVolumeClaims(claimClone.Namespace).Update(context.TODO(), claimClone, metav1.UpdateOptions{}) _, err := c.client.CoreV1().PersistentVolumeClaims(claimClone.Namespace).Update(context.TODO(), claimClone, metav1.UpdateOptions{})
if err != nil { if err != nil {
klog.V(3).Infof("Error removing protection finalizer from PVC %s/%s: %v", pvc.Namespace, pvc.Name, err) klog.ErrorS(err, "Error removing protection finalizer from PVC", pvc.Namespace, "/", pvc.Name)
return err return err
} }
klog.V(3).Infof("Removed protection finalizer from PVC %s/%s", pvc.Namespace, pvc.Name) klog.V(3).InfoS("Removed protection finalizer from PVC", pvc.Namespace, "/", pvc.Name)
return nil return nil
} }
@ -238,7 +238,7 @@ func (c *Controller) isBeingUsed(pvc *v1.PersistentVolumeClaim) (bool, error) {
} }
func (c *Controller) askInformer(pvc *v1.PersistentVolumeClaim) (bool, error) { func (c *Controller) askInformer(pvc *v1.PersistentVolumeClaim) (bool, error) {
klog.V(4).Infof("Looking for Pods using PVC %s/%s in the Informer's cache", pvc.Namespace, pvc.Name) klog.V(4).InfoS("Looking for Pods using PVC", pvc.Namespace, "/", pvc.Name, "in the Informer's cache")
// The indexer is used to find pods which might use the PVC. // The indexer is used to find pods which might use the PVC.
objs, err := c.podIndexer.ByIndex(common.PodPVCIndex, fmt.Sprintf("%s/%s", pvc.Namespace, pvc.Name)) objs, err := c.podIndexer.ByIndex(common.PodPVCIndex, fmt.Sprintf("%s/%s", pvc.Namespace, pvc.Name))
@ -270,12 +270,12 @@ func (c *Controller) askInformer(pvc *v1.PersistentVolumeClaim) (bool, error) {
return true, nil return true, nil
} }
klog.V(4).Infof("No Pod using PVC %s/%s was found in the Informer's cache", pvc.Namespace, pvc.Name) klog.V(4).InfoS("No Pod using PVC", pvc.Namespace, "/", pvc.Name, "was found in the Informer's cache")
return false, nil return false, nil
} }
func (c *Controller) askAPIServer(pvc *v1.PersistentVolumeClaim) (bool, error) { func (c *Controller) askAPIServer(pvc *v1.PersistentVolumeClaim) (bool, error) {
klog.V(4).Infof("Looking for Pods using PVC %s/%s with a live list", pvc.Namespace, pvc.Name) klog.V(4).InfoS("Looking for Pods using PVC", pvc.Namespace, "/", pvc.Name, "with a live list")
podsList, err := c.client.CoreV1().Pods(pvc.Namespace).List(context.TODO(), metav1.ListOptions{}) podsList, err := c.client.CoreV1().Pods(pvc.Namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil { if err != nil {
@ -288,7 +288,7 @@ func (c *Controller) askAPIServer(pvc *v1.PersistentVolumeClaim) (bool, error) {
} }
} }
klog.V(2).Infof("PVC %s/%s is unused", pvc.Namespace, pvc.Name) klog.V(2).InfoS("PVC", pvc.Namespace, "/", pvc.Name, "is unused")
return false, nil return false, nil
} }
@ -300,7 +300,7 @@ func (c *Controller) podUsesPVC(pod *v1.Pod, pvc *v1.PersistentVolumeClaim) bool
for _, volume := range pod.Spec.Volumes { for _, volume := range pod.Spec.Volumes {
if volume.PersistentVolumeClaim != nil && volume.PersistentVolumeClaim.ClaimName == pvc.Name || if volume.PersistentVolumeClaim != nil && volume.PersistentVolumeClaim.ClaimName == pvc.Name ||
c.genericEphemeralVolumeFeatureEnabled && !podIsShutDown(pod) && volume.Ephemeral != nil && pod.Name+"-"+volume.Name == pvc.Name && metav1.IsControlledBy(pvc, pod) { c.genericEphemeralVolumeFeatureEnabled && !podIsShutDown(pod) && volume.Ephemeral != nil && pod.Name+"-"+volume.Name == pvc.Name && metav1.IsControlledBy(pvc, pod) {
klog.V(2).Infof("Pod %s/%s uses PVC %s", pod.Namespace, pod.Name, pvc) klog.V(2).InfoS("Pod", pvc.Namespace, "/", pvc.Name, "uses PVC", pvc)
return true return true
} }
} }
@ -357,7 +357,7 @@ func (c *Controller) pvcAddedUpdated(obj interface{}) {
utilruntime.HandleError(fmt.Errorf("couldn't get key for Persistent Volume Claim %#v: %v", pvc, err)) utilruntime.HandleError(fmt.Errorf("couldn't get key for Persistent Volume Claim %#v: %v", pvc, err))
return return
} }
klog.V(4).Infof("Got event on PVC %s", key) klog.V(4).InfoS("Got event on PVC", key)
if protectionutil.NeedToAddFinalizer(pvc, volumeutil.PVCProtectionFinalizer) || protectionutil.IsDeletionCandidate(pvc, volumeutil.PVCProtectionFinalizer) { if protectionutil.NeedToAddFinalizer(pvc, volumeutil.PVCProtectionFinalizer) || protectionutil.IsDeletionCandidate(pvc, volumeutil.PVCProtectionFinalizer) {
c.queue.Add(key) c.queue.Add(key)
@ -406,7 +406,7 @@ func (c *Controller) enqueuePVCs(pod *v1.Pod, deleted bool) {
return return
} }
klog.V(4).Infof("Enqueuing PVCs for Pod %s/%s (UID=%s)", pod.Namespace, pod.Name, pod.UID) klog.V(4).InfoS("Enqueuing PVCs for Pod", pod.Namespace, "/", pod.Name, "(UID=", pod.UID, ")")
// Enqueue all PVCs that the pod uses // Enqueue all PVCs that the pod uses
for _, volume := range pod.Spec.Volumes { for _, volume := range pod.Spec.Volumes {