Search in sources :

Example 6 with Quantity

use of io.fabric8.kubernetes.api.model.Quantity in project syndesis by syndesisio.

the class OpenShiftServiceImplTest method testDeploy.

@SuppressWarnings({ "PMD.ExcessiveMethodLength", "PMD.JUnitTestsShouldIncludeAssert" })
@Test
public void testDeploy() {
    String name = "test-deployment";
    OpenShiftConfigurationProperties config = new OpenShiftConfigurationProperties();
    OpenShiftServiceImpl service = new OpenShiftServiceImpl(client, config);
    DeploymentData deploymentData = new DeploymentData.Builder().addAnnotation("testName", testName.getMethodName()).addLabel("type", "test").addSecretEntry("secret-key", "secret-val").withImage("testimage").build();
    ImageStream expectedImageStream = new ImageStreamBuilder().withNewMetadata().withName(name).endMetadata().build();
    Secret expectedSecret = new SecretBuilder().withNewMetadata().withName(name).addToAnnotations(deploymentData.getAnnotations()).addToLabels(deploymentData.getLabels()).endMetadata().withStringData(deploymentData.getSecret()).build();
    DeploymentConfig expectedDeploymentConfig = new DeploymentConfigBuilder().withNewMetadata().withName(OpenShiftServiceImpl.openshiftName(name)).addToAnnotations(deploymentData.getAnnotations()).addToLabels(deploymentData.getLabels()).endMetadata().withNewSpec().withReplicas(1).addToSelector("integration", name).withNewStrategy().withType("Recreate").withNewResources().addToLimits("memory", new Quantity(config.getDeploymentMemoryLimitMi() + "Mi")).addToRequests("memory", new Quantity(config.getDeploymentMemoryRequestMi() + "Mi")).endResources().endStrategy().withRevisionHistoryLimit(0).withNewTemplate().withNewMetadata().addToLabels("integration", name).addToLabels(OpenShiftServiceImpl.COMPONENT_LABEL, "integration").addToLabels(deploymentData.getLabels()).addToAnnotations(deploymentData.getAnnotations()).addToAnnotations("prometheus.io/scrape", "true").addToAnnotations("prometheus.io/port", "9779").endMetadata().withNewSpec().addNewContainer().withImage(deploymentData.getImage()).withImagePullPolicy("Always").withName(name).addToEnv(new EnvVarBuilder().withName("LOADER_HOME").withValue(config.getIntegrationDataPath()).build()).addToEnv(new EnvVarBuilder().withName("AB_JMX_EXPORTER_CONFIG").withValue("/tmp/src/prometheus-config.yml").build()).addNewPort().withName("jolokia").withContainerPort(8778).endPort().addNewVolumeMount().withName("secret-volume").withMountPath("/deployments/config").withReadOnly(false).endVolumeMount().endContainer().addNewVolume().withName("secret-volume").withNewSecret().withSecretName(expectedSecret.getMetadata().getName()).endSecret().endVolume().endSpec().endTemplate().addNewTrigger().withType("ConfigChange").endTrigger().endSpec().withNewStatus().withLatestVersion(1L).endStatus().build();
    server.expect().withPath("/oapi/v1/namespaces/test/imagestreams").andReturn(200, expectedImageStream).always();
    server.expect().withPath("/api/v1/namespaces/test/secrets").andReturn(200, expectedSecret).always();
    server.expect().withPath("/oapi/v1/namespaces/test/deploymentconfigs").andReturn(200, expectedDeploymentConfig).always();
    server.expect().withPath("/oapi/v1/namespaces/test/deploymentconfigs/i-test-deployment").andReturn(200, expectedDeploymentConfig).always();
    server.expect().withPath("/oapi/v1/namespaces/test/deploymentconfigs/test-deployment").andReturn(200, expectedDeploymentConfig).always();
    service.deploy(name, deploymentData);
}
Also used : DeploymentConfigBuilder(io.fabric8.openshift.api.model.DeploymentConfigBuilder) ImageStream(io.fabric8.openshift.api.model.ImageStream) Quantity(io.fabric8.kubernetes.api.model.Quantity) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) ImageStreamBuilder(io.fabric8.openshift.api.model.ImageStreamBuilder) Secret(io.fabric8.kubernetes.api.model.Secret) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) Test(org.junit.Test)

Example 7 with Quantity

use of io.fabric8.kubernetes.api.model.Quantity in project syndesis-qe by syndesisio.

the class OperatorValidationSteps method checkMemoryLimits.

@Then("check correct memory limits")
public void checkMemoryLimits() {
    final String file = "src/test/resources/operator/spec/components/resources.limits.requests.memory.cpu.yml";
    Yaml yaml = new Yaml();
    Object data = null;
    try (FileInputStream fis = FileUtils.openInputStream(new File(file))) {
        data = yaml.load(fis);
    } catch (IOException e) {
        fail("Unable to load file " + file, e);
    }
    SoftAssertions softAssertions = new SoftAssertions();
    JSONObject components = new JSONObject((Map) data).getJSONObject("spec").getJSONObject("components");
    components.keySet().forEach(component -> {
        String expectedMemoryLimit = components.getJSONObject(component).getJSONObject("resources").getJSONObject("limit").getString("memory");
        String expectedCpuLimit = components.getJSONObject(component).getJSONObject("resources").getJSONObject("limit").getString("cpu");
        String expectedMemoryRequests = components.getJSONObject(component).getJSONObject("resources").getJSONObject("request").getString("memory");
        String expectedCpuRequests = components.getJSONObject(component).getJSONObject("resources").getJSONObject("request").getString("cpu");
        List<DeploymentConfig> dcList = OpenShiftUtils.getInstance().deploymentConfigs().withLabel("syndesis.io/component", "syndesis-" + ("database".equals(component) ? "db" : component)).list().getItems();
        softAssertions.assertThat(dcList).hasSize(1);
        final Quantity currentMemoryLimit = dcList.get(0).getSpec().getTemplate().getSpec().getContainers().get(0).getResources().getLimits().get("memory");
        softAssertions.assertThat(currentMemoryLimit).as(component + " memory limit is null").isNotNull();
        if (currentMemoryLimit != null) {
            softAssertions.assertThat(currentMemoryLimit.getAmount() + currentMemoryLimit.getFormat()).as(component + " memory limit").isEqualTo(expectedMemoryLimit);
        }
        final Quantity currentCpuLimit = dcList.get(0).getSpec().getTemplate().getSpec().getContainers().get(0).getResources().getLimits().get("cpu");
        softAssertions.assertThat(currentCpuLimit).as(component + " cpu limit is null").isNotNull();
        if (currentCpuLimit != null) {
            softAssertions.assertThat(currentCpuLimit.getAmount() + currentCpuLimit.getFormat()).as(component + " cpu limit").isEqualTo(expectedCpuLimit);
        }
        final Quantity currentMemoryRequests = dcList.get(0).getSpec().getTemplate().getSpec().getContainers().get(0).getResources().getRequests().get("memory");
        softAssertions.assertThat(currentMemoryRequests).as(component + " memory requests is null").isNotNull();
        if (currentMemoryRequests != null) {
            softAssertions.assertThat(currentMemoryRequests.getAmount() + currentMemoryRequests.getFormat()).as(component + " memory requests").isEqualTo(expectedMemoryRequests);
        }
        final Quantity currentCpuRequests = dcList.get(0).getSpec().getTemplate().getSpec().getContainers().get(0).getResources().getRequests().get("cpu");
        softAssertions.assertThat(currentCpuRequests).as(component + " cpu requests is null").isNotNull();
        if (currentCpuRequests != null) {
            softAssertions.assertThat(currentCpuRequests.getAmount() + currentCpuRequests.getFormat()).as(component + " cpu requests").isEqualTo(expectedCpuRequests);
        }
    });
    softAssertions.assertAll();
}
Also used : JSONObject(org.json.JSONObject) SoftAssertions(org.assertj.core.api.SoftAssertions) Quantity(io.fabric8.kubernetes.api.model.Quantity) JSONObject(org.json.JSONObject) IOException(java.io.IOException) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) ZipFile(java.util.zip.ZipFile) File(java.io.File) Yaml(org.yaml.snakeyaml.Yaml) FileInputStream(java.io.FileInputStream) Then(io.cucumber.java.en.Then)

Example 8 with Quantity

use of io.fabric8.kubernetes.api.model.Quantity in project syndesis-qe by syndesisio.

the class OpenshiftValidationSteps method setResourceValues.

@When("set resources for deployment config {string}")
public void setResourceValues(String dcName, DataTable dataTable) {
    DeploymentConfig dc = OpenShiftUtils.getInstance().getDeploymentConfig(dcName);
    Map<String, Quantity> currentLimits = dc.getSpec().getTemplate().getSpec().getContainers().get(0).getResources().getLimits();
    Map<String, Quantity> currentRequests = dc.getSpec().getTemplate().getSpec().getContainers().get(0).getResources().getRequests();
    Map<String, Quantity> limits = currentLimits == null ? new HashMap<>() : new HashMap<>(currentLimits);
    Map<String, Quantity> requests = currentRequests == null ? new HashMap<>() : new HashMap<>(currentRequests);
    for (List<String> l : dataTable.asLists()) {
        if ("limits".equals(l.get(0))) {
            limits.put(l.get(1), new Quantity(l.get(2)));
        } else {
            requests.put(l.get(1), new Quantity(l.get(2)));
        }
    }
    // @formatter:off
    OpenShiftUtils.getInstance().deploymentConfigs().withName(dcName).edit().editSpec().editTemplate().editSpec().editFirstContainer().editResources().withLimits(limits).withRequests(requests).endResources().endContainer().endSpec().endTemplate().endSpec().done();
// @formatter:on
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) When(io.cucumber.java.en.When)

Example 9 with Quantity

use of io.fabric8.kubernetes.api.model.Quantity 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 10 with Quantity

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

Quantity (io.fabric8.kubernetes.api.model.Quantity)20 HashMap (java.util.HashMap)8 Test (org.junit.Test)7 ResourceRequirements (io.fabric8.kubernetes.api.model.ResourceRequirements)6 Map (java.util.Map)6 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)5 Then (io.cucumber.java.en.Then)3 PersistentVolumeClaimBuilder (io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder)3 ResourceRequirementsBuilder (io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder)3 Service (io.fabric8.kubernetes.api.model.Service)3 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)3 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)2 ContainerPortBuilder (io.fabric8.kubernetes.api.model.ContainerPortBuilder)2 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)2 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)2 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)2 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)2 ServicePortBuilder (io.fabric8.kubernetes.api.model.ServicePortBuilder)2 ServiceSpecBuilder (io.fabric8.kubernetes.api.model.ServiceSpecBuilder)2 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)2