Java Code Examples for io.fabric8.kubernetes.api.model.ServicePort#getName()

The following examples show how to use io.fabric8.kubernetes.api.model.ServicePort#getName() . 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: ExternalServerExposer.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Exposes service port on given service externally (outside kubernetes cluster). The exposed
 * service port is associated with a specific Server configuration. Server configuration should be
 * encoded in the exposing object's annotations, to be used by {@link KubernetesServerResolver}.
 *
 * @param k8sEnv Kubernetes environment
 * @param machineName machine containing servers
 * @param serviceName service associated with machine, mapping all machine server ports
 * @param serverId non-null for a unique server, null for a compound set of servers that should be
 *     exposed together.
 * @param servicePort specific service port to be exposed externally
 * @param externalServers server configs of servers to be exposed externally
 */
public void expose(
    T k8sEnv,
    @Nullable String machineName,
    String serviceName,
    String serverId,
    ServicePort servicePort,
    Map<String, ServerConfig> externalServers) {

  if (serverId == null) {
    // this is the ID for non-unique servers
    serverId = servicePort.getName();
  }

  Ingress ingress =
      generateIngress(machineName, serviceName, serverId, servicePort, externalServers);

  k8sEnv.getIngresses().put(ingress.getMetadata().getName(), ingress);
}
 
Example 2
Source File: URLFromOpenshiftRouteImpl.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
public String getURL(Service service, String portName, String namespace, KubernetesClient client) {
  String serviceName = service.getMetadata().getName();
  ServicePort port = URLFromServiceUtil.getServicePortByName(service, portName);
  if(port != null && port.getName() != null && isOpenShift(client)) {
    try {
      String serviceProtocol = port.getProtocol();
      OpenShiftClient openShiftClient = client.adapt(OpenShiftClient.class);
      Route route = openShiftClient.routes().inNamespace(namespace).withName(service.getMetadata().getName()).get();
      if (route != null) {
        return (serviceProtocol + "://" + route.getSpec().getHost()).toLowerCase(Locale.ROOT);
      }
    } catch (KubernetesClientException e) {
      if(e.getCode() == HttpURLConnection.HTTP_FORBIDDEN) {
        logger.warn("Could not lookup route:" + serviceName + " in namespace:"+ namespace +", due to: " + e.getMessage());
      }
    }
  }
  return null;
}
 
Example 3
Source File: ServiceOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Finds out if corresponding port from desired service also exists in current service.
 * If it exists, it will copy the node port.
 * That will make sure the node port doesn't change with every reconciliation.
 *
 * @param current   Current Service
 * @param desired   Desired Service
 */
protected void patchNodePorts(Service current, Service desired) {
    for (ServicePort desiredPort : desired.getSpec().getPorts())    {
        String portName = desiredPort.getName();

        for (ServicePort currentPort : current.getSpec().getPorts())    {
            if (desiredPort.getNodePort() == null && portName.equals(currentPort.getName()) && currentPort.getNodePort() != null) {
                desiredPort.setNodePort(currentPort.getNodePort());
            }
        }
    }
}
 
Example 4
Source File: MultiHostExternalServiceExposureStrategyTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private void assertThatExternalServerIsExposed(
    String machineName,
    String serviceName,
    String serverNameRegex,
    String portProtocol,
    Integer port,
    ServicePort servicePort,
    ServerConfigImpl expected) {

  // ensure that required ingress is created
  String ingressName =
      serviceName + "-" + (expected.isUnique() ? serverNameRegex : servicePort.getName());
  Ingress ingress = kubernetesEnvironment.getIngresses().get(ingressName);
  IngressRule ingressRule = ingress.getSpec().getRules().get(0);
  assertEquals(ingressRule.getHost(), ingressName + "." + DOMAIN);
  assertEquals(ingressRule.getHttp().getPaths().get(0).getPath(), "/");
  IngressBackend backend = ingressRule.getHttp().getPaths().get(0).getBackend();
  assertEquals(backend.getServiceName(), serviceName);
  assertEquals(backend.getServicePort().getStrVal(), servicePort.getName());

  Annotations.Deserializer ingressAnnotations =
      Annotations.newDeserializer(ingress.getMetadata().getAnnotations());
  Map<String, ServerConfigImpl> servers = ingressAnnotations.servers();
  ServerConfig serverConfig = servers.get(serverNameRegex);
  assertEquals(serverConfig, expected);

  assertEquals(ingressAnnotations.machineName(), machineName);
}
 
Example 5
Source File: DefaultServiceEnricher.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
private void ensurePortName(ServicePort port, String protocol) {
    if (port.getName() != null && !port.getName().isEmpty()) {
        port.setName(getDefaultPortName(port.getPort(), getProtocol(protocol)));
    }
}