use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.
the class VolumePermissionEnricherTest method testAdapt.
@Test
public void testAdapt() throws Exception {
final TestConfig[] data = new TestConfig[] { new TestConfig(null, null), new TestConfig(null, VolumePermissionEnricher.ENRICHER_NAME, "volumeA"), new TestConfig(null, VolumePermissionEnricher.ENRICHER_NAME, "volumeA", "volumeB") };
for (final TestConfig tc : data) {
final ProcessorConfig config = new ProcessorConfig(null, null, Collections.singletonMap(VolumePermissionEnricher.ENRICHER_NAME, new TreeMap(Collections.singletonMap(VolumePermissionEnricher.Config.permission.name(), tc.permission))));
// Setup mock behaviour
new Expectations() {
{
context.getConfig();
result = config;
}
};
VolumePermissionEnricher enricher = new VolumePermissionEnricher(context);
PodTemplateBuilder ptb = createEmptyPodTemplate();
for (String vn : tc.volumeNames) {
ptb = addVolume(ptb, vn);
}
KubernetesListBuilder klb = new KubernetesListBuilder().addToPodTemplateItems(ptb.build());
enricher.adapt(klb);
PodTemplate pt = (PodTemplate) klb.buildItem(0);
String initContainers = pt.getTemplate().getMetadata().getAnnotations().get(InitContainerHandler.INIT_CONTAINER_ANNOTATION);
boolean shouldHaveInitContainer = tc.volumeNames.length > 0;
assertEquals(shouldHaveInitContainer, initContainers != null);
if (!shouldHaveInitContainer) {
continue;
}
JSONArray ja = new JSONArray(initContainers);
assertEquals(1, ja.length());
JSONObject jo = ja.getJSONObject(0);
assertEquals(tc.initContainerName, jo.get("name"));
String permission = Strings.isNullOrBlank(tc.permission) ? "777" : tc.permission;
JSONArray chmodCmd = new JSONArray();
chmodCmd.put("chmod");
chmodCmd.put(permission);
for (String vn : tc.volumeNames) {
chmodCmd.put("/tmp/" + vn);
}
assertEquals(chmodCmd.toString(), jo.getJSONArray("command").toString());
}
}
use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.
the class DockerHealthCheckEnricherTest method testEnrichFromDoubleImage.
@Test
public void testEnrichFromDoubleImage() throws Exception {
// Setup mock behaviour
new Expectations() {
{
context.getImages();
result = Arrays.asList(new ImageConfiguration.Builder().alias("myImage").buildConfig(new BuildImageConfiguration.Builder().healthCheck(new HealthCheckConfiguration.Builder().mode(HealthCheckMode.cmd).cmd("/bin/check").timeout("1s").interval("1h1s").retries(3).build()).build()).build(), new ImageConfiguration.Builder().alias("myImage2").buildConfig(new BuildImageConfiguration.Builder().healthCheck(new HealthCheckConfiguration.Builder().mode(HealthCheckMode.cmd).cmd("/xxx/check").timeout("3s").interval("3h1s").retries(9).build()).build()).build());
}
};
KubernetesListBuilder builder = addDeployment(createDeployment("myImage"), "myImage2");
DockerHealthCheckEnricher enricher = new DockerHealthCheckEnricher(context);
enricher.addMissingResources(builder);
KubernetesList list = builder.build();
assertEquals(2, list.getItems().size());
assertHealthCheckMatching(builder.build().getItems().get(0), "livenessProbe", "/bin/check", 1, 3601, 3);
assertHealthCheckMatching(builder.build().getItems().get(0), "readinessProbe", "/bin/check", 1, 3601, 3);
assertHealthCheckMatching(builder.build().getItems().get(1), "livenessProbe", "/xxx/check", 3, 10801, 9);
assertHealthCheckMatching(builder.build().getItems().get(1), "readinessProbe", "/xxx/check", 3, 10801, 9);
}
use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.
the class DockerHealthCheckEnricherTest method testInvalidHealthCheck.
@Test
public void testInvalidHealthCheck() throws Exception {
// Setup mock behaviour
new Expectations() {
{
context.getImages();
result = Arrays.asList(new ImageConfiguration.Builder().alias("myImage").buildConfig(new BuildImageConfiguration.Builder().healthCheck(new HealthCheckConfiguration.Builder().mode(HealthCheckMode.none).build()).build()).build());
}
};
KubernetesListBuilder builder = createDeployment("myImage");
DockerHealthCheckEnricher enricher = new DockerHealthCheckEnricher(context);
enricher.addMissingResources(builder);
KubernetesList list = builder.build();
assertEquals(1, list.getItems().size());
assertNoProbes(list.getItems().get(0));
}
use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.
the class DependencyEnricher method filterAndAddItemsToBuilder.
public void filterAndAddItemsToBuilder(KubernetesListBuilder builder, List<HasMetadata> items) {
Map<KindAndName, Integer> aIndexMap = new HashMap<>();
int nItems = 0;
// Populate map with existing items in the builder
for (int index = 0; index < builder.getItems().size(); index++, nItems++) {
HasMetadata aItem = builder.getItems().get(index);
KindAndName aKey = new KindAndName(aItem);
aIndexMap.put(aKey, index);
}
for (HasMetadata item : items) {
KindAndName aKey = new KindAndName(item);
if (aIndexMap.containsKey(aKey)) {
// Merge the override fragments, and remove duplicate
HasMetadata duplicateItem = builder.getItems().get(aIndexMap.get(aKey));
item = KubernetesResourceUtil.mergeResources(item, duplicateItem, log, false);
builder.setToItems(aIndexMap.get(aKey), item);
} else {
aIndexMap.put(aKey, nItems++);
builder.addToItems(item);
}
}
}
use of io.fabric8.kubernetes.api.model.KubernetesListBuilder in project fabric8-maven-plugin by fabric8io.
the class DependencyEnricherTest method checkDuplicatesInResource.
@Test
public void checkDuplicatesInResource() throws Exception {
// Generate given Resources
KubernetesListBuilder aBuilder = createResourcesForTest();
// Enrich
KubernetesList aResourceList = enrichResources(aBuilder);
// Assert
assertTrue(aResourceList.getItems() != null);
assertEquals(checkUniqueResources(aResourceList.getItems()), true);
}
Aggregations