use of io.fabric8.kubernetes.api.model.PodSpec 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.PodSpec in project fabric8-maven-plugin by fabric8io.
the class DockerImageWatcher method updateImageName.
private boolean updateImageName(HasMetadata entity, PodTemplateSpec template, String imagePrefix, String imageName) {
boolean answer = false;
PodSpec spec = template.getSpec();
if (spec != null) {
List<Container> containers = spec.getContainers();
if (containers != null) {
for (Container container : containers) {
String image = container.getImage();
if (image != null && image.startsWith(imagePrefix)) {
container.setImage(imageName);
log.info("Updating " + getKind(entity) + " " + KubernetesHelper.getName(entity) + " to use image: " + imageName);
answer = true;
}
}
}
}
return answer;
}
use of io.fabric8.kubernetes.api.model.PodSpec in project halyard by spinnaker.
the class KubernetesV2Service method getResourceYaml.
default String getResourceYaml(AccountDeploymentDetails<KubernetesAccount> details, GenerateService.ResolvedConfiguration resolvedConfiguration) {
ServiceSettings settings = resolvedConfiguration.getServiceSettings(getService());
String namespace = getNamespace(settings);
List<ConfigSource> configSources = stageConfig(details, resolvedConfiguration);
Map<String, String> env = configSources.stream().map(ConfigSource::getEnv).map(Map::entrySet).flatMap(Collection::stream).collect(Collectors.toMap(Entry::getKey, Entry::getValue));
env.putAll(settings.getEnv());
List<String> volumes = configSources.stream().map(c -> {
TemplatedResource volume = new JinjaJarResource("/kubernetes/manifests/volume.yml");
volume.addBinding("name", c.getId());
return volume.toString();
}).collect(Collectors.toList());
List<String> volumeMounts = configSources.stream().map(c -> {
TemplatedResource volume = new JinjaJarResource("/kubernetes/manifests/volumeMount.yml");
volume.addBinding("name", c.getId());
volume.addBinding("mountPath", c.getMountPath());
return volume.toString();
}).collect(Collectors.toList());
TemplatedResource probe;
if (StringUtils.isNotEmpty(settings.getHealthEndpoint())) {
probe = new JinjaJarResource("/kubernetes/manifests/execReadinessProbe.yml");
probe.addBinding("command", getReadinessExecCommand(settings));
} else {
probe = new JinjaJarResource("/kubernetes/manifests/tcpSocketReadinessProbe.yml");
probe.addBinding("port", settings.getPort());
}
TemplatedResource container = new JinjaJarResource("/kubernetes/manifests/container.yml");
container.addBinding("name", getService().getCanonicalName());
container.addBinding("imageId", settings.getArtifactId());
container.addBinding("port", settings.getPort());
container.addBinding("volumeMounts", volumeMounts);
container.addBinding("probe", probe.toString());
container.addBinding("env", env);
List<String> containers = Collections.singletonList(container.toString());
TemplatedResource podSpec = new JinjaJarResource("/kubernetes/manifests/podSpec.yml").addBinding("containers", containers).addBinding("volumes", volumes);
return new JinjaJarResource("/kubernetes/manifests/deployment.yml").addBinding("name", getService().getCanonicalName()).addBinding("namespace", namespace).addBinding("replicas", settings.getTargetSize()).addBinding("podAnnotations", settings.getKubernetes().getPodAnnotations()).addBinding("podSpec", podSpec.toString()).toString();
}
use of io.fabric8.kubernetes.api.model.PodSpec in project wildfly-camel by wildfly-extras.
the class KubernetesIntegrationTest method createPodSpec.
private PodSpec createPodSpec() throws IOException {
PodSpec podSpec = new PodSpec();
podSpec.setHostname("localhost");
Container container = new Container();
container.setImage("docker.io/wildflyext/wildfly-camel:latest");
container.setName("wildfly-camel-test");
ContainerPort port = new ContainerPort();
port.setHostIP("0.0.0.0");
port.setContainerPort(8080);
List<ContainerPort> ports = new ArrayList<>();
ports.add(port);
container.setPorts(ports);
List<Container> containers = new ArrayList<>();
containers.add(container);
podSpec.setContainers(containers);
return podSpec;
}
use of io.fabric8.kubernetes.api.model.PodSpec in project ephemerals by LivePersonInc.
the class DefaultKubernetesDeploymentStrategy method pod.
@Override
public PodSpec pod(Deployment deployment) {
PodSpecBuilder podSpec = new PodSpecBuilder();
ContainerizedDeploymentUnit deploymentUnit = (ContainerizedDeploymentUnit) deployment.getDeploymentUnit();
List<ContainerPort> containerPortList = deployment.getDeploymentUnit().getPorts().stream().map(port -> new ContainerPortBuilder().withContainerPort(port.getPort()).withName(port.getName()).build()).collect(Collectors.toList());
Probe healthProbe = null;
Probe readinessProbe = null;
if (deploymentUnit.getHealthProbe() != null) {
healthProbe = DefaultKubernetesDeploymentStrategy.probe(deploymentUnit.getHealthProbe());
}
if (deploymentUnit.getReadinessProbe() != null) {
readinessProbe = DefaultKubernetesDeploymentStrategy.probe(deploymentUnit.getReadinessProbe());
}
ContainerBuilder containerBuilder = new ContainerBuilder();
if (healthProbe != null) {
containerBuilder.withLivenessProbe(healthProbe);
}
if (readinessProbe != null) {
containerBuilder.withReadinessProbe(readinessProbe);
}
Container container = containerBuilder.withName(deployment.getId()).withPorts(containerPortList).withImage(deploymentUnit.getImage()).withEnv(envVars(deploymentUnit.getEnvVars())).withResources(new ResourceRequirementsBuilder().addToLimits("cpu", new QuantityBuilder().withAmount(String.valueOf(deploymentUnit.getCpu() * 1000) + "m").build()).addToLimits("memory", new QuantityBuilder().withAmount(String.valueOf(deploymentUnit.getMem()) + "Mi").build()).build()).build();
container.setVolumeMounts(deploymentUnit.getVolumes().entrySet().stream().map(pair -> new VolumeMountBuilder().withName(pair.getKey().getName()).withMountPath(pair.getKey().getMountPath()).build()).collect(Collectors.toList()));
podSpec.addToContainers(container);
podSpec.withVolumes(deploymentUnit.getVolumes().entrySet().stream().filter(pair -> pair != null).map(pair -> {
Volume volume = volume(pair.getValue());
volume.setName(pair.getKey().getName());
return volume;
}).collect(Collectors.toList()));
return podSpec.build();
}
Aggregations