diff --git a/controller/controlplane/controlplane_controller_reconciler_utils_test.go b/controller/controlplane/controlplane_controller_reconciler_utils_test.go index 9590ccfa6..58427548e 100644 --- a/controller/controlplane/controlplane_controller_reconciler_utils_test.go +++ b/controller/controlplane/controlplane_controller_reconciler_utils_test.go @@ -44,7 +44,36 @@ func TestEnsureClusterRole(t *testing.T) { wrongClusterRole2 := clusterRole.DeepCopy() wrongClusterRole2.ObjectMeta.Labels["aaa"] = "bbb" - clusterRole.ObjectMeta.Labels[consts.GatewayOperatorManagedByNameLabel] = "test-controlplane" + controlplane := operatorv1beta1.ControlPlane{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "gateway-operator.konghq.com/v1beta1", + Kind: "ControlPlane", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "test-controlplane", + Namespace: "test-namespace", + UID: types.UID(uuid.NewString()), + }, + Spec: operatorv1beta1.ControlPlaneSpec{ + ControlPlaneOptions: operatorv1beta1.ControlPlaneOptions{ + Deployment: operatorv1beta1.ControlPlaneDeploymentOptions{ + PodTemplateSpec: &corev1.PodTemplateSpec{ + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: consts.ControlPlaneControllerContainerName, + Image: consts.DefaultControlPlaneImage, + }, + }, + }, + }, + }, + }, + }, + } + + k8sutils.SetOwnerForObjectThroughLabels(clusterRole, &controlplane) + testCases := []struct { Name string controlplane operatorv1beta1.ControlPlane @@ -54,98 +83,20 @@ func TestEnsureClusterRole(t *testing.T) { err error }{ { - Name: "no existing clusterrole", - controlplane: operatorv1beta1.ControlPlane{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "gateway-operator.konghq.com/v1beta1", - Kind: "ControlPlane", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "test-controlplane", - Namespace: "test-namespace", - UID: types.UID(uuid.NewString()), - }, - Spec: operatorv1beta1.ControlPlaneSpec{ - ControlPlaneOptions: operatorv1beta1.ControlPlaneOptions{ - Deployment: operatorv1beta1.ControlPlaneDeploymentOptions{ - PodTemplateSpec: &corev1.PodTemplateSpec{ - Spec: corev1.PodSpec{ - Containers: []corev1.Container{ - { - Name: consts.ControlPlaneControllerContainerName, - Image: consts.DefaultControlPlaneImage, - }, - }, - }, - }, - }, - }, - }, - }, + Name: "no existing clusterrole", + controlplane: controlplane, createdorUpdated: true, expectedClusterRole: *clusterRole, }, { - Name: "up to date clusterrole", - controlplane: operatorv1beta1.ControlPlane{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "gateway-operator.konghq.com/v1beta1", - Kind: "ControlPlane", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "test-controlplane", - Namespace: "test-namespace", - UID: types.UID(uuid.NewString()), - }, - Spec: operatorv1beta1.ControlPlaneSpec{ - ControlPlaneOptions: operatorv1beta1.ControlPlaneOptions{ - Deployment: operatorv1beta1.ControlPlaneDeploymentOptions{ - PodTemplateSpec: &corev1.PodTemplateSpec{ - Spec: corev1.PodSpec{ - Containers: []corev1.Container{ - { - Name: consts.ControlPlaneControllerContainerName, - Image: consts.DefaultControlPlaneImage, - }, - }, - }, - }, - }, - }, - }, - }, + Name: "up to date clusterrole", + controlplane: controlplane, existingClusterRole: clusterRole, expectedClusterRole: *clusterRole, }, { - Name: "out of date clusterrole, object meta", - controlplane: operatorv1beta1.ControlPlane{ - TypeMeta: metav1.TypeMeta{ - APIVersion: "gateway-operator.konghq.com/v1beta1", - Kind: "ControlPlane", - }, - ObjectMeta: metav1.ObjectMeta{ - Name: "test-controlplane", - Namespace: "test-namespace", - UID: types.UID(uuid.NewString()), - }, - Spec: operatorv1beta1.ControlPlaneSpec{ - ControlPlaneOptions: operatorv1beta1.ControlPlaneOptions{ - Deployment: operatorv1beta1.ControlPlaneDeploymentOptions{ - PodTemplateSpec: &corev1.PodTemplateSpec{ - Spec: corev1.PodSpec{ - Containers: []corev1.Container{ - { - Name: consts.ControlPlaneControllerContainerName, - Image: consts.DefaultControlPlaneImage, - }, - }, - }, - }, - }, - }, - }, - }, + Name: "out of date clusterrole, object meta", + controlplane: controlplane, existingClusterRole: wrongClusterRole2, createdorUpdated: true, expectedClusterRole: *clusterRole, diff --git a/pkg/utils/test/asserts.go b/pkg/utils/test/asserts.go index 922b9b855..c53f36c4e 100644 --- a/pkg/utils/test/asserts.go +++ b/pkg/utils/test/asserts.go @@ -44,10 +44,7 @@ func MustListControlPlaneClusterRoles(t *testing.T, ctx context.Context, control clusterRoles, err := k8sutils.ListClusterRoles( ctx, clients.MgrClient, - client.MatchingLabels{ - consts.GatewayOperatorManagedByLabel: consts.ControlPlaneManagedLabelValue, - consts.GatewayOperatorManagedByNameLabel: controlplane.Name, - }, + client.MatchingLabels(k8sutils.GetManagedByLabelSet(controlplane)), ) require.NoError(t, err) return clusterRoles @@ -59,10 +56,7 @@ func MustListControlPlaneClusterRoleBindings(t *testing.T, ctx context.Context, clusterRoleBindings, err := k8sutils.ListClusterRoleBindings( ctx, clients.MgrClient, - client.MatchingLabels{ - consts.GatewayOperatorManagedByLabel: consts.ControlPlaneManagedLabelValue, - consts.GatewayOperatorManagedByNameLabel: controlplane.Name, - }, + client.MatchingLabels(k8sutils.GetManagedByLabelSet(controlplane)), ) require.NoError(t, err) return clusterRoleBindings