use of io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder in project elastest-torm by elastest.
the class K8sService method createDaemonSetFromContainerInfo.
public boolean createDaemonSetFromContainerInfo(DockerContainer container, String namespace) {
Boolean created = false;
try {
logger.info("Creating Daemon Set from container name {} in namespace {}", container.getContainerName().get(), namespace);
if (container.getCmd().isPresent()) {
logger.info(String.join(",", container.getCmd().get()));
}
String containerNameWithoutUnderscore = container.getContainerName().get().replace("_", "-");
Map<String, String> k8sPobLabels = container.getLabels().get();
k8sPobLabels.put(LABEL_POD_NAME, container.getContainerName().get());
k8sPobLabels.put(LABEL_COMPONENT, containerNameWithoutUnderscore);
// Create Container
ContainerBuilder containerBuilder = new ContainerBuilder();
containerBuilder.withName(containerNameWithoutUnderscore).withImage(container.getImageId()).withEnv(getEnvVarListFromStringList(container.getEnvs().get()));
// Add ports
if (container.getExposedPorts().isPresent() && !container.getExposedPorts().get().isEmpty()) {
List<ContainerPort> ports = new ArrayList<>();
container.getExposedPorts().get().forEach(port -> {
ContainerPort containerPort = new ContainerPort();
containerPort.setContainerPort(new Integer(port));
ports.add(containerPort);
});
containerBuilder.withPorts(ports);
}
if (container.getCapAdd().isPresent() && !container.getCapAdd().get().isEmpty()) {
SecurityContextBuilder securityContextBuilder = new SecurityContextBuilder();
List<String> stringCapabilities = new ArrayList<>();
container.getCapAdd().get().forEach(cap -> {
stringCapabilities.add(cap);
});
Capabilities capabilities = new CapabilitiesBuilder().withAdd(stringCapabilities).build();
securityContextBuilder.withCapabilities(capabilities);
containerBuilder.withSecurityContext(securityContextBuilder.build());
}
// Add volumes if there are
List<Volume> volumes = new ArrayList<>();
List<VolumeMount> volumeMounts = new ArrayList<>();
if (container.getVolumeBindList().isPresent() && !container.getVolumeBindList().get().isEmpty()) {
int count = 0;
for (Bind dockerVolume : container.getVolumeBindList().get()) {
VolumeMount volumeMount = new VolumeMountBuilder().withName("v-" + count).withMountPath(dockerVolume.to()).build();
volumeMounts.add(volumeMount);
HostPathVolumeSource hostPath = new HostPathVolumeSourceBuilder().withPath(dockerVolume.to()).build();
Volume volume = new VolumeBuilder().withName("v-" + count).withHostPath(hostPath).build();
volumes.add(volume);
count++;
}
containerBuilder.withVolumeMounts(volumeMounts);
}
LabelSelectorBuilder selectorBuilder = new LabelSelectorBuilder();
LabelSelector labelSelector = selectorBuilder.withMatchLabels(Collections.singletonMap(LABEL_COMPONENT, containerNameWithoutUnderscore)).build();
// String dName = "daemonset-" + containerNameWithoutUnderscore;
DaemonSet daemonSet = new DaemonSetBuilder().withNewMetadata().withName(containerNameWithoutUnderscore).withNamespace(namespace).withLabels(k8sPobLabels).endMetadata().withNewSpec().withSelector(labelSelector).withNewTemplate().withNewMetadata().withLabels(k8sPobLabels).endMetadata().withNewSpec().addNewContainerLike(containerBuilder.build()).endContainer().withVolumes(volumes).endSpec().endTemplate().endSpec().build();
client.apps().daemonSets().create(daemonSet);
client.apps().daemonSets().inNamespace(namespace).withName(containerNameWithoutUnderscore).cascading(true);
} catch (Exception e) {
logger.error("Error deploying DaemonSet");
e.printStackTrace();
throw e;
}
return created;
}
use of io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder in project fabric8-maven-plugin by fabric8io.
the class ImageEnricherTest method checkEnrichDaemonSet.
@Test
public void checkEnrichDaemonSet() throws Exception {
KubernetesListBuilder builder = new KubernetesListBuilder().addToItems(new DaemonSetBuilder().build());
imageEnricher.create(PlatformMode.kubernetes, builder);
assertCorrectlyGeneratedResources(builder.build(), "DaemonSet");
}
use of io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder in project fabric8-maven-plugin by fabric8io.
the class TriggersAnnotationEnricherTest method testDaemonSetEnrichment.
@Test
public void testDaemonSetEnrichment() throws IOException {
KubernetesListBuilder builder = new KubernetesListBuilder().addToItems(new DaemonSetBuilder().withNewMetadata().addToAnnotations("annkey", "annvalue").endMetadata().withNewSpec().withNewTemplate().withNewSpec().withContainers(createContainers("c1", "iss:1.1.0", "c2", "docker.io/a-docker-user/is:latest")).endSpec().endTemplate().endSpec().build());
TriggersAnnotationEnricher enricher = new TriggersAnnotationEnricher(context);
enricher.enrich(PlatformMode.kubernetes, builder);
DaemonSet res = (DaemonSet) builder.build().getItems().get(0);
String triggers = res.getMetadata().getAnnotations().get("image.openshift.io/triggers");
assertNotNull(triggers);
List<ImageChangeTrigger> triggerList = OBJECT_MAPPER.readValue(triggers, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, ImageChangeTrigger.class));
assertEquals(1, triggerList.size());
ImageChangeTrigger trigger = triggerList.get(0);
assertEquals("ImageStreamTag", trigger.getFrom().getKind());
assertEquals("iss:1.1.0", trigger.getFrom().getName());
assertTrue(trigger.getAdditionalProperties().containsKey("fieldPath"));
assertEquals("annvalue", res.getMetadata().getAnnotations().get("annkey"));
}
Aggregations