Search in sources :

Example 1 with PersistentVolumeClaimSpec

use of io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpec in project camel by apache.

the class KubernetesPersistentVolumesClaimsProducerTest method createListAndDeletePersistentVolumeClaim.

@Test
public void createListAndDeletePersistentVolumeClaim() throws Exception {
    if (ObjectHelper.isEmpty(authToken)) {
        return;
    }
    Exchange ex = template.request("direct:create", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_CLAIM_NAME, "test");
            Map<String, String> labels = new HashMap<String, String>();
            labels.put("this", "rocks");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUMES_CLAIMS_LABELS, labels);
            PersistentVolumeClaimSpec pvcSpec = new PersistentVolumeClaimSpec();
            ResourceRequirements rr = new ResourceRequirements();
            Map<String, Quantity> mp = new HashMap<String, Quantity>();
            mp.put("storage", new Quantity("100"));
            rr.setLimits(mp);
            Map<String, Quantity> req = new HashMap<String, Quantity>();
            req.put("storage", new Quantity("100"));
            rr.setRequests(req);
            pvcSpec.setResources(rr);
            pvcSpec.setVolumeName("vol001");
            List<String> access = new ArrayList<String>();
            access.add("ReadWriteOnce");
            pvcSpec.setAccessModes(access);
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_CLAIM_SPEC, pvcSpec);
        }
    });
    PersistentVolumeClaim pvc = ex.getOut().getBody(PersistentVolumeClaim.class);
    assertEquals(pvc.getMetadata().getName(), "test");
    ex = template.request("direct:listByLabels", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            Map<String, String> labels = new HashMap<String, String>();
            labels.put("this", "rocks");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUMES_CLAIMS_LABELS, labels);
        }
    });
    List<PersistentVolumeClaim> result = ex.getOut().getBody(List.class);
    boolean pvcExists = false;
    Iterator<PersistentVolumeClaim> it = result.iterator();
    while (it.hasNext()) {
        PersistentVolumeClaim pvcLocal = it.next();
        if ("test".equalsIgnoreCase(pvcLocal.getMetadata().getName())) {
            pvcExists = true;
        }
    }
    assertTrue(pvcExists);
    ex = template.request("direct:delete", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_PERSISTENT_VOLUME_CLAIM_NAME, "test");
        }
    });
    boolean pvcDeleted = ex.getOut().getBody(Boolean.class);
    assertTrue(pvcDeleted);
}
Also used : Processor(org.apache.camel.Processor) HashMap(java.util.HashMap) Quantity(io.fabric8.kubernetes.api.model.Quantity) ResourceRequirements(io.fabric8.kubernetes.api.model.ResourceRequirements) Exchange(org.apache.camel.Exchange) PersistentVolumeClaimSpec(io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpec) ArrayList(java.util.ArrayList) List(java.util.List) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with PersistentVolumeClaimSpec

use of io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpec 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)

Aggregations

PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)2 PersistentVolumeClaimSpec (io.fabric8.kubernetes.api.model.PersistentVolumeClaimSpec)2 DoneablePersistentVolumeClaim (io.fabric8.kubernetes.api.model.DoneablePersistentVolumeClaim)1 PersistentVolumeClaimBuilder (io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder)1 Quantity (io.fabric8.kubernetes.api.model.Quantity)1 ResourceRequirements (io.fabric8.kubernetes.api.model.ResourceRequirements)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Exchange (org.apache.camel.Exchange)1 Processor (org.apache.camel.Processor)1 Test (org.junit.Test)1