Esempio n. 1
0
  @Test
  public void shouldRetrievePublicPort() {
    Port port = new Port();
    port.setPrivatePort(1);
    port.setPublicPort(2);
    Container container = new Container();
    container.setPorts(Arrays.asList(port));
    given(docker.containers(anyInt(), anyInt(), anyString(), anyString(), anyInt()))
        .willReturn(Arrays.asList(container));

    // When
    Set<Integer> ports = Dockers.getUsedPorts(docker);

    // Then
    assertEquals(1, ports.size());
    assertEquals(port.getPublicPort(), ports.iterator().next());
  }
  /**
   * Create new pod
   *
   * @param podId Identifier of the pod
   * @param podLabel Pod name to be used by the pod label
   * @param dockerImage Docker image to be used by the pod
   * @param cpu Number of cpu cores
   * @param memory Memory allocation in megabytes
   * @param ports Ports exposed by the pod
   * @param environmentVariables Environment variables to be passed to the pod
   * @throws KubernetesClientException
   */
  @Override
  public void createPod(
      String podId,
      String podLabel,
      String dockerImage,
      int cpu,
      int memory,
      List<ContainerPort> ports,
      List<EnvVar> environmentVariables)
      throws KubernetesClientException {

    try {
      int memoryInMB = 1024 * 1024 * memory;
      if (log.isDebugEnabled()) {
        log.debug(
            String.format(
                "Creating kubernetes pod: [pod-id] %s [pod-name] %s [docker-image] %s "
                    + "[cpu] %d [memory] %d MB [ports] %s",
                podId, podLabel, dockerImage, cpu, memoryInMB, ports));
      }

      // Create pod definition
      Pod pod = new Pod();
      pod.setApiVersion(Pod.ApiVersion.V_1);
      pod.setKind(KubernetesConstants.KIND_POD);

      pod.setSpec(new PodSpec());
      pod.setMetadata(new ObjectMeta());

      pod.getMetadata().setName(podId);

      Map<String, String> labels = new HashMap<String, String>();
      labels.put(KubernetesConstants.LABEL_NAME, podLabel);
      pod.getMetadata().setLabels(labels);

      // Set container template
      Container containerTemplate = new Container();
      containerTemplate.setName(podLabel);
      containerTemplate.setImage(dockerImage);
      containerTemplate.setEnv(environmentVariables);
      List<Container> containerTemplates = new ArrayList<Container>();
      containerTemplates.add(containerTemplate);
      pod.getSpec().setContainers(containerTemplates);

      // Set resource limits
      ResourceRequirements resources = new ResourceRequirements();
      Map<String, Quantity> limits = new HashMap<String, Quantity>();
      limits.put(KubernetesConstants.RESOURCE_CPU, new Quantity(String.valueOf(cpu)));
      limits.put(KubernetesConstants.RESOURCE_MEMORY, new Quantity(String.valueOf(memoryInMB)));
      resources.setLimits(limits);
      containerTemplate.setResources(resources);

      containerTemplate.setPorts(ports);
      containerTemplate.setImagePullPolicy(KubernetesConstants.POLICY_PULL_IF_NOT_PRESENT);
      if (environmentVariables != null) {
        containerTemplate.setEnv(environmentVariables);
      }

      // Invoke the api to create the pod
      kubernetesClient.createPod(pod);

      if (log.isDebugEnabled()) {
        log.debug(String.format("Kubernetes pod created successfully: [pod-id] %s", podId));
      }
    } catch (Exception e) {
      String msg = String.format("Could not create kubernetes pod: [pod-id] %s", podId);
      log.error(msg, e);
      throw new KubernetesClientException(msg, e);
    }
  }