Java Code Examples for io.fabric8.kubernetes.api.model.Container#getResources()

The following examples show how to use io.fabric8.kubernetes.api.model.Container#getResources() . 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: KubeUtil.java    From enmasse with Apache License 2.0 6 votes vote down vote up
public static void applyCpuMemory(PodTemplateSpec podTemplateSpec, String cpu, String memory) {
    Map<String, Quantity> resources = new LinkedHashMap<>();
    if (cpu != null) {
        resources.put("cpu", new Quantity(cpu));
    }
    if (memory != null) {
        resources.put("memory", new Quantity(memory));
    }

    for (Container container : podTemplateSpec.getSpec().getContainers()) {
        if (container.getResources() == null) {
            container.setResources(new ResourceRequirements());
        }

        if (container.getResources().getRequests() == null) {
            container.getResources().setRequests(new LinkedHashMap<>());
        }
        container.getResources().getRequests().putAll(resources);

        if (container.getResources().getLimits() == null) {
            container.getResources().setLimits(new LinkedHashMap<>());
        }

        container.getResources().getLimits().putAll(resources);
    }
}
 
Example 2
Source File: Containers.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the RAM limit in bytes, if it is present in given container otherwise 0 will be
 * returned.
 */
public static long getRamLimit(Container container) {
  final ResourceRequirements resources = container.getResources();
  final Quantity quantity;
  if (resources != null
      && resources.getLimits() != null
      && (quantity = resources.getLimits().get("memory")) != null
      && quantity.getAmount() != null) {
    return Quantity.getAmountInBytes(quantity).longValue();
  }
  return 0;
}
 
Example 3
Source File: Containers.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the RAM request in bytes, if it is present in given container otherwise 0 will be
 * returned.
 */
public static long getRamRequest(Container container) {
  final ResourceRequirements resources = container.getResources();
  final Quantity quantity;
  if (resources != null
      && resources.getRequests() != null
      && (quantity = resources.getRequests().get("memory")) != null
      && quantity.getAmount() != null) {
    return Quantity.getAmountInBytes(quantity).longValue();
  }
  return 0;
}
 
Example 4
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 5
Source File: Containers.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Returns the CPU request in bytes, if it is present in given container otherwise 0 will be
 * returned.
 */
public static float getCpuRequest(Container container) {
  final ResourceRequirements resources = container.getResources();
  final Quantity quantity;
  if (resources != null
      && resources.getRequests() != null
      && (quantity = resources.getRequests().get("cpu")) != null
      && quantity.getAmount() != null) {
    return KubernetesSize.toCores(quantity.getAmount());
  }
  return 0;
}
 
Example 6
Source File: LanderPodFactoryTest.java    From data-highway with Apache License 2.0 4 votes vote down vote up
@Test
public void simple() {
  when(podNameFactory.newName(any())).thenReturn(TRUCK_PARK_NAME);
  when(configMapSupplier.get()).thenReturn(configMap);
  when(configMap.getData()).thenReturn(ImmutableMap
      .<String, String> builder()
      .put(DOCKER_IMAGE, DOCKER_IMAGE_NAME)
      .put(VERSION, IMAGE_VERSION)
      .put(CPU, "cpu1")
      .put(MEMORY, "mem1")
      .put(JVM_ARGS, "args1")
      .put(CLOUDWATCH_REGION, "region")
      .put(CLOUDWATCH_GROUP, "group")
      .put("annotations", "{\"annotation1\": \"value1\", \"annotation2/slashsomething\": \"value2\"}")
      .build());

  LanderConfiguration config = new LanderConfiguration(NAME, "topic", emptyMap(), "s3Prefix", false,
      PARTITION_COLUMN_VALUE, false);
  Pod pod = underTest.newInstance(config, singletonList(ARG));

  ObjectMeta metadata = pod.getMetadata();
  assertThat(metadata.getName(), is(TRUCK_PARK_NAME));
  assertThat(metadata.getLabels(),
      is(ImmutableMap
          .<String, String> builder()
          .put(APP, TRUCK_PARK)
          .put(VERSION, IMAGE_VERSION)
          .put(ROAD, NAME)
          .build()));
  assertThat(metadata.getAnnotations(),
      is(ImmutableMap
          .<String, String> builder()
          .put("annotation1", "value1")
          .put("annotation2/slashsomething", "value2")
          .build()));

  PodSpec spec = pod.getSpec();
  assertThat(spec.getRestartPolicy(), is(RESTART_POLICY_NEVER));

  List<Container> containers = spec.getContainers();
  assertThat(containers.size(), is(1));

  Container container = containers.get(0);
  assertThat(container.getName(), is(TRUCK_PARK_NAME));
  assertThat(container.getImage(), is(DOCKER_IMAGE_NAME));
  assertThat(container.getArgs(), is(singletonList(ARG)));

  List<EnvVar> env = container.getEnv();
  assertThat(env.size(), is(7));

  EnvVar kubeNamespace = env.get(0);
  assertThat(kubeNamespace.getName(), is("KUBERNETES_NAMESPACE"));
  assertThat(kubeNamespace.getValue(), is(nullValue()));

  EnvVar podNameEnv = env.get(1);
  assertThat(podNameEnv.getName(), is("POD_NAME"));
  assertThat(podNameEnv.getValue(), is(TRUCK_PARK_NAME));

  EnvVar environment = env.get(2);
  assertThat(environment.getName(), is("ENVIRONMENT"));
  assertThat(environment.getValue(), is("lab"));

  EnvVar jvmArgs = env.get(3);
  assertThat(jvmArgs.getName(), is("JVM_ARGS"));
  assertThat(jvmArgs.getValue(), is("args1"));

  EnvVar cloudwatchRegion = env.get(4);
  assertThat(cloudwatchRegion.getName(), is("CLOUDWATCH_REGION"));
  assertThat(cloudwatchRegion.getValue(), is("region"));

  EnvVar cloudwatchGroup = env.get(5);
  assertThat(cloudwatchGroup.getName(), is("CLOUDWATCH_GROUP"));
  assertThat(cloudwatchGroup.getValue(), is("group"));

  EnvVar cloudwatchStream = env.get(6);
  assertThat(cloudwatchStream.getName(), is("CLOUDWATCH_STREAM"));
  assertThat(cloudwatchStream.getValue(), is("${KUBERNETES_NAMESPACE}-truck-park-the_name"));

  ResourceRequirements resources = container.getResources();
  assertThat(resources.getLimits().get(CPU), is(new Quantity("cpu1")));
  assertThat(resources.getLimits().get(MEMORY), is(new Quantity("mem1")));
  assertThat(resources.getRequests().get(CPU), is(new Quantity("cpu1")));
  assertThat(resources.getRequests().get(MEMORY), is(new Quantity("mem1")));
}
 
Example 7
Source File: KubeUtil.java    From enmasse with Apache License 2.0 4 votes vote down vote up
private static void applyDesiredResources(Container desiredContainer, Container actualContainer) {
    if (desiredContainer.getResources() != null) {
        actualContainer.setResources(desiredContainer.getResources());
    }
}