Skip to content

Commit

Permalink
fix: helpers adjusted to labels
Browse files Browse the repository at this point in the history
Signed-off-by: Mattia Lavacca <[email protected]>
  • Loading branch information
mlavacca committed May 15, 2024
1 parent 51eb2a8 commit b77d3e1
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 31 deletions.
16 changes: 6 additions & 10 deletions controller/controlplane/controller_reconciler_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
22 changes: 9 additions & 13 deletions pkg/utils/kubernetes/lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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{}
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/kubernetes/lists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 6 additions & 6 deletions pkg/utils/test/asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/test/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
Expand Down

0 comments on commit b77d3e1

Please sign in to comment.