Search in sources :

Example 11 with Quantity

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

the class OperatorValidationSteps method checkDbPvCapacity.

@Then("check that {string} persistent volume capacity is greater or equals to {string}")
public void checkDbPvCapacity(String pvc, String expected) {
    try {
        OpenShiftWaitUtils.waitFor(() -> OpenShiftUtils.getInstance().getPersistentVolumeClaim(pvc) != null);
        OpenShiftWaitUtils.waitFor(() -> OpenShiftUtils.getInstance().getPersistentVolumeClaim(pvc).getStatus().getPhase().equals("Bound"));
    } catch (TimeoutException | InterruptedException e) {
        fail("Unable to get " + pvc + " pvc: ", e);
    }
    final Quantity capacity = OpenShiftUtils.getInstance().getPersistentVolumeClaim(pvc).getStatus().getCapacity().get("storage");
    assertThat(capacity.getFormat()).isEqualTo("Gi");
    assertThat(Integer.parseInt(capacity.getAmount())).isGreaterThanOrEqualTo(Integer.parseInt(expected.replaceAll("[a-zA-Z]", "")));
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) TimeoutException(java.util.concurrent.TimeoutException) Then(io.cucumber.java.en.Then)

Example 12 with Quantity

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

the class OperatorValidationSteps method createPv.

@Given("create test persistent volumes with {string} storage class name")
public void createPv(String className) {
    // Create 3 PVs, two with higher capacity that will be used by meta and prometheus, because the binding of PV is FCFS,
    // so they theoretically can steal the test-pv from db
    // These volumes won't work, but they will be available to bind
    Map<String, Quantity> capacity = new HashMap<>();
    capacity.put("storage", new Quantity("3Gi"));
    if (!"".equals(className) && OpenShiftUtils.getInstance().storage().storageClasses().withName(className).get() == null) {
        log.info("Creating storage class " + className);
        OpenShiftUtils.getInstance().storage().storageClasses().createOrReplaceWithNew().withNewMetadata().withName(className).endMetadata().withProvisioner("kubernetes.io/cinder").done();
    }
    PersistentVolumeFluent.SpecNested<DoneablePersistentVolume> pv = OpenShiftUtils.getInstance().persistentVolumes().createOrReplaceWithNew().withNewMetadata().withName(TEST_PV_NAME).withLabels(TestUtils.map("operator", "test")).endMetadata().withNewSpec().withAccessModes("ReadOnlyMany", "ReadWriteOnce", "ReadWriteMany").withCapacity(capacity).withNewNfs().withNewServer("testServer").withNewPath("/testPath").endNfs();
    // The default storage class for OCP3 is empty, for OCP4 is "standard", so if the className is empty, we should use the default one
    if ("".equals(className)) {
        if (!OpenShiftUtils.isOpenshift3()) {
            if (OpenShiftUtils.isOSD()) {
                pv.withStorageClassName("gp2");
            } else {
                pv.withStorageClassName("standard");
            }
        }
    } else {
        pv.withStorageClassName(className);
    }
    pv.endSpec().done();
    capacity.put("storage", new Quantity("5Gi"));
    for (int i = 1; i < 3; i++) {
        pv = OpenShiftUtils.getInstance().persistentVolumes().createOrReplaceWithNew().withNewMetadata().withName(TEST_PV_NAME + "-" + i).endMetadata().withNewSpec().withAccessModes("ReadWriteOnce").withCapacity(capacity).withNewNfs().withNewServer("testServer").withNewPath("/testPath").endNfs();
        if (!OpenShiftUtils.isOpenshift3()) {
            // This should always be the default value despite the actual value of className - that is used only in "test-pv" intentionally
            if (OpenShiftUtils.isOSD()) {
                pv.withStorageClassName("gp2");
            } else {
                pv.withStorageClassName("standard");
            }
        }
        pv.endSpec().done();
    }
}
Also used : HashMap(java.util.HashMap) Quantity(io.fabric8.kubernetes.api.model.Quantity) DoneablePersistentVolume(io.fabric8.kubernetes.api.model.DoneablePersistentVolume) IntegrationsEndpoint(io.syndesis.qe.endpoint.IntegrationsEndpoint) PersistentVolumeFluent(io.fabric8.kubernetes.api.model.PersistentVolumeFluent) Given(io.cucumber.java.en.Given)

Example 13 with Quantity

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

the class OpenshiftValidationSteps method verifyResourceValues.

@Then("^verify that the deployment config \"([^\"]*)\" has the (cpu|memory) (limits|requests) set to \"([^\"]*)\"$")
public void verifyResourceValues(String dcName, String which, String what, String value) {
    ResourceRequirements resources = OpenShiftUtils.getInstance().getDeploymentConfig(dcName).getSpec().getTemplate().getSpec().getContainers().get(0).getResources();
    Map<String, Quantity> check = "limits".equals(what) ? resources.getLimits() : resources.getRequests();
    assertThat(check).isNotNull();
    assertThat(check).containsKey(which);
    assertThat(check.get(which).getAmount() + check.get(which).getFormat()).isEqualTo(value);
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) ResourceRequirements(io.fabric8.kubernetes.api.model.ResourceRequirements) Then(io.cucumber.java.en.Then)

Example 14 with Quantity

use of io.fabric8.kubernetes.api.model.Quantity in project flink by apache.

the class KubernetesUtils method getResourceRequirements.

/**
 * Get resource requirements from memory and cpu.
 *
 * @param resourceRequirements resource requirements in pod template
 * @param mem Memory in mb.
 * @param memoryLimitFactor limit factor for the memory, used to set the limit resources.
 * @param cpu cpu.
 * @param cpuLimitFactor limit factor for the cpu, used to set the limit resources.
 * @param externalResources external resources
 * @param externalResourceConfigKeys config keys of external resources
 * @return KubernetesResource requirements.
 */
public static ResourceRequirements getResourceRequirements(ResourceRequirements resourceRequirements, int mem, double memoryLimitFactor, double cpu, double cpuLimitFactor, Map<String, ExternalResource> externalResources, Map<String, String> externalResourceConfigKeys) {
    final Quantity cpuQuantity = new Quantity(String.valueOf(cpu));
    final Quantity cpuLimitQuantity = new Quantity(String.valueOf(cpu * cpuLimitFactor));
    final Quantity memQuantity = new Quantity(mem + Constants.RESOURCE_UNIT_MB);
    final Quantity memQuantityLimit = new Quantity(((int) (mem * memoryLimitFactor)) + Constants.RESOURCE_UNIT_MB);
    ResourceRequirementsBuilder resourceRequirementsBuilder = new ResourceRequirementsBuilder(resourceRequirements).addToRequests(Constants.RESOURCE_NAME_MEMORY, memQuantity).addToRequests(Constants.RESOURCE_NAME_CPU, cpuQuantity).addToLimits(Constants.RESOURCE_NAME_MEMORY, memQuantityLimit).addToLimits(Constants.RESOURCE_NAME_CPU, cpuLimitQuantity);
    // Add the external resources to resource requirement.
    for (Map.Entry<String, ExternalResource> externalResource : externalResources.entrySet()) {
        final String configKey = externalResourceConfigKeys.get(externalResource.getKey());
        if (!StringUtils.isNullOrWhitespaceOnly(configKey)) {
            final Quantity resourceQuantity = new Quantity(String.valueOf(externalResource.getValue().getValue().longValue()));
            resourceRequirementsBuilder.addToRequests(configKey, resourceQuantity).addToLimits(configKey, resourceQuantity);
            LOG.info("Request external resource {} with config key {}.", resourceQuantity.getAmount(), configKey);
        }
    }
    return resourceRequirementsBuilder.build();
}
Also used : ResourceRequirementsBuilder(io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder) Quantity(io.fabric8.kubernetes.api.model.Quantity) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) Map(java.util.Map) HashMap(java.util.HashMap) ExternalResource(org.apache.flink.api.common.resources.ExternalResource)

Example 15 with Quantity

use of io.fabric8.kubernetes.api.model.Quantity in project flink by apache.

the class KubernetesJobManagerFactoryTest method testPodSpec.

@Test
public void testPodSpec() throws IOException {
    kubernetesJobManagerSpecification = KubernetesJobManagerFactory.buildKubernetesJobManagerSpecification(flinkPod, kubernetesJobManagerParameters);
    final PodSpec resultPodSpec = this.kubernetesJobManagerSpecification.getDeployment().getSpec().getTemplate().getSpec();
    assertEquals(1, resultPodSpec.getContainers().size());
    assertEquals(SERVICE_ACCOUNT_NAME, resultPodSpec.getServiceAccountName());
    assertEquals(3, resultPodSpec.getVolumes().size());
    final Container resultedMainContainer = resultPodSpec.getContainers().get(0);
    assertEquals(Constants.MAIN_CONTAINER_NAME, resultedMainContainer.getName());
    assertEquals(CONTAINER_IMAGE, resultedMainContainer.getImage());
    assertEquals(CONTAINER_IMAGE_PULL_POLICY.name(), resultedMainContainer.getImagePullPolicy());
    assertEquals(3, resultedMainContainer.getEnv().size());
    assertTrue(resultedMainContainer.getEnv().stream().anyMatch(envVar -> envVar.getName().equals("key1")));
    assertEquals(3, resultedMainContainer.getPorts().size());
    final Map<String, Quantity> requests = resultedMainContainer.getResources().getRequests();
    assertEquals(Double.toString(JOB_MANAGER_CPU), requests.get("cpu").getAmount());
    assertEquals(String.valueOf(JOB_MANAGER_MEMORY), requests.get("memory").getAmount());
    assertEquals(1, resultedMainContainer.getCommand().size());
    // The args list is [bash, -c, 'java -classpath $FLINK_CLASSPATH ...'].
    assertEquals(3, resultedMainContainer.getArgs().size());
    assertEquals(3, resultedMainContainer.getVolumeMounts().size());
}
Also used : Quantity(io.fabric8.kubernetes.api.model.Quantity) SecurityOptions(org.apache.flink.configuration.SecurityOptions) KubernetesTestUtils(org.apache.flink.kubernetes.KubernetesTestUtils) KubernetesJobManagerTestBase(org.apache.flink.kubernetes.kubeclient.KubernetesJobManagerTestBase) CONFIG_FILE_LOG4J_NAME(org.apache.flink.kubernetes.utils.Constants.CONFIG_FILE_LOG4J_NAME) Map(java.util.Map) ExternalServiceDecorator(org.apache.flink.kubernetes.kubeclient.decorators.ExternalServiceDecorator) DeploymentSpec(io.fabric8.kubernetes.api.model.apps.DeploymentSpec) KubernetesUtils(org.apache.flink.kubernetes.utils.KubernetesUtils) KubernetesConfigOptions(org.apache.flink.kubernetes.configuration.KubernetesConfigOptions) FlinkPod(org.apache.flink.kubernetes.kubeclient.FlinkPod) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) Base64(java.util.Base64) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Secret(io.fabric8.kubernetes.api.model.Secret) Matchers.is(org.hamcrest.Matchers.is) Constants(org.apache.flink.kubernetes.utils.Constants) Container(io.fabric8.kubernetes.api.model.Container) KubernetesConfigOptionsInternal(org.apache.flink.kubernetes.configuration.KubernetesConfigOptionsInternal) HashMap(java.util.HashMap) OwnerReference(io.fabric8.kubernetes.api.model.OwnerReference) KubernetesHaServicesFactory(org.apache.flink.kubernetes.highavailability.KubernetesHaServicesFactory) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) DeploymentOptions(org.apache.flink.configuration.DeploymentOptions) FlinkConfMountDecorator(org.apache.flink.kubernetes.kubeclient.decorators.FlinkConfMountDecorator) Service(io.fabric8.kubernetes.api.model.Service) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) KubernetesDeploymentTarget(org.apache.flink.kubernetes.configuration.KubernetesDeploymentTarget) FLINK_CONF_FILENAME(org.apache.flink.configuration.GlobalConfiguration.FLINK_CONF_FILENAME) InternalServiceDecorator(org.apache.flink.kubernetes.kubeclient.decorators.InternalServiceDecorator) Assert.assertNotNull(org.junit.Assert.assertNotNull) KubernetesJobManagerSpecification(org.apache.flink.kubernetes.kubeclient.KubernetesJobManagerSpecification) Matchers(org.hamcrest.Matchers) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) KubernetesSessionClusterEntrypoint(org.apache.flink.kubernetes.entrypoint.KubernetesSessionClusterEntrypoint) HadoopConfMountDecorator(org.apache.flink.kubernetes.kubeclient.decorators.HadoopConfMountDecorator) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) KerberosMountDecorator(org.apache.flink.kubernetes.kubeclient.decorators.KerberosMountDecorator) Assert.assertNull(org.junit.Assert.assertNull) HeadlessClusterIPService(org.apache.flink.kubernetes.kubeclient.services.HeadlessClusterIPService) CONFIG_FILE_LOGBACK_NAME(org.apache.flink.kubernetes.utils.Constants.CONFIG_FILE_LOGBACK_NAME) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) Collections(java.util.Collections) HighAvailabilityOptions(org.apache.flink.configuration.HighAvailabilityOptions) Assert.assertEquals(org.junit.Assert.assertEquals) Container(io.fabric8.kubernetes.api.model.Container) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Quantity(io.fabric8.kubernetes.api.model.Quantity) 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