Search in sources :

Example 81 with Configuration

use of io.fabric8.maven.core.model.Configuration in project strimzi by strimzi.

the class KafkaAssemblyOperatorTest method deleteCluster.

private void deleteCluster(TestContext context, ConfigMap clusterCm) {
    ZookeeperCluster zookeeperCluster = ZookeeperCluster.fromConfigMap(clusterCm);
    KafkaCluster kafkaCluster = KafkaCluster.fromConfigMap(clusterCm);
    TopicController topicController = TopicController.fromConfigMap(clusterCm);
    // 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 clusterCmName = clusterCm.getMetadata().getName();
    String clusterCmNamespace = clusterCm.getMetadata().getNamespace();
    StatefulSet kafkaSs = kafkaCluster.generateStatefulSet(true);
    StatefulSet zkSs = zookeeperCluster.generateStatefulSet(true);
    when(mockKsOps.get(clusterCmNamespace, KafkaCluster.kafkaClusterName(clusterCmName))).thenReturn(kafkaSs);
    when(mockZsOps.get(clusterCmNamespace, ZookeeperCluster.zookeeperClusterName(clusterCmName))).thenReturn(zkSs);
    when(mockCmOps.get(clusterCmNamespace, clusterCmName)).thenReturn(clusterCm);
    ArgumentCaptor<String> serviceCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> ssCaptor = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String> metricsCaptor = ArgumentCaptor.forClass(String.class);
    when(mockCmOps.reconcile(eq(clusterCmNamespace), metricsCaptor.capture(), isNull())).thenReturn(Future.succeededFuture());
    when(mockServiceOps.reconcile(eq(clusterCmNamespace), serviceCaptor.capture(), isNull())).thenReturn(Future.succeededFuture());
    when(mockKsOps.reconcile(anyString(), ssCaptor.capture(), isNull())).thenReturn(Future.succeededFuture());
    when(mockZsOps.reconcile(anyString(), ssCaptor.capture(), isNull())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> pvcCaptor = ArgumentCaptor.forClass(String.class);
    when(mockPvcOps.reconcile(eq(clusterCmNamespace), pvcCaptor.capture(), isNull())).thenReturn(Future.succeededFuture());
    ArgumentCaptor<String> depCaptor = ArgumentCaptor.forClass(String.class);
    when(mockDepOps.reconcile(eq(clusterCmNamespace), depCaptor.capture(), isNull())).thenReturn(Future.succeededFuture());
    if (topicController != null) {
        Deployment tcDep = topicController.generateDeployment();
        when(mockDepOps.get(clusterCmNamespace, TopicController.topicControllerName(clusterCmName))).thenReturn(tcDep);
    }
    KafkaAssemblyOperator ops = new KafkaAssemblyOperator(vertx, openShift, ClusterControllerConfig.DEFAULT_OPERATION_TIMEOUT_MS, mockCmOps, mockServiceOps, mockZsOps, mockKsOps, mockPvcOps, mockDepOps);
    // Now try to delete a KafkaCluster based on this CM
    Async async = context.async();
    ops.delete(new Reconciliation("test-trigger", AssemblyType.KAFKA, clusterCmNamespace, clusterCmName), createResult -> {
        context.assertTrue(createResult.succeeded());
        /*Set<String> metricsNames = new HashSet<>();
            if (kafkaCluster.isMetricsEnabled()) {
                metricsNames.add(KafkaCluster.metricConfigsName(clusterCmName));
            }
            if (zookeeperCluster.isMetricsEnabled()) {
                metricsNames.add(ZookeeperCluster.zookeeperMetricsName(clusterCmName));
            }
            context.assertEquals(metricsNames, captured(metricsCaptor));*/
        verify(mockZsOps).reconcile(eq(clusterCmNamespace), eq(ZookeeperCluster.zookeeperClusterName(clusterCmName)), isNull());
        context.assertEquals(set(ZookeeperCluster.zookeeperHeadlessName(clusterCmName), ZookeeperCluster.zookeeperClusterName(clusterCmName), KafkaCluster.kafkaClusterName(clusterCmName), KafkaCluster.headlessName(clusterCmName)), captured(serviceCaptor));
        // verify deleted Statefulsets
        context.assertEquals(set(zookeeperCluster.getName(), kafkaCluster.getName()), captured(ssCaptor));
        // PvcOperations only used for deletion
        Set<String> expectedPvcDeletions = new HashSet<>();
        for (int i = 0; deleteClaim && i < kafkaCluster.getReplicas(); i++) {
            expectedPvcDeletions.add("data-" + clusterCmName + "-kafka-" + i);
        }
        for (int i = 0; deleteClaim && i < zookeeperCluster.getReplicas(); i++) {
            expectedPvcDeletions.add("data-" + clusterCmName + "-zookeeper-" + i);
        }
        context.assertEquals(expectedPvcDeletions, captured(pvcCaptor));
        // if topic controller configuration was defined in the CM
        if (topicController != null) {
            Set<String> expectedDepNames = new HashSet<>();
            expectedDepNames.add(TopicController.topicControllerName(clusterCmName));
            context.assertEquals(expectedDepNames, captured(depCaptor));
        }
        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) ZookeeperCluster(io.strimzi.controller.cluster.model.ZookeeperCluster) 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) 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) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) HashSet(java.util.HashSet)

Example 82 with Configuration

use of io.fabric8.maven.core.model.Configuration in project fabric8 by fabric8io.

the class KubernetesModelProcessorProcessor method process.

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    CompilationTaskFactory compilationTaskFactory = new CompilationTaskFactory(processingEnv);
    Set<TypeElement> processors = new HashSet<>();
    // 1st pass collect classes to compile.
    for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesModelProcessor.class)) {
        processors.add(getClassElement(element));
    }
    if (processors.isEmpty()) {
        return true;
    }
    StringWriter writer = new StringWriter();
    try {
        Callable<Boolean> compileTask = compilationTaskFactory.create(processors, writer);
        if (!compileTask.call()) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Failed to compile provider classes. See output below.");
            printCompileErrors(compilationTaskFactory);
            return false;
        }
    } catch (Exception e) {
        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error to compile provider classes, due to: " + e.getMessage() + ". See output below.");
        return false;
    } finally {
        String output = writer.toString();
        if (Strings.isNullOrBlank(output)) {
            output = "success";
        }
        processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE, "Fabric8 model generator compiler output:" + output);
    }
    // 2nd pass generate json.
    for (Element element : roundEnv.getElementsAnnotatedWith(KubernetesModelProcessor.class)) {
        KubernetesModelProcessor annotation = element.getAnnotation(KubernetesModelProcessor.class);
        String kubernetesJsonFileName = annotation.value();
        KubernetesResource json = readJson(kubernetesJsonFileName);
        Builder<? extends KubernetesResource> builder;
        if (json instanceof KubernetesList) {
            builder = new KubernetesListBuilder((KubernetesList) json);
        } else if (json instanceof Template) {
            builder = new TemplateBuilder((Template) json);
        } else if (json != null) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Unknown Kubernetes json type:" + json.getClass());
            return false;
        } else {
            return false;
        }
        try {
            if (element instanceof TypeElement) {
                for (ExecutableElement methodElement : ElementFilter.methodsIn(element.getEnclosedElements())) {
                    TypeElement classElement = getClassElement(element);
                    Class<?> cls = Class.forName(classElement.getQualifiedName().toString());
                    final Object instance = cls.newInstance();
                    final String methodName = methodElement.getSimpleName().toString();
                    if (builder instanceof Visitable) {
                        ((Visitable) builder).accept(new Visitor() {

                            @Override
                            public void visit(Object o) {
                                for (Method m : findMethods(instance, methodName, o.getClass())) {
                                    Named named = m.getAnnotation(Named.class);
                                    if (named != null && !Strings.isNullOrBlank(named.value())) {
                                        String objectName = getName(o);
                                        // If a name has been explicitly specified check if there is a match
                                        if (!named.value().equals(objectName)) {
                                            processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, "Named method:" + m.getName() + " with name:" + named.value() + " doesn't match: " + objectName + ", ignoring");
                                            return;
                                        }
                                    }
                                    try {
                                        m.invoke(instance, o);
                                    } catch (IllegalAccessException e) {
                                        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error invoking visitor method:" + m.getName() + " on:" + instance + "with argument:" + o);
                                    } catch (InvocationTargetException e) {
                                        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error invoking visitor method:" + m.getName() + " on:" + instance + "with argument:" + o);
                                    }
                                }
                            }
                        });
                    } else {
                        processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Json type is not visitable.");
                    }
                }
            }
            json = builder.build();
            generateJson(kubernetesJsonFileName, json);
        } catch (Exception ex) {
            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Error creating Kubernetes configuration:" + ex.getMessage());
        }
    }
    return true;
}
Also used : Visitor(io.fabric8.kubernetes.api.builder.Visitor) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) Element(javax.lang.model.element.Element) TemplateBuilder(io.fabric8.openshift.api.model.TemplateBuilder) ExecutableElement(javax.lang.model.element.ExecutableElement) Visitable(io.fabric8.kubernetes.api.builder.Visitable) KubernetesModelProcessor(io.fabric8.kubernetes.generator.annotation.KubernetesModelProcessor) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Template(io.fabric8.openshift.api.model.Template) StringWriter(java.io.StringWriter) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) Named(javax.inject.Named) TypeElement(javax.lang.model.element.TypeElement) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 83 with Configuration

use of io.fabric8.maven.core.model.Configuration in project fabric8 by fabric8io.

the class PipelineConfiguration method savePipelineConfiguration.

/**
 * Saves the {@link PipelineConfiguration} into a {@link ConfigMap} in the given namespace
 */
public static void savePipelineConfiguration(KubernetesClient kubernetesClient, String namespace, PipelineConfiguration configuration) {
    ConfigMap configMap = configuration.createConfigMap();
    kubernetesClient.configMaps().inNamespace(namespace).withName(FABRIC8_PIPELINES).createOrReplace(configMap);
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap)

Example 84 with Configuration

use of io.fabric8.maven.core.model.Configuration in project fabric8 by fabric8io.

the class OpenShiftPipelineTest method testPipelinesFromConfigMap.

@Test
public void testPipelinesFromConfigMap() throws Exception {
    String namespace = "myproject";
    final ConfigMap configMap = loadTestConfigMap();
    server.expect().withPath("/api/v1/namespaces/" + namespace + "/configmaps/" + FABRIC8_PIPELINES).andReturn(200, configMap).once();
    PipelineConfiguration configuration = PipelineConfiguration.loadPipelineConfiguration(getKubernetesClient(), namespace);
    assertJobName(configuration, "foo", "dummy", PipelineKind.CD);
    assertJobName(configuration, "bar", "dummy", PipelineKind.CI);
    assertJobName(configuration, "whatnot", "dummy", PipelineKind.Developer);
    assertJobName(configuration, "random", "master", "git@github.com:fabric8io/random.git", PipelineKind.CD);
    assertJobName(configuration, "random", "master", "https://github.com/fabric8io/random.git", PipelineKind.CD);
    assertJobName(configuration, "random", "not-master", "https://github.com/fabric8io/random.git", PipelineKind.Developer);
    assertJobName(configuration, "random", "master", "https://github.com/bar/foo.git", PipelineKind.Developer);
    assertJobName(configuration, "random", "master", "git@github.com:bar/foo.git", PipelineKind.Developer);
    // lets show we can opt out of CD pipelines for specific builds in an organisation if required
    assertJobName(configuration, "random", "master", "https://github.com/random/whatnot.git", PipelineKind.Developer);
    assertJobName(configuration, "random", "release", "https://github.com/random/whatnot.git", PipelineKind.CD);
    assertJobName(configuration, "random", "whatever", "https://github.com/random/whatnot.git", PipelineKind.CI);
}
Also used : PipelineConfigurationParseTest.loadTestConfigMap(io.fabric8.kubernetes.api.pipelines.PipelineConfigurationParseTest.loadTestConfigMap) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Test(org.junit.Test)

Example 85 with Configuration

use of io.fabric8.maven.core.model.Configuration in project fabric8 by fabric8io.

the class PipelineConfigurationParseTest method testLoadMutateAndLoadPipelineConfiguration.

@Test
public void testLoadMutateAndLoadPipelineConfiguration() throws Exception {
    ConfigMap configMap = loadTestConfigMap();
    PipelineConfiguration pipelineConfiguration = PipelineConfiguration.getPipelineConfiguration(configMap);
    pipelineConfiguration.setCdBranchPatterns(Arrays.asList("cd-1", "cd-2"));
    pipelineConfiguration.setCiBranchPatterns(Arrays.asList("ci-1", "ci-2"));
    pipelineConfiguration.setJobNameToKindMap(new HashMap<>());
    pipelineConfiguration.setJobNamesCD("job-cd");
    pipelineConfiguration.setJobNamesCI("job-ci");
    HashMap<String, List<String>> cdGitHostAndOrganisationToBranchPatterns = new HashMap<>();
    cdGitHostAndOrganisationToBranchPatterns.put("github.com/acme", Arrays.asList("release1"));
    cdGitHostAndOrganisationToBranchPatterns.put("github.com/another", Arrays.asList("release2"));
    pipelineConfiguration.setCdGitHostAndOrganisationToBranchPatterns(cdGitHostAndOrganisationToBranchPatterns);
    // reload the modified configuration
    configMap = pipelineConfiguration.createConfigMap();
    pipelineConfiguration = PipelineConfiguration.getPipelineConfiguration(configMap);
    assertThat(pipelineConfiguration.getCdBranchPatterns()).describedAs("getCdBranchPatterns()").contains("cd-1").contains("cd-2");
    assertThat(pipelineConfiguration.getCiBranchPatterns()).describedAs("getCiBranchPatterns()").contains("ci-1").contains("ci-2");
    assertThat(pipelineConfiguration.getJobNameToKindMap()).describedAs("getJobNameToKindMap()").containsEntry("job-cd", PipelineKind.CD).containsEntry("job-ci", PipelineKind.CI);
    assertThat(pipelineConfiguration.getCdGitHostAndOrganisationToBranchPatterns()).describedAs("getCdGitHostAndOrganisationToBranchPatterns()").containsEntry("github.com/acme", Arrays.asList("release1")).containsEntry("github.com/another", Arrays.asList("release2"));
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) HashMap(java.util.HashMap) List(java.util.List) Test(org.junit.Test)

Aggregations

ParallelTest (io.strimzi.test.annotations.ParallelTest)142 Kafka (io.strimzi.api.kafka.model.Kafka)138 KafkaBuilder (io.strimzi.api.kafka.model.KafkaBuilder)132 HashMap (java.util.HashMap)97 IOException (java.io.IOException)86 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)83 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)80 Map (java.util.Map)79 StatefulSet (io.fabric8.kubernetes.api.model.apps.StatefulSet)76 ArrayList (java.util.ArrayList)76 List (java.util.List)70 TopologySpreadConstraint (io.fabric8.kubernetes.api.model.TopologySpreadConstraint)66 GenericKafkaListenerBuilder (io.strimzi.api.kafka.model.listener.arraylistener.GenericKafkaListenerBuilder)66 Matchers.containsString (org.hamcrest.Matchers.containsString)58 Service (io.fabric8.kubernetes.api.model.Service)55 Collectors (java.util.stream.Collectors)55 Container (io.fabric8.kubernetes.api.model.Container)54 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)50 Quantity (io.fabric8.kubernetes.api.model.Quantity)48 File (java.io.File)47