Search in sources :

Example 1 with Configuration

use of org.eclipse.jkube.kit.enricher.api.model.Configuration in project jkube by eclipse.

the class AutoTLSEnricherTest method testAdapt.

@Test
public void testAdapt() {
    final AdaptTestConfig[] data = new AdaptTestConfig[] { AdaptTestConfig.builder().mode(RuntimeMode.KUBERNETES).build(), new AdaptTestConfig(RuntimeMode.OPENSHIFT, null, "tls-jks-converter", null, "jimmidyson/pemtokeystore:v0.1.0", null, "tls-pem", null, "tls-jks"), new AdaptTestConfig(RuntimeMode.OPENSHIFT, null, "tls-jks-converter", null, "jimmidyson/pemtokeystore:v0.1.0", "tls-a", "tls-a", null, "tls-jks"), new AdaptTestConfig(RuntimeMode.OPENSHIFT, null, "tls-jks-converter", null, "jimmidyson/pemtokeystore:v0.1.0", null, "tls-pem", "jks-b", "jks-b"), new AdaptTestConfig(RuntimeMode.OPENSHIFT, "test-container-name", "test-container-name", "image/123", "image/123", "tls-a", "tls-a", "jks-b", "jks-b") };
    for (final AdaptTestConfig tc : data) {
        TreeMap<String, Object> configMap = new TreeMap<>();
        configMap.put("pemToJKSInitContainerName", tc.initContainerNameConfig);
        configMap.put("pemToJKSInitContainerImage", tc.initContainerImageConfig);
        configMap.put("tlsSecretVolumeName", tc.tlsSecretVolumeNameConfig);
        configMap.put("jksVolumeName", tc.jksVolumeNameConfig);
        final ProcessorConfig config = new ProcessorConfig(null, null, Collections.singletonMap("jkube-openshift-autotls", configMap));
        final Properties properties = new Properties();
        properties.put(RuntimeMode.JKUBE_EFFECTIVE_PLATFORM_MODE, tc.mode.name());
        // @formatter:off
        new Expectations() {

            {
                Configuration configuration = Configuration.builder().processorConfig(config).build();
                context.getProperties();
                result = properties;
                context.getConfiguration();
                result = configuration;
            }
        };
        // @formatter:on
        AutoTLSEnricher enricher = new AutoTLSEnricher(context);
        KubernetesListBuilder klb = new KubernetesListBuilder().addNewPodTemplateItem().withNewMetadata().and().withNewTemplate().withNewMetadata().and().withNewSpec().and().and().and().addNewServiceItem().and();
        enricher.enrich(PlatformMode.kubernetes, klb);
        PodTemplate pt = (PodTemplate) klb.buildItems().get(0);
        Service service = (Service) klb.buildItems().get(1);
        ObjectMeta om = service.getMetadata();
        List<Container> initContainers = pt.getTemplate().getSpec().getInitContainers();
        assertEquals(tc.mode == RuntimeMode.OPENSHIFT, !initContainers.isEmpty());
        if (tc.mode == RuntimeMode.KUBERNETES) {
            continue;
        }
        // Test metadata annotation
        Map<String, String> generatedAnnotation = om.getAnnotations();
        Assert.assertTrue(generatedAnnotation.containsKey(AutoTLSEnricher.AUTOTLS_ANNOTATION_KEY));
        Assert.assertTrue(generatedAnnotation.containsValue(context.getGav().getArtifactId() + "-tls"));
        // Test Pod template
        Gson gson = new Gson();
        JsonArray ja = new JsonParser().parse(gson.toJson(initContainers, new TypeToken<Collection<Container>>() {
        }.getType())).getAsJsonArray();
        assertEquals(1, ja.size());
        JsonObject jo = ja.get(0).getAsJsonObject();
        assertEquals(tc.initContainerName, jo.get("name").getAsString());
        assertEquals(tc.initContainerImage, jo.get("image").getAsString());
        // Test volumes are created
        List<Volume> volumes = pt.getTemplate().getSpec().getVolumes();
        assertEquals(2, volumes.size());
        List<String> volumeNames = volumes.stream().map(Volume::getName).collect(Collectors.toList());
        Assert.assertTrue(volumeNames.contains(tc.tlsSecretVolumeName));
        Assert.assertTrue(volumeNames.contains(tc.jksVolumeName));
        // Test volume mounts are created
        JsonArray mounts = jo.get("volumeMounts").getAsJsonArray();
        assertEquals(2, mounts.size());
        JsonObject mount = mounts.get(0).getAsJsonObject();
        assertEquals(tc.tlsSecretVolumeName, mount.get("name").getAsString());
        mount = mounts.get(1).getAsJsonObject();
        assertEquals(tc.jksVolumeName, mount.get("name").getAsString());
    }
}
Also used : Configuration(org.eclipse.jkube.kit.enricher.api.model.Configuration) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) Properties(java.util.Properties) ProcessorConfig(org.eclipse.jkube.kit.config.resource.ProcessorConfig) Container(io.fabric8.kubernetes.api.model.Container) PodTemplate(io.fabric8.kubernetes.api.model.PodTemplate) JsonParser(com.google.gson.JsonParser) Expectations(mockit.Expectations) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Service(io.fabric8.kubernetes.api.model.Service) TreeMap(java.util.TreeMap) JsonArray(com.google.gson.JsonArray) Volume(io.fabric8.kubernetes.api.model.Volume) TypeToken(com.google.common.reflect.TypeToken) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 2 with Configuration

use of org.eclipse.jkube.kit.enricher.api.model.Configuration in project jkube by eclipse.

the class ImageEnricherTest method givenResourceConfigWithEnvVar.

private void givenResourceConfigWithEnvVar(String name, String value) {
    // @formatter:off
    new Expectations() {

        {
            Configuration configuration = Configuration.builder().resource(ResourceConfig.builder().env(Collections.singletonMap(name, value)).build()).image(imageConfiguration).build();
            context.getConfiguration();
            result = configuration;
        }
    };
// @formatter:on
}
Also used : Expectations(mockit.Expectations) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) Configuration(org.eclipse.jkube.kit.enricher.api.model.Configuration)

Example 3 with Configuration

use of org.eclipse.jkube.kit.enricher.api.model.Configuration in project jkube by eclipse.

the class IngressEnricherTest method testNetworkingV1IngressIsGenerated.

@Test
public void testNetworkingV1IngressIsGenerated() {
    // Given
    final TreeMap<String, Object> config = new TreeMap<>();
    config.put("host", "test.192.168.39.25.nip.io");
    config.put("targetApiVersion", "networking.k8s.io/v1");
    new Expectations() {

        {
            Configuration configuration = Configuration.builder().image(imageConfiguration).processorConfig(new ProcessorConfig(null, null, Collections.singletonMap("jkube-ingress", config))).build();
            context.getConfiguration();
            result = configuration;
            context.getProperty("jkube.createExternalUrls");
            result = "true";
        }
    };
    KubernetesListBuilder kubernetesListBuilder = new KubernetesListBuilder();
    kubernetesListBuilder.addToItems(initTestService().build());
    // When
    ingressEnricher.create(PlatformMode.kubernetes, kubernetesListBuilder);
    // Then
    assertThat(kubernetesListBuilder).extracting(KubernetesListBuilder::buildItems).asList().hasSize(2).element(1).extracting("spec.rules").asList().extracting("host").containsExactly("test.192.168.39.25.nip.io");
}
Also used : Expectations(mockit.Expectations) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) Configuration(org.eclipse.jkube.kit.enricher.api.model.Configuration) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) TreeMap(java.util.TreeMap) ProcessorConfig(org.eclipse.jkube.kit.config.resource.ProcessorConfig) Test(org.junit.Test)

Example 4 with Configuration

use of org.eclipse.jkube.kit.enricher.api.model.Configuration in project jkube by eclipse.

the class DefaultMetadataEnricherTest method setUp.

@Before
public void setUp() throws Exception {
    Configuration configuration = Configuration.builder().resource(ResourceConfig.builder().annotations(MetaDataConfig.builder().all(properties("all-annotation", 1)).deployment(properties("deployment", "Deployment")).ingress(properties("ingress", "Ingress")).serviceAccount(properties("service-account", "ServiceAccount")).build()).labels(MetaDataConfig.builder().all(properties("all-label", 10L)).deployment(properties("deployment-label", "Deployment")).ingress(properties("ingress-label", "Ingress")).serviceAccount(properties("service-account-label", "ServiceAccount")).build()).build()).build();
    // @formatter:off
    new Expectations() {

        {
            buildContext.getConfiguration();
            result = configuration;
        }
    };
    // @formatter:on
    defaultMetadataEnricher = new DefaultMetadataEnricher(buildContext);
    configMap = new ConfigMapBuilder().withNewMetadata().endMetadata();
    deployment = new DeploymentBuilder();
    genericResource = new GenericKubernetesResourceBuilder().withNewMetadata().endMetadata();
    ingressV1 = new io.fabric8.kubernetes.api.model.networking.v1.IngressBuilder();
    ingressV1beta1 = new io.fabric8.kubernetes.api.model.networking.v1beta1.IngressBuilder();
    serviceAccount = new ServiceAccountBuilder();
    klb = new KubernetesListBuilder().addToItems(configMap).addToItems(deployment).addToItems(genericResource).addToItems(ingressV1).addToItems(ingressV1beta1).addToItems(serviceAccount);
}
Also used : Expectations(mockit.Expectations) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) Configuration(org.eclipse.jkube.kit.enricher.api.model.Configuration) ServiceAccountBuilder(io.fabric8.kubernetes.api.model.ServiceAccountBuilder) GenericKubernetesResourceBuilder(io.fabric8.kubernetes.api.model.GenericKubernetesResourceBuilder) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder) Before(org.junit.Before)

Example 5 with Configuration

use of org.eclipse.jkube.kit.enricher.api.model.Configuration in project jkube by eclipse.

the class DefaultServiceEnricherTest method setupExpectations.

private void setupExpectations(final boolean withPorts, String... configParams) {
    // Setup mock behaviour
    final TreeMap<String, Object> config = new TreeMap<>();
    for (int i = 0; i < configParams.length; i += 2) {
        config.put(configParams[i], configParams[i + 1]);
    }
    new Expectations() {

        {
            Configuration configuration = Configuration.builder().image(imageConfiguration).processorConfig(new ProcessorConfig(null, null, Collections.singletonMap("jkube-service", config))).build();
            context.getConfiguration();
            result = configuration;
            imageConfiguration.getBuildConfiguration();
            result = initBuildConfig(withPorts);
        }
    };
}
Also used : Expectations(mockit.Expectations) BuildConfiguration(org.eclipse.jkube.kit.config.image.build.BuildConfiguration) ImageConfiguration(org.eclipse.jkube.kit.config.image.ImageConfiguration) Configuration(org.eclipse.jkube.kit.enricher.api.model.Configuration) TreeMap(java.util.TreeMap) ProcessorConfig(org.eclipse.jkube.kit.config.resource.ProcessorConfig)

Aggregations

Configuration (org.eclipse.jkube.kit.enricher.api.model.Configuration)9 Expectations (mockit.Expectations)8 ProcessorConfig (org.eclipse.jkube.kit.config.resource.ProcessorConfig)5 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)4 TreeMap (java.util.TreeMap)4 ImageConfiguration (org.eclipse.jkube.kit.config.image.ImageConfiguration)4 Test (org.junit.Test)4 JsonObject (com.google.gson.JsonObject)2 BuildConfiguration (org.eclipse.jkube.kit.config.image.build.BuildConfiguration)2 TypeToken (com.google.common.reflect.TypeToken)1 Gson (com.google.gson.Gson)1 JsonArray (com.google.gson.JsonArray)1 JsonParser (com.google.gson.JsonParser)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)1 Container (io.fabric8.kubernetes.api.model.Container)1 GenericKubernetesResourceBuilder (io.fabric8.kubernetes.api.model.GenericKubernetesResourceBuilder)1 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)1 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 PodTemplate (io.fabric8.kubernetes.api.model.PodTemplate)1