Service serviceCreating = new ServiceBuilder() .withNewMetadata().withName(serviceName).withLabels(labels) .endMetadata().withSpec(serviceSpec).build(); service = getEndpoint().getKubernetesClient().services() .inNamespace(namespaceName).create(serviceCreating);
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); } }
protected Service exposeService(String namespace, Map<String, String> metadata, String serviceName, Iterable<Integer> inboundPorts) { List<ServicePort> servicePorts = Lists.newArrayList(); for (Integer inboundPort : inboundPorts) { servicePorts.add(new ServicePortBuilder().withName(Integer.toString(inboundPort)).withPort(inboundPort).build()); } Service service = new ServiceBuilder().withNewMetadata().withName(serviceName).endMetadata() .withNewSpec() .addToSelector(metadata) .addToPorts(Iterables.toArray(servicePorts, ServicePort.class)) .withType(NODE_PORT) .endSpec() .build(); client.services().inNamespace(namespace).create(service); service = getService(namespace, serviceName); LOG.debug("Exposed service {} in namespace {}.", service, namespace); return service; }
.addToLabels(LabelKeys.INFRA_TYPE, addressSpace.getSpec().getType()) .addToLabels(LabelKeys.INFRA_UUID, infraUuid) .endMetadata() .editOrNewSpec() .withPorts(servicePorts)
.endMetadata().withSpec(spec.build()).done();
.withAnnotations(getAnnotations(service)) .withLabels(getLabels(service)) .endMetadata();
private Service getDefaultService() { // No image config, no service if (!hasImageConfiguration()) { return null; } String serviceName = getConfig(Config.name, MavenUtil.createDefaultResourceName(getContext().getGav().getSanitizedArtifactId())); // Create service only for all images which are supposed to live in a single pod List<ServicePort> ports = extractPorts(getImages().get()); ServiceBuilder builder = new ServiceBuilder() .withNewMetadata() .withName(serviceName) .withLabels(extractLabels()) .endMetadata(); ServiceFluent.SpecNested<ServiceBuilder> specBuilder = builder.withNewSpec(); if (!ports.isEmpty()) { specBuilder.withPorts(ports); } else if (Configs.asBoolean(getConfig(Config.headless))) { specBuilder.withClusterIP("None"); } else { // No ports, no headless --> no service return null; } if (hasConfig(Config.type)) { specBuilder.withType(getConfig(Config.type)); } specBuilder.endSpec(); return builder.build(); }
.withNamespace(dataHolder.getNamespace()) .addToLabels(serviceModel.getLabels()) .endMetadata() .withNewSpec() .addNewPort()
private Service getDefaultService() { // No image config, no service if (!hasImageConfiguration()) { return null; } // Create service only for all images which are supposed to live in a single pod List<ServicePort> ports = extractPorts(getImages()); ServiceBuilder builder = new ServiceBuilder() .withNewMetadata() .withName(getConfig(Config.name, MavenUtil.createDefaultResourceName(getProject()))) .withLabels(extractLabels()) .endMetadata(); ServiceFluent.SpecNested<ServiceBuilder> specBuilder = builder.withNewSpec(); if (!ports.isEmpty()) { specBuilder.withPorts(ports); } else if (Configs.asBoolean(getConfig(Config.headless))) { specBuilder.withClusterIP("None"); } else { // No ports, no headless --> no service return null; } if (hasConfig(Config.type)) { specBuilder.withType(getConfig(Config.type)); } specBuilder.endSpec(); return builder.build(); }
private void ensureService(String name, DeploymentData deploymentData) { final Map<String, String> labels = prepareServiceLabels(deploymentData); final Map<String, String> annotations = prepareServiceAnnotations(deploymentData); openShiftClient.services().withName(name).createOrReplaceWithNew() .withNewMetadata() .withName(name) .addToAnnotations(annotations) .addToLabels(labels) .endMetadata() .withNewSpec() .addToSelector(INTEGRATION_NAME_LABEL, name) .addNewPort() .withName("http") .withProtocol("TCP") .withPort(INTEGRATION_SERVICE_PORT) .withNewTargetPort(INTEGRATION_SERVICE_PORT) .endPort() .endSpec() .done(); }
public void onTemplate(TemplateBuilder builder) { builder.addNewServiceObject() .withNewMetadata() .withName("elasticsearch-cluster") .endMetadata() .withNewSpec() .withClusterIP("None") .addNewPort() .withName("") .withProtocol("TCP") .withPort(9300) .withNewTargetPort(9300) .endPort() .addToSelector("component", "elasticsearch") .addToSelector("provider", "fabric8") .endSpec() .and() .build(); }
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; }
@Override public void provideCert(AddressSpace addressSpace, EndpointInfo endpointInfo) { Secret secret = client.secrets().inNamespace(namespace).withName(endpointInfo.getCertSpec().getSecretName()).get(); if (secret == null) { String serviceName = KubeUtil.getAddressSpaceServiceName(endpointInfo.getServiceName(), addressSpace); Service service = client.services().withName(serviceName).get(); if (service != null && (service.getMetadata().getAnnotations() == null || service.getMetadata().getAnnotations().get(AnnotationKeys.OPENSHIFT_SERVING_CERT_SECRET_NAME) == null)) { log.info("Adding service annotation to generate OpenShift cert"); client.services().withName(serviceName).edit() .editMetadata() .addToAnnotations(AnnotationKeys.OPENSHIFT_SERVING_CERT_SECRET_NAME, endpointInfo.getCertSpec().getSecretName()) .endMetadata() .done(); } } } }
private ObjectMeta ensureServiceMetadata(ServiceBuilder service, Service defaultService) { if (!service.hasMetadata() && defaultService != null) { service.withNewMetadataLike(defaultService.getMetadata()).endMetadata(); } return service.buildMetadata(); }
@Override protected ObjectMeta getOrCreateMetadata(ServiceBuilder item) { return item.hasMetadata() ? item.buildMetadata() : item.withNewMetadata().endMetadata().buildMetadata(); } }
@Override protected Service resource() { return new ServiceBuilder().withNewMetadata().withNamespace(NAMESPACE).withName(RESOURCE_NAME).endMetadata().build(); }
private ObjectMeta ensureServiceMetadata(ServiceBuilder service, Service defaultService) { if (!service.hasMetadata() && defaultService != null) { service.withNewMetadataLike(defaultService.getMetadata()).endMetadata(); } return service.buildMetadata(); }
.withNewMetadata().withName("testservice").endMetadata() .withNewSpec() .addNewPort().withPort(80).withNewTargetPort().withIntVal(80).endTargetPort().endPort() .endSpec() .done()); log("Updated service", client.services().inNamespace("thisisatest").withName("testservice").edit().editMetadata().addToLabels("test", "label").endMetadata().done()); client.replicationControllers().inNamespace("thisisatest").withField("metadata.name", "testservice").delete(); log("Deleted service by field");