Beispiel #1
0
 private static ServicePort toNamedServicePort(String serviceId, ServicePort servicePort) {
   String portName = servicePort.getName();
   String protocol = servicePort.getProtocol();
   Integer nodePort = servicePort.getNodePort();
   IntOrString targetPort = servicePort.getTargetPort();
   String name =
       !Strings.isNullOrBlank(portName) ? portName : serviceId + "-" + targetPort.toString();
   int port = KubernetesHelper.intOrStringToInteger(targetPort, "service: " + name);
   return new ServicePort(name, nodePort, port, protocol, targetPort);
 }
  /**
   * Create kubernetes service
   *
   * @param serviceId Service id
   * @param serviceLabel Service name to be used by the label name
   * @param nodePort Port to be exposed by the kubernetes node
   * @param containerPortName Container port name defined in the port label
   * @param containerPort Container port
   * @param sessionAffinity Session affinity configuration
   * @throws KubernetesClientException
   */
  @Override
  public void createService(
      String serviceId,
      String serviceLabel,
      int nodePort,
      String containerPortName,
      int containerPort,
      String sessionAffinity)
      throws KubernetesClientException {

    try {
      if (log.isDebugEnabled()) {
        log.debug(
            String.format(
                "Creating kubernetes service: [service-id] %s [service-name] %s [service-port] %d "
                    + "[container-port-name] %s",
                serviceId, serviceLabel, nodePort, containerPortName));
      }

      // Create service definition
      Service service = new Service();
      service.setSpec(new ServiceSpec());
      service.setMetadata(new ObjectMeta());

      service.setApiVersion(Service.ApiVersion.V_1);
      service.setKind(KubernetesConstants.KIND_SERVICE);

      service.getMetadata().setName(serviceId);
      service.getSpec().setSessionAffinity(sessionAffinity);
      service.getSpec().setType(KubernetesConstants.NODE_PORT);

      // Set port
      List<ServicePort> ports = new ArrayList<ServicePort>();
      ServicePort port = new ServicePort();
      port.setName(containerPortName);
      port.setPort(containerPort);
      port.setTargetPort(new IntOrString(containerPort));
      port.setNodePort(nodePort);
      ports.add(port);
      service.getSpec().setPorts(ports);

      // Set label
      Map<String, String> labels = new HashMap<String, String>();
      labels.put(KubernetesConstants.LABEL_NAME, serviceLabel);
      service.getMetadata().setLabels(labels);

      // Set service selector
      Map<String, String> selector = new HashMap<String, String>();
      selector.put(KubernetesConstants.LABEL_NAME, serviceLabel);
      service.getSpec().setSelector(selector);

      // Invoke the api to create the service
      kubernetesClient.createService(service);

      if (log.isDebugEnabled()) {
        log.debug(
            String.format(
                "Kubernetes service created successfully: [service-id] %s [service-name] %s "
                    + "[node-port] %d [container-port-name] %s [container-port] %d",
                serviceId, serviceLabel, nodePort, containerPortName, containerPort));
      }
    } catch (Exception e) {
      String message =
          String.format(
              "Could not create kubernetes service: [service-id] %s [service-name] %s "
                  + "[node-port] %d [container-port-name] %s [container-port] %d",
              serviceId, serviceLabel, nodePort, containerPortName, containerPort);
      log.error(message, e);
      throw new KubernetesClientException(message, e);
    }
  }