use of io.strimzi.operator.common.operator.resource.CrdOperator in project strimzi-kafka-operator by strimzi.
the class KafkaAssemblyOperatorRbacScopeTest method testRolesDeployedWhenNamespaceRbacScopeAndMultiWatchNamespace.
/**
* This test checks that when STRIMZI_RBAC_SCOPE feature is set to 'NAMESPACE', the cluster operator
* binds to ClusterRoles when it can't use Roles due to cross namespace permissions
*/
@Test
public void testRolesDeployedWhenNamespaceRbacScopeAndMultiWatchNamespace(VertxTestContext context) {
Kafka kafka = new KafkaBuilder().withNewMetadata().withName(clusterName).withNamespace(namespace).endMetadata().withNewSpec().withNewKafka().withReplicas(3).endKafka().withNewZookeeper().withReplicas(3).endZookeeper().withNewEntityOperator().withNewUserOperator().withWatchedNamespace("other-ns").endUserOperator().withNewTopicOperator().withWatchedNamespace("another-ns").endTopicOperator().endEntityOperator().endSpec().build();
ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(false);
// Mock the CRD Operator for Kafka resources
CrdOperator mockKafkaOps = supplier.kafkaOperator;
when(mockKafkaOps.getAsync(eq(namespace), eq(clusterName))).thenReturn(Future.succeededFuture(kafka));
when(mockKafkaOps.get(eq(namespace), eq(clusterName))).thenReturn(kafka);
when(mockKafkaOps.updateStatusAsync(any(), any(Kafka.class))).thenReturn(Future.succeededFuture());
// Mock the operations for Roles
RoleOperator mockRoleOps = supplier.roleOperations;
// Capture the names of reconciled Roles and their patched state
ArgumentCaptor<String> roleNameCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Role> roleCaptor = ArgumentCaptor.forClass(Role.class);
when(mockRoleOps.reconcile(any(), anyString(), roleNameCaptor.capture(), roleCaptor.capture())).thenReturn(Future.succeededFuture());
// Mock the operations for RoleBindings
RoleBindingOperator mockRoleBindingOps = supplier.roleBindingOperations;
// Capture the names of reconciled RoleBindings and their patched state
ArgumentCaptor<String> roleBindingNameCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<RoleBinding> roleBindingCaptor = ArgumentCaptor.forClass(RoleBinding.class);
when(mockRoleBindingOps.reconcile(any(), anyString(), roleBindingNameCaptor.capture(), roleBindingCaptor.capture())).thenReturn(Future.succeededFuture());
KafkaAssemblyOperatorRolesSubset kao = new KafkaAssemblyOperatorRolesSubset(vertx, new PlatformFeaturesAvailability(false, kubernetesVersion), certManager, passwordGenerator, supplier, configNamespaceRbacScope);
Checkpoint async = context.checkpoint();
kao.reconcile(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, namespace, clusterName)).onComplete(context.succeeding(v -> context.verify(() -> {
List<String> roleBindingNames = roleBindingNameCaptor.getAllValues();
List<RoleBinding> roleBindings = roleBindingCaptor.getAllValues();
assertThat(roleBindingNames, hasSize(4));
assertThat(roleBindings, hasSize(4));
// Check all RoleBindings, easier to index by order applied
assertThat(roleBindingNames.get(0), is("test-instance-entity-topic-operator-role"));
assertThat(roleBindings.get(0).getMetadata().getNamespace(), is("another-ns"));
assertThat(roleBindings.get(0), hasRoleRef(new RoleRefBuilder().withApiGroup("rbac.authorization.k8s.io").withKind("Role").withName(EntityOperator.getRoleName(clusterName)).build()));
assertThat(roleBindingNames.get(1), is("test-instance-entity-topic-operator-role"));
assertThat(roleBindings.get(1).getMetadata().getNamespace(), is("test-ns"));
assertThat(roleBindings.get(1), hasRoleRef(new RoleRefBuilder().withApiGroup("rbac.authorization.k8s.io").withKind("Role").withName(EntityOperator.getRoleName(clusterName)).build()));
assertThat(roleBindingNames.get(2), is("test-instance-entity-user-operator-role"));
assertThat(roleBindings.get(2).getMetadata().getNamespace(), is("other-ns"));
assertThat(roleBindings.get(2), hasRoleRef(new RoleRefBuilder().withApiGroup("rbac.authorization.k8s.io").withKind("Role").withName(EntityOperator.getRoleName(clusterName)).build()));
assertThat(roleBindingNames.get(3), is("test-instance-entity-user-operator-role"));
assertThat(roleBindings.get(3).getMetadata().getNamespace(), is("test-ns"));
assertThat(roleBindings.get(3), hasRoleRef(new RoleRefBuilder().withApiGroup("rbac.authorization.k8s.io").withKind("Role").withName(EntityOperator.getRoleName(clusterName)).build()));
List<String> roleNames = roleNameCaptor.getAllValues();
List<Role> roles = roleCaptor.getAllValues();
assertThat(roleNames, hasSize(3));
assertThat(roles, hasSize(3));
// Check all Roles, easier to index by order applied
assertThat(roleNames.get(0), is("test-instance-entity-operator"));
assertThat(roles.get(0).getMetadata().getNamespace(), is("test-ns"));
assertThat(roleNames.get(1), is("test-instance-entity-operator"));
assertThat(roles.get(1).getMetadata().getNamespace(), is("other-ns"));
assertThat(roleNames.get(2), is("test-instance-entity-operator"));
assertThat(roles.get(2).getMetadata().getNamespace(), is("another-ns"));
async.flag();
})));
}
use of io.strimzi.operator.common.operator.resource.CrdOperator in project strimzi-kafka-operator by strimzi.
the class KafkaAssemblyOperatorRbacScopeTest method testRolesDeployedWhenClusterRbacScope.
/**
* This test checks that when STRIMZI_RBAC_SCOPE feature is set to 'CLUSTER', the cluster operator
* binds to ClusterRoles
*/
@Test
public void testRolesDeployedWhenClusterRbacScope(VertxTestContext context) {
Kafka kafka = new KafkaBuilder().withNewMetadata().withName(clusterName).withNamespace(namespace).endMetadata().withNewSpec().withNewKafka().withReplicas(3).endKafka().withNewZookeeper().withReplicas(3).endZookeeper().withNewEntityOperator().withNewUserOperator().endUserOperator().withNewTopicOperator().endTopicOperator().endEntityOperator().endSpec().build();
ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(false);
// Mock the CRD Operator for Kafka resources
CrdOperator mockKafkaOps = supplier.kafkaOperator;
when(mockKafkaOps.getAsync(eq(namespace), eq(clusterName))).thenReturn(Future.succeededFuture(kafka));
when(mockKafkaOps.get(eq(namespace), eq(clusterName))).thenReturn(kafka);
when(mockKafkaOps.updateStatusAsync(any(), any(Kafka.class))).thenReturn(Future.succeededFuture());
// Mock the operations for RoleBindings
RoleBindingOperator mockRoleBindingOps = supplier.roleBindingOperations;
// Capture the names of reconciled rolebindings and their patched state
ArgumentCaptor<String> roleBindingNameCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<RoleBinding> roleBindingCaptor = ArgumentCaptor.forClass(RoleBinding.class);
when(mockRoleBindingOps.reconcile(any(), eq(namespace), roleBindingNameCaptor.capture(), roleBindingCaptor.capture())).thenReturn(Future.succeededFuture());
KafkaAssemblyOperatorRolesSubset kao = new KafkaAssemblyOperatorRolesSubset(vertx, new PlatformFeaturesAvailability(false, kubernetesVersion), certManager, passwordGenerator, supplier, config);
Checkpoint async = context.checkpoint();
kao.reconcile(new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, namespace, clusterName)).onComplete(context.succeeding(v -> context.verify(() -> {
List<String> roleBindingNames = roleBindingNameCaptor.getAllValues();
List<RoleBinding> roleBindings = roleBindingCaptor.getAllValues();
assertThat(roleBindingNames, hasSize(2));
assertThat(roleBindings, hasSize(2));
// Check all RoleBindings, easier to index by order applied
assertThat(roleBindingNames.get(0), is("test-instance-entity-topic-operator-role"));
assertThat(roleBindings.get(0), hasRoleRef(new RoleRefBuilder().withApiGroup("rbac.authorization.k8s.io").withKind("Role").withName("test-instance-entity-operator").build()));
assertThat(roleBindingNames.get(1), is("test-instance-entity-user-operator-role"));
assertThat(roleBindings.get(1), hasRoleRef(new RoleRefBuilder().withApiGroup("rbac.authorization.k8s.io").withKind("Role").withName("test-instance-entity-operator").build()));
async.flag();
})));
}
use of io.strimzi.operator.common.operator.resource.CrdOperator in project strimzi by strimzi.
the class KafkaAssemblyOperatorIngressKafkaListenerTest method testIngressV1Beta1.
@Test
public void testIngressV1Beta1(VertxTestContext context) {
Kafka kafka = new KafkaBuilder().withNewMetadata().withName(NAME).withNamespace(NAMESPACE).endMetadata().withNewSpec().withNewKafka().withReplicas(3).withListeners(new GenericKafkaListenerBuilder().withName("ingress").withPort(9094).withTls(true).withType(KafkaListenerType.INGRESS).withNewConfiguration().withNewBootstrap().withHost("bootstrap.mydomain.tld").endBootstrap().withBrokers(new GenericKafkaListenerConfigurationBrokerBuilder().withBroker(0).withHost("broker-0.mydomain.tld").build(), new GenericKafkaListenerConfigurationBrokerBuilder().withBroker(1).withHost("broker-1.mydomain.tld").build(), new GenericKafkaListenerConfigurationBrokerBuilder().withBroker(2).withHost("broker-2.mydomain.tld").build()).endConfiguration().build()).withNewEphemeralStorage().endEphemeralStorage().endKafka().withNewZookeeper().withReplicas(3).withNewEphemeralStorage().endEphemeralStorage().endZookeeper().withNewEntityOperator().withNewUserOperator().endUserOperator().withNewTopicOperator().endTopicOperator().endEntityOperator().endSpec().build();
ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(false);
// Mock the CRD Operator for Kafka resources
CrdOperator mockKafkaOps = supplier.kafkaOperator;
when(mockKafkaOps.getAsync(eq(NAMESPACE), eq(NAME))).thenReturn(Future.succeededFuture(kafka));
when(mockKafkaOps.get(eq(NAMESPACE), eq(NAME))).thenReturn(kafka);
when(mockKafkaOps.updateStatusAsync(any(), any(Kafka.class))).thenReturn(Future.succeededFuture());
// Mock the KafkaSet operations
StatefulSetOperator mockStsOps = supplier.stsOperations;
when(mockStsOps.getAsync(eq(NAMESPACE), eq(KafkaCluster.kafkaClusterName(NAME)))).thenReturn(Future.succeededFuture());
// Mock the StrimziPodSet operator
CrdOperator<KubernetesClient, StrimziPodSet, StrimziPodSetList> mockPodSetOps = supplier.strimziPodSetOperator;
when(mockPodSetOps.getAsync(any(), any())).thenReturn(Future.succeededFuture(null));
// Mock the Pod operations
PodOperator mockPodOps = supplier.podOperations;
when(mockPodOps.listAsync(eq(NAMESPACE), any(Labels.class))).thenReturn(Future.succeededFuture(emptyList()));
// Mock ingress v1beta1 ops
IngressV1Beta1Operator mockIngressV1Beta1ops = supplier.ingressV1Beta1Operations;
ArgumentCaptor<io.fabric8.kubernetes.api.model.networking.v1beta1.Ingress> ingressV1Beta1Captor = ArgumentCaptor.forClass(io.fabric8.kubernetes.api.model.networking.v1beta1.Ingress.class);
when(mockIngressV1Beta1ops.listAsync(eq(NAMESPACE), any(Labels.class))).thenReturn(Future.succeededFuture(emptyList()));
when(mockIngressV1Beta1ops.reconcile(any(), anyString(), anyString(), ingressV1Beta1Captor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new io.fabric8.kubernetes.api.model.networking.v1beta1.Ingress())));
when(mockIngressV1Beta1ops.hasIngressAddress(any(), eq(NAMESPACE), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
// Mock ingress v1 ops
IngressOperator mockIngressOps = supplier.ingressOperations;
ArgumentCaptor<Ingress> ingressCaptor = ArgumentCaptor.forClass(Ingress.class);
when(mockIngressOps.listAsync(eq(NAMESPACE), any(Labels.class))).thenReturn(Future.succeededFuture(emptyList()));
when(mockIngressOps.reconcile(any(), anyString(), anyString(), ingressCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new Ingress())));
when(mockIngressOps.hasIngressAddress(any(), eq(NAMESPACE), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
KafkaAssemblyOperator op = new MockKafkaAssemblyOperatorForIngressTests(vertx, new PlatformFeaturesAvailability(false, KubernetesVersion.V1_16), certManager, passwordGenerator, supplier, ResourceUtils.dummyClusterOperatorConfig(KafkaVersionTestUtils.getKafkaVersionLookup()));
Reconciliation reconciliation = new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, NAME);
Checkpoint async = context.checkpoint();
op.reconcile(reconciliation).onComplete(context.succeeding(v -> context.verify(() -> {
assertThat(ingressCaptor.getAllValues().size(), is(0));
assertThat(ingressV1Beta1Captor.getAllValues().size(), is(4));
verify(mockIngressOps, never()).list(any(), any());
verify(mockIngressOps, never()).reconcile(any(), any(), any(), any());
verify(mockIngressOps, never()).hasIngressAddress(any(), any(), any(), anyLong(), anyLong());
async.flag();
})));
}
use of io.strimzi.operator.common.operator.resource.CrdOperator in project strimzi by strimzi.
the class KafkaAssemblyOperatorIngressKafkaListenerTest method testIngressV1.
@Test
public void testIngressV1(VertxTestContext context) {
Kafka kafka = new KafkaBuilder().withNewMetadata().withName(NAME).withNamespace(NAMESPACE).endMetadata().withNewSpec().withNewKafka().withReplicas(3).withListeners(new GenericKafkaListenerBuilder().withName("ingress").withPort(9094).withTls(true).withType(KafkaListenerType.INGRESS).withNewConfiguration().withNewBootstrap().withHost("bootstrap.mydomain.tld").endBootstrap().withBrokers(new GenericKafkaListenerConfigurationBrokerBuilder().withBroker(0).withHost("broker-0.mydomain.tld").build(), new GenericKafkaListenerConfigurationBrokerBuilder().withBroker(1).withHost("broker-1.mydomain.tld").build(), new GenericKafkaListenerConfigurationBrokerBuilder().withBroker(2).withHost("broker-2.mydomain.tld").build()).endConfiguration().build()).withNewEphemeralStorage().endEphemeralStorage().endKafka().withNewZookeeper().withReplicas(3).withNewEphemeralStorage().endEphemeralStorage().endZookeeper().withNewEntityOperator().withNewUserOperator().endUserOperator().withNewTopicOperator().endTopicOperator().endEntityOperator().endSpec().build();
ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(false);
// Mock the CRD Operator for Kafka resources
CrdOperator mockKafkaOps = supplier.kafkaOperator;
when(mockKafkaOps.getAsync(eq(NAMESPACE), eq(NAME))).thenReturn(Future.succeededFuture(kafka));
when(mockKafkaOps.get(eq(NAMESPACE), eq(NAME))).thenReturn(kafka);
when(mockKafkaOps.updateStatusAsync(any(), any(Kafka.class))).thenReturn(Future.succeededFuture());
// Mock the KafkaSet operations
StatefulSetOperator mockStsOps = supplier.stsOperations;
when(mockStsOps.getAsync(eq(NAMESPACE), eq(KafkaCluster.kafkaClusterName(NAME)))).thenReturn(Future.succeededFuture());
// Mock the StrimziPodSet operator
CrdOperator<KubernetesClient, StrimziPodSet, StrimziPodSetList> mockPodSetOps = supplier.strimziPodSetOperator;
when(mockPodSetOps.getAsync(any(), any())).thenReturn(Future.succeededFuture(null));
// Mock the Pod operations
PodOperator mockPodOps = supplier.podOperations;
when(mockPodOps.listAsync(eq(NAMESPACE), any(Labels.class))).thenReturn(Future.succeededFuture(emptyList()));
// Mock ingress v1beta1 ops
IngressV1Beta1Operator mockIngressV1Beta1ops = supplier.ingressV1Beta1Operations;
ArgumentCaptor<io.fabric8.kubernetes.api.model.networking.v1beta1.Ingress> ingressV1Beta1Captor = ArgumentCaptor.forClass(io.fabric8.kubernetes.api.model.networking.v1beta1.Ingress.class);
when(mockIngressV1Beta1ops.listAsync(eq(NAMESPACE), any(Labels.class))).thenReturn(Future.succeededFuture(emptyList()));
when(mockIngressV1Beta1ops.reconcile(any(), anyString(), anyString(), ingressV1Beta1Captor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new io.fabric8.kubernetes.api.model.networking.v1beta1.Ingress())));
when(mockIngressV1Beta1ops.hasIngressAddress(any(), eq(NAMESPACE), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
// Mock ingress v1 ops
IngressOperator mockIngressOps = supplier.ingressOperations;
ArgumentCaptor<Ingress> ingressCaptor = ArgumentCaptor.forClass(Ingress.class);
when(mockIngressOps.listAsync(eq(NAMESPACE), any(Labels.class))).thenReturn(Future.succeededFuture(emptyList()));
when(mockIngressOps.reconcile(any(), anyString(), anyString(), ingressCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created(new Ingress())));
when(mockIngressOps.hasIngressAddress(any(), eq(NAMESPACE), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
KafkaAssemblyOperator op = new MockKafkaAssemblyOperatorForIngressTests(vertx, new PlatformFeaturesAvailability(false, KubernetesVersion.V1_19), certManager, passwordGenerator, supplier, ResourceUtils.dummyClusterOperatorConfig(KafkaVersionTestUtils.getKafkaVersionLookup()));
Reconciliation reconciliation = new Reconciliation("test-trigger", Kafka.RESOURCE_KIND, NAMESPACE, NAME);
Checkpoint async = context.checkpoint();
op.reconcile(reconciliation).onComplete(context.succeeding(v -> context.verify(() -> {
assertThat(ingressCaptor.getAllValues().size(), is(4));
assertThat(ingressV1Beta1Captor.getAllValues().size(), is(0));
verify(mockIngressV1Beta1ops, never()).list(any(), any());
verify(mockIngressV1Beta1ops, never()).reconcile(any(), any(), any(), any());
verify(mockIngressV1Beta1ops, never()).hasIngressAddress(any(), any(), any(), anyLong(), anyLong());
async.flag();
})));
}
use of io.strimzi.operator.common.operator.resource.CrdOperator in project strimzi by strimzi.
the class KafkaAssemblyOperatorLoadBalancerKafkaListenerTest method prepareResourceOperatorSupplier.
private ResourceOperatorSupplier prepareResourceOperatorSupplier(Kafka kafka) {
ResourceOperatorSupplier supplier = ResourceUtils.supplierWithMocks(false);
// Mock the CRD Operator for Kafka resources
CrdOperator mockKafkaOps = supplier.kafkaOperator;
when(mockKafkaOps.getAsync(eq(NAMESPACE), eq(NAME))).thenReturn(Future.succeededFuture(kafka));
when(mockKafkaOps.get(eq(NAMESPACE), eq(NAME))).thenReturn(kafka);
when(mockKafkaOps.updateStatusAsync(any(), any(Kafka.class))).thenReturn(Future.succeededFuture());
// Mock the KafkaSet operations
StatefulSetOperator mockStsOps = supplier.stsOperations;
when(mockStsOps.getAsync(eq(NAMESPACE), eq(KafkaCluster.kafkaClusterName(NAME)))).thenReturn(Future.succeededFuture());
// Mock the StrimziPodSet operator
CrdOperator<KubernetesClient, StrimziPodSet, StrimziPodSetList> mockPodSetOps = supplier.strimziPodSetOperator;
when(mockPodSetOps.getAsync(any(), any())).thenReturn(Future.succeededFuture(null));
// Mock the Pod operations
PodOperator mockPodOps = supplier.podOperations;
when(mockPodOps.listAsync(eq(NAMESPACE), any(Labels.class))).thenReturn(Future.succeededFuture(emptyList()));
// Mock the Services for the kafka.
Service mockServiceBootstrap = mock(Service.class, RETURNS_DEEP_STUBS);
Service mockServiceBroker0 = mock(Service.class, RETURNS_DEEP_STUBS);
Service mockServiceBroker1 = mock(Service.class, RETURNS_DEEP_STUBS);
Service mockServiceBroker2 = mock(Service.class, RETURNS_DEEP_STUBS);
when(mockServiceBootstrap.getStatus().getLoadBalancer().getIngress().get(0).getHostname()).thenReturn(DNS_NAME_FOR_BOOTSTRAP_SERVICE);
when(mockServiceBroker0.getStatus().getLoadBalancer().getIngress().get(0).getHostname()).thenReturn(DNS_NAME_FOR_BROKER_0);
when(mockServiceBroker1.getStatus().getLoadBalancer().getIngress().get(0).getHostname()).thenReturn(DNS_NAME_FOR_BROKER_1);
when(mockServiceBroker2.getStatus().getLoadBalancer().getIngress().get(0).getHostname()).thenReturn(DNS_NAME_FOR_BROKER_2);
// Mock the ServiceOperator for the kafka services.
ServiceOperator mockServiceOperator = supplier.serviceOperations;
when(mockServiceOperator.getAsync(NAMESPACE, NAME + "-kafka-external-bootstrap")).thenReturn(Future.succeededFuture(mockServiceBootstrap));
when(mockServiceOperator.getAsync(NAMESPACE, NAME + "-kafka-0")).thenReturn(Future.succeededFuture(mockServiceBroker0));
when(mockServiceOperator.getAsync(NAMESPACE, NAME + "-kafka-1")).thenReturn(Future.succeededFuture(mockServiceBroker1));
when(mockServiceOperator.getAsync(NAMESPACE, NAME + "-kafka-2")).thenReturn(Future.succeededFuture(mockServiceBroker2));
return supplier;
}
Aggregations