public static void main(String args[]) { try (final KubernetesClient client = new DefaultKubernetesClient()) { Service service = new ServiceBuilder() .withNewMetadata() .withName("my-service") .endMetadata() .withNewSpec() .withSelector(Collections.singletonMap("app", "MyApp")) .addNewPort() .withName("test-port") .withProtocol("TCP") .withPort(80) .withTargetPort(new IntOrString(9376)) .endPort() .withType("LoadBalancer") .endSpec() .withNewStatus() .withNewLoadBalancer() .addNewIngress() .withIp("146.148.47.155") .endIngress() .endLoadBalancer() .endStatus() .build(); service = client.services().inNamespace(client.getNamespace()).create(service); log("Created service with name ", service.getMetadata().getName()); String serviceURL = client.services().inNamespace(client.getNamespace()).withName(service.getMetadata().getName()).getURL("test-port"); log("Service URL", serviceURL); } }
.endMetadata() .withNewSpec() .withSelector(serviceSelector) .withPorts(new ServicePortBuilder().withPort(port).withName("http").build(), new ServicePortBuilder().withPort(monitoringSettings.getPort()).withName("monitoring").build())
.endMetadata() .withNewSpec() .withSelector(serviceSelector) .withPorts(new ServicePortBuilder().withPort(port).withName("http").build(), new ServicePortBuilder().withPort(monitoringSettings.getPort()).withName("monitoring").build())
.editOrNewSpec() .withPorts(servicePorts) .withSelector(service.getSpec().getSelector()) .withLoadBalancerSourceRanges(exposeSpec.getLoadBalancerSourceRanges() != null ? exposeSpec.getLoadBalancerSourceRanges() : Collections.emptyList()) .withType("LoadBalancer")
protected Service createHeadlessService(List<ServicePort> ports, Map<String, String> annotations) { Service service = new ServiceBuilder() .withNewMetadata() .withName(headlessServiceName) .withLabels(getLabelsWithName(headlessServiceName, templateHeadlessServiceLabels)) .withNamespace(namespace) .withAnnotations(mergeAnnotations(annotations, templateHeadlessServiceAnnotations)) .withOwnerReferences(createOwnerReference()) .endMetadata() .withNewSpec() .withType("ClusterIP") .withClusterIP("None") .withSelector(getSelectorLabels()) .withPorts(ports) .endSpec() .build(); log.trace("Created headless service {}", service); return service; }
private static Service getMessagingClientServiceResource() { return new ServiceBuilder() .withNewMetadata() .withName(SystemtestsOpenshiftApp.MESSAGING_CLIENTS.toString()) .addToLabels("run", SystemtestsOpenshiftApp.MESSAGING_CLIENTS.toString()) .endMetadata() .withNewSpec() .withSelector(Collections.singletonMap("app", SystemtestsOpenshiftApp.MESSAGING_CLIENTS.toString())) .addNewPort() .withName("http") .withPort(4242) .withProtocol("TCP") .endPort() .endSpec() .build(); } }
protected Service createService(String name, String type, List<ServicePort> ports, Map<String, String> labels, Map<String, String> selector, Map<String, String> annotations) { Service service = new ServiceBuilder() .withNewMetadata() .withName(name) .withLabels(labels) .withNamespace(namespace) .withAnnotations(annotations) .withOwnerReferences(createOwnerReference()) .endMetadata() .withNewSpec() .withType(type) .withSelector(selector) .withPorts(ports) .endSpec() .build(); log.trace("Created service {}", service); return service; }