Search in sources :

Example 11 with PersistentVolumeClaim

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);
}
Also used : DoneablePersistentVolumeClaim(io.fabric8.kubernetes.api.model.DoneablePersistentVolumeClaim) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim)

Example 12 with PersistentVolumeClaim

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);
}
Also used : PersistentVolumeClaimSpec(io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpec) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) DoneablePersistentVolumeClaim(io.fabric8.kubernetes.api.model.DoneablePersistentVolumeClaim) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim)

Example 13 with PersistentVolumeClaim

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;
}
Also used : LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) HashMap(java.util.HashMap) PodSecurityContext(io.fabric8.kubernetes.api.model.PodSecurityContext) StatefulSetUpdateStrategyBuilder(io.fabric8.kubernetes.api.model.extensions.StatefulSetUpdateStrategyBuilder) ArrayList(java.util.ArrayList) StatefulSetBuilder(io.fabric8.kubernetes.api.model.extensions.StatefulSetBuilder) Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) PodSecurityContextBuilder(io.fabric8.kubernetes.api.model.PodSecurityContextBuilder) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet)

Example 14 with PersistentVolumeClaim

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();
            }
        }
    });
}
Also used : Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) JSONArray(org.json.JSONArray) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) PersistentVolumeClaimVolumeSource(io.fabric8.kubernetes.api.model.PersistentVolumeClaimVolumeSource) Container(io.fabric8.kubernetes.api.model.Container) JSONObject(org.json.JSONObject) Volume(io.fabric8.kubernetes.api.model.Volume) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 15 with PersistentVolumeClaim

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);
    }
}
Also used : JSONObject(org.json.JSONObject) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) FileNotFoundException(java.io.FileNotFoundException) OpenShiftNotAvailableException(io.fabric8.openshift.client.OpenShiftNotAvailableException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Aggregations

PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)12 PersistentVolumeClaimBuilder (io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder)5 StatefulSet (io.fabric8.kubernetes.api.model.extensions.StatefulSet)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 DoneablePersistentVolumeClaim (io.fabric8.kubernetes.api.model.DoneablePersistentVolumeClaim)4 Pod (io.fabric8.kubernetes.api.model.Pod)4 Quantity (io.fabric8.kubernetes.api.model.Quantity)4 Service (io.fabric8.kubernetes.api.model.Service)4 HashMap (java.util.HashMap)4 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)3 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)3 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)3 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 Container (io.fabric8.kubernetes.api.model.Container)2 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)2 Namespace (io.fabric8.kubernetes.api.model.Namespace)2 PersistentVolumeClaimList (io.fabric8.kubernetes.api.model.PersistentVolumeClaimList)2 PersistentVolumeClaimSpec (io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpec)2