From b77d3e1b24daab34461a463945dea66509acec02 Mon Sep 17 00:00:00 2001 From: Mattia Lavacca Date: Wed, 15 May 2024 12:38:24 +0200 Subject: [PATCH] fix: helpers adjusted to labels Signed-off-by: Mattia Lavacca --- .../controller_reconciler_utils.go | 16 +++++--------- pkg/utils/kubernetes/lists.go | 22 ++++++++----------- pkg/utils/kubernetes/lists_test.go | 2 +- pkg/utils/test/asserts.go | 12 +++++----- pkg/utils/test/predicates.go | 2 +- 5 files changed, 23 insertions(+), 31 deletions(-) diff --git a/controller/controlplane/controller_reconciler_utils.go b/controller/controlplane/controller_reconciler_utils.go index 10c7fa469..7761b350a 100644 --- a/controller/controlplane/controller_reconciler_utils.go +++ b/controller/controlplane/controller_reconciler_utils.go @@ -293,10 +293,9 @@ func (r *Reconciler) ensureClusterRole( ctx context.Context, controlplane *operatorv1beta1.ControlPlane, ) (createdOrUpdated bool, cr *rbacv1.ClusterRole, err error) { - clusterRoles, err := k8sutils.ListClusterRolesForOwner( + clusterRoles, err := k8sutils.ListClusterRoles( ctx, r.Client, - controlplane.UID, client.MatchingLabels{ consts.GatewayOperatorManagedByLabel: consts.ControlPlaneManagedLabelValue, consts.GatewayOperatorManagedByNameLabel: controlplane.Name, @@ -353,10 +352,9 @@ func (r *Reconciler) ensureClusterRoleBinding( ) (createdOrUpdate bool, crb *rbacv1.ClusterRoleBinding, err error) { logger := log.GetLogger(ctx, "controlplane.ensureClusterRoleBinding", r.DevelopmentMode) - clusterRoleBindings, err := k8sutils.ListClusterRoleBindingsForOwner( + clusterRoleBindings, err := k8sutils.ListClusterRoleBindings( ctx, r.Client, - controlplane.UID, client.MatchingLabels{ consts.GatewayOperatorManagedByLabel: consts.ControlPlaneManagedLabelValue, consts.GatewayOperatorManagedByNameLabel: controlplane.Name, @@ -489,9 +487,8 @@ func (r *Reconciler) ensureOwnedClusterRolesDeleted( ctx context.Context, controlplane *operatorv1beta1.ControlPlane, ) (deletions bool, err error) { - clusterRoles, err := k8sutils.ListClusterRolesForOwner( + clusterRoles, err := k8sutils.ListClusterRoles( ctx, r.Client, - controlplane.UID, client.MatchingLabels{ consts.GatewayOperatorManagedByLabel: consts.ControlPlaneManagedLabelValue, consts.GatewayOperatorManagedByNameLabel: controlplane.Name, @@ -523,9 +520,8 @@ func (r *Reconciler) ensureOwnedClusterRoleBindingsDeleted( ctx context.Context, controlplane *operatorv1beta1.ControlPlane, ) (deletions bool, err error) { - clusterRoleBindings, err := k8sutils.ListClusterRoleBindingsForOwner( + clusterRoleBindings, err := k8sutils.ListClusterRoleBindings( ctx, r.Client, - controlplane.UID, client.MatchingLabels{ consts.GatewayOperatorManagedByLabel: consts.ControlPlaneManagedLabelValue, consts.GatewayOperatorManagedByNameLabel: controlplane.Name, @@ -551,7 +547,7 @@ func (r *Reconciler) ensureOwnedClusterRoleBindingsDeleted( } func (r *Reconciler) ensureOwnedValidatingWebhookConfigurationDeleted(ctx context.Context, cp *operatorv1beta1.ControlPlane) (deletions bool, err error) { - validatingWebhookConfigurations, err := k8sutils.ListValidatingWebhookConfigurationsForOwner( + validatingWebhookConfigurations, err := k8sutils.ListValidatingWebhookConfigurations( ctx, r.Client, client.MatchingLabels{ @@ -647,7 +643,7 @@ func (r *Reconciler) ensureValidatingWebhookConfiguration( ) (op.CreatedUpdatedOrNoop, error) { logger := log.GetLogger(ctx, "controlplane.ensureValidatingWebhookConfiguration", r.DevelopmentMode) - validatingWebhookConfigurations, err := k8sutils.ListValidatingWebhookConfigurationsForOwner( + validatingWebhookConfigurations, err := k8sutils.ListValidatingWebhookConfigurations( ctx, r.Client, client.MatchingLabels{ diff --git a/pkg/utils/kubernetes/lists.go b/pkg/utils/kubernetes/lists.go index 85537b297..1e303c294 100644 --- a/pkg/utils/kubernetes/lists.go +++ b/pkg/utils/kubernetes/lists.go @@ -152,13 +152,11 @@ func ListServiceAccountsForOwner( return serviceAccounts, nil } -// ListClusterRolesForOwner is a helper function to map a list of ClusterRoles -// by list options and reduce by OwnerReference UID to efficiently -// list only the objects owned by the provided UID. -func ListClusterRolesForOwner( +// ListClusterRoles is a helper function to map a list of ClusterRoles +// by list options. +func ListClusterRoles( ctx context.Context, c client.Client, - uid types.UID, listOpts ...client.ListOption, ) ([]rbacv1.ClusterRole, error) { clusterRoleList := &rbacv1.ClusterRoleList{} @@ -175,13 +173,11 @@ func ListClusterRolesForOwner( return clusterRoleList.Items, nil } -// ListClusterRoleBindingsForOwner is a helper function to map a list of ClusterRoleBindings -// by list options and reduce by OwnerReference UID to efficiently -// list only the objects owned by the provided UID. -func ListClusterRoleBindingsForOwner( +// ListClusterRoleBindings is a helper function to map a list of ClusterRoleBindings +// by list options. +func ListClusterRoleBindings( ctx context.Context, c client.Client, - uid types.UID, listOpts ...client.ListOption, ) ([]rbacv1.ClusterRoleBinding, error) { clusterRoleBindingList := &rbacv1.ClusterRoleBindingList{} @@ -228,9 +224,9 @@ func ListSecretsForOwner(ctx context.Context, return secrets, nil } -// ListValidatingWebhookConfigurationsForOwner is a helper function to map a list of ValidatingWebhookConfiguration -// by list options and reduce by OwnerReference UID to efficiently list only the objects owned by the provided UID. -func ListValidatingWebhookConfigurationsForOwner( +// ListValidatingWebhookConfigurations is a helper function to map a list of ValidatingWebhookConfiguration +// by list options. +func ListValidatingWebhookConfigurations( ctx context.Context, c client.Client, listOpts ...client.ListOption, diff --git a/pkg/utils/kubernetes/lists_test.go b/pkg/utils/kubernetes/lists_test.go index 63ff3227f..75eb1a9a0 100644 --- a/pkg/utils/kubernetes/lists_test.go +++ b/pkg/utils/kubernetes/lists_test.go @@ -74,7 +74,7 @@ func TestListValidatingWebhookConfigurationsForOwner(t *testing.T) { tc := tc t.Run(tc.name, func(t *testing.T) { c := fake.NewFakeClient(tc.objects...) - ownedCfgs, err := k8sutils.ListValidatingWebhookConfigurationsForOwner(ctx, + ownedCfgs, err := k8sutils.ListValidatingWebhookConfigurations(ctx, c, client.MatchingLabels{ consts.GatewayOperatorManagedByNameLabel: "owner", diff --git a/pkg/utils/test/asserts.go b/pkg/utils/test/asserts.go index 24fa54007..922b9b855 100644 --- a/pkg/utils/test/asserts.go +++ b/pkg/utils/test/asserts.go @@ -41,12 +41,12 @@ func MustListControlPlaneDeployments(t *testing.T, ctx context.Context, controlp // MustListControlPlaneClusterRoles is a helper function for tests that // conveniently lists all clusterroles owned by a given controlplane. func MustListControlPlaneClusterRoles(t *testing.T, ctx context.Context, controlplane *operatorv1beta1.ControlPlane, clients K8sClients) []rbacv1.ClusterRole { - clusterRoles, err := k8sutils.ListClusterRolesForOwner( + clusterRoles, err := k8sutils.ListClusterRoles( ctx, clients.MgrClient, - controlplane.UID, client.MatchingLabels{ - consts.GatewayOperatorManagedByLabel: consts.ControlPlaneManagedLabelValue, + consts.GatewayOperatorManagedByLabel: consts.ControlPlaneManagedLabelValue, + consts.GatewayOperatorManagedByNameLabel: controlplane.Name, }, ) require.NoError(t, err) @@ -56,12 +56,12 @@ func MustListControlPlaneClusterRoles(t *testing.T, ctx context.Context, control // MustListControlPlaneClusterRoleBindings is a helper function for tests that // conveniently lists all clusterrolebindings owned by a given controlplane. func MustListControlPlaneClusterRoleBindings(t *testing.T, ctx context.Context, controlplane *operatorv1beta1.ControlPlane, clients K8sClients) []rbacv1.ClusterRoleBinding { - clusterRoleBindings, err := k8sutils.ListClusterRoleBindingsForOwner( + clusterRoleBindings, err := k8sutils.ListClusterRoleBindings( ctx, clients.MgrClient, - controlplane.UID, client.MatchingLabels{ - consts.GatewayOperatorManagedByLabel: consts.ControlPlaneManagedLabelValue, + consts.GatewayOperatorManagedByLabel: consts.ControlPlaneManagedLabelValue, + consts.GatewayOperatorManagedByNameLabel: controlplane.Name, }, ) require.NoError(t, err) diff --git a/pkg/utils/test/predicates.go b/pkg/utils/test/predicates.go index 88d6c6f48..32170ca03 100644 --- a/pkg/utils/test/predicates.go +++ b/pkg/utils/test/predicates.go @@ -306,7 +306,7 @@ func ControlPlaneHasAdmissionWebhookCertificateSecret(t *testing.T, ctx context. // that can be used to check if a ControlPlane has an admission webhook configuration. func ControlPlaneHasAdmissionWebhookConfiguration(t *testing.T, ctx context.Context, cp *operatorv1beta1.ControlPlane, clients K8sClients) func() bool { return func() bool { - services, err := k8sutils.ListValidatingWebhookConfigurationsForOwner(ctx, clients.MgrClient, client.MatchingLabels{ + services, err := k8sutils.ListValidatingWebhookConfigurations(ctx, clients.MgrClient, client.MatchingLabels{ consts.GatewayOperatorManagedByLabel: consts.ControlPlaneManagedLabelValue, consts.GatewayOperatorManagedByNameLabel: cp.Name, })