io.fabric8.kubernetes.api.model.ServiceBuilder Java Examples
The following examples show how to use
io.fabric8.kubernetes.api.model.ServiceBuilder.
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: Minikube.java From enmasse with Apache License 2.0 | 6 votes |
@Override public void createExternalEndpoint(String name, String namespace, Service service, ServicePort targetPort) { if (!name.endsWith("-external")) { name += "-external"; } ServiceBuilder builder = new ServiceBuilder() .editOrNewMetadata() .withName(name) .withNamespace(namespace) .endMetadata() .editOrNewSpec() .withType("LoadBalancer") .addToPorts(targetPort) .withSelector(service.getSpec().getSelector()) .endSpec(); client.services().inNamespace(namespace).withName(name).createOrReplace(builder.build()); }
Example #2
Source File: IngressEnricherTest.java From jkube with Eclipse Public License 2.0 | 6 votes |
private ServiceBuilder getTestService() { return new ServiceBuilder() .withNewMetadata() .withName("test-svc") .addToLabels("expose", "true") .endMetadata() .withNewSpec() .addNewPort() .withName("http") .withPort(8080) .withProtocol("TCP") .withTargetPort(new IntOrString(8080)) .endPort() .addToSelector("group", "test") .withType("LoadBalancer") .endSpec(); }
Example #3
Source File: RouteEnricher.java From jkube with Eclipse Public License 2.0 | 6 votes |
private RoutePort createRoutePort(ServiceBuilder serviceBuilder) { RoutePort routePort = null; ServiceSpec spec = serviceBuilder.buildSpec(); if (spec != null) { List<ServicePort> ports = spec.getPorts(); if (ports != null && !ports.isEmpty()) { ServicePort servicePort = ports.get(0); if (servicePort != null) { IntOrString targetPort = servicePort.getTargetPort(); if (targetPort != null) { routePort = new RoutePort(); routePort.setTargetPort(targetPort); } } } } return routePort; }
Example #4
Source File: RouteEnricher.java From jkube with Eclipse Public License 2.0 | 6 votes |
@Override public void create(PlatformMode platformMode, final KubernetesListBuilder listBuilder) { ResourceConfig resourceConfig = getConfiguration().getResource(); if (resourceConfig != null && resourceConfig.getRouteDomain() != null) { routeDomainPostfix = resourceConfig.getRouteDomain(); } if(platformMode == PlatformMode.openshift && generateRoute.equals(Boolean.TRUE)) { final List<Route> routes = new ArrayList<>(); listBuilder.accept(new TypedVisitor<ServiceBuilder>() { @Override public void visit(ServiceBuilder serviceBuilder) { addRoute(listBuilder, serviceBuilder, routes); } }); if (!routes.isEmpty()) { Route[] routeArray = new Route[routes.size()]; routes.toArray(routeArray); listBuilder.addToItems(routeArray); } } }
Example #5
Source File: IngressEnricher.java From jkube with Eclipse Public License 2.0 | 6 votes |
private static Integer getServicePort(ServiceBuilder serviceBuilder) { ServiceSpec spec = serviceBuilder.buildSpec(); if (spec != null) { List<ServicePort> ports = spec.getPorts(); if (ports != null && !ports.isEmpty()) { for (ServicePort port : ports) { if (port.getName().equals("http") || port.getProtocol().equals("http")) { return port.getPort(); } } ServicePort servicePort = ports.get(0); if (servicePort != null) { return servicePort.getPort(); } } } return 0; }
Example #6
Source File: IngressEnricher.java From jkube with Eclipse Public License 2.0 | 6 votes |
/** * Should we try to create an external URL for the given service? * <p> * By default lets ignore the kubernetes services and any service which does not expose ports 80 and 443 * * @return true if we should create an Ingress for this service. */ private static boolean shouldCreateExternalURLForService(ServiceBuilder service, KitLogger log) { String serviceName = service.buildMetadata().getName(); ServiceSpec spec = service.buildSpec(); if (spec != null && !isKuberentesSystemService(serviceName)) { List<ServicePort> ports = spec.getPorts(); log.debug("Service " + serviceName + " has ports: " + ports); if (ports.size() == 1) { String type = spec.getType(); if (Objects.equals(type, "LoadBalancer")) { return true; } log.info("Not generating Ingress for service " + serviceName + " type is not LoadBalancer: " + type); } else { log.info("Not generating Ingress for service " + serviceName + " as only single port services are supported. Has ports: " + ports); } } return false; }
Example #7
Source File: RouteEnricherTest.java From jkube with Eclipse Public License 2.0 | 6 votes |
private ServiceBuilder getTestService() { return new ServiceBuilder() .withNewMetadata() .withName("test-svc") .addToLabels("expose", "true") .endMetadata() .withNewSpec() .addNewPort() .withName("http") .withPort(8080) .withProtocol("TCP") .withTargetPort(new IntOrString(8080)) .endPort() .addToSelector("group", "test") .withType("LoadBalancer") .endSpec(); }
Example #8
Source File: DefaultServiceEnricher.java From jkube with Eclipse Public License 2.0 | 6 votes |
private void mergeInDefaultServiceParameters(KubernetesListBuilder builder, final Service defaultService) { builder.accept(new TypedVisitor<ServiceBuilder>() { @Override public void visit(ServiceBuilder service) { // Only update single service matching the default service's name String defaultServiceName = getDefaultServiceName(defaultService); ObjectMeta serviceMetadata = ensureServiceMetadata(service, defaultService); String serviceName = ensureServiceName(serviceMetadata, service, defaultServiceName); if (defaultService != null && defaultServiceName != null && defaultServiceName.equals(serviceName)) { addMissingServiceParts(service, defaultService); } } }); }
Example #9
Source File: IngressEnricher.java From jkube with Eclipse Public License 2.0 | 6 votes |
@Override public void create(PlatformMode platformMode, final KubernetesListBuilder listBuilder) { ResourceConfig resourceConfig = getConfiguration().getResource(); Boolean shouldCreateIngress = getValueFromConfig(CREATE_EXTERNAL_URLS, false); if (shouldCreateIngress.equals(Boolean.FALSE)) { return; } if (platformMode == PlatformMode.kubernetes) { listBuilder.accept(new TypedVisitor<ServiceBuilder>() { @Override public void visit(ServiceBuilder serviceBuilder) { Ingress ingress = addIngress(listBuilder, serviceBuilder, getRouteDomain(resourceConfig), log); if (ingress != null) { listBuilder.addToItems(ingress); } } }); } }
Example #10
Source File: KubernetesQueryTest.java From remoting-kafka-plugin with MIT License | 6 votes |
@Test public void testGetFirstNodePortByServiceName() { KubernetesClient client = k.getClient(); Service svc = new ServiceBuilder() .withNewMetadata() .withName("kafka-svc") .endMetadata() .withNewSpec() .withType("NodePort") .addNewPort() .withNodePort(31234) .endPort() .endSpec() .build(); client.services().create(svc); KubernetesQuery query = new KubernetesQuery(client); assertThat(query.getFirstNodePortByServiceName("kafka-svc"), is(31234)); }
Example #11
Source File: KubernetesDiscoveryClientFilterMetadataTest.java From spring-cloud-kubernetes with Apache License 2.0 | 6 votes |
private void setupServiceWithLabelsAndAnnotationsAndPorts(String serviceId, String namespace, Map<String, String> labels, Map<String, String> annotations, Map<Integer, String> ports) { final Service service = new ServiceBuilder().withNewMetadata() .withNamespace(namespace).withLabels(labels).withAnnotations(annotations) .endMetadata().withNewSpec().withPorts(getServicePorts(ports)).endSpec() .build(); when(this.serviceOperation.withName(serviceId)).thenReturn(this.serviceResource); when(this.serviceResource.get()).thenReturn(service); when(this.kubernetesClient.services()).thenReturn(this.serviceOperation); when(this.kubernetesClient.services().inNamespace(anyString())) .thenReturn(this.serviceOperation); ObjectMeta objectMeta = new ObjectMeta(); objectMeta.setNamespace(namespace); final Endpoints endpoints = new EndpointsBuilder().withMetadata(objectMeta) .addNewSubset().addAllToPorts(getEndpointPorts(ports)).addNewAddress() .endAddress().endSubset().build(); when(this.endpointsResource.get()).thenReturn(endpoints); when(this.endpointsOperation.withName(serviceId)) .thenReturn(this.endpointsResource); when(this.kubernetesClient.endpoints()).thenReturn(this.endpointsOperation); }
Example #12
Source File: AbstractModel.java From strimzi-kafka-operator with Apache License 2.0 | 6 votes |
protected Service createService(String name, String type, List<ServicePort> ports, Labels labels, Labels selector, Map<String, String> annotations, String loadBalancerIP) { Service service = new ServiceBuilder() .withNewMetadata() .withName(name) .withLabels(labels.toMap()) .withNamespace(namespace) .withAnnotations(annotations) .withOwnerReferences(createOwnerReference()) .endMetadata() .withNewSpec() .withType(type) .withSelector(selector.toMap()) .withPorts(ports) .withLoadBalancerIP(loadBalancerIP) .endSpec() .build(); log.trace("Created service {}", service); return service; }
Example #13
Source File: AbstractModel.java From strimzi-kafka-operator with Apache License 2.0 | 6 votes |
/** * Creates a headless service * * Uses Alpha annotation service.alpha.kubernetes.io/tolerate-unready-endpoints for older versions of Kubernetes still supported by Strimzi, * replaced by the publishNotReadyAddresses field in the spec, annotation is ignored in later versions of Kubernetes */ protected Service createHeadlessService(List<ServicePort> ports) { Map<String, String> annotations = Collections.singletonMap("service.alpha.kubernetes.io/tolerate-unready-endpoints", "true"); Service service = new ServiceBuilder() .withNewMetadata() .withName(headlessServiceName) .withLabels(getLabelsWithStrimziName(headlessServiceName, templateHeadlessServiceLabels).toMap()) .withNamespace(namespace) .withAnnotations(mergeLabelsOrAnnotations(annotations, templateHeadlessServiceAnnotations)) .withOwnerReferences(createOwnerReference()) .endMetadata() .withNewSpec() .withType("ClusterIP") .withClusterIP("None") .withSelector(getSelectorLabels().toMap()) .withPorts(ports) .withPublishNotReadyAddresses(true) .endSpec() .build(); log.trace("Created headless service {}", service); return service; }
Example #14
Source File: ServiceTest.java From kubernetes-client with Apache License 2.0 | 6 votes |
@Test void testUpdate() { Service serviceFromServer = new ServiceBuilder(service) .editOrNewMetadata().addToLabels("foo", "bar").endMetadata() .editOrNewSpec().withClusterIP("10.96.129.1").endSpec().build(); server.expect().get() .withPath("/api/v1/namespaces/test/services/httpbin") .andReturn(200, serviceFromServer) .times(3); server.expect().patch() .withPath("/api/v1/namespaces/test/services/httpbin") .andReturn(200, serviceFromServer) .once(); KubernetesClient client = server.getClient(); Service responseFromServer = client.services().inNamespace("test").withName("httpbin").edit() .editOrNewMetadata().addToLabels("foo", "bar").endMetadata() .done(); assertNotNull(responseFromServer); assertEquals("bar", responseFromServer.getMetadata().getLabels().get("foo")); }
Example #15
Source File: KubernetesResource.java From strimzi-kafka-operator with Apache License 2.0 | 6 votes |
public static ServiceBuilder getSystemtestsServiceResource(String appName, int port, String namespace, String transportProtocol) { return new ServiceBuilder() .withNewMetadata() .withName(appName) .withNamespace(namespace) .addToLabels("run", appName) .endMetadata() .withNewSpec() .withSelector(Collections.singletonMap("app", appName)) .addNewPort() .withName("http") .withPort(port) .withProtocol(transportProtocol) .endPort() .endSpec(); }
Example #16
Source File: PrometheusEnricher.java From jkube with Eclipse Public License 2.0 | 6 votes |
@Override public void create(PlatformMode platformMode, KubernetesListBuilder builder) { builder.accept(new TypedVisitor<ServiceBuilder>() { @Override public void visit(ServiceBuilder serviceBuilder) { String prometheusPort = findPrometheusPort(); if (StringUtils.isNotBlank(prometheusPort)) { Map<String, String> annotations = new HashMap<>(); MapUtil.putIfAbsent(annotations, ANNOTATION_PROMETHEUS_PORT, prometheusPort); MapUtil.putIfAbsent(annotations, ANNOTATION_PROMETHEUS_SCRAPE, "true"); String prometheusPath = getConfig(Config.prometheusPath); if (StringUtils.isNotBlank(prometheusPath)) { MapUtil.putIfAbsent(annotations, ANNOTATION_PROMETHEUS_PATH, prometheusPath); } log.verbose("Adding prometheus.io annotations: %s", annotations.entrySet() .stream() .map(Object::toString) .collect(Collectors.joining(", "))); serviceBuilder.editMetadata().addToAnnotations(annotations).endMetadata(); } } }); }
Example #17
Source File: SystemtestsKubernetesApps.java From enmasse with Apache License 2.0 | 6 votes |
private static Service getProxyApiServiceResource() { return new ServiceBuilder() .withNewMetadata() .withName(API_PROXY) .addToLabels("app", API_PROXY) .addToAnnotations("service.alpha.openshift.io/serving-cert-secret-name", "api-proxy-cert") .endMetadata() .withNewSpec() .addToSelector("app", API_PROXY) .addNewPort() .withName("https") .withPort(8443) .withProtocol("TCP") .withNewTargetPort("https") .endPort() .withClusterIP("None") // Headless .endSpec() .build(); }
Example #18
Source File: SystemtestsKubernetesApps.java From enmasse with Apache License 2.0 | 6 votes |
private static Service getSystemtestsServiceResource(String appName, int port) { return new ServiceBuilder() .withNewMetadata() .withName(appName) .addToLabels("run", appName) .endMetadata() .withNewSpec() .addToSelector("app", appName) .addNewPort() .withName("http") .withPort(port) .withProtocol("TCP") .endPort() .endSpec() .build(); }
Example #19
Source File: SystemtestsKubernetesApps.java From enmasse with Apache License 2.0 | 6 votes |
private static Service getScaleTestClientServiceResource(String clientId, Map<String, String> labels) { return new ServiceBuilder() .withNewMetadata() .withName(SCALE_TEST_CLIENT + "-" + clientId) .addToLabels(labels) .endMetadata() .withNewSpec() .addToSelector(labels) .addNewPort() .withName("http") .withPort(8080) .withProtocol("TCP") .endPort() .endSpec() .build(); }
Example #20
Source File: KubernetesMockTest.java From vxms with Apache License 2.0 | 6 votes |
@Test public void findServices() { final ObjectMeta build = new ObjectMetaBuilder().addToLabels("test", "test").build(); final ServiceSpec spec = new ServiceSpecBuilder().addNewPort().and().withClusterIP("192.168.1.1").build(); final Service service = new ServiceBuilder().withMetadata(build).withSpec(spec).build(); server .expect() .withPath("/api/v1/namespaces/default/services") .andReturn(200, new ServiceListBuilder().addToItems().addToItems(service).build()) .once(); KubernetesClient client = this.client; final ServiceList list = client.services().inNamespace("default").list(); assertNotNull(list); assertEquals("test", list.getItems().get(0).getMetadata().getLabels().get("test")); System.out.println(list.getItems().get(0).getSpec().getClusterIP()); }
Example #21
Source File: KubernetesClientTestBase.java From flink with Apache License 2.0 | 6 votes |
private Service buildExternalService( KubernetesConfigOptions.ServiceExposedType serviceExposedType, ServicePort servicePort, @Nullable ServiceStatus serviceStatus) { final ServiceBuilder serviceBuilder = new ServiceBuilder() .editOrNewMetadata() .withName(ExternalServiceDecorator.getExternalServiceName(CLUSTER_ID)) .endMetadata() .editOrNewSpec() .withType(serviceExposedType.name()) .addNewPortLike(servicePort) .endPort() .endSpec(); if (serviceStatus != null) { serviceBuilder.withStatus(serviceStatus); } return serviceBuilder.build(); }
Example #22
Source File: OpenShiftEnvironmentFactoryTest.java From che with Eclipse Public License 2.0 | 6 votes |
@Test public void shouldCreateOpenShiftEnvironmentWithServicesFromRecipe() throws Exception { // given Service service1 = new ServiceBuilder().withNewMetadata().withName("service1").endMetadata().build(); Service service2 = new ServiceBuilder().withNewMetadata().withName("service2").endMetadata().build(); when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(asList(service1, service2)); // when KubernetesEnvironment osEnv = osEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList()); // then assertEquals(osEnv.getServices().size(), 2); assertEquals(osEnv.getServices().get("service1"), service1); assertEquals(osEnv.getServices().get("service2"), service2); }
Example #23
Source File: ExternalServiceDecorator.java From flink with Apache License 2.0 | 6 votes |
@Override public List<HasMetadata> buildAccompanyingKubernetesResources() throws IOException { final String serviceName = getExternalServiceName(kubernetesJobManagerParameters.getClusterId()); final Service externalService = new ServiceBuilder() .withApiVersion(Constants.API_VERSION) .withNewMetadata() .withName(serviceName) .withLabels(kubernetesJobManagerParameters.getCommonLabels()) .withAnnotations(kubernetesJobManagerParameters.getRestServiceAnnotations()) .endMetadata() .withNewSpec() .withType(kubernetesJobManagerParameters.getRestServiceExposedType().name()) .withSelector(kubernetesJobManagerParameters.getLabels()) .addNewPort() .withName(Constants.REST_PORT_NAME) .withPort(kubernetesJobManagerParameters.getRestPort()) .withNewTargetPort(kubernetesJobManagerParameters.getRestBindPort()) .endPort() .endSpec() .build(); return Collections.singletonList(externalService); }
Example #24
Source File: KubernetesEnvironmentProvisionerTest.java From che with Eclipse Public License 2.0 | 6 votes |
@Test( expectedExceptions = DevfileFormatException.class, expectedExceptionsMessageRegExp = "Components can not have objects with the same name and kind but there are multiple objects with kind 'Service' and name 'db'") public void shouldThrowExceptionIfComponentHasMultipleObjectsWithTheSameKindAndName() throws Exception { // given List<HasMetadata> objects = new ArrayList<>(); Service service = new ServiceBuilder() .withNewMetadata() .withName("db") .endMetadata() .withNewSpec() .endSpec() .build(); objects.add(new ServiceBuilder(service).build()); objects.add(new ServiceBuilder(service).build()); // when k8sEnvProvisioner.provision(workspaceConfig, KubernetesEnvironment.TYPE, objects, emptyMap()); }
Example #25
Source File: KubernetesEnvironmentFactoryTest.java From che with Eclipse Public License 2.0 | 6 votes |
@Test public void shouldCreateK8sEnvironmentWithServicesFromRecipe() throws Exception { // given Service service1 = new ServiceBuilder().withNewMetadata().withName("service1").endMetadata().build(); Service service2 = new ServiceBuilder().withNewMetadata().withName("service2").endMetadata().build(); when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(asList(service1, service2)); // when KubernetesEnvironment k8sEnv = k8sEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList()); // then assertEquals(k8sEnv.getServices().size(), 2); assertEquals(k8sEnv.getServices().get("service1"), service1); assertEquals(k8sEnv.getServices().get("service2"), service2); }
Example #26
Source File: KubernetesServerResolverTest.java From che with Eclipse Public License 2.0 | 6 votes |
private Service createService( String name, String machineName, Integer port, Map<String, ServerConfigImpl> servers) { Serializer serializer = Annotations.newSerializer(); serializer.machineName(machineName); if (servers != null) { serializer.servers(servers); } return new ServiceBuilder() .withNewMetadata() .withName(name) .withAnnotations(serializer.annotations()) .endMetadata() .withNewSpec() .withPorts( new ServicePortBuilder() .withPort(port) .withNewTargetPort() .withIntVal(port) .endTargetPort() .build()) .endSpec() .build(); }
Example #27
Source File: SidecarServicesProvisionerTest.java From che with Eclipse Public License 2.0 | 6 votes |
private Service createService(ChePluginEndpoint endpoint) { ServicePort servicePort = new ServicePortBuilder() .withPort(endpoint.getTargetPort()) .withProtocol("TCP") .withNewTargetPort(endpoint.getTargetPort()) .build(); return new ServiceBuilder() .withNewMetadata() .withName(endpoint.getName()) .endMetadata() .withNewSpec() .withSelector(singletonMap(CHE_ORIGINAL_NAME_LABEL, POD_NAME)) .withPorts(singletonList(servicePort)) .endSpec() .build(); }
Example #28
Source File: PropagationPolicyTest.java From kubernetes-client with Apache License 2.0 | 6 votes |
@Test @DisplayName("Should delete a Service with PropagationPolicy=Background") void testDeleteService() throws InterruptedException { // Given server.expect().delete().withPath("/api/v1/namespaces/ns1/services/myservice") .andReturn(HttpURLConnection.HTTP_OK, new ServiceBuilder().build()) .once(); KubernetesClient client = server.getClient(); // When Boolean isDeleted = client.services().inNamespace("ns1").withName("myservice").delete(); // Then assertTrue(isDeleted); assertDeleteOptionsInLastRecordedRequest(DeletionPropagation.BACKGROUND.toString(), server.getLastRequest()); }
Example #29
Source File: PropagationPolicyTest.java From kubernetes-client with Apache License 2.0 | 6 votes |
@Test @DisplayName("Should delete a Service with specified PropagationPolicy") void testDeleteServiceWithPropagationPolicy() throws InterruptedException { // Given server.expect().delete().withPath("/api/v1/namespaces/ns1/services/myservice") .andReturn(HttpURLConnection.HTTP_OK, new ServiceBuilder().build()) .once(); KubernetesClient client = server.getClient(); // When Boolean isDeleted = client.services().inNamespace("ns1").withName("myservice").delete(); // Then assertTrue(isDeleted); assertDeleteOptionsInLastRecordedRequest(DeletionPropagation.BACKGROUND.toString(), server.getLastRequest()); }
Example #30
Source File: ServiceTest.java From kubernetes-client with Apache License 2.0 | 6 votes |
@BeforeEach void prepareService() { service = new ServiceBuilder() .withNewMetadata() .withName("httpbin") .withLabels(Collections.singletonMap("app", "httpbin")) .endMetadata() .withNewSpec() .addNewPort() .withName("http") .withPort(5511) .withTargetPort(new IntOrString(8080)) .endPort() .addToSelector("deploymentconfig", "httpbin") .endSpec() .build(); }