From 532b22f80d5dfc7836634ce85d29f360155a9fda Mon Sep 17 00:00:00 2001 From: calvin0327 Date: Sun, 3 Jul 2022 21:51:07 +0800 Subject: [PATCH] cleanup federated resource quota Signed-off-by: calvin0327 --- ...ederated_resource_quota_sync_controller.go | 20 +++++-------------- pkg/controllers/hpa/hpa_controller.go | 5 +---- .../mcs/service_export_controller.go | 13 ++---------- .../dependencies_distributor.go | 6 +----- pkg/detector/detector.go | 7 +------ pkg/util/helper/binding.go | 12 ++--------- pkg/util/helper/mcs.go | 6 +----- 7 files changed, 13 insertions(+), 56 deletions(-) diff --git a/pkg/controllers/federatedresourcequota/federated_resource_quota_sync_controller.go b/pkg/controllers/federatedresourcequota/federated_resource_quota_sync_controller.go index 3aa8e3527..24314bf42 100644 --- a/pkg/controllers/federatedresourcequota/federated_resource_quota_sync_controller.go +++ b/pkg/controllers/federatedresourcequota/federated_resource_quota_sync_controller.go @@ -6,7 +6,6 @@ import ( corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/errors" "k8s.io/client-go/tools/record" @@ -120,12 +119,9 @@ func (c *SyncController) SetupWithManager(mgr controllerruntime.Manager) error { func (c *SyncController) cleanUpWorks(namespace, name string) error { var errs []error workList := &workv1alpha1.WorkList{} - if err := c.List(context.TODO(), workList, &client.ListOptions{ - LabelSelector: labels.SelectorFromSet( - labels.Set{ - util.FederatedResourceQuotaNamespaceLabel: namespace, - util.FederatedResourceQuotaNameLabel: name, - }), + if err := c.List(context.TODO(), workList, client.MatchingLabels{ + util.FederatedResourceQuotaNamespaceLabel: namespace, + util.FederatedResourceQuotaNameLabel: name, }); err != nil { klog.Errorf("Failed to list works, err: %v", err) return err @@ -139,10 +135,7 @@ func (c *SyncController) cleanUpWorks(namespace, name string) error { } } - if len(errs) > 0 { - return errors.NewAggregate(errs) - } - return nil + return errors.NewAggregate(errs) } func (c *SyncController) buildWorks(quota *policyv1alpha1.FederatedResourceQuota, clusters []clusterv1alpha1.Cluster) error { @@ -190,10 +183,7 @@ func (c *SyncController) buildWorks(quota *policyv1alpha1.FederatedResourceQuota } } - if len(errs) > 0 { - return errors.NewAggregate(errs) - } - return nil + return errors.NewAggregate(errs) } func extractClusterHardResourceList(spec policyv1alpha1.FederatedResourceQuotaSpec, cluster string) corev1.ResourceList { diff --git a/pkg/controllers/hpa/hpa_controller.go b/pkg/controllers/hpa/hpa_controller.go index 8bed865e2..4a84a5f81 100644 --- a/pkg/controllers/hpa/hpa_controller.go +++ b/pkg/controllers/hpa/hpa_controller.go @@ -173,8 +173,5 @@ func (c *HorizontalPodAutoscalerController) deleteWorks(workName string) error { } } } - if len(errs) > 0 { - return utilerrors.NewAggregate(errs) - } - return nil + return utilerrors.NewAggregate(errs) } diff --git a/pkg/controllers/mcs/service_export_controller.go b/pkg/controllers/mcs/service_export_controller.go index 3c6a991b5..5e4d21259 100644 --- a/pkg/controllers/mcs/service_export_controller.go +++ b/pkg/controllers/mcs/service_export_controller.go @@ -354,12 +354,7 @@ func (c *ServiceExportController) reportEndpointSliceWithServiceExportCreate(ser errs = append(errs, err) } } - - if len(errs) > 0 { - return utilerrors.NewAggregate(errs) - } - - return nil + return utilerrors.NewAggregate(errs) } // reportEndpointSliceWithEndpointSliceCreateOrUpdate reports the EndpointSlice when referencing service has been exported. @@ -436,11 +431,7 @@ func cleanupWorkWithServiceExportDelete(c client.Client, serviceExportKey keys.F errs = append(errs, err) } } - if len(errs) > 0 { - return utilerrors.NewAggregate(errs) - } - - return nil + return utilerrors.NewAggregate(errs) } func cleanupWorkWithEndpointSliceDelete(c client.Client, endpointSliceKey keys.FederatedKey) error { diff --git a/pkg/dependenciesdistributor/dependencies_distributor.go b/pkg/dependenciesdistributor/dependencies_distributor.go index 05fc549c6..824d0923d 100644 --- a/pkg/dependenciesdistributor/dependencies_distributor.go +++ b/pkg/dependenciesdistributor/dependencies_distributor.go @@ -546,11 +546,7 @@ func (d *DependenciesDistributor) removeScheduleResultFromAttachedBindings(bindi } } - if len(errs) > 0 { - return utilerrors.NewAggregate(errs) - } - - return nil + return utilerrors.NewAggregate(errs) } func buildAttachedBinding(binding *workv1alpha2.ResourceBinding, object *unstructured.Unstructured) *workv1alpha2.ResourceBinding { diff --git a/pkg/detector/detector.go b/pkg/detector/detector.go index 0d934aca6..61ede539e 100644 --- a/pkg/detector/detector.go +++ b/pkg/detector/detector.go @@ -973,12 +973,7 @@ func (d *ResourceDetector) HandleClusterPropagationPolicyDeletion(policyName str } } } - - if len(errs) > 0 { - return errors.NewAggregate(errs) - } - - return nil + return errors.NewAggregate(errs) } // HandlePropagationPolicyCreation handles PropagationPolicy add event. diff --git a/pkg/util/helper/binding.go b/pkg/util/helper/binding.go index 42d674f3c..702b175ba 100644 --- a/pkg/util/helper/binding.go +++ b/pkg/util/helper/binding.go @@ -132,10 +132,7 @@ func RemoveOrphanWorks(c client.Client, works []workv1alpha1.Work) error { } klog.Infof("Delete orphan work %s/%s successfully.", work.GetNamespace(), work.GetName()) } - if len(errs) > 0 { - return errors.NewAggregate(errs) - } - return nil + return errors.NewAggregate(errs) } // FetchWorkload fetches the kubernetes resource to be propagated. @@ -223,12 +220,7 @@ func DeleteWorks(c client.Client, namespace, name string) error { errs = append(errs, err) } } - - if len(errs) > 0 { - return errors.NewAggregate(errs) - } - - return nil + return errors.NewAggregate(errs) } // GenerateNodeClaimByPodSpec will return a NodeClaim from PodSpec. diff --git a/pkg/util/helper/mcs.go b/pkg/util/helper/mcs.go index 824d2481d..3ead689a2 100644 --- a/pkg/util/helper/mcs.go +++ b/pkg/util/helper/mcs.go @@ -73,9 +73,5 @@ func DeleteEndpointSlice(c client.Client, selector labels.Set) error { } } - if len(errs) > 0 { - return errors.NewAggregate(errs) - } - - return nil + return errors.NewAggregate(errs) }