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

The following examples show how to use io.fabric8.kubernetes.api.model.ServicePort#setPort() . 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: CustomServiceController.java    From java-operator-sdk with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<CustomService> createOrUpdateResource(CustomService resource) {
    log.info("Execution createOrUpdateResource for: {}", resource.getMetadata().getName());

    ServicePort servicePort = new ServicePort();
    servicePort.setPort(8080);
    ServiceSpec serviceSpec = new ServiceSpec();
    serviceSpec.setPorts(Arrays.asList(servicePort));

    kubernetesClient.services().inNamespace(resource.getMetadata().getNamespace()).createOrReplaceWithNew()
            .withNewMetadata()
            .withName(resource.getSpec().getName())
            .addToLabels("testLabel", resource.getSpec().getLabel())
            .endMetadata()
            .withSpec(serviceSpec)
            .done();
    return Optional.of(resource);
}
 
Example 2
Source File: TillerInstaller.java    From microbean-helm with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 3
Source File: KubernetesAppDeployer.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
private Set<ServicePort> addAdditionalServicePorts(String additionalServicePorts) {
	Set<ServicePort> ports = new HashSet<>();

	String[] servicePorts = additionalServicePorts.split(",");
	for (String servicePort : servicePorts) {
		Integer port = Integer.parseInt(servicePort.trim());
		ServicePort extraServicePort = new ServicePort();
		extraServicePort.setPort(port);
		extraServicePort.setName("port-" + port);

		ports.add(extraServicePort);
	}

	return ports;
}
 
Example 4
Source File: ServicesTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFindPortWhenExists() {
  final int PORT = 1234;

  Service service = new Service();
  ServiceSpec spec = new ServiceSpec();
  ServicePort port = new ServicePort();
  port.setPort(PORT);
  spec.setPorts(Arrays.asList(port, new ServicePort()));
  service.setSpec(spec);

  assertEquals(Services.findPort(service, PORT).get(), port);
}
 
Example 5
Source File: ServicesTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFindServiceWhenExists() {
  final int PORT = 1234;

  Service service = new Service();
  ServiceSpec spec = new ServiceSpec();
  ServicePort port = new ServicePort();
  port.setPort(PORT);
  spec.setPorts(Collections.singletonList(port));
  service.setSpec(spec);

  assertEquals(
      Services.findServiceWithPort(Arrays.asList(service, new Service()), PORT).get(), service);
}
 
Example 6
Source File: KubernetesAppDeployer.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 4 votes vote down vote up
protected void createService(AppDeploymentRequest request) {

		String appId = createDeploymentId(request);

		int externalPort = getExternalPort(request);

		logger.debug(String.format("Creating Service: %s on %d", appId, externalPort));

		Map<String, String> idMap = createIdMap(appId, request);

		ServiceSpecBuilder spec = new ServiceSpecBuilder();
		boolean isCreateLoadBalancer = false;
		String createLoadBalancer = PropertyParserUtils.getDeploymentPropertyValue(request.getDeploymentProperties(),
			"spring.cloud.deployer.kubernetes.createLoadBalancer");
		String createNodePort = PropertyParserUtils.getDeploymentPropertyValue(request.getDeploymentProperties(),
			"spring.cloud.deployer.kubernetes.createNodePort");
		String additionalServicePorts = PropertyParserUtils.getDeploymentPropertyValue(request.getDeploymentProperties(),
				"spring.cloud.deployer.kubernetes.servicePorts");

		if (createLoadBalancer != null && createNodePort != null) {
			throw new IllegalArgumentException("Cannot create NodePort and LoadBalancer at the same time.");
		}

		if (createLoadBalancer == null) {
			isCreateLoadBalancer = properties.isCreateLoadBalancer();
		}
		else {
			if ("true".equals(createLoadBalancer.toLowerCase())) {
				isCreateLoadBalancer = true;
			}
		}

		if (isCreateLoadBalancer) {
			spec.withType("LoadBalancer");
		}

		ServicePort servicePort = new ServicePort();
		servicePort.setPort(externalPort);
		servicePort.setName("port-" + externalPort);

		if (createNodePort != null) {
			spec.withType("NodePort");
			if (!"true".equals(createNodePort.toLowerCase())) {
				try {
					Integer nodePort = Integer.valueOf(createNodePort);
					servicePort.setNodePort(nodePort);
				}
				catch (NumberFormatException e) {
					throw new IllegalArgumentException(
						String.format("Invalid value: %s: provided port is not valid.", createNodePort));
				}
			}
		}

		Set<ServicePort> servicePorts = new HashSet<>();
		servicePorts.add(servicePort);

		if (StringUtils.hasText(additionalServicePorts)) {
			servicePorts.addAll(addAdditionalServicePorts(additionalServicePorts));
		}

		spec.withSelector(idMap).addAllToPorts(servicePorts);

		Map<String, String> annotations = this.deploymentPropertiesResolver.getServiceAnnotations(request.getDeploymentProperties());

		client.services().createNew().withNewMetadata().withName(appId)
			.withLabels(idMap).withAnnotations(annotations).addToLabels(SPRING_MARKER_KEY, SPRING_MARKER_VALUE)
			.endMetadata().withSpec(spec.build()).done();
	}