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

The following examples show how to use io.fabric8.kubernetes.api.model.QuantityBuilder. 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: 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 #2
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 #3
Source File: ResourceQuotaHandler.java    From module-ballerina-kubernetes with Apache License 2.0 5 votes vote down vote up
/**
 * Convert hard limits map.
 * @param hard Hard limit map from model.
 * @return Converted map.
 */
private Map<String, Quantity> getHard(Map<String, String> hard) {
    return hard.entrySet().stream()
            .collect(Collectors.toMap(Map.Entry::getKey, hardEntry -> {
                String amount = hardEntry.getValue().replaceAll("\\D+", "");
                String format = hardEntry.getValue().replace(amount, "");
                return new QuantityBuilder()
                        .withAmount(amount)
                        .withFormat(format)
                        .build();
            }));
}
 
Example #4
Source File: PersistentVolumeClaimHandler.java    From module-ballerina-kubernetes with Apache License 2.0 5 votes vote down vote up
private void generate(PersistentVolumeClaimModel volumeClaimModel) throws KubernetesPluginException {
    Quantity quantity = new QuantityBuilder()
            .withAmount(volumeClaimModel.getVolumeClaimSizeAmount())
            .withFormat(volumeClaimModel.getVolumeClaimSizeFormat())
            .build();
    
    Map<String, Quantity> requests = new HashMap<>();
    requests.put("storage", quantity);
    PersistentVolumeClaim claim = new PersistentVolumeClaimBuilder()
            .withNewMetadata()
            .withName(volumeClaimModel.getName())
            .withNamespace(dataHolder.getNamespace())
            .withAnnotations(volumeClaimModel.getAnnotations())
            .endMetadata()
            .withNewSpec()
            .withAccessModes(volumeClaimModel.getAccessMode())
            .withNewResources()
            .withRequests(requests)
            .endResources()
            .endSpec()
            .build();
    try {
        String claimContent = SerializationUtils.dumpWithoutRuntimeStateAsYaml(claim);
        KubernetesUtils.writeToFile(claimContent,
                VOLUME_CLAIM_FILE_POSTFIX + YAML);
    } catch (IOException e) {
        String errorMessage = "error while generating yaml file for volume claim: " + volumeClaimModel.getName();
        throw new KubernetesPluginException(errorMessage, e);
    }
}
 
Example #5
Source File: KnativeResourceQuotaHandler.java    From module-ballerina-kubernetes with Apache License 2.0 5 votes vote down vote up
/**
 * Convert hard limits map.
 * @param hard Hard limit map from model.
 * @return Converted map.
 */
private Map<String, Quantity> getHard(Map<String, String> hard) {
    return hard.entrySet().stream()
            .collect(Collectors.toMap(Map.Entry::getKey, hardEntry -> new QuantityBuilder()
                    .withAmount(hardEntry.getValue())
                    .build()));
}
 
Example #6
Source File: KubernetesDockerRunner.java    From styx with Apache License 2.0 5 votes vote down vote up
private static Container keepaliveContainer() {
  return new ContainerBuilder()
      .withName(KEEPALIVE_CONTAINER_NAME)
      // Use the k8s pause container image. It sleeps forever until terminated.
      .withImage("k8s.gcr.io/pause:3.1")
      .withNewResources()
      .addToRequests("cpu", new QuantityBuilder()
          .withAmount("0")
          .build())
      .addToRequests("memory", new QuantityBuilder()
          .withAmount("0")
          .build())
      .endResources()
      .build();
}
 
Example #7
Source File: KubeUtilTest.java    From enmasse with Apache License 2.0 5 votes vote down vote up
@Test
public void appliesContainerResourcesToPodTemplate() {
    Container actualContainer = new ContainerBuilder()
            .withName("foo").build();
    Map<String, Quantity> widgets = Collections.singletonMap("widgets", new QuantityBuilder().withAmount("10").build());
    ResourceRequirements resources = new ResourceRequirementsBuilder().withLimits(widgets).build();
    Container desiredContainer = new ContainerBuilder()
            .withName("foo")
            .withResources(resources).build();

    PodTemplateSpec actual = doApplyContainers(actualContainer, desiredContainer);

    Container container = actual.getSpec().getContainers().get(0);
    assertThat(container.getResources(), equalTo(resources));
}
 
Example #8
Source File: KubeUtilTest.java    From enmasse with Apache License 2.0 5 votes vote down vote up
@Test
public void appliesContainerOrderIgnored() {
    Container actualFooContainer = new ContainerBuilder()
            .withName("foo").build();
    Container actualBarContainer = new ContainerBuilder()
            .withName("bar").build();

    Map<String, Quantity> widgets = Collections.singletonMap("widgets", new QuantityBuilder().withAmount("10").build());
    ResourceRequirements resources = new ResourceRequirementsBuilder().withLimits(widgets).build();
    Container desiredFooContainer = new ContainerBuilder()
            .withName("foo")
            .withResources(resources).build();

    PodTemplateSpec actual1 = new PodTemplateSpecBuilder()
            .withNewSpec()
            .addToContainers(actualBarContainer, actualFooContainer)
            .endSpec()
            .build();

    PodTemplateSpec desired = new PodTemplateSpecBuilder()
            .withNewSpec()
            .addToContainers(desiredFooContainer)
            .endSpec()
            .build();

    KubeUtil.applyPodTemplate(actual1, desired);

    PodTemplateSpec actual = actual1;

    Container barContainer = actual.getSpec().getContainers().get(0);
    assertThat(barContainer.getName(), equalTo("bar"));
    assertThat(barContainer.getResources(), nullValue());

    Container fooContainer = actual.getSpec().getContainers().get(1);
    assertThat(fooContainer.getName(), equalTo("foo"));
    assertThat(fooContainer.getResources(), equalTo(resources));
}