Beispiel #1
0
  /**
   * Create new pod and pass environment variables.
   *
   * @param memberContext
   * @param kubernetesApi
   * @param kubernetesClusterContext
   * @throws KubernetesClientException
   */
  private void createPod(
      ClusterContext clusterContext,
      MemberContext memberContext,
      KubernetesApiClient kubernetesApi,
      KubernetesClusterContext kubernetesClusterContext)
      throws KubernetesClientException {

    String applicationId = memberContext.getApplicationId();
    String cartridgeType = memberContext.getCartridgeType();
    String clusterId = memberContext.getClusterId();
    String memberId = memberContext.getMemberId();

    if (log.isInfoEnabled()) {
      log.info(
          String.format(
              "Creating kubernetes pod: [application] %s [cartridge] %s [member] %s",
              applicationId, cartridgeType, memberId));
    }

    Partition partition = memberContext.getPartition();
    if (partition == null) {
      String message =
          String.format(
              "Partition not found in member context: [application] %s [cartridge] %s "
                  + "[member] %s ",
              applicationId, cartridgeType, memberId);
      log.error(message);
      throw new RuntimeException(message);
    }

    Cartridge cartridge = CloudControllerContext.getInstance().getCartridge(cartridgeType);
    if (cartridge == null) {
      String message = "Could not find cartridge: [cartridge] " + cartridgeType;
      log.error(message);
      throw new RuntimeException(message);
    }

    // Set default values to zero to avoid cpu and memory restrictions
    int cpu = Integer.getInteger(KUBERNETES_CONTAINER_CPU_DEFAULT, 0);
    int memory = Integer.getInteger(KUBERNETES_CONTAINER_MEMORY_DEFAULT, 0);
    Property cpuProperty = cartridge.getProperties().getProperty(KUBERNETES_CONTAINER_CPU);
    if (cpuProperty != null) {
      cpu = Integer.parseInt(cpuProperty.getValue());
    }
    Property memoryProperty = cartridge.getProperties().getProperty(KUBERNETES_CONTAINER_MEMORY);
    if (memoryProperty != null) {
      memory = Integer.parseInt(memoryProperty.getValue());
    }

    IaasProvider iaasProvider =
        CloudControllerContext.getInstance()
            .getIaasProviderOfPartition(cartridge.getUuid(), partition.getUuid());
    if (iaasProvider == null) {
      String message = "Could not find iaas provider: [partition] " + partition.getUuid();
      log.error(message);
      throw new RuntimeException(message);
    }

    // Add dynamic payload to the member context
    memberContext.setDynamicPayload(payload.toArray(new NameValuePair[payload.size()]));

    // Create pod
    long podSeqNo = kubernetesClusterContext.getPodSeqNo().incrementAndGet();
    String podId = "pod" + "-" + podSeqNo;
    String podLabel = DigestUtils.md5Hex(clusterId);
    String dockerImage = iaasProvider.getImage();
    List<EnvVar> environmentVariables =
        KubernetesIaasUtil.prepareEnvironmentVariables(clusterContext, memberContext);

    List<ContainerPort> ports =
        KubernetesIaasUtil.convertPortMappings(Arrays.asList(cartridge.getPortMappings()));

    log.info(
        String.format(
            "Starting pod: [application] %s [cartridge] %s [member] %s "
                + "[cpu] %d [memory] %d MB",
            memberContext.getApplicationId(),
            memberContext.getCartridgeType(),
            memberContext.getMemberId(),
            cpu,
            memory));

    kubernetesApi.createPod(podId, podLabel, dockerImage, cpu, memory, ports, environmentVariables);

    log.info(
        String.format(
            "Pod started successfully: [application] %s [cartridge] %s [member] %s "
                + "[pod] %s [cpu] %d [memory] %d MB",
            memberContext.getApplicationId(),
            memberContext.getCartridgeType(),
            memberContext.getMemberId(),
            podId,
            cpu,
            memory));

    // Add pod id to member context
    memberContext.setKubernetesPodId(podId);
    memberContext.setKubernetesPodLabel(podLabel);

    // Create instance metadata
    InstanceMetadata instanceMetadata = new InstanceMetadata();
    instanceMetadata.setImageId(dockerImage);
    instanceMetadata.setCpu(cpu);
    instanceMetadata.setRam(memory);
    memberContext.setInstanceMetadata(instanceMetadata);

    // Persist cloud controller context
    CloudControllerContext.getInstance().persist();
  }
Beispiel #2
0
  /**
   * Starts a container via kubernetes for the given member context.
   *
   * @param memberContext
   * @return
   * @throws CartridgeNotFoundException
   */
  public MemberContext startContainer(MemberContext memberContext)
      throws CartridgeNotFoundException {
    Lock lock = null;
    try {
      lock = CloudControllerContext.getInstance().acquireMemberContextWriteLock();

      handleNullObject(memberContext, "member context is null");
      log.info(
          String.format(
              "Starting container: [application] %s [cartridge] %s [member] %s",
              memberContext.getApplicationId(),
              memberContext.getCartridgeType(),
              memberContext.getMemberId()));

      // Validate cluster id
      String clusterId = memberContext.getClusterId();
      String memberId = memberContext.getMemberId();
      handleNullObject(clusterId, "cluster id is null in member context");

      // Validate cluster context
      ClusterContext clusterContext =
          CloudControllerContext.getInstance().getClusterContext(clusterId);
      handleNullObject(
          clusterContext,
          String.format(
              "Cluster context not found: [application] %s [cartridge] %s " + "[cluster] %s",
              memberContext.getApplicationId(), memberContext.getCartridgeType(), clusterId));

      // Validate partition
      Partition partition = memberContext.getPartition();
      handleNullObject(
          partition,
          String.format(
              "partition not found in member context: [application] %s "
                  + "[cartridge] %s [member] %s",
              memberContext.getApplicationId(),
              memberContext.getCartridgeType(),
              memberContext.getMemberId()));

      // Validate cartridge
      String cartridgeType = clusterContext.getCartridgeUuid();
      Cartridge cartridge = CloudControllerContext.getInstance().getCartridge(cartridgeType);
      if (cartridge == null) {
        String msg =
            String.format(
                "Cartridge not found: [application] %s [cartridge] %s",
                memberContext.getApplicationId(), memberContext.getCartridgeType());
        log.error(msg);
        throw new CartridgeNotFoundException(msg);
      }

      String kubernetesClusterId = partition.getKubernetesClusterId();
      clusterContext.setKubernetesClusterId(kubernetesClusterId);
      KubernetesCluster kubernetesCluster =
          CloudControllerContext.getInstance().getKubernetesCluster(kubernetesClusterId);
      handleNullObject(
          kubernetesCluster,
          "kubernetes cluster not found: "
              + "[kubernetes-cluster] "
              + kubernetesClusterId
              + " [cluster] "
              + clusterId
              + " [member] "
              + memberId);

      // Prepare kubernetes context
      String kubernetesMasterIp = kubernetesCluster.getKubernetesMaster().getPrivateIPAddress();
      PortRange kubernetesPortRange = kubernetesCluster.getPortRange();
      String kubernetesMasterPort =
          CloudControllerUtil.getProperty(
              kubernetesCluster.getKubernetesMaster().getProperties(),
              StratosConstants.KUBERNETES_MASTER_PORT,
              StratosConstants.KUBERNETES_MASTER_DEFAULT_PORT);

      // Add kubernetes cluster payload parameters to payload
      if ((kubernetesCluster.getProperties() != null)
          && (kubernetesCluster.getProperties().getProperties() != null)) {
        for (Property property : kubernetesCluster.getProperties().getProperties()) {
          if (property != null) {
            if (property.getName().startsWith(PAYLOAD_PARAMETER_PREFIX)) {
              String name = property.getName().replace(PAYLOAD_PARAMETER_PREFIX, "");
              payload.add(new NameValuePair(name, property.getValue()));
            }
          }
        }
      }

      KubernetesClusterContext kubernetesClusterContext =
          getKubernetesClusterContext(
              kubernetesClusterId,
              kubernetesMasterIp,
              kubernetesMasterPort,
              kubernetesPortRange.getUpper(),
              kubernetesPortRange.getLower());

      // Generate kubernetes service ports and update port mappings in cartridge
      generateKubernetesServicePorts(
          clusterContext.getApplicationUuid(),
          clusterContext.getClusterId(),
          kubernetesClusterContext,
          cartridge);

      // Create kubernetes services for port mappings
      KubernetesApiClient kubernetesApi = kubernetesClusterContext.getKubApi();
      createKubernetesServices(
          kubernetesApi, clusterContext, kubernetesCluster, kubernetesClusterContext);

      // Create pod
      createPod(clusterContext, memberContext, kubernetesApi, kubernetesClusterContext);

      // Wait for pod status to be changed to running
      Pod pod = waitForPodToBeActivated(memberContext, kubernetesApi);

      // Update member context
      updateMemberContext(memberContext, pod, kubernetesCluster);

      log.info(
          String.format(
              "Container started successfully: [application] %s [cartridge] %s [member] %s "
                  + "[pod] %s [cpu] %d [memory] %d MB",
              memberContext.getApplicationId(),
              memberContext.getCartridgeType(),
              memberContext.getMemberId(),
              memberContext.getKubernetesPodId(),
              memberContext.getInstanceMetadata().getCpu(),
              memberContext.getInstanceMetadata().getRam()));
      return memberContext;
    } catch (Exception e) {
      String msg =
          String.format(
              "Could not start container: [application] %s [cartridge] %s [member] %s",
              memberContext.getApplicationId(),
              memberContext.getCartridgeType(),
              memberContext.getMemberId());
      log.error(msg, e);
      throw new RuntimeException(msg, e);
    } finally {
      if (lock != null) {
        CloudControllerContext.getInstance().releaseWriteLock(lock);
      }
    }
  }