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"));
}
}
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();
}
}
});
}
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);
}
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));
}
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());
}
Aggregations