Ejemplo n.º 1
0
  /**
   * Edits the container and all of the children containers.
   *
   * @pre parameters != null
   * @pre canEditContainer( newContainer ) == true
   * @post if( checkCanEdit(container) ) { edit(container); }
   * @param oldContainer The original container
   * @param newContainer The new container values
   * @throws IllegalArgumentException if oldContainer == null or newContainer == null or
   *     newContainer.canEdit == false
   */
  public void editContainer(Container oldContainer, Container newContainer)
      throws IllegalArgumentException {

    if (oldContainer == null || newContainer == null || !canEditContainer(newContainer)) {
      throw new IllegalArgumentException();
    }

    System.out.println(" ------ Editing container --------");
    System.out.println("Before: " + oldContainer + "\nAfter:" + newContainer + "");

    if (oldContainer instanceof ProductGroup) {
      Container parent = oldContainer.getContainer(); // added for bug fix
      parent.getProductGroups().remove((ProductGroup) oldContainer);
      oldContainer.setName(newContainer.getName());
      ((ProductGroup) oldContainer)
          .setThreeMonthSupply(((ProductGroup) newContainer).getThreeMonthSupply());
      parent.getProductGroups().add((ProductGroup) oldContainer);
    } else {
      storageUnits.remove(oldContainer);
      oldContainer.setName(newContainer.getName());
      storageUnits.add((StorageUnit) oldContainer); // added for bug fix
    }

    containerDAO.update(new ContainerDTO(oldContainer));
    ChangeObject hint = getHintObject(oldContainer);
    setChanged();
    notifyObservers(hint);
  }
Ejemplo n.º 2
0
  /**
   * 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);
    }
  }
Ejemplo n.º 3
0
 @Test
 public void test_getCanonicalName_WhenNameContainsASlashCharacter() {
   container.setName("Name1/Name2");
   assertEquals("/System/Name1Name2", container.getCanonicalName());
 }