Search in sources :

Example 1 with ResourceQuotaSpec

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

the class KubernetesResourcesQuotaProducer method doCreateResourceQuota.

protected void doCreateResourceQuota(Exchange exchange, String operation) throws Exception {
    ResourceQuota rq = null;
    String rqName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_RESOURCES_QUOTA_NAME, String.class);
    String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
    ResourceQuotaSpec rqSpec = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_RESOURCE_QUOTA_SPEC, ResourceQuotaSpec.class);
    if (ObjectHelper.isEmpty(rqName)) {
        LOG.error("Create a specific resource quota require specify a resource quota name");
        throw new IllegalArgumentException("Create a specific resource quota require specify a resource quota name");
    }
    if (ObjectHelper.isEmpty(namespaceName)) {
        LOG.error("Create a specific resource quota require specify a namespace name");
        throw new IllegalArgumentException("Create a specific resource quota require specify a namespace name");
    }
    if (ObjectHelper.isEmpty(rqSpec)) {
        LOG.error("Create a specific resource quota require specify a resource quota spec bean");
        throw new IllegalArgumentException("Create a specific resource quota require specify a resource quota spec bean");
    }
    Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_RESOURCES_QUOTA_LABELS, Map.class);
    ResourceQuota rqCreating = new ResourceQuotaBuilder().withNewMetadata().withName(rqName).withLabels(labels).endMetadata().withSpec(rqSpec).build();
    rq = getEndpoint().getKubernetesClient().resourceQuotas().inNamespace(namespaceName).create(rqCreating);
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
    exchange.getOut().setBody(rq);
}
Also used : DoneableResourceQuota(io.fabric8.kubernetes.api.model.DoneableResourceQuota) ResourceQuota(io.fabric8.kubernetes.api.model.ResourceQuota) ResourceQuotaSpec(io.fabric8.kubernetes.api.model.ResourceQuotaSpec) ResourceQuotaBuilder(io.fabric8.kubernetes.api.model.ResourceQuotaBuilder)

Example 2 with ResourceQuotaSpec

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

the class KubernetesResourcesQuotaProducerTest method createAndDeleteResourceQuota.

@Test
public void createAndDeleteResourceQuota() 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_RESOURCES_QUOTA_NAME, "test");
            Map<String, String> labels = new HashMap<String, String>();
            labels.put("this", "rocks");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_RESOURCES_QUOTA_LABELS, labels);
            ResourceQuotaSpec rsSpec = new ResourceQuotaSpec();
            Map<String, Quantity> mp = new HashMap<String, Quantity>();
            mp.put("pods", new Quantity("100"));
            rsSpec.setHard(mp);
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_RESOURCE_QUOTA_SPEC, rsSpec);
        }
    });
    ResourceQuota rs = ex.getOut().getBody(ResourceQuota.class);
    assertEquals(rs.getMetadata().getName(), "test");
    ex = template.request("direct:get", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_RESOURCES_QUOTA_NAME, "test");
        }
    });
    ResourceQuota rsGet = ex.getOut().getBody(ResourceQuota.class);
    assertEquals(rsGet.getMetadata().getName(), "test");
    assertEquals(rsGet.getSpec().getHard().get("pods"), new Quantity("100"));
    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_RESOURCES_QUOTA_NAME, "test");
        }
    });
    boolean rqDeleted = ex.getOut().getBody(Boolean.class);
    assertTrue(rqDeleted);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) ResourceQuota(io.fabric8.kubernetes.api.model.ResourceQuota) ResourceQuotaSpec(io.fabric8.kubernetes.api.model.ResourceQuotaSpec) Quantity(io.fabric8.kubernetes.api.model.Quantity) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

ResourceQuota (io.fabric8.kubernetes.api.model.ResourceQuota)2 ResourceQuotaSpec (io.fabric8.kubernetes.api.model.ResourceQuotaSpec)2 DoneableResourceQuota (io.fabric8.kubernetes.api.model.DoneableResourceQuota)1 Quantity (io.fabric8.kubernetes.api.model.Quantity)1 ResourceQuotaBuilder (io.fabric8.kubernetes.api.model.ResourceQuotaBuilder)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Exchange (org.apache.camel.Exchange)1 Processor (org.apache.camel.Processor)1 Test (org.junit.Test)1