io.fabric8.kubernetes.api.model.Quantity Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.Quantity. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ResourceQuotaTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuild() {
  server.expect().withPath("/api/v1/namespaces/myspace/resourcequotas/compute-quota").andReturn(200, new ResourceQuotaBuilder()
    .withNewMetadata().withName("compute-quota").withNamespace("myspace").endMetadata()
    .withNewSpec().addToHard("pods", new Quantity("2"))
    .addToHard("requests.cpu", new Quantity("1"))
    .addToHard("limits.cpu", new Quantity("2")).endSpec().build()).once();

  KubernetesClient client = server.getClient();

  Deployment deployment = client.apps().deployments().load(getClass().getResourceAsStream("/test-resourcequota-deployment.yml")).get();
  server.expect().withPath("/apis/apps/v1/namespaces/myspace/deployments/deployment").andReturn(200, deployment).once();

  ResourceQuota resourcequota = client.resourceQuotas().inNamespace("myspace").withName("compute-quota").get();
  assertNotNull(resourcequota);

  deployment = client.apps().deployments().inNamespace("myspace").withName("deployment").get();
  assertNotNull(deployment);
}
 
Example #2
Source File: KubernetesEnvironmentFactoryTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
private static PodData createPodData(String machineName, long ramLimit, long ramRequest) {
  final String containerName = "container_" + machineName;
  final Container containerMock = mock(Container.class);
  final ResourceRequirements resourcesMock = mock(ResourceRequirements.class);
  final Quantity limitQuantityMock = mock(Quantity.class);
  final Quantity requestQuantityMock = mock(Quantity.class);
  final PodSpec specMock = mock(PodSpec.class);
  final ObjectMeta metadataMock = mock(ObjectMeta.class);
  when(limitQuantityMock.getAmount()).thenReturn(String.valueOf(ramLimit));
  when(requestQuantityMock.getAmount()).thenReturn(String.valueOf(ramRequest));
  when(resourcesMock.getLimits()).thenReturn(ImmutableMap.of("memory", limitQuantityMock));
  when(resourcesMock.getRequests()).thenReturn(ImmutableMap.of("memory", requestQuantityMock));
  when(containerMock.getName()).thenReturn(containerName);
  when(containerMock.getResources()).thenReturn(resourcesMock);
  when(metadataMock.getAnnotations())
      .thenReturn(Names.createMachineNameAnnotations(containerName, machineName));
  when(specMock.getContainers()).thenReturn(ImmutableList.of(containerMock));
  return new PodData(specMock, metadataMock);
}
 
Example #3
Source File: KubernetesObjectUtil.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Returns new instance of {@link PersistentVolumeClaim} with specified name, accessMode, quantity
 * and storageClassName.
 */
public static PersistentVolumeClaim newPVC(
    String name, String accessMode, String quantity, String storageClassName) {
  SpecNested<PersistentVolumeClaimBuilder> specs =
      new PersistentVolumeClaimBuilder()
          .withNewMetadata()
          .withName(name)
          .endMetadata()
          .withNewSpec()
          .withAccessModes(accessMode);
  if (!isNullOrEmpty(storageClassName)) {
    specs.withStorageClassName(storageClassName);
  }
  return specs
      .withNewResources()
      .withRequests(ImmutableMap.of(STORAGE_PARAM, new Quantity(quantity)))
      .endResources()
      .endSpec()
      .build();
}
 
Example #4
Source File: TektonHandler.java    From dekorate with Apache License 2.0 6 votes vote down vote up
public PersistentVolumeClaim createM2WorkspacePvc(TektonConfig config) {
  Map<String, Quantity> requests = new HashMap<String, Quantity>() {{
      put("storage", new QuantityBuilder().withAmount(String.valueOf(config.getM2WorkspaceClaim().getSize())).withFormat(config.getM2WorkspaceClaim().getUnit()).build());
  }};
  LabelSelector selector = null;
  if (config.getM2WorkspaceClaim().getMatchLabels().length != 0) {
    selector = new LabelSelectorBuilder()
      .withMatchLabels(Arrays.stream(config.getM2WorkspaceClaim().getMatchLabels()).collect(Collectors.toMap(l -> l.getKey(), l -> l.getValue())))
      .build();
  }

  return new PersistentVolumeClaimBuilder()
    .withNewMetadata()
    .withName(m2WorkspaceClaimName(config))
    .endMetadata()
    .withNewSpec()
    .withAccessModes(config.getM2WorkspaceClaim().getAccessMode().name())
    .withStorageClassName(config.getM2WorkspaceClaim().getStorageClass())
    .withNewResources().withRequests(requests).endResources()
    .withSelector(selector)
    .endSpec()
    .build();
}
 
Example #5
Source File: TektonHandler.java    From dekorate with Apache License 2.0 6 votes vote down vote up
public PersistentVolumeClaim createSourceWorkspacePvc(TektonConfig config) {
  Map<String, Quantity> requests = new HashMap<String, Quantity>() {{
      put("storage", new QuantityBuilder().withAmount(String.valueOf(config.getSourceWorkspaceClaim().getSize())).withFormat(config.getSourceWorkspaceClaim().getUnit()).build());
  }};
  LabelSelector selector = null;


  if (config.getSourceWorkspaceClaim().getMatchLabels().length != 0) {
    selector = new LabelSelectorBuilder()
      .withMatchLabels(Arrays.stream(config.getSourceWorkspaceClaim().getMatchLabels()).collect(Collectors.toMap(l -> l.getKey(), l -> l.getValue())))
      .build();
  }
  return new PersistentVolumeClaimBuilder()
    .withNewMetadata()
    .withName(sourceWorkspaceClaimName(config))
    .endMetadata()
    .withNewSpec()
    .withAccessModes(config.getSourceWorkspaceClaim().getAccessMode().name())
    .withStorageClassName(config.getSourceWorkspaceClaim().getStorageClass())
    .withNewResources().withRequests(requests).endResources()
    .withSelector(selector)
    .endSpec()
    .build();
}
 
Example #6
Source File: ContainerResourceProvisionerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testOverridesContainerRamLimitAndRequestFromMachineAttribute() throws Exception {
  ResourceRequirements resourceRequirements =
      new ResourceRequirementsBuilder()
          .addToLimits(of("memory", new Quantity("3221225472"), "cpu", new Quantity("0.678")))
          .addToRequests(of("memory", new Quantity("1231231423"), "cpu", new Quantity("0.333")))
          .build();
  container.setResources(resourceRequirements);

  resourceProvisioner.provision(k8sEnv, identity);

  assertEquals(container.getResources().getLimits().get("memory").getAmount(), RAM_LIMIT_VALUE);
  assertEquals(container.getResources().getLimits().get("cpu").getAmount(), CPU_LIMIT_VALUE);
  assertEquals(
      container.getResources().getRequests().get("memory").getAmount(), RAM_REQUEST_VALUE);
  assertEquals(container.getResources().getRequests().get("cpu").getAmount(), CPU_REQUEST_VALUE);
}
 
Example #7
Source File: KafkaBridgeClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testResources() {
    Map<String, Quantity> requests = new HashMap<>(2);
    requests.put("cpu", new Quantity("250m"));
    requests.put("memory", new Quantity("512Mi"));

    Map<String, Quantity> limits = new HashMap<>(2);
    limits.put("cpu", new Quantity("500m"));
    limits.put("memory", new Quantity("1024Mi"));

    KafkaBridge resource = new KafkaBridgeBuilder(this.resource)
            .editSpec()
                .withResources(new ResourceRequirementsBuilder().withLimits(limits).withRequests(requests).build())
            .endSpec()
            .build();
    KafkaBridgeCluster kbc = KafkaBridgeCluster.fromCrd(resource, VERSIONS);

    Deployment dep = kbc.generateDeployment(Collections.EMPTY_MAP, true, null, null);
    Container cont = dep.getSpec().getTemplate().getSpec().getContainers().get(0);
    assertThat(cont.getResources().getLimits(), is(limits));
    assertThat(cont.getResources().getRequests(), is(requests));
}
 
Example #8
Source File: KafkaMirrorMakerClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testResources() {
    Map<String, Quantity> requests = new HashMap<>(2);
    requests.put("cpu", new Quantity("250m"));
    requests.put("memory", new Quantity("512Mi"));

    Map<String, Quantity> limits = new HashMap<>(2);
    limits.put("cpu", new Quantity("500m"));
    limits.put("memory", new Quantity("1024Mi"));

    KafkaMirrorMaker resource = new KafkaMirrorMakerBuilder(this.resource)
            .editSpec()
                .withResources(new ResourceRequirementsBuilder().withLimits(limits).withRequests(requests).build())
            .endSpec()
            .build();
    KafkaMirrorMakerCluster mmc = KafkaMirrorMakerCluster.fromCrd(resource, VERSIONS);

    Deployment dep = mmc.generateDeployment(emptyMap(), true, null, null);
    Container cont = dep.getSpec().getTemplate().getSpec().getContainers().get(0);
    assertThat(cont.getResources().getLimits(), is(limits));
    assertThat(cont.getResources().getRequests(), is(requests));
}
 
Example #9
Source File: PVCSubPathHelperTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testSetMemoryLimitAndRequest() throws Exception {
  when(podStatus.getPhase()).thenReturn(POD_PHASE_SUCCEEDED);

  pvcSubPathHelper.createDirs(
      identity, WORKSPACE_ID, PVC_NAME, emptyMap(), WORKSPACE_ID + PROJECTS_PATH);

  verify(osDeployments).create(podCaptor.capture());
  ResourceRequirements actual =
      podCaptor.getValue().getSpec().getContainers().get(0).getResources();
  ResourceRequirements expected =
      new ResourceRequirementsBuilder()
          .addToLimits(of("memory", new Quantity(jobMemoryLimit)))
          .addToRequests(of("memory", new Quantity(jobMemoryLimit)))
          .build();
  assertEquals(actual, expected);
  verify(osDeployments).wait(anyString(), anyInt(), any());
  verify(podStatus).getPhase();
  verify(osDeployments).delete(anyString());
  verify(securityContextProvisioner).provision(any());
}
 
Example #10
Source File: PodTemplateBuilderTest.java    From kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
@Test
@TestCaseName("{method}(directConnection={0})")
@Parameters({ "true", "false" })
public void testBuildFromYaml(boolean directConnection) throws Exception {
    cloud.setDirectConnection(directConnection);
    PodTemplate template = new PodTemplate();
    template.setYaml(loadYamlFile("pod-busybox.yaml"));
    setupStubs();
    Pod pod = new PodTemplateBuilder(template).withSlave(slave).build();
    validatePod(pod, directConnection);
    assertThat(pod.getMetadata().getLabels(), hasEntry("jenkins", "slave"));

    Map<String, Container> containers = toContainerMap(pod);
    assertEquals(2, containers.size());

    Container container0 = containers.get("busybox");
    assertNotNull(container0.getResources());
    assertNotNull(container0.getResources().getRequests());
    assertNotNull(container0.getResources().getLimits());
    assertThat(container0.getResources().getRequests(), hasEntry("example.com/dongle", new Quantity("42")));
    assertThat(container0.getResources().getLimits(), hasEntry("example.com/dongle", new Quantity("42")));
}
 
Example #11
Source File: PersistentVolumeClaimTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
void testBuild() {
  PersistentVolumeClaim persistentVolumeClaim = new PersistentVolumeClaimBuilder()
    .withNewMetadata().withName("test-pv-claim").withNamespace("test").endMetadata()
    .withNewSpec()
    .withStorageClassName("my-local-storage")
    .withAccessModes("ReadWriteOnce")
    .withNewResources()
    .addToRequests("storage", new Quantity("500Gi"))
    .endResources()
    .endSpec()
    .build();

  server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/test-pv-claim").andReturn(200, persistentVolumeClaim).once();

  KubernetesClient client = server.getClient();
  persistentVolumeClaim = client.persistentVolumeClaims().inNamespace("test").withName("test-pv-claim").get();
  assertNotNull(persistentVolumeClaim);
}
 
Example #12
Source File: CustomResourceStatusST.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
void testKafkaBridgeStatus() {
    String bridgeUrl = KafkaBridgeResources.url(CLUSTER_NAME, NAMESPACE, 8080);
    KafkaBridgeResource.kafkaBridge(CLUSTER_NAME, KafkaResources.plainBootstrapAddress(CLUSTER_NAME), 1).done();
    KafkaBridgeUtils.waitForKafkaBridgeReady(CLUSTER_NAME);
    assertKafkaBridgeStatus(1, bridgeUrl);

    KafkaBridgeResource.replaceBridgeResource(CLUSTER_NAME, kb -> kb.getSpec().setResources(new ResourceRequirementsBuilder()
            .addToRequests("cpu", new Quantity("100000000m"))
            .build()));
    KafkaBridgeUtils.waitForKafkaBridgeNotReady(CLUSTER_NAME);

    KafkaBridgeResource.replaceBridgeResource(CLUSTER_NAME, kb -> kb.getSpec().setResources(new ResourceRequirementsBuilder()
            .addToRequests("cpu", new Quantity("10m"))
            .build()));
    KafkaBridgeUtils.waitForKafkaBridgeReady(CLUSTER_NAME);
    assertKafkaBridgeStatus(3, bridgeUrl);
}
 
Example #13
Source File: KubernetesUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Get resource requirements from memory and cpu.
 *
 * @param mem Memory in mb.
 * @param cpu cpu.
 * @param externalResources external resources
 * @return KubernetesResource requirements.
 */
public static ResourceRequirements getResourceRequirements(int mem, double cpu, Map<String, Long> externalResources) {
	final Quantity cpuQuantity = new Quantity(String.valueOf(cpu));
	final Quantity memQuantity = new Quantity(mem + Constants.RESOURCE_UNIT_MB);

	ResourceRequirementsBuilder resourceRequirementsBuilder = new ResourceRequirementsBuilder()
		.addToRequests(Constants.RESOURCE_NAME_MEMORY, memQuantity)
		.addToRequests(Constants.RESOURCE_NAME_CPU, cpuQuantity)
		.addToLimits(Constants.RESOURCE_NAME_MEMORY, memQuantity)
		.addToLimits(Constants.RESOURCE_NAME_CPU, cpuQuantity);

	// Add the external resources to resource requirement.
	for (Map.Entry<String, Long> externalResource: externalResources.entrySet()) {
		final Quantity resourceQuantity = new Quantity(String.valueOf(externalResource.getValue()));
		resourceRequirementsBuilder
			.addToRequests(externalResource.getKey(), resourceQuantity)
			.addToLimits(externalResource.getKey(), resourceQuantity);
		LOG.info("Request external resource {} with config key {}.", resourceQuantity.getAmount(), externalResource.getKey());
	}

	return resourceRequirementsBuilder.build();
}
 
Example #14
Source File: KafkaMirrorMaker2ClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testResources() {
    Map<String, Quantity> requests = new HashMap<>(2);
    requests.put("cpu", new Quantity("250m"));
    requests.put("memory", new Quantity("512Mi"));

    Map<String, Quantity> limits = new HashMap<>(2);
    limits.put("cpu", new Quantity("500m"));
    limits.put("memory", new Quantity("1024Mi"));

    KafkaMirrorMaker2 resource = new KafkaMirrorMaker2Builder(this.resource)
            .editSpec()
                .withResources(new ResourceRequirementsBuilder().withLimits(limits).withRequests(requests).build())
            .endSpec()
            .build();
    KafkaMirrorMaker2Cluster kmm2 = KafkaMirrorMaker2Cluster.fromCrd(resource, VERSIONS);

    Deployment dep = kmm2.generateDeployment(Collections.EMPTY_MAP, true, null, null);
    Container cont = getContainer(dep);
    assertThat(cont.getResources().getLimits(), is(limits));
    assertThat(cont.getResources().getRequests(), is(requests));
}
 
Example #15
Source File: KafkaConnectS2IClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testResources() {
    Map<String, Quantity> requests = new HashMap<>(2);
    requests.put("cpu", new Quantity("250m"));
    requests.put("memory", new Quantity("512Mi"));

    Map<String, Quantity> limits = new HashMap<>(2);
    limits.put("cpu", new Quantity("500m"));
    limits.put("memory", new Quantity("1024Mi"));

    KafkaConnectS2I resource = new KafkaConnectS2IBuilder(this.resource)
            .editSpec()
                .withResources(new ResourceRequirementsBuilder().withLimits(limits).withRequests(requests).build())
            .endSpec()
            .build();
    KafkaConnectS2ICluster kc = KafkaConnectS2ICluster.fromCrd(resource, VERSIONS);

    DeploymentConfig dep = kc.generateDeploymentConfig(Collections.EMPTY_MAP, true, null, null);
    Container cont = dep.getSpec().getTemplate().getSpec().getContainers().get(0);
    assertThat(cont.getResources().getLimits(), is(limits));
    assertThat(cont.getResources().getRequests(), is(requests));
}
 
Example #16
Source File: CustomResourceStatusST.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
@OpenShiftOnly
@Tag(CONNECT_S2I)
@Tag(CONNECT_COMPONENTS)
void testKafkaConnectS2IStatus() {
    String connectS2IDeploymentConfigName = KafkaConnectS2IResources.deploymentName(CONNECTS2I_CLUSTER_NAME);
    String connectS2IUrl = KafkaConnectS2IResources.url(CONNECTS2I_CLUSTER_NAME, NAMESPACE, 8083);

    KafkaConnectS2IResource.kafkaConnectS2I(CONNECTS2I_CLUSTER_NAME, CLUSTER_NAME, 1)
        .editMetadata()
            .addToAnnotations(Annotations.STRIMZI_IO_USE_CONNECTOR_RESOURCES, "true")
        .endMetadata().done();

    assertKafkaConnectS2IStatus(1, connectS2IUrl, connectS2IDeploymentConfigName);

    KafkaConnectS2IResource.replaceConnectS2IResource(CONNECTS2I_CLUSTER_NAME, kb -> kb.getSpec().setResources(new ResourceRequirementsBuilder()
            .addToRequests("cpu", new Quantity("100000000m"))
            .build()));
    KafkaConnectS2IUtils.waitForConnectS2INotReady(CONNECTS2I_CLUSTER_NAME);

    KafkaConnectS2IResource.replaceConnectS2IResource(CONNECTS2I_CLUSTER_NAME, kb -> kb.getSpec().setResources(new ResourceRequirementsBuilder()
            .addToRequests("cpu", new Quantity("100m"))
            .build()));
    KafkaConnectS2IUtils.waitForConnectS2IReady(CONNECTS2I_CLUSTER_NAME);
    assertKafkaConnectS2IStatus(3, connectS2IUrl, connectS2IDeploymentConfigName);
}
 
Example #17
Source File: CommonPVCStrategyTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testProvisionVolumesIntoKubernetesEnvironment() throws Exception {
  // given
  k8sEnv.getPersistentVolumeClaims().put("pvc1", newPVC("pvc1"));
  k8sEnv.getPersistentVolumeClaims().put("pvc2", newPVC("pvc2"));

  // when
  commonPVCStrategy.provision(k8sEnv, IDENTITY);

  // then
  provisionOrder.verify(volumeConverter).convertCheVolumes(k8sEnv, WORKSPACE_ID);
  provisionOrder.verify(subpathPrefixes).prefixVolumeMountsSubpaths(k8sEnv, WORKSPACE_ID);
  provisionOrder.verify(podsVolumes).replacePVCVolumesWithCommon(k8sEnv.getPodsData(), PVC_NAME);
  assertEquals(k8sEnv.getPersistentVolumeClaims().size(), 1);
  PersistentVolumeClaim commonPVC = k8sEnv.getPersistentVolumeClaims().get(PVC_NAME);
  assertNotNull(commonPVC);
  assertEquals(commonPVC.getMetadata().getName(), PVC_NAME);
  assertEquals(commonPVC.getSpec().getAccessModes(), Collections.singletonList(PVC_ACCESS_MODE));
  assertEquals(
      commonPVC.getSpec().getResources().getRequests().get("storage"),
      new Quantity(PVC_QUANTITY));
}
 
Example #18
Source File: BaseST.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
protected void assertResources(String namespace, String podName, String containerName, String memoryLimit, String cpuLimit, String memoryRequest, String cpuRequest) {
    Pod po = kubeClient().getPod(podName);
    assertThat("Not found an expected pod  " + podName + " in namespace " + namespace + " but found " +
        kubeClient().listPods().stream().map(p -> p.getMetadata().getName()).collect(Collectors.toList()), po, is(notNullValue()));

    Optional optional = po.getSpec().getContainers().stream().filter(c -> c.getName().equals(containerName)).findFirst();
    assertThat("Not found an expected container " + containerName, optional.isPresent(), is(true));

    Container container = (Container) optional.get();
    Map<String, Quantity> limits = container.getResources().getLimits();
    assertThat(limits.get("memory").getAmount(), is(memoryLimit));
    assertThat(limits.get("cpu").getAmount(), is(cpuLimit));
    Map<String, Quantity> requests = container.getResources().getRequests();
    assertThat(requests.get("memory").getAmount(), is(memoryRequest));
    assertThat(requests.get("cpu").getAmount(), is(cpuRequest));
}
 
Example #19
Source File: PodTemplateUtilsTest.java    From kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCombineAllResources() {
    Container container1 = new Container();
    container1.setResources(new ResourceRequirementsBuilder() //
            .addToLimits("cpu", new Quantity("1")) //
            .addToLimits("memory", new Quantity("1Gi")) //
            .addToRequests("cpu", new Quantity("100m")) //
            .addToRequests("memory", new Quantity("156Mi")) //
            .build());

    Container container2 = new Container();
    container2.setResources(new ResourceRequirementsBuilder() //
            .addToLimits("cpu", new Quantity("2")) //
            .addToLimits("memory", new Quantity("2Gi")) //
            .addToRequests("cpu", new Quantity("200m")) //
            .addToRequests("memory", new Quantity("256Mi")) //
            .build());

    Container result = combine(container1, container2);

    assertQuantity("2", result.getResources().getLimits().get("cpu"));
    assertQuantity("2Gi", result.getResources().getLimits().get("memory"));
    assertQuantity("200m", result.getResources().getRequests().get("cpu"));
    assertQuantity("256Mi", result.getResources().getRequests().get("memory"));
}
 
Example #20
Source File: DeploymentPropertiesResolver.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
/**
 * Get the resource limits for the deployment request. A Pod can define its maximum needed resources by setting the
 * limits and Kubernetes can provide more resources if any are free.
 * <p>
 * Falls back to the server properties if not present in the deployment request.
 * <p>
 *
 * @param kubernetesDeployerProperties the kubernetes deployment properties map
 * @return the resource limits to use
 */
Map<String, Quantity> deduceResourceLimits(Map<String, String> kubernetesDeployerProperties) {
	String memory = PropertyParserUtils.getDeploymentPropertyValue(kubernetesDeployerProperties,
			this.propertyPrefix + ".limits.memory");

	if (StringUtils.isEmpty(memory)) {
		memory = properties.getLimits().getMemory();
	}

	String cpu = PropertyParserUtils.getDeploymentPropertyValue(kubernetesDeployerProperties,
			this.propertyPrefix + ".limits.cpu");

	if (StringUtils.isEmpty(cpu)) {
		cpu = properties.getLimits().getCpu();
	}

	Map<String,Quantity> limits = new HashMap<String,Quantity>();
	limits.put("memory", new Quantity(memory));
	limits.put("cpu", new Quantity(cpu));

	logger.debug("Using limits - cpu: " + cpu + " mem: " + memory);

	return limits;
}
 
Example #21
Source File: KafkaClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testGeneratePersistentVolumeClaimsPersistentWithClaimDeletion() {
    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCm, configuration, emptyMap()))
            .editSpec()
            .editKafka()
            .withNewPersistentClaimStorage().withStorageClass("gp2-ssd").withDeleteClaim(true).withSize("100Gi").endPersistentClaimStorage()
            .endKafka()
            .endSpec()
            .build();
    KafkaCluster kc = KafkaCluster.fromCrd(kafkaAssembly, VERSIONS);

    // Check Storage annotation on STS
    assertThat(kc.generateStatefulSet(true, ImagePullPolicy.NEVER, null).getMetadata().getAnnotations().get(AbstractModel.ANNO_STRIMZI_IO_STORAGE),
            is(ModelUtils.encodeStorageToJson(kafkaAssembly.getSpec().getKafka().getStorage())));

    // Check PVCs
    List<PersistentVolumeClaim> pvcs = kc.generatePersistentVolumeClaims(kc.getStorage());

    assertThat(pvcs.size(), is(3));

    for (PersistentVolumeClaim pvc : pvcs) {
        assertThat(pvc.getSpec().getResources().getRequests().get("storage"), is(new Quantity("100Gi")));
        assertThat(pvc.getSpec().getStorageClassName(), is("gp2-ssd"));
        assertThat(pvc.getMetadata().getName().startsWith(kc.VOLUME_NAME), is(true));
        assertThat(pvc.getMetadata().getOwnerReferences().size(), is(1));
        assertThat(pvc.getMetadata().getAnnotations().get(AbstractModel.ANNO_STRIMZI_IO_DELETE_CLAIM), is("true"));
    }
}
 
Example #22
Source File: SystemtestsKubernetesApps.java    From enmasse with Apache License 2.0 5 votes vote down vote up
private static PersistentVolumeClaim getPostgresPVC() {
    return new PersistentVolumeClaimBuilder()
            .withNewMetadata()
            .withName(POSTGRES_APP)
            .addToLabels("app", POSTGRES_APP)
            .endMetadata()
            .withNewSpec()
            .withAccessModes("ReadWriteOnce")
            .withNewResources()
            .addToRequests("storage", new Quantity("5Gi"))
            .endResources()
            .endSpec()
            .build();
}
 
Example #23
Source File: PvcOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testRevertingImmutableFields()   {
    PersistentVolumeClaim desired = new PersistentVolumeClaimBuilder()
            .withNewMetadata()
                .withName("my-pvc")
                .withNamespace("my-namespace")
            .endMetadata()
            .withNewSpec()
                .withNewResources()
                    .withRequests(Collections.singletonMap("storage", new Quantity("100", null)))
                .endResources()
            .endSpec()
            .build();

    PersistentVolumeClaim current = new PersistentVolumeClaimBuilder()
            .withNewMetadata()
                .withName("my-pvc")
                .withNamespace("my-namespace")
            .endMetadata()
            .withNewSpec()
                .withAccessModes("ReadWriteOnce")
                .withNewResources()
                    .withRequests(Collections.singletonMap("storage", new Quantity("10", null)))
                .endResources()
                .withStorageClassName("my-storage-class")
                .withSelector(new LabelSelector(null, Collections.singletonMap("key", "label")))
                .withVolumeName("pvc-ce9ebf52-435a-11e9-8fbc-06b5ff7c7748")
            .endSpec()
            .build();

    PvcOperator op = createResourceOperations(vertx, mock(KubernetesClient.class));
    op.revertImmutableChanges(current, desired);

    assertThat(current.getSpec().getStorageClassName(), is(desired.getSpec().getStorageClassName()));
    assertThat(current.getSpec().getAccessModes(), is(desired.getSpec().getAccessModes()));
    assertThat(current.getSpec().getSelector(), is(desired.getSpec().getSelector()));
    assertThat(current.getSpec().getVolumeName(), is(desired.getSpec().getVolumeName()));
}
 
Example #24
Source File: ContainersTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testReturnsZeroContainerLimitWhenActualValueIsNull() {
  when(resource.getLimits())
      .thenReturn(ImmutableMap.of("memory", new Quantity(), "cpu", new Quantity()));

  assertEquals(Containers.getRamLimit(container), 0);
  assertEquals(Containers.getCpuLimit(container), 0, 0.0);
}
 
Example #25
Source File: KafkaCluster.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Container> getInitContainers(ImagePullPolicy imagePullPolicy) {
    List<Container> initContainers = new ArrayList<>(1);

    if (rack != null || isExposedWithNodePort()) {
        ResourceRequirements resources = new ResourceRequirementsBuilder()
                .addToRequests("cpu", new Quantity("100m"))
                .addToRequests("memory", new Quantity("128Mi"))
                .addToLimits("cpu", new Quantity("1"))
                .addToLimits("memory", new Quantity("256Mi"))
                .build();

        Container initContainer = new ContainerBuilder()
                .withName(INIT_NAME)
                .withImage(initImage)
                .withArgs("/opt/strimzi/bin/kafka_init_run.sh")
                .withResources(resources)
                .withEnv(getInitContainerEnvVars())
                .withVolumeMounts(VolumeUtils.createVolumeMount(INIT_VOLUME_NAME, INIT_VOLUME_MOUNT))
                .withImagePullPolicy(determineImagePullPolicy(imagePullPolicy, initImage))
                .withSecurityContext(templateInitContainerSecurityContext)
                .build();

        initContainers.add(initContainer);
    }

    return initContainers;
}
 
Example #26
Source File: KafkaClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testGeneratePersistentVolumeClaimsPersistentWithoutClaimDeletion() {
    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCm, configuration, emptyMap()))
            .editSpec()
            .editKafka()
            .withNewPersistentClaimStorage().withStorageClass("gp2-ssd").withDeleteClaim(false).withSize("100Gi").endPersistentClaimStorage()
            .endKafka()
            .endSpec()
            .build();
    KafkaCluster kc = KafkaCluster.fromCrd(kafkaAssembly, VERSIONS);

    // Check Storage annotation on STS
    assertThat(kc.generateStatefulSet(true, ImagePullPolicy.NEVER, null).getMetadata().getAnnotations().get(AbstractModel.ANNO_STRIMZI_IO_STORAGE),
            is(ModelUtils.encodeStorageToJson(kafkaAssembly.getSpec().getKafka().getStorage())));

    // Check PVCs
    List<PersistentVolumeClaim> pvcs = kc.generatePersistentVolumeClaims(kc.getStorage());

    assertThat(pvcs.size(), is(3));

    for (PersistentVolumeClaim pvc : pvcs) {
        assertThat(pvc.getSpec().getResources().getRequests().get("storage"), is(new Quantity("100Gi")));
        assertThat(pvc.getSpec().getStorageClassName(), is("gp2-ssd"));
        assertThat(pvc.getMetadata().getName().startsWith(kc.VOLUME_NAME), is(true));
        assertThat(pvc.getMetadata().getOwnerReferences().size(), is(0));
        assertThat(pvc.getMetadata().getAnnotations().get(AbstractModel.ANNO_STRIMZI_IO_DELETE_CLAIM), is("false"));
    }
}
 
Example #27
Source File: Containers.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the CPU limit in cores, if it is present in given container otherwise 0 will be
 * returned.
 */
public static float getCpuLimit(Container container) {
  final ResourceRequirements resources = container.getResources();
  final Quantity quantity;
  if (resources != null
      && resources.getLimits() != null
      && (quantity = resources.getLimits().get("cpu")) != null
      && quantity.getAmount() != null) {
    return KubernetesSize.toCores(quantity.getAmount());
  }
  return 0;
}
 
Example #28
Source File: ContainersTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testReturnsZeroContainerRequestWhenActualValueIsNull() {
  when(resource.getRequests())
      .thenReturn(ImmutableMap.of("memory", new Quantity(), "cpu", new Quantity()));

  assertEquals(Containers.getRamRequest(container), 0);
  assertEquals(Containers.getCpuRequest(container), 0, 0.0);
}
 
Example #29
Source File: AbstractModel.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Adds KAFKA_HEAP_OPTS variable to the EnvVar list if any heap related options were specified.
 * NOTE: If Xmx Java Options are not set DYNAMIC_HEAP_FRACTION and DYNAMIC_HEAP_MAX may also be set
 *
 * @param envVars List of Environment Variables to add to
 * @param dynamicHeapFraction List of Environment Variables
 * @param dynamicHeapMaxBytes List of Environment Variables
 */
protected void heapOptions(List<EnvVar> envVars, double dynamicHeapFraction, long dynamicHeapMaxBytes) {
    StringBuilder kafkaHeapOpts = new StringBuilder();

    String xms = jvmOptions != null ? jvmOptions.getXms() : null;
    if (xms != null) {
        kafkaHeapOpts.append("-Xms")
                .append(xms);
    }

    String xmx = jvmOptions != null ? jvmOptions.getXmx() : null;
    if (xmx != null) {
        // Honour user provided explicit max heap
        kafkaHeapOpts.append(' ').append("-Xmx").append(xmx);
    } else {
        ResourceRequirements resources = getResources();
        Map<String, Quantity> cpuMemory = resources != null ? resources.getRequests() : null;
        // Delegate to the container to figure out only when CGroup memory limits are defined to prevent allocating
        // too much memory on the kubelet.
        if (cpuMemory != null && cpuMemory.get("memory") != null) {
            envVars.add(buildEnvVar(ENV_VAR_DYNAMIC_HEAP_FRACTION, Double.toString(dynamicHeapFraction)));
            if (dynamicHeapMaxBytes > 0) {
                envVars.add(buildEnvVar(ENV_VAR_DYNAMIC_HEAP_MAX, Long.toString(dynamicHeapMaxBytes)));
            }
        // When no memory limit, `Xms`, and `Xmx` are defined then set a default `Xms` and
        // leave `Xmx` undefined.
        } else if (xms == null) {
            kafkaHeapOpts.append("-Xms").append(DEFAULT_JVM_XMS);
        }
    }

    String kafkaHeapOptsString = kafkaHeapOpts.toString().trim();
    if (!kafkaHeapOptsString.isEmpty()) {
        envVars.add(buildEnvVar(ENV_VAR_KAFKA_HEAP_OPTS, kafkaHeapOptsString));
    }
}
 
Example #30
Source File: DynamicPVCWorkspaceVolume.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
protected Map<String, Quantity> getResourceMap() {
    ImmutableMap.Builder<String, Quantity> builder = ImmutableMap.<String, Quantity>builder();
    String actualStorage = substituteEnv(getRequestsSizeOrDefault());
        Quantity storageQuantity = new Quantity(actualStorage);
        builder.put("storage", storageQuantity);
    return builder.build();
}