Java Code Examples for io.fabric8.kubernetes.api.model.Service#getSpec()

The following examples show how to use io.fabric8.kubernetes.api.model.Service#getSpec() . 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: ServiceOperator.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * Patches the resource with the given namespace and name to match the given desired resource
 * and completes the given future accordingly.
 *
 * ServiceOperator needs its own version of this method to patch the NodePorts for NodePort and LoadBalancer type Services.
 * Patching the service with service definition without the NodePort would cause regenerating the node port
 * which triggers rolling update.
 *
 * @param namespace Namespace of the service
 * @param name      Name of the service
 * @param current   Current servicve
 * @param desired   Desired Service
 *
 * @return  Future with reconciliation result
 */
@Override
protected Future<ReconcileResult<Service>> internalPatch(String namespace, String name, Service current, Service desired) {
    try {
        if (current.getSpec() != null && desired.getSpec() != null
                && (("NodePort".equals(current.getSpec().getType()) && "NodePort".equals(desired.getSpec().getType()))
                || ("LoadBalancer".equals(current.getSpec().getType()) && "LoadBalancer".equals(desired.getSpec().getType()))))   {
            patchNodePorts(current, desired);
            patchHealthCheckPorts(current, desired);
        }

        return super.internalPatch(namespace, name, current, desired);
    } catch (Exception e) {
        log.error("Caught exception while patching {} {} in namespace {}", resourceKind, name, namespace, e);
        return Future.failedFuture(e);
    }
}
 
Example 2
Source File: ServiceOperator.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if the Service already has assigned node ports.
 *
 * @param namespace The namespace.
 * @param name The route name.
 * @return Whether the Service already has assigned node ports.
 */
public boolean isNodePortReady(String namespace, String name) {
    ServiceResource<Service, DoneableService> resourceOp = operation().inNamespace(namespace).withName(name);
    Service resource = resourceOp.get();

    if (resource != null && resource.getSpec() != null && resource.getSpec().getPorts() != null) {
        boolean ready = true;

        for (ServicePort port : resource.getSpec().getPorts())  {
            if (port.getNodePort() == null) {
                ready = false;
            }
        }
        return ready;
    }

    return false;
}
 
Example 3
Source File: Fabric8FlinkKubeClient.java    From flink with Apache License 2.0 6 votes vote down vote up
private Optional<Endpoint> getRestEndPointFromService(Service service, int restPort) {
	if (service.getStatus() == null) {
		return Optional.empty();
	}

	LoadBalancerStatus loadBalancer = service.getStatus().getLoadBalancer();
	boolean hasExternalIP = service.getSpec() != null &&
		service.getSpec().getExternalIPs() != null && !service.getSpec().getExternalIPs().isEmpty();

	if (loadBalancer != null) {
		return getLoadBalancerRestEndpoint(loadBalancer, restPort);
	} else if (hasExternalIP) {
		final String address = service.getSpec().getExternalIPs().get(0);
		if (address != null && !address.isEmpty()) {
			return Optional.of(new Endpoint(address, restPort));
		}
	}
	return Optional.empty();
}
 
Example 4
Source File: ExposeEnricher.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private boolean hasWebPort(Service service) {
    ServiceSpec spec = service.getSpec();
    if (spec != null) {
        List<ServicePort> ports = spec.getPorts();
        if (ports != null) {
            for (ServicePort port : ports) {
                Integer portNumber = port.getPort();
                if (portNumber != null && webPorts.contains(portNumber)) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 5
Source File: DefaultServiceEnricher.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private void addDefaultService(KubernetesListBuilder builder, Service defaultService) {
    if (defaultService == null) {
        return;
    }
    ServiceSpec spec = defaultService.getSpec();
    List<ServicePort> ports = spec.getPorts();
    if (!ports.isEmpty()) {
        log.info("Adding a default service '%s' with ports [%s]",
                 defaultService.getMetadata().getName(), formatPortsAsList(ports));
    } else {
        log.info("Adding headless default service '%s'",
                 defaultService.getMetadata().getName());
    }
    builder.addToServiceItems(defaultService);
}
 
Example 6
Source File: Services.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Try to find port in given service.
 *
 * @return {@link Optional} of found {@link ServicePort}, or {@link Optional#empty()} when not
 *     found.
 */
public static Optional<ServicePort> findPort(Service service, int port) {
  if (service == null || service.getSpec() == null || service.getSpec().getPorts() == null) {
    return Optional.empty();
  }
  return service
      .getSpec()
      .getPorts()
      .stream()
      .filter(p -> p.getPort() != null && p.getPort() == port)
      .findFirst();
}
 
Example 7
Source File: KubernetesObjectUtil.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/** Adds selector into target Kubernetes service. */
public static void putSelector(Service target, String key, String value) {
  ServiceSpec spec = target.getSpec();

  if (spec == null) {
    target.setSpec(spec = new ServiceSpec());
  }

  Map<String, String> selector = spec.getSelector();
  if (selector == null) {
    spec.setSelector(selector = new HashMap<>());
  }

  selector.put(key, value);
}
 
Example 8
Source File: TemplateTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
protected static void assertListIsServiceWithPort8080(List<HasMetadata> items) {
  assertNotNull(items);
  assertEquals(1, items.size());
  HasMetadata item = items.get(0);
  assertTrue(item instanceof Service);
  Service service = (Service) item;
  ServiceSpec serviceSpec = service.getSpec();
  assertNotNull(serviceSpec);
  List<ServicePort> ports = serviceSpec.getPorts();
  assertEquals(1, ports.size());
  ServicePort port = ports.get(0);
  assertEquals(new Integer(8080), port.getPort());
}