AutoTLSEnricher extends BaseEnricher { @Override public void enrich(PlatformMode platformMode, KubernetesListBuilder builder) { if (!isOpenShiftMode()) { return; } builder.accept(new TypedVisitor<PodTemplateSpecBuilder>() { @Override public void visit(PodTemplateSpecBuilder builder) { initContainerHandler.appendInitContainer(builder, createInitContainer()); } private Container createInitContainer() { return new ContainerBuilder() .withName(getConfig(Config.pemToJKSInitContainerName)) .withImage(getConfig(Config.pemToJKSInitContainerImage)) .withImagePullPolicy("IfNotPresent") .withArgs(createArgsArray()) .withVolumeMounts(createMounts()) .build(); } private List<String> createArgsArray() { List<String> ret = new ArrayList<>(); ret.add("-cert-file"); ret.add(getConfig(Config.keystoreCertAlias) + "=/tls-pem/tls.crt"); ret.add("-key-file"); ret.add(getConfig(Config.keystoreCertAlias) + "=/tls-pem/tls.key"); ret.add("-keystore"); ret.add("/tls-jks/" + getConfig(Config.keystoreFileName)); ret.add("-keystore-password"); ret.add(getConfig(Config.keystorePassword)); return ret; } private List<VolumeMount> createMounts() { VolumeMount pemMountPoint = new VolumeMountBuilder() .withName(getConfig(Config.tlsSecretVolumeName)) .withMountPath("/tls-pem") .build(); VolumeMount jksMountPoint = new VolumeMountBuilder() .withName(getConfig(Config.jksVolumeName)) .withMountPath("/tls-jks") .build(); return Arrays.asList(pemMountPoint, jksMountPoint); } }); } AutoTLSEnricher(MavenEnricherContext buildContext); @Override void create(PlatformMode platformMode, KubernetesListBuilder builder); @Override void enrich(PlatformMode platformMode, KubernetesListBuilder builder);  }
@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(RuntimeMode.FABRIC8_EFFECTIVE_PLATFORM_MODE, tc.mode.name()); new Expectations() { { Configuration configuration = new Configuration.Builder() .properties(projectProps) .processorConfig(config) .build(); context.getConfiguration(); result = configuration; project.getArtifactId(); result = "projectA"; minTimes = 0; } }; AutoTLSEnricher enricher = new AutoTLSEnricher(context); KubernetesListBuilder klb = new KubernetesListBuilder().addNewPodTemplateItem().withNewMetadata().and() .withNewTemplate().withNewMetadata().and().withNewSpec().and().and().and(); enricher.enrich(PlatformMode.kubernetes, klb); PodTemplate pt = (PodTemplate) klb.getItems().get(0); List<Container> initContainers = pt.getTemplate().getSpec().getInitContainers(); assertEquals(tc.mode == PlatformMode.openshift, !initContainers.isEmpty()); if (tc.mode == PlatformMode.kubernetes) { continue; } 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()); 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()); } }