Skip to content

Update logging format for federated resource quota #6592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ type QuotaEnforcementController struct {
// The SyncController will requeue the Request to be processed again if an error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (c *QuotaEnforcementController) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
klog.V(4).Infof("QuotaEnforcementController reconciling %s", req.NamespacedName.String())
klog.V(4).InfoS("QuotaEnforcementController reconciling", "namespacedName", req.NamespacedName.String())

quota := &policyv1alpha1.FederatedResourceQuota{}
if err := c.Get(ctx, req.NamespacedName, quota); err != nil {
// FederatedResourceQuota may no longer exist, in which case we stop processing.
if apierrors.IsNotFound(err) {
return controllerruntime.Result{}, nil
}
klog.Errorf("Error fetching FederatedResourceQuota %s: %v", req.NamespacedName.String(), err)
klog.ErrorS(err, "Error fetching FederatedResourceQuota", "federatedResourceQuota", req.NamespacedName.String())
return controllerruntime.Result{}, err
}

Expand All @@ -83,7 +83,7 @@ func (c *QuotaEnforcementController) Reconcile(ctx context.Context, req controll
}

if err := c.collectQuotaStatus(quota); err != nil {
klog.Errorf("Failed to collect status for FederatedResourceQuota(%s), error: %v", req.NamespacedName.String(), err)
klog.ErrorS(err, "Failed to collect status for FederatedResourceQuota", "federatedResourceQuota", req.NamespacedName.String())
c.EventRecorder.Eventf(quota, corev1.EventTypeWarning, events.EventReasonCollectFederatedResourceQuotaOverallStatusFailed, err.Error())
return controllerruntime.Result{}, err
}
Expand All @@ -99,13 +99,13 @@ func (c *QuotaEnforcementController) SetupWithManager(mgr controllerruntime.Mana
func(ctx context.Context, obj client.Object) []reconcile.Request {
rb, ok := obj.(*workv1alpha2.ResourceBinding)
if !ok {
klog.Errorf("Failed to convert object %v to ResourceBinding", obj)
klog.ErrorS(fmt.Errorf("unexpected type: %T", obj), "Failed to convert object to ResourceBinding", "object", obj)
return []reconcile.Request{}
}

federatedResourceQuotaList := &policyv1alpha1.FederatedResourceQuotaList{}
if err := c.Client.List(ctx, federatedResourceQuotaList, &client.ListOptions{Namespace: rb.GetNamespace()}); err != nil {
klog.Errorf("Failed to list FederatedResourceQuota, error: %v", err)
klog.ErrorS(err, "Failed to list FederatedResourceQuota")
return []reconcile.Request{}
}

Expand All @@ -129,7 +129,7 @@ func (c *QuotaEnforcementController) SetupWithManager(mgr controllerruntime.Mana
func(ctx context.Context, _ client.Object) []reconcile.Request {
federatedResourceQuotaList := &policyv1alpha1.FederatedResourceQuotaList{}
if err := c.Client.List(ctx, federatedResourceQuotaList); err != nil {
klog.Errorf("Failed to list FederatedResourceQuota, error: %v", err)
klog.ErrorS(err, "Failed to list FederatedResourceQuota")
return []reconcile.Request{}
}

Expand Down Expand Up @@ -217,7 +217,7 @@ func (q *QuotaRecalculation) Start(ctx context.Context) error {
defer close(q.resyncEvent)

if q.ResyncPeriod.Duration > 0 {
klog.Infof("Starting FederatedResourceQuota recalculation process with period %s", q.ResyncPeriod.Duration.String())
klog.InfoS("Starting FederatedResourceQuota recalculation process with period", "duration", q.ResyncPeriod.Duration.String())
ticker := time.NewTicker(q.ResyncPeriod.Duration)
defer ticker.Stop()
for {
Expand All @@ -242,7 +242,7 @@ func (c *QuotaEnforcementController) collectQuotaStatus(quota *policyv1alpha1.Fe
// TODO: Consider adding filtering step to ResourceBinding list once scope is added to the quota
bindingList, err := helper.GetResourceBindingsByNamespace(c.Client, quota.Namespace)
if err != nil {
klog.Errorf("Failed to list resourcebindings tracked by FederatedResourceQuota(%s), error: %v", klog.KObj(quota).String(), err)
klog.ErrorS(err, "Failed to list resourcebindings tracked by FederatedResourceQuota", "federatedResourceQuota", klog.KObj(quota).String())
return err
}

Expand All @@ -251,7 +251,7 @@ func (c *QuotaEnforcementController) collectQuotaStatus(quota *policyv1alpha1.Fe
quotaStatus.OverallUsed = calculateUsedWithResourceBinding(bindingList.Items, quota.Spec.Overall)

if reflect.DeepEqual(quota.Status, *quotaStatus) {
klog.V(4).Infof("New quotaStatus is equal with old federatedResourceQuota(%s) status, no update required.", klog.KObj(quota).String())
klog.V(4).InfoS("New quotaStatus is equal with old federatedResourceQuota status, no update required.", "federatedResourceQuota", klog.KObj(quota).String())
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type StatusController struct {
// The SyncController will requeue the Request to be processed again if an error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (c *StatusController) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
klog.V(4).Infof("FederatedResourceQuota status controller reconciling %s", req.NamespacedName.String())
klog.V(4).InfoS("FederatedResourceQuota status controller reconciling", "namespacedName", req.NamespacedName.String())

quota := &policyv1alpha1.FederatedResourceQuota{}
if err := c.Get(ctx, req.NamespacedName, quota); err != nil {
Expand All @@ -82,7 +82,7 @@ func (c *StatusController) Reconcile(ctx context.Context, req controllerruntime.
}

if err := c.collectQuotaStatus(ctx, quota); err != nil {
klog.Errorf("Failed to collect status from works to federatedResourceQuota(%s), error: %v", req.NamespacedName.String(), err)
klog.ErrorS(err, "Failed to collect status from works to federatedResourceQuota", "federatedResourceQuota", req.NamespacedName.String())
c.EventRecorder.Eventf(quota, corev1.EventTypeWarning, events.EventReasonCollectFederatedResourceQuotaStatusFailed, err.Error())
return controllerruntime.Result{}, err
}
Expand Down Expand Up @@ -154,7 +154,7 @@ func (c *StatusController) collectQuotaStatus(ctx context.Context, quota *policy
util.FederatedResourceQuotaNameLabel: quota.Name,
})
if err != nil {
klog.Errorf("Failed to list workList created by federatedResourceQuota(%s), error: %v", klog.KObj(quota).String(), err)
klog.ErrorS(err, "Failed to list workList created by federatedResourceQuota", "federatedResourceQuota", klog.KObj(quota).String())
return err
}

Expand All @@ -172,7 +172,7 @@ func (c *StatusController) collectQuotaStatus(ctx context.Context, quota *policy
}

if reflect.DeepEqual(quota.Status, *quotaStatus) {
klog.V(4).Infof("New quotaStatus are equal with old federatedResourceQuota(%s) status, no update required.", klog.KObj(quota).String())
klog.V(4).InfoS("New quotaStatus is equal with old federatedResourceQuota status, no update required.", "federatedResourceQuota", klog.KObj(quota).String())
return nil
}

Expand Down Expand Up @@ -214,13 +214,13 @@ func aggregatedStatusFormWorks(works []workv1alpha1.Work) ([]policyv1alpha1.Clus

clusterName, err := names.GetClusterName(work.Namespace)
if err != nil {
klog.Errorf("Failed to get clusterName from work namespace %s. Error: %v.", work.Namespace, err)
klog.ErrorS(err, "Failed to get clusterName from work namespace.", "workNamespace", work.Namespace)
return nil, err
}

status := &corev1.ResourceQuotaStatus{}
if err := json.Unmarshal(work.Status.ManifestStatuses[0].Status.Raw, status); err != nil {
klog.Errorf("Failed to unmarshal work(%s) status to ResourceQuotaStatus", klog.KObj(&work).String())
klog.ErrorS(err, "Failed to unmarshal work status to ResourceQuotaStatus", "work", klog.KObj(&work).String())
return nil, err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ type SyncController struct {
// The SyncController will requeue the Request to be processed again if an error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (c *SyncController) Reconcile(ctx context.Context, req controllerruntime.Request) (controllerruntime.Result, error) {
klog.V(4).Infof("FederatedResourceQuota sync controller reconciling %s", req.NamespacedName.String())
klog.V(4).InfoS("FederatedResourceQuota sync controller reconciling", "namespacedName", req.NamespacedName.String())

quota := &policyv1alpha1.FederatedResourceQuota{}
if err := c.Client.Get(ctx, req.NamespacedName, quota); err != nil {
if apierrors.IsNotFound(err) {
klog.V(4).Infof("Begin to cleanup works created by federatedResourceQuota(%s)", req.NamespacedName.String())
klog.V(4).InfoS("Begin to cleanup works created by federatedResourceQuota", "namespacedName", req.NamespacedName.String())
if err = c.cleanUpWorks(ctx, req.Namespace, req.Name); err != nil {
klog.Errorf("Failed to cleanup works created by federatedResourceQuota(%s)", req.NamespacedName.String())
klog.ErrorS(err, "Failed to cleanup works created by federatedResourceQuota", "namespacedName", req.NamespacedName.String())
return controllerruntime.Result{}, err
}
return controllerruntime.Result{}, nil
Expand All @@ -79,18 +79,18 @@ func (c *SyncController) Reconcile(ctx context.Context, req controllerruntime.Re
}

if err := c.cleanUpOrphanWorks(ctx, quota); err != nil {
klog.Errorf("Failed to cleanup orphan works for federatedResourceQuota(%s), error: %v", req.NamespacedName.String(), err)
klog.ErrorS(err, "Failed to cleanup orphan works for federatedResourceQuota", "namespacedName", req.NamespacedName.String())
return controllerruntime.Result{}, err
}

clusterList := &clusterv1alpha1.ClusterList{}
if err := c.Client.List(ctx, clusterList); err != nil {
klog.Errorf("Failed to list clusters, error: %v", err)
klog.ErrorS(err, "Failed to list clusters")
return controllerruntime.Result{}, err
}

if err := c.buildWorks(ctx, quota, clusterList.Items); err != nil {
klog.Errorf("Failed to build works for federatedResourceQuota(%s), error: %v", req.NamespacedName.String(), err)
klog.ErrorS(err, "Failed to build works for federatedResourceQuota", "namespacedName", req.NamespacedName.String())
c.EventRecorder.Eventf(quota, corev1.EventTypeWarning, events.EventReasonSyncFederatedResourceQuotaFailed, err.Error())
return controllerruntime.Result{}, err
}
Expand All @@ -114,7 +114,7 @@ func (c *SyncController) SetupWithManager(mgr controllerruntime.Manager) error {

FederatedResourceQuotaList := &policyv1alpha1.FederatedResourceQuotaList{}
if err := c.Client.List(ctx, FederatedResourceQuotaList); err != nil {
klog.Errorf("Failed to list FederatedResourceQuota, error: %v", err)
klog.ErrorS(err, "Failed to list FederatedResourceQuota")
}

for _, federatedResourceQuota := range FederatedResourceQuotaList.Items {
Expand Down Expand Up @@ -189,14 +189,14 @@ func (c *SyncController) cleanUpWorks(ctx context.Context, namespace, name strin
util.FederatedResourceQuotaNamespaceLabel: namespace,
util.FederatedResourceQuotaNameLabel: name,
}); err != nil {
klog.Errorf("Failed to list works, err: %v", err)
klog.ErrorS(err, "Failed to list works")
return err
}

for index := range workList.Items {
work := &workList.Items[index]
if err := c.Delete(ctx, work); err != nil && !apierrors.IsNotFound(err) {
klog.Errorf("Failed to delete work(%s): %v", klog.KObj(work).String(), err)
klog.ErrorS(err, "Failed to delete work", "work", klog.KObj(work).String())
errs = append(errs, err)
}
}
Expand All @@ -211,7 +211,7 @@ func (c *SyncController) cleanUpOrphanWorks(ctx context.Context, quota *policyv1
util.FederatedResourceQuotaNamespaceLabel: quota.GetNamespace(),
util.FederatedResourceQuotaNameLabel: quota.GetName(),
}); err != nil {
klog.Errorf("Failed to list works, err: %v", err)
klog.ErrorS(err, "Failed to list works")
return err
}

Expand All @@ -221,7 +221,7 @@ func (c *SyncController) cleanUpOrphanWorks(ctx context.Context, quota *policyv1
continue
}
if err := c.Delete(ctx, work); err != nil && !apierrors.IsNotFound(err) {
klog.Errorf("Failed to delete work(%s): %v", klog.KObj(work).String(), err)
klog.ErrorS(err, "Failed to delete work", "work", klog.KObj(work).String())
errs = append(errs, err)
}
}
Expand All @@ -246,7 +246,7 @@ func (c *SyncController) buildWorks(ctx context.Context, quota *policyv1alpha1.F

resourceQuotaObj, err := helper.ToUnstructured(resourceQuota)
if err != nil {
klog.Errorf("Failed to transform resourceQuota(%s), error: %v", klog.KObj(resourceQuota).String(), err)
klog.ErrorS(err, "Failed to transform resourceQuota", "resourceQuota", klog.KObj(resourceQuota).String())
errs = append(errs, err)
continue
}
Expand Down