public ServicePortFluentImpl(ServicePort instance){ this.withName(instance.getName()); this.withNodePort(instance.getNodePort()); this.withPort(instance.getPort()); this.withProtocol(instance.getProtocol()); this.withTargetPort(instance.getTargetPort()); }
public ServicePortBuilder(){ this(new ServicePort()); } public ServicePortBuilder( ServicePortFluent<?> fluent ){
/** * 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()); } } } }
private static boolean portsMatch(ServicePort servicePort, IntOrString intOrString) { if (intOrString != null) { Integer port = servicePort.getPort(); Integer intVal = intOrString.getIntVal(); String strVal = intOrString.getStrVal(); if (intVal != null) { if (port != null) { return port.intValue() == intVal.intValue(); } else { /// should we find the port by name now? } } else if (strVal != null ){ return strVal.equals(servicePort.getName()); } } return false; }
public String getURL(Service service, String portName, String namespace, KubernetesClient client) { ServicePort port = URLFromServiceUtil.getServicePortByName(service, portName); String serviceProto = port.getProtocol(); NodePortUrlComponents urlComponents = null; Integer nodePort = port.getNodePort(); if(nodePort != null) { try { NodeList nodeList = client.nodes().list(); if(nodeList != null && nodeList.getItems() != null) { for(Node item : nodeList.getItems()) { urlComponents = getUrlComponentsFromNodeList(item.getStatus(), nodePort); if(urlComponents != null) { break; } } } } catch (KubernetesClientException exception) { logger.warn("Could not find a node! " + exception); } } return urlComponents != null ? (serviceProto + "://" + urlComponents.getClusterIP() + ":" + urlComponents.getPortNumber()).toLowerCase() : null; }
@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(); } } 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; }
String protocol = servicePort.getName(); if (APIMgtConstants.HTTP.equals(protocol) || APIMgtConstants.HTTPS.equals(protocol)) { int port = servicePort.getPort(); addNodePortEndpoint(serviceName, servicePort.getNodePort(), protocol, namespace, labels, endpointList); } else if (log.isDebugEnabled()) { log.debug("Service:{} Namespace:{} Port:{}/{} Application level protocol not defined.", serviceName, namespace, servicePort.getPort(), protocol);
private static String buildServiceHostString( String clusterIP, ServicePort port, String protocol) { String hostString; if (StringUtil.isNullOrEmpty(protocol)) { hostString = clusterIP + SEPERATOR + port.getPort(); } else { hostString = protocol + "://" + clusterIP + SEPERATOR + port.getPort(); } return hostString; }
protected Service createService(String hostName, String port) { Service service = new Service(); ServiceSpec spec = getOrCreateSpec(service); try { Integer portNumber = Integer.parseInt(port); if (portNumber != null) { List<ServicePort> ports = new ArrayList<>(); ServicePort servicePort = new ServicePort(); servicePort.setPort(portNumber); IntOrString containerPort = new IntOrString(); containerPort.setIntVal(portNumber); servicePort.setTargetPort(containerPort); ports.add(servicePort); spec.setPorts(ports); } } catch (NumberFormatException e) { LOG.warn("Failed to parse port text: " + port + ". " + e, e); } spec.setPortalIP(hostName); return service; }
public static ServicePort getServicePortByName(Service service, String portName) { if (portName.isEmpty()) { return service.getSpec().getPorts().iterator().next(); } for (ServicePort servicePort : service.getSpec().getPorts()) { if (Objects.equals(servicePort.getName(), portName)) { return servicePort; } } return null; } }
Map<Integer, Integer> portToNodePort = new HashMap<>(); for (ServicePort servicePort : originalPorts) { Integer port = servicePort.getPort(); Integer nodePort = servicePort.getNodePort(); if (port != null && nodePort != null) { portToNodePort.put(servicePort.getPort(), servicePort.getNodePort()); Integer currentNodePort = servicePort.getNodePort(); if (currentNodePort != null && currentNodePort != 0) { continue; Integer port = servicePort.getPort(); if (port != null) { Integer nodePort = portToNodePort.get(port); if (nodePort != null) { servicePort.setNodePort(nodePort);
private String formatPortsAsList(List<ServicePort> ports) { List<String> p = new ArrayList<>(); for (ServicePort port : ports) { String targetPort = getPortValue(port.getTargetPort()); String servicePort= port.getPort() != null ? Integer.toString(port.getPort()) : targetPort; p.add(targetPort.equals(servicePort) ? targetPort : servicePort + ":" + targetPort); } return StringUtils.join(p.iterator(), ","); }
@Override public boolean apply(ServicePort input) { return input.getProtocol().equalsIgnoreCase("TCP") && input.getPort().intValue() == 22; } });
Integer portNumber = port.getPort(); if (StringUtils.isBlank(clusterIP)) { IngressList ingresses = client.extensions().ingresses().inNamespace(serviceNamespace).list(); Integer nodePort = port.getNodePort(); if (nodePort != null) { NodeList nodeList = client.nodes().list();
public ContainerService(ServicePort servicePort, Pod pod) throws URISyntaxException { this.servicePort = servicePort; this.pod = pod; int serviceContainerPort = KubernetesHelper.intOrStringToInteger(servicePort.getTargetPort(), this.toString()); int port = NodeHelper.findHostPortForService(pod, serviceContainerPort); // lets get host / port of the container String host = null; PodStatus currentState = pod.getStatus(); if (currentState != null) { host = currentState.getHostIP(); if (Strings.isBlank(host)) { host = currentState.getPodIP(); } } if (Strings.isBlank(host)) { throw new IllegalArgumentException("No host for pod " + KubernetesHelper.getName(pod) + " so cannot use it with service port: " + servicePort.getName()); } else { uri = new URI("tcp://" + host + ":" + port); } }
protected ServiceSpec createServiceSpec(final Map<String, String> labels) { final ServiceSpec serviceSpec = new ServiceSpec(); serviceSpec.setType("ClusterIP"); final ServicePort servicePort = new ServicePort(); servicePort.setName(DEFAULT_NAME); servicePort.setPort(Integer.valueOf(44134)); servicePort.setTargetPort(new IntOrString(DEFAULT_NAME)); serviceSpec.setPorts(Arrays.asList(servicePort)); serviceSpec.setSelector(normalizeLabels(labels)); return serviceSpec; }
protected LocationSpec<KubernetesSshMachineLocation> prepareSshableLocationSpec(Entity entity, ConfigBag setup, Namespace namespace, String deploymentName, Service service, Pod pod) { InetAddress node = Networking.getInetAddressWithFixedName(pod.getSpec().getNodeName()); String podAddress = pod.getStatus().getPodIP(); LocationSpec<KubernetesSshMachineLocation> locationSpec = LocationSpec.create(KubernetesSshMachineLocation.class) .configure("address", node) .configure(SshMachineLocation.PRIVATE_ADDRESSES, ImmutableSet.of(podAddress)) .configure(CALLER_CONTEXT, setup.get(CALLER_CONTEXT)); if (!isDockerContainer(entity)) { Optional<ServicePort> sshPort = Iterables.tryFind(service.getSpec().getPorts(), new Predicate<ServicePort>() { @Override public boolean apply(ServicePort input) { return input.getProtocol().equalsIgnoreCase("TCP") && input.getPort().intValue() == 22; } }); Optional<Integer> sshPortNumber; if (sshPort.isPresent()) { sshPortNumber = Optional.of(sshPort.get().getNodePort()); } else { LOG.warn("No port-mapping found to ssh port 22, for container {}", service); sshPortNumber = Optional.absent(); } locationSpec.configure(CloudLocationConfig.USER, setup.get(KubernetesLocationConfig.LOGIN_USER)) .configure(SshMachineLocation.PASSWORD, setup.get(KubernetesLocationConfig.LOGIN_USER_PASSWORD)) .configureIfNotNull(SshMachineLocation.SSH_PORT, sshPortNumber.orNull()) .configure(BrooklynConfigKeys.SKIP_ON_BOX_BASE_DIR_RESOLUTION, true) .configure(BrooklynConfigKeys.ONBOX_BASE_DIR, "/tmp"); } return locationSpec; }
@Override public Service transform(Service service, Deployment apolloDeployment, io.logz.apollo.models.Service apolloService, Environment apolloEnvironment, DeployableVersion apolloDeployableVersion) { service.getSpec().getPorts().forEach(servicePort -> { if (servicePort.getNodePort() != null) { servicePort.setNodePort(servicePort.getNodePort() + apolloEnvironment.getServicePortCoefficient()); } }); return service; } }
private ServicePort updateMissingTargetPort(ServicePort port, Integer targetPort) { if (port.getTargetPort() == null) { return new ServicePortBuilder(port).withNewTargetPort(targetPort).build(); } return port; }