Search in sources :

Example 11 with DeploymentOperator

use of io.strimzi.controller.cluster.operator.resource.DeploymentOperator in project strimzi by strimzi.

the class KafkaConnectAssemblyOperatorTest method testUpdateClusterNoDiff.

@Test
public void testUpdateClusterNoDiff(TestContext context) {
    ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
    ServiceOperator mockServiceOps = mock(ServiceOperator.class);
    DeploymentOperator mockDcOps = mock(DeploymentOperator.class);
    String clusterCmName = "foo";
    String clusterCmNamespace = "test";
    ConfigMap clusterCm = ResourceUtils.createEmptyKafkaConnectClusterConfigMap(clusterCmNamespace, clusterCmName);
    KafkaConnectCluster connect = KafkaConnectCluster.fromConfigMap(clusterCm);
    when(mockCmOps.get(clusterCmNamespace, clusterCmName)).thenReturn(clusterCm);
    when(mockServiceOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateService());
    when(mockDcOps.get(clusterCmNamespace, connect.getName())).thenReturn(connect.generateDeployment());
    ArgumentCaptor<String> serviceNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Service> serviceCaptor = ArgumentCaptor.forClass(Service.class);
    when(mockServiceOps.reconcile(eq(clusterCmNamespace), serviceNameCaptor.capture(), serviceCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> dcNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Deployment> dcCaptor = ArgumentCaptor.forClass(Deployment.class);
    when(mockDcOps.reconcile(eq(clusterCmNamespace), dcNameCaptor.capture(), dcCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> dcScaleUpNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Integer> dcScaleUpReplicasCaptor = ArgumentCaptor.forClass(Integer.class);
    when(mockDcOps.scaleUp(eq(clusterCmNamespace), dcScaleUpNameCaptor.capture(), dcScaleUpReplicasCaptor.capture())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> dcScaleDownNameCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<Integer> dcScaleDownReplicasCaptor = ArgumentCaptor.forClass(Integer.class);
    when(mockDcOps.scaleDown(eq(clusterCmNamespace), dcScaleDownNameCaptor.capture(), dcScaleDownReplicasCaptor.capture())).thenReturn(Future.succeededFuture());
    KafkaConnectAssemblyOperator ops = new KafkaConnectAssemblyOperator(vertx, true, mockCmOps, mockDcOps, mockServiceOps);
    Async async = context.async();
    ops.createOrUpdate(new Reconciliation("test-trigger", AssemblyType.CONNECT, clusterCmNamespace, clusterCmName), clusterCm, createResult -> {
        context.assertTrue(createResult.succeeded());
        // Vertify service
        List<Service> capturedServices = serviceCaptor.getAllValues();
        context.assertEquals(1, capturedServices.size());
        // Verify Deployment Config
        List<Deployment> capturedDc = dcCaptor.getAllValues();
        context.assertEquals(1, capturedDc.size());
        // Verify scaleDown / scaleUp were not called
        context.assertEquals(1, dcScaleDownNameCaptor.getAllValues().size());
        context.assertEquals(1, dcScaleUpNameCaptor.getAllValues().size());
        async.complete();
    });
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Service(io.fabric8.kubernetes.api.model.Service) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) KafkaConnectCluster(io.strimzi.controller.cluster.model.KafkaConnectCluster) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ServiceOperator(io.strimzi.controller.cluster.operator.resource.ServiceOperator) Async(io.vertx.ext.unit.Async) Reconciliation(io.strimzi.controller.cluster.Reconciliation) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) DeploymentOperator(io.strimzi.controller.cluster.operator.resource.DeploymentOperator) Test(org.junit.Test)

Example 12 with DeploymentOperator

use of io.strimzi.controller.cluster.operator.resource.DeploymentOperator in project strimzi by strimzi.

the class KafkaAssemblyOperatorTest method createCluster.

private void createCluster(TestContext context, ConfigMap clusterCm) {
    // create CM, Service, headless service, statefulset and so on
    ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
    ServiceOperator mockServiceOps = mock(ServiceOperator.class);
    ZookeeperSetOperator mockZsOps = mock(ZookeeperSetOperator.class);
    KafkaSetOperator mockKsOps = mock(KafkaSetOperator.class);
    PvcOperator mockPvcOps = mock(PvcOperator.class);
    DeploymentOperator mockDepOps = mock(DeploymentOperator.class);
    // Create a CM
    String clusterCmName = clusterCm.getMetadata().getName();
    String clusterCmNamespace = clusterCm.getMetadata().getNamespace();
    when(mockCmOps.get(clusterCmNamespace, clusterCmName)).thenReturn(clusterCm);
    ArgumentCaptor<Service> serviceCaptor = ArgumentCaptor.forClass(Service.class);
    when(mockServiceOps.reconcile(anyString(), anyString(), serviceCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created()));
    when(mockServiceOps.endpointReadiness(anyString(), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<StatefulSet> ssCaptor = ArgumentCaptor.forClass(StatefulSet.class);
    when(mockZsOps.reconcile(anyString(), anyString(), ssCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created()));
    when(mockZsOps.scaleDown(anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(null));
    when(mockZsOps.rollingUpdate(anyString(), anyString())).thenReturn(Future.succeededFuture());
    when(mockZsOps.scaleUp(anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
    when(mockKsOps.reconcile(anyString(), anyString(), ssCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created()));
    when(mockKsOps.scaleDown(anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(null));
    when(mockKsOps.rollingUpdate(anyString(), anyString())).thenReturn(Future.succeededFuture());
    when(mockKsOps.scaleUp(anyString(), anyString(), anyInt())).thenReturn(Future.succeededFuture(42));
    ArgumentCaptor<Deployment> depCaptor = ArgumentCaptor.forClass(Deployment.class);
    when(mockDepOps.reconcile(anyString(), anyString(), depCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created()));
    // when(mockSsOps.readiness(any(), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    // when(mockPodOps.readiness(any(), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    // when(mockEndpointOps.readiness(any(), any(), anyLong(), anyLong())).thenReturn(Future.succeededFuture());
    KafkaCluster kafkaCluster = KafkaCluster.fromConfigMap(clusterCm);
    ZookeeperCluster zookeeperCluster = ZookeeperCluster.fromConfigMap(clusterCm);
    TopicController topicController = TopicController.fromConfigMap(clusterCm);
    ArgumentCaptor<ConfigMap> metricsCaptor = ArgumentCaptor.forClass(ConfigMap.class);
    ArgumentCaptor<String> metricsNameCaptor = ArgumentCaptor.forClass(String.class);
    when(mockCmOps.reconcile(anyString(), metricsNameCaptor.capture(), metricsCaptor.capture())).thenReturn(Future.succeededFuture(ReconcileResult.created()));
    KafkaAssemblyOperator ops = new KafkaAssemblyOperator(vertx, openShift, ClusterControllerConfig.DEFAULT_OPERATION_TIMEOUT_MS, mockCmOps, mockServiceOps, mockZsOps, mockKsOps, mockPvcOps, mockDepOps);
    // Now try to create a KafkaCluster based on this CM
    Async async = context.async();
    ops.createOrUpdate(new Reconciliation("test-trigger", AssemblyType.KAFKA, clusterCmNamespace, clusterCmName), clusterCm, createResult -> {
        if (createResult.failed()) {
            createResult.cause().printStackTrace();
        }
        context.assertTrue(createResult.succeeded());
        // No metrics config  => no CMs created
        Set<String> metricsNames = new HashSet<>();
        if (kafkaCluster.isMetricsEnabled()) {
            metricsNames.add(KafkaCluster.metricConfigsName(clusterCmName));
        }
        if (zookeeperCluster.isMetricsEnabled()) {
            metricsNames.add(ZookeeperCluster.zookeeperMetricsName(clusterCmName));
        }
        /*
            Map<String, ConfigMap> cmsByName = new HashMap<>();
            Iterator<ConfigMap> it2 = metricsCaptor.getAllValues().iterator();
            for (Iterator<String> it = metricsNameCaptor.getAllValues().iterator(); it.hasNext(); ) {
                cmsByName.put(it.next(), it2.next());
            }
            context.assertEquals(metricsNames, cmsByName.keySet(),
                    "Unexpected metrics ConfigMaps");
            if (kafkaCluster.isMetricsEnabled()) {
                ConfigMap kafkaMetricsCm = cmsByName.get(KafkaCluster.metricConfigsName(clusterCmName));
                context.assertEquals(ResourceUtils.labels(Labels.STRIMZI_TYPE_LABEL, "kafka",
                        Labels.STRIMZI_CLUSTER_LABEL, clusterCmName,
                        "my-user-label", "cromulent"), kafkaMetricsCm.getMetadata().getLabels());
            }
            if (zookeeperCluster.isMetricsEnabled()) {
                ConfigMap zookeeperMetricsCm = cmsByName.get(ZookeeperCluster.zookeeperMetricsName(clusterCmName));
                context.assertEquals(ResourceUtils.labels(Labels.STRIMZI_TYPE_LABEL, "zookeeper",
                        Labels.STRIMZI_CLUSTER_LABEL, clusterCmName,
                        "my-user-label", "cromulent"), zookeeperMetricsCm.getMetadata().getLabels());
            }*/
        // We expect a headless and headful service
        List<Service> capturedServices = serviceCaptor.getAllValues();
        context.assertEquals(4, capturedServices.size());
        context.assertEquals(set(KafkaCluster.kafkaClusterName(clusterCmName), KafkaCluster.headlessName(clusterCmName), ZookeeperCluster.zookeeperClusterName(clusterCmName), ZookeeperCluster.zookeeperHeadlessName(clusterCmName)), capturedServices.stream().map(svc -> svc.getMetadata().getName()).collect(Collectors.toSet()));
        // Assertions on the statefulset
        List<StatefulSet> capturedSs = ssCaptor.getAllValues();
        // We expect a statefulSet for kafka and zookeeper...
        context.assertEquals(set(KafkaCluster.kafkaClusterName(clusterCmName), ZookeeperCluster.zookeeperClusterName(clusterCmName)), capturedSs.stream().map(ss -> ss.getMetadata().getName()).collect(Collectors.toSet()));
        // if topic controller configuration was defined in the CM
        if (topicController != null) {
            List<Deployment> capturedDeps = depCaptor.getAllValues();
            context.assertEquals(1, capturedDeps.size());
            context.assertEquals(TopicController.topicControllerName(clusterCmName), capturedDeps.get(0).getMetadata().getName());
        }
        // PvcOperations only used for deletion
        verifyNoMoreInteractions(mockPvcOps);
        async.complete();
    });
}
Also used : KafkaCluster(io.strimzi.controller.cluster.model.KafkaCluster) ZookeeperSetOperator(io.strimzi.controller.cluster.operator.resource.ZookeeperSetOperator) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PvcOperator(io.strimzi.controller.cluster.operator.resource.PvcOperator) ServiceOperator(io.strimzi.controller.cluster.operator.resource.ServiceOperator) TopicController(io.strimzi.controller.cluster.model.TopicController) Async(io.vertx.ext.unit.Async) Reconciliation(io.strimzi.controller.cluster.Reconciliation) DeploymentOperator(io.strimzi.controller.cluster.operator.resource.DeploymentOperator) HashSet(java.util.HashSet) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Service(io.fabric8.kubernetes.api.model.Service) ZookeeperCluster(io.strimzi.controller.cluster.model.ZookeeperCluster) KafkaSetOperator(io.strimzi.controller.cluster.operator.resource.KafkaSetOperator) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet)

Example 13 with DeploymentOperator

use of io.strimzi.controller.cluster.operator.resource.DeploymentOperator in project strimzi by strimzi.

the class KafkaAssemblyOperatorTest method testReconcile.

@Test
public void testReconcile(TestContext context) {
    Async async = context.async(3);
    // create CM, Service, headless service, statefulset
    ConfigMapOperator mockCmOps = mock(ConfigMapOperator.class);
    ServiceOperator mockServiceOps = mock(ServiceOperator.class);
    ZookeeperSetOperator mockZsOps = mock(ZookeeperSetOperator.class);
    KafkaSetOperator mockKsOps = mock(KafkaSetOperator.class);
    PvcOperator mockPvcOps = mock(PvcOperator.class);
    DeploymentOperator mockDepOps = mock(DeploymentOperator.class);
    String clusterCmNamespace = "myNamespace";
    ConfigMap foo = getConfigMap("foo");
    ConfigMap bar = getConfigMap("bar");
    ConfigMap baz = getConfigMap("baz");
    when(mockCmOps.list(eq(clusterCmNamespace), any())).thenReturn(asList(foo, bar));
    // when requested ConfigMap for a specific Kafka cluster
    when(mockCmOps.get(eq(clusterCmNamespace), eq("foo"))).thenReturn(foo);
    when(mockCmOps.get(eq(clusterCmNamespace), eq("bar"))).thenReturn(bar);
    // providing the list of ALL StatefulSets for all the Kafka clusters
    Labels newLabels = Labels.forType(AssemblyType.KAFKA);
    when(mockKsOps.list(eq(clusterCmNamespace), eq(newLabels))).thenReturn(asList(KafkaCluster.fromConfigMap(bar).generateStatefulSet(openShift), KafkaCluster.fromConfigMap(baz).generateStatefulSet(openShift)));
    // providing the list StatefulSets for already "existing" Kafka clusters
    Labels barLabels = Labels.forCluster("bar");
    when(mockKsOps.list(eq(clusterCmNamespace), eq(barLabels))).thenReturn(asList(KafkaCluster.fromConfigMap(bar).generateStatefulSet(openShift)));
    Labels bazLabels = Labels.forCluster("baz");
    when(mockKsOps.list(eq(clusterCmNamespace), eq(bazLabels))).thenReturn(asList(KafkaCluster.fromConfigMap(baz).generateStatefulSet(openShift)));
    Set<String> createdOrUpdated = new HashSet<>();
    Set<String> deleted = new HashSet<>();
    KafkaAssemblyOperator ops = new KafkaAssemblyOperator(vertx, openShift, ClusterControllerConfig.DEFAULT_OPERATION_TIMEOUT_MS, mockCmOps, mockServiceOps, mockZsOps, mockKsOps, mockPvcOps, mockDepOps) {

        @Override
        public void createOrUpdate(Reconciliation reconciliation, ConfigMap assemblyCm, Handler h) {
            createdOrUpdated.add(assemblyCm.getMetadata().getName());
            async.countDown();
            h.handle(Future.succeededFuture());
        }

        @Override
        public void delete(Reconciliation reconciliation, Handler h) {
            deleted.add(reconciliation.assemblyName());
            async.countDown();
            h.handle(Future.succeededFuture());
        }
    };
    // Now try to reconcile all the Kafka clusters
    ops.reconcileAll("test", clusterCmNamespace, Labels.EMPTY);
    async.await();
    context.assertEquals(new HashSet(asList("foo", "bar")), createdOrUpdated);
    context.assertEquals(singleton("baz"), deleted);
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ZookeeperSetOperator(io.strimzi.controller.cluster.operator.resource.ZookeeperSetOperator) Handler(io.vertx.core.Handler) Labels(io.strimzi.controller.cluster.model.Labels) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PvcOperator(io.strimzi.controller.cluster.operator.resource.PvcOperator) ServiceOperator(io.strimzi.controller.cluster.operator.resource.ServiceOperator) Async(io.vertx.ext.unit.Async) KafkaSetOperator(io.strimzi.controller.cluster.operator.resource.KafkaSetOperator) Reconciliation(io.strimzi.controller.cluster.Reconciliation) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) DeploymentOperator(io.strimzi.controller.cluster.operator.resource.DeploymentOperator) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 14 with DeploymentOperator

use of io.strimzi.controller.cluster.operator.resource.DeploymentOperator in project strimzi by strimzi.

the class Main method run.

static CompositeFuture run(Vertx vertx, KubernetesClient client, boolean isOpenShift, Map<String, String> env) {
    ClusterControllerConfig config = ClusterControllerConfig.fromMap(env);
    ServiceOperator serviceOperations = new ServiceOperator(vertx, client);
    ZookeeperSetOperator zookeeperSetOperations = new ZookeeperSetOperator(vertx, client);
    KafkaSetOperator kafkaSetOperations = new KafkaSetOperator(vertx, client);
    ConfigMapOperator configMapOperations = new ConfigMapOperator(vertx, client);
    PvcOperator pvcOperations = new PvcOperator(vertx, client);
    DeploymentOperator deploymentOperations = new DeploymentOperator(vertx, client);
    KafkaAssemblyOperator kafkaClusterOperations = new KafkaAssemblyOperator(vertx, isOpenShift, config.getOperationTimeoutMs(), configMapOperations, serviceOperations, zookeeperSetOperations, kafkaSetOperations, pvcOperations, deploymentOperations);
    KafkaConnectAssemblyOperator kafkaConnectClusterOperations = new KafkaConnectAssemblyOperator(vertx, isOpenShift, configMapOperations, deploymentOperations, serviceOperations);
    DeploymentConfigOperator deploymentConfigOperations = null;
    ImageStreamOperator imagesStreamOperations = null;
    BuildConfigOperator buildConfigOperations = null;
    KafkaConnectS2IAssemblyOperator kafkaConnectS2IClusterOperations = null;
    if (isOpenShift) {
        imagesStreamOperations = new ImageStreamOperator(vertx, client.adapt(OpenShiftClient.class));
        buildConfigOperations = new BuildConfigOperator(vertx, client.adapt(OpenShiftClient.class));
        deploymentConfigOperations = new DeploymentConfigOperator(vertx, client.adapt(OpenShiftClient.class));
        kafkaConnectS2IClusterOperations = new KafkaConnectS2IAssemblyOperator(vertx, isOpenShift, configMapOperations, deploymentConfigOperations, serviceOperations, imagesStreamOperations, buildConfigOperations);
    }
    List<Future> futures = new ArrayList<>();
    for (String namespace : config.getNamespaces()) {
        Future<String> fut = Future.future();
        futures.add(fut);
        ClusterController controller = new ClusterController(namespace, config.getReconciliationIntervalMs(), client, kafkaClusterOperations, kafkaConnectClusterOperations, kafkaConnectS2IClusterOperations);
        vertx.deployVerticle(controller, res -> {
            if (res.succeeded()) {
                log.info("Cluster Controller verticle started in namespace {}", namespace);
            } else {
                log.error("Cluster Controller verticle in namespace {} failed to start", namespace, res.cause());
                System.exit(1);
            }
            fut.completer().handle(res);
        });
    }
    return CompositeFuture.join(futures);
}
Also used : KafkaAssemblyOperator(io.strimzi.controller.cluster.operator.assembly.KafkaAssemblyOperator) ZookeeperSetOperator(io.strimzi.controller.cluster.operator.resource.ZookeeperSetOperator) KafkaConnectS2IAssemblyOperator(io.strimzi.controller.cluster.operator.assembly.KafkaConnectS2IAssemblyOperator) ArrayList(java.util.ArrayList) PvcOperator(io.strimzi.controller.cluster.operator.resource.PvcOperator) ServiceOperator(io.strimzi.controller.cluster.operator.resource.ServiceOperator) KafkaConnectAssemblyOperator(io.strimzi.controller.cluster.operator.assembly.KafkaConnectAssemblyOperator) ImageStreamOperator(io.strimzi.controller.cluster.operator.resource.ImageStreamOperator) BuildConfigOperator(io.strimzi.controller.cluster.operator.resource.BuildConfigOperator) KafkaSetOperator(io.strimzi.controller.cluster.operator.resource.KafkaSetOperator) CompositeFuture(io.vertx.core.CompositeFuture) Future(io.vertx.core.Future) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) DeploymentOperator(io.strimzi.controller.cluster.operator.resource.DeploymentOperator) DeploymentConfigOperator(io.strimzi.controller.cluster.operator.resource.DeploymentConfigOperator)

Aggregations

ConfigMapOperator (io.strimzi.controller.cluster.operator.resource.ConfigMapOperator)14 DeploymentOperator (io.strimzi.controller.cluster.operator.resource.DeploymentOperator)14 ServiceOperator (io.strimzi.controller.cluster.operator.resource.ServiceOperator)14 Reconciliation (io.strimzi.controller.cluster.Reconciliation)13 Async (io.vertx.ext.unit.Async)13 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)10 Test (org.junit.Test)9 KafkaConnectCluster (io.strimzi.controller.cluster.model.KafkaConnectCluster)7 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)6 KafkaSetOperator (io.strimzi.controller.cluster.operator.resource.KafkaSetOperator)6 PvcOperator (io.strimzi.controller.cluster.operator.resource.PvcOperator)6 ZookeeperSetOperator (io.strimzi.controller.cluster.operator.resource.ZookeeperSetOperator)6 Service (io.fabric8.kubernetes.api.model.Service)5 HashSet (java.util.HashSet)4 KafkaCluster (io.strimzi.controller.cluster.model.KafkaCluster)3 TopicController (io.strimzi.controller.cluster.model.TopicController)3 ZookeeperCluster (io.strimzi.controller.cluster.model.ZookeeperCluster)3 StatefulSet (io.fabric8.kubernetes.api.model.extensions.StatefulSet)2 Labels (io.strimzi.controller.cluster.model.Labels)2