use of io.fabric8.kubernetes.api.model.PersistentVolumeClaim in project camel by apache.
the class KubernetesPersistentVolumesClaimsProducer method doGetPersistentVolumeClaim.
protected void doGetPersistentVolumeClaim(Exchange exchange, String operation) throws Exception {
PersistentVolumeClaim pvc = null;
String pvcName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_CLAIM_NAME, String.class);
String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
if (ObjectHelper.isEmpty(pvcName)) {
LOG.error("Get a specific Persistent Volume Claim require specify a Persistent Volume Claim name");
throw new IllegalArgumentException("Get a specific Persistent Volume Claim require specify a Persistent Volume Claim name");
}
if (ObjectHelper.isEmpty(namespaceName)) {
LOG.error("Get a specific Persistent Volume Claim require specify a namespace name");
throw new IllegalArgumentException("Get a specific Persistent Volume Claim require specify a namespace name");
}
pvc = getEndpoint().getKubernetesClient().persistentVolumeClaims().inNamespace(namespaceName).withName(pvcName).get();
MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
exchange.getOut().setBody(pvc);
}
use of io.fabric8.kubernetes.api.model.PersistentVolumeClaim in project camel by apache.
the class KubernetesPersistentVolumesClaimsProducer method doCreatePersistentVolumeClaim.
protected void doCreatePersistentVolumeClaim(Exchange exchange, String operation) throws Exception {
PersistentVolumeClaim pvc = null;
String pvcName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_CLAIM_NAME, String.class);
String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
PersistentVolumeClaimSpec pvcSpec = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_CLAIM_SPEC, PersistentVolumeClaimSpec.class);
if (ObjectHelper.isEmpty(pvcName)) {
LOG.error("Create a specific Persistent Volume Claim require specify a Persistent Volume Claim name");
throw new IllegalArgumentException("Create a specific Persistent Volume Claim require specify a Persistent Volume Claim name");
}
if (ObjectHelper.isEmpty(namespaceName)) {
LOG.error("Create a specific Persistent Volume Claim require specify a namespace name");
throw new IllegalArgumentException("Create a specific Persistent Volume Claim require specify a namespace name");
}
if (ObjectHelper.isEmpty(pvcSpec)) {
LOG.error("Create a specific Persistent Volume Claim require specify a Persistent Volume Claim spec bean");
throw new IllegalArgumentException("Create a specific Persistent Volume Claim require specify a Persistent Volume Claim spec bean");
}
Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUMES_CLAIMS_LABELS, Map.class);
PersistentVolumeClaim pvcCreating = new PersistentVolumeClaimBuilder().withNewMetadata().withName(pvcName).withLabels(labels).endMetadata().withSpec(pvcSpec).build();
pvc = getEndpoint().getKubernetesClient().persistentVolumeClaims().inNamespace(namespaceName).create(pvcCreating);
MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
exchange.getOut().setBody(pvc);
}
use of io.fabric8.kubernetes.api.model.PersistentVolumeClaim in project strimzi by strimzi.
the class AbstractModel method createStatefulSet.
protected StatefulSet createStatefulSet(List<ContainerPort> ports, List<Volume> volumes, List<PersistentVolumeClaim> volumeClaims, List<VolumeMount> volumeMounts, Probe livenessProbe, Probe readinessProbe, boolean isOpenShift) {
Map<String, String> annotations = new HashMap<>();
annotations.put(String.format("%s/%s", ClusterController.STRIMZI_CLUSTER_CONTROLLER_DOMAIN, Storage.DELETE_CLAIM_FIELD), String.valueOf(storage.isDeleteClaim()));
Container container = new ContainerBuilder().withName(name).withImage(getImage()).withEnv(getEnvVars()).withVolumeMounts(volumeMounts).withPorts(ports).withLivenessProbe(livenessProbe).withReadinessProbe(readinessProbe).build();
List<Container> initContainers = new ArrayList<>();
PodSecurityContext securityContext = null;
// there is an hack on volume mounting which needs an "init-container"
if ((this.storage.type() == Storage.StorageType.PERSISTENT_CLAIM) && !isOpenShift) {
String chown = String.format("chown -R %d:%d %s", AbstractModel.VOLUME_MOUNT_HACK_GROUPID, AbstractModel.VOLUME_MOUNT_HACK_GROUPID, volumeMounts.get(0).getMountPath());
Container initContainer = new ContainerBuilder().withName(AbstractModel.VOLUME_MOUNT_HACK_NAME).withImage(AbstractModel.VOLUME_MOUNT_HACK_IMAGE).withVolumeMounts(volumeMounts.get(0)).withCommand("sh", "-c", chown).build();
initContainers.add(initContainer);
securityContext = new PodSecurityContextBuilder().withFsGroup(AbstractModel.VOLUME_MOUNT_HACK_GROUPID).build();
}
StatefulSet statefulSet = new StatefulSetBuilder().withNewMetadata().withName(name).withLabels(getLabelsWithName()).withNamespace(namespace).withAnnotations(annotations).endMetadata().withNewSpec().withPodManagementPolicy("Parallel").withUpdateStrategy(new StatefulSetUpdateStrategyBuilder().withType("OnDelete").build()).withSelector(new LabelSelectorBuilder().withMatchLabels(getLabelsWithName()).build()).withServiceName(headlessName).withReplicas(replicas).withNewTemplate().withNewMetadata().withName(name).withLabels(getLabelsWithName()).withAnnotations(getPrometheusAnnotations()).endMetadata().withNewSpec().withSecurityContext(securityContext).withInitContainers(initContainers).withContainers(container).withVolumes(volumes).endSpec().endTemplate().withVolumeClaimTemplates(volumeClaims).endSpec().build();
return statefulSet;
}
use of io.fabric8.kubernetes.api.model.PersistentVolumeClaim 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.PersistentVolumeClaim in project fabric8 by fabric8io.
the class Controller method doCreatePersistentVolumeClaim.
protected void doCreatePersistentVolumeClaim(PersistentVolumeClaim entity, String namespace, String sourceName) {
LOG.info("Creating a PersistentVolumeClaim from " + sourceName + " namespace " + namespace + " name " + getName(entity));
try {
Object answer;
if (Strings.isNotBlank(namespace)) {
answer = kubernetesClient.persistentVolumeClaims().inNamespace(namespace).create(entity);
} else {
answer = kubernetesClient.persistentVolumeClaims().inNamespace(getNamespace()).create(entity);
}
logGeneratedEntity("Created PersistentVolumeClaim: ", namespace, entity, answer);
} catch (Exception e) {
onApplyError("Failed to create PersistentVolumeClaim from " + sourceName + ". " + e + ". " + entity, e);
}
}
Aggregations