Search in sources :

Example 31 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.

the class AutoTLSEnricherTest method testAdapt.

@Test
public void testAdapt() throws Exception {
    final AdaptTestConfig[] data = new AdaptTestConfig[] { new AdaptTestConfig(PlatformMode.kubernetes, null, null, null, null, null, null, null, null), new AdaptTestConfig(PlatformMode.openshift, null, "tls-jks-converter", null, "jimmidyson/pemtokeystore:v0.1.0", null, "tls-pem", null, "tls-jks"), new AdaptTestConfig(PlatformMode.openshift, null, "tls-jks-converter", null, "jimmidyson/pemtokeystore:v0.1.0", "tls-a", "tls-a", null, "tls-jks"), new AdaptTestConfig(PlatformMode.openshift, null, "tls-jks-converter", null, "jimmidyson/pemtokeystore:v0.1.0", null, "tls-pem", "jks-b", "jks-b"), new AdaptTestConfig(PlatformMode.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 configMap = new TreeMap() {

            {
                put(AutoTLSEnricher.Config.pemToJKSInitContainerName.name(), tc.initContainerNameConfig);
                put(AutoTLSEnricher.Config.pemToJKSInitContainerImage.name(), tc.initContainerImageConfig);
                put(AutoTLSEnricher.Config.tlsSecretVolumeName.name(), tc.tlsSecretVolumeNameConfig);
                put(AutoTLSEnricher.Config.jksVolumeName.name(), tc.jksVolumeNameConfig);
            }
        };
        final ProcessorConfig config = new ProcessorConfig(null, null, Collections.singletonMap(AutoTLSEnricher.ENRICHER_NAME, configMap));
        final Properties projectProps = new Properties();
        projectProps.put(PlatformMode.FABRIC8_EFFECTIVE_PLATFORM_MODE, tc.mode.name());
        // Setup mock behaviour
        new Expectations() {

            {
                project.getProperties();
                result = projectProps;
                project.getArtifactId();
                result = "projectA";
                minTimes = 0;
                context.getProject();
                result = project;
                context.getConfig();
                result = config;
            }
        };
        AutoTLSEnricher enricher = new AutoTLSEnricher(context);
        KubernetesListBuilder klb = new KubernetesListBuilder().addNewPodTemplateItem().withNewMetadata().and().withNewTemplate().withNewMetadata().and().withNewSpec().and().and().and();
        enricher.adapt(klb);
        PodTemplate pt = (PodTemplate) klb.getItems().get(0);
        String initContainers = pt.getTemplate().getMetadata().getAnnotations().get(InitContainerHandler.INIT_CONTAINER_ANNOTATION);
        assertEquals(tc.mode == PlatformMode.openshift, initContainers != null);
        if (tc.mode == PlatformMode.kubernetes) {
            continue;
        }
        JSONArray ja = new JSONArray(initContainers);
        assertEquals(1, ja.length());
        JSONObject jo = ja.getJSONObject(0);
        assertEquals(tc.initContainerName, jo.get("name"));
        assertEquals(tc.initContainerImage, jo.get("image"));
        JSONArray mounts = jo.getJSONArray("volumeMounts");
        assertEquals(2, mounts.length());
        JSONObject mount = mounts.getJSONObject(0);
        assertEquals(tc.tlsSecretVolumeName, mount.get("name"));
        mount = mounts.getJSONObject(1);
        assertEquals(tc.jksVolumeName, mount.get("name"));
    }
}
Also used : Expectations(mockit.Expectations) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ProcessorConfig(io.fabric8.maven.core.config.ProcessorConfig) PodTemplate(io.fabric8.kubernetes.api.model.PodTemplate) Test(org.junit.Test)

Example 32 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.

the class VolumePermissionEnricher method adapt.

@Override
public void adapt(KubernetesListBuilder builder) {
    builder.accept(new TypedVisitor<PodTemplateSpecBuilder>() {

        @Override
        public void visit(PodTemplateSpecBuilder builder) {
            PodSpec podSpec = builder.buildSpec();
            if (podSpec == null) {
                return;
            }
            if (!checkForPvc(podSpec)) {
                return;
            }
            List<Container> containers = podSpec.getContainers();
            if (containers == null || containers.isEmpty()) {
                return;
            }
            log.verbose("Adding init container for changing persistent volumes access mode to %s", getConfig(Config.permission));
            if (!initContainerHandler.hasInitContainer(builder, ENRICHER_NAME)) {
                initContainerHandler.appendInitContainer(builder, createPvInitContainer(podSpec));
            }
        }

        private boolean checkForPvc(PodSpec podSpec) {
            List<Volume> volumes = podSpec.getVolumes();
            if (volumes != null) {
                for (Volume volume : volumes) {
                    PersistentVolumeClaimVolumeSource persistentVolumeClaim = volume.getPersistentVolumeClaim();
                    if (persistentVolumeClaim != null) {
                        return true;
                    }
                }
            }
            return false;
        }

        private JSONObject createPvInitContainer(PodSpec podSpec) {
            Map<String, String> mountPoints = extractMountPoints(podSpec);
            JSONObject entry = new JSONObject();
            entry.put("name", ENRICHER_NAME);
            entry.put("image", "busybox");
            entry.put("imagePullPolicy", "IfNotPresent");
            entry.put("command", createChmodCommandArray(mountPoints));
            entry.put("volumeMounts", createMounts(mountPoints));
            return entry;
        }

        private JSONArray createChmodCommandArray(Map<String, String> mountPoints) {
            JSONArray ret = new JSONArray();
            ret.put("chmod");
            ret.put(getConfig(Config.permission));
            Set<String> uniqueNames = new LinkedHashSet<>(mountPoints.values());
            for (String name : uniqueNames) {
                ret.put(name);
            }
            return ret;
        }

        private JSONArray createMounts(Map<String, String> mountPoints) {
            JSONArray ret = new JSONArray();
            for (Map.Entry<String, String> entry : mountPoints.entrySet()) {
                JSONObject mount = new JSONObject();
                mount.put("name", entry.getKey());
                mount.put("mountPath", entry.getValue());
                ret.put(mount);
            }
            return ret;
        }

        private Map<String, String> extractMountPoints(PodSpec podSpec) {
            Map<String, String> nameToMount = new LinkedHashMap<>();
            List<Volume> volumes = podSpec.getVolumes();
            if (volumes != null) {
                for (Volume volume : volumes) {
                    PersistentVolumeClaimVolumeSource persistentVolumeClaim = volume.getPersistentVolumeClaim();
                    if (persistentVolumeClaim != null) {
                        String name = volume.getName();
                        String mountPath = getMountPath(podSpec.getContainers(), name);
                        nameToMount.put(name, mountPath);
                    }
                }
            }
            return nameToMount;
        }

        private String getMountPath(List<Container> containers, String name) {
            for (Container container : containers) {
                List<VolumeMount> volumeMounts = container.getVolumeMounts();
                if (volumeMounts != null) {
                    for (VolumeMount volumeMount : volumeMounts) {
                        if (name.equals(volumeMount.getName())) {
                            return volumeMount.getMountPath();
                        }
                    }
                }
            }
            throw new IllegalArgumentException("No matching volume mount found for volume " + name);
        }
    });
    builder.accept(new TypedVisitor<PersistentVolumeClaimBuilder>() {

        @Override
        public void visit(PersistentVolumeClaimBuilder pvcBuilder) {
            // lets ensure we have a default storage class so that PVs will get dynamically created OOTB
            if (pvcBuilder.buildMetadata() == null) {
                pvcBuilder.withNewMetadata().endMetadata();
            }
            String storageClass = getConfig(Config.defaultStorageClass);
            if (Strings.isNotBlank(storageClass) && !pvcBuilder.buildMetadata().getAnnotations().containsKey(VOLUME_STORAGE_CLASS_ANNOTATION)) {
                pvcBuilder.editMetadata().addToAnnotations(VOLUME_STORAGE_CLASS_ANNOTATION, storageClass).endMetadata();
            }
        }
    });
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) JSONArray(org.json.JSONArray) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) PersistentVolumeClaimVolumeSource(io.fabric8.kubernetes.api.model.PersistentVolumeClaimVolumeSource) Container(io.fabric8.kubernetes.api.model.Container) JSONObject(org.json.JSONObject) Volume(io.fabric8.kubernetes.api.model.Volume) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 33 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.

the class DefaultServiceEnricherTest method headlessServiceNegative.

@Test
public void headlessServiceNegative() throws Exception {
    setupExpectations(false, "headless", "false");
    DefaultServiceEnricher serviceEnricher = new DefaultServiceEnricher(context);
    KubernetesListBuilder builder = new KubernetesListBuilder();
    serviceEnricher.addMissingResources(builder);
    // Validate that the generated resource contains
    KubernetesList list = builder.build();
    assertEquals(list.getItems().size(), 0);
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Test(org.junit.Test)

Example 34 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.

the class DefaultServiceEnricherTest method enrich.

// ======================================================================================================
private String enrich() throws com.fasterxml.jackson.core.JsonProcessingException {
    // Enrich
    DefaultServiceEnricher serviceEnricher = new DefaultServiceEnricher(context);
    KubernetesListBuilder builder = new KubernetesListBuilder();
    serviceEnricher.addMissingResources(builder);
    // Validate that the generated resource contains
    KubernetesList list = builder.build();
    assertEquals(list.getItems().size(), 1);
    return KubernetesResourceUtil.toJson(list.getItems().get(0));
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList)

Example 35 with KubernetesListBuilder

use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.

the class DockerRegistrySecretEnricherTest method testDockerRegistryWithBadKind.

@Test
public void testDockerRegistryWithBadKind() {
    new Expectations() {

        {
            {
                context.getSettings();
                result = createSettings();
            }
        }
    };
    DockerRegistrySecretEnricher enricher = new DockerRegistrySecretEnricher(context);
    KubernetesListBuilder builder = new KubernetesListBuilder();
    Secret secret = createBaseSecret(true);
    secret.setKind("Secrets");
    builder.addToSecretItems(createBaseSecret(true));
    KubernetesList expected = builder.build();
    enricher.addMissingResources(builder);
    assertEquals(expected, builder.build());
}
Also used : Expectations(mockit.Expectations) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) Secret(io.fabric8.kubernetes.api.model.Secret) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) Test(org.junit.Test)

Aggregations

KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)30 Test (org.junit.Test)25 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)17 Expectations (mockit.Expectations)13 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)8 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)5 ImageConfiguration (io.fabric8.maven.docker.config.ImageConfiguration)5 File (java.io.File)5 ProcessorConfig (io.fabric8.maven.core.config.ProcessorConfig)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)3 Secret (io.fabric8.kubernetes.api.model.Secret)3 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)3 IOException (java.io.IOException)3 Map (java.util.Map)3 JSONArray (org.json.JSONArray)3 JSONObject (org.json.JSONObject)3 Visitable (io.fabric8.kubernetes.api.builder.Visitable)2 Visitor (io.fabric8.kubernetes.api.builder.Visitor)2 KubernetesResource (io.fabric8.kubernetes.api.model.KubernetesResource)2