Java Code Examples for io.fabric8.kubernetes.api.model.Service#setSpec()

The following examples show how to use io.fabric8.kubernetes.api.model.Service#setSpec() . 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: TillerInstaller.java    From microbean-helm with Apache License 2.0 6 votes vote down vote up
protected Service createService(final String namespace,
                                final String serviceName,
                                Map<String, String> labels) {
  labels = normalizeLabels(labels);

  final Service service = new Service();
  
  final ObjectMeta metadata = new ObjectMeta();
  metadata.setNamespace(normalizeNamespace(namespace));
  metadata.setName(normalizeServiceName(serviceName));
  metadata.setLabels(labels);
  
  service.setMetadata(metadata);
  service.setSpec(this.createServiceSpec(labels));

  return service;
}
 
Example 2
Source File: OpenShiftPreviewUrlCommandProvisionerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldDoNothingWhenCantFindRouteForPreviewUrl() throws InfrastructureException {
  int port = 8080;
  List<CommandImpl> commands =
      Collections.singletonList(
          new CommandImpl("a", "a", "a", new PreviewUrlImpl(port, null), Collections.emptyMap()));
  OpenShiftEnvironment env =
      OpenShiftEnvironment.builder().setCommands(new ArrayList<>(commands)).build();

  Mockito.when(mockProject.services()).thenReturn(mockServices);
  Service service = new Service();
  ServiceSpec spec = new ServiceSpec();
  spec.setPorts(
      Collections.singletonList(new ServicePort("a", null, port, "TCP", new IntOrString(port))));
  service.setSpec(spec);
  Mockito.when(mockServices.get()).thenReturn(Collections.singletonList(service));

  Mockito.when(mockProject.routes()).thenReturn(mockRoutes);
  Mockito.when(mockRoutes.get()).thenReturn(Collections.emptyList());

  previewUrlCommandProvisioner.provision(env, mockProject);

  assertTrue(commands.containsAll(env.getCommands()));
  assertTrue(env.getCommands().containsAll(commands));
  assertEquals(
      env.getWarnings().get(0).getCode(), Warnings.NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL);
}
 
Example 3
Source File: OpenShiftPreviewUrlCommandProvisionerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldUpdateCommandWhenServiceAndIngressFound() throws InfrastructureException {
  int port = 8080;
  List<CommandImpl> commands =
      Collections.singletonList(
          new CommandImpl("a", "a", "a", new PreviewUrlImpl(port, null), Collections.emptyMap()));
  OpenShiftEnvironment env =
      OpenShiftEnvironment.builder().setCommands(new ArrayList<>(commands)).build();

  Mockito.when(mockProject.services()).thenReturn(mockServices);
  Service service = new Service();
  ObjectMeta metadata = new ObjectMeta();
  metadata.setName("servicename");
  service.setMetadata(metadata);
  ServiceSpec spec = new ServiceSpec();
  spec.setPorts(
      Collections.singletonList(
          new ServicePort("8080", null, port, "TCP", new IntOrString(port))));
  service.setSpec(spec);
  Mockito.when(mockServices.get()).thenReturn(Collections.singletonList(service));

  Route route = new Route();
  RouteSpec routeSpec = new RouteSpec();
  routeSpec.setPort(new RoutePort(new IntOrString("8080")));
  routeSpec.setTo(new RouteTargetReference("a", "servicename", 1));
  routeSpec.setHost("testhost");
  route.setSpec(routeSpec);

  Mockito.when(mockProject.routes()).thenReturn(mockRoutes);
  Mockito.when(mockRoutes.get()).thenReturn(Collections.singletonList(route));

  previewUrlCommandProvisioner.provision(env, mockProject);

  assertTrue(env.getCommands().get(0).getAttributes().containsKey("previewUrl"));
  assertEquals(env.getCommands().get(0).getAttributes().get("previewUrl"), "testhost");
  assertTrue(env.getWarnings().isEmpty());
}
 
Example 4
Source File: OpenShiftPreviewUrlExposerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldNotProvisionWhenServiceAndRouteFound() throws InternalInfrastructureException {
  final int PORT = 8080;
  final String SERVER_PORT_NAME = "server-" + PORT;

  CommandImpl command =
      new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap());

  Service service = new Service();
  ObjectMeta serviceMeta = new ObjectMeta();
  serviceMeta.setName("servicename");
  service.setMetadata(serviceMeta);
  ServiceSpec serviceSpec = new ServiceSpec();
  serviceSpec.setPorts(
      singletonList(new ServicePort(SERVER_PORT_NAME, null, PORT, "TCP", new IntOrString(PORT))));
  service.setSpec(serviceSpec);

  Route route = new Route();
  RouteSpec routeSpec = new RouteSpec();
  routeSpec.setPort(new RoutePort(new IntOrString(SERVER_PORT_NAME)));
  routeSpec.setTo(new RouteTargetReference("routekind", "servicename", 1));
  route.setSpec(routeSpec);

  Map<String, Service> services = new HashMap<>();
  services.put("servicename", service);
  Map<String, Route> routes = new HashMap<>();
  routes.put("routename", route);

  OpenShiftEnvironment env =
      OpenShiftEnvironment.builder()
          .setCommands(singletonList(new CommandImpl(command)))
          .setServices(services)
          .setRoutes(routes)
          .build();

  assertEquals(env.getRoutes().size(), 1);
  previewUrlEndpointsProvisioner.expose(env);
  assertEquals(env.getRoutes().size(), 1);
}
 
Example 5
Source File: OpenShiftPreviewUrlExposerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldProvisionRouteWhenNotFound() throws InternalInfrastructureException {
  final int PORT = 8080;
  final String SERVER_PORT_NAME = "server-" + PORT;
  final String SERVICE_NAME = "servicename";

  CommandImpl command =
      new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap());

  Service service = new Service();
  ObjectMeta serviceMeta = new ObjectMeta();
  serviceMeta.setName(SERVICE_NAME);
  service.setMetadata(serviceMeta);
  ServiceSpec serviceSpec = new ServiceSpec();
  serviceSpec.setPorts(
      singletonList(new ServicePort(SERVER_PORT_NAME, null, PORT, "TCP", new IntOrString(PORT))));
  service.setSpec(serviceSpec);

  Map<String, Service> services = new HashMap<>();
  services.put(SERVICE_NAME, service);

  OpenShiftEnvironment env =
      OpenShiftEnvironment.builder()
          .setCommands(singletonList(new CommandImpl(command)))
          .setServices(services)
          .setRoutes(new HashMap<>())
          .build();

  previewUrlEndpointsProvisioner.expose(env);
  assertEquals(env.getRoutes().size(), 1);
  Route provisionedRoute = env.getRoutes().values().iterator().next();
  assertEquals(provisionedRoute.getSpec().getTo().getName(), SERVICE_NAME);
  assertEquals(
      provisionedRoute.getSpec().getPort().getTargetPort().getStrVal(), SERVER_PORT_NAME);
}
 
Example 6
Source File: RoutesTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private Service createService(String portString, int portInt) {
  Service service = new Service();
  ObjectMeta metadata = new ObjectMeta();
  metadata.setName("servicename");
  service.setMetadata(metadata);
  ServiceSpec spec = new ServiceSpec();
  spec.setPorts(
      Collections.singletonList(new ServicePort(portString, null, portInt, "TCP", null)));
  service.setSpec(spec);
  return service;
}
 
Example 7
Source File: KubernetesObjectUtil.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/** Adds selector into target Kubernetes service. */
public static void putSelector(Service target, String key, String value) {
  ServiceSpec spec = target.getSpec();

  if (spec == null) {
    target.setSpec(spec = new ServiceSpec());
  }

  Map<String, String> selector = spec.getSelector();
  if (selector == null) {
    spec.setSelector(selector = new HashMap<>());
  }

  selector.put(key, value);
}
 
Example 8
Source File: KubernetesPreviewUrlCommandProvisionerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldDoNothingWhenCantFindIngressForPreviewUrl() throws InfrastructureException {
  int port = 8080;
  List<CommandImpl> commands =
      Collections.singletonList(
          new CommandImpl("a", "a", "a", new PreviewUrlImpl(port, null), Collections.emptyMap()));
  KubernetesEnvironment env =
      KubernetesEnvironment.builder().setCommands(new ArrayList<>(commands)).build();

  Mockito.when(mockNamespace.services()).thenReturn(mockServices);
  Service service = new Service();
  ServiceSpec spec = new ServiceSpec();
  spec.setPorts(
      Collections.singletonList(new ServicePort("a", null, port, "TCP", new IntOrString(port))));
  service.setSpec(spec);
  Mockito.when(mockServices.get()).thenReturn(Collections.singletonList(service));

  Mockito.when(mockNamespace.ingresses()).thenReturn(mockIngresses);
  Mockito.when(mockIngresses.get()).thenReturn(Collections.emptyList());

  previewUrlCommandProvisioner.provision(env, mockNamespace);

  assertTrue(commands.containsAll(env.getCommands()));
  assertTrue(env.getCommands().containsAll(commands));
  assertEquals(
      env.getWarnings().get(0).getCode(), Warnings.NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL);
}
 
Example 9
Source File: PreviewUrlExposerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldProvisionIngressWhenNotFound() throws InternalInfrastructureException {
  Mockito.when(
          externalServiceExposureStrategy.getExternalPath(Mockito.anyString(), Mockito.any()))
      .thenReturn("some-server-path");

  final int PORT = 8080;
  final String SERVER_PORT_NAME = "server-" + PORT;
  final String SERVICE_NAME = "servicename";

  CommandImpl command =
      new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap());

  Service service = new Service();
  ObjectMeta serviceMeta = new ObjectMeta();
  serviceMeta.setName(SERVICE_NAME);
  service.setMetadata(serviceMeta);
  ServiceSpec serviceSpec = new ServiceSpec();
  serviceSpec.setPorts(
      singletonList(new ServicePort(SERVER_PORT_NAME, null, PORT, "TCP", new IntOrString(PORT))));
  service.setSpec(serviceSpec);

  Map<String, Service> services = new HashMap<>();
  services.put(SERVICE_NAME, service);

  KubernetesEnvironment env =
      KubernetesEnvironment.builder()
          .setCommands(singletonList(new CommandImpl(command)))
          .setServices(services)
          .setIngresses(new HashMap<>())
          .build();

  previewUrlExposer.expose(env);
  assertEquals(env.getIngresses().size(), 1);
  Ingress provisionedIngress = env.getIngresses().values().iterator().next();
  IngressBackend provisionedIngressBackend =
      provisionedIngress.getSpec().getRules().get(0).getHttp().getPaths().get(0).getBackend();
  assertEquals(provisionedIngressBackend.getServicePort().getStrVal(), SERVER_PORT_NAME);
  assertEquals(provisionedIngressBackend.getServiceName(), SERVICE_NAME);
}
 
Example 10
Source File: IngressesTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private Service createService(String serverPortName, int port) {
  Service service = new Service();
  ObjectMeta serviceMeta = new ObjectMeta();
  serviceMeta.setName("servicename");
  service.setMetadata(serviceMeta);
  ServiceSpec serviceSpec = new ServiceSpec();
  serviceSpec.setPorts(
      singletonList(new ServicePort(serverPortName, null, port, "TCP", new IntOrString(port))));
  service.setSpec(serviceSpec);
  return service;
}
 
Example 11
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 12
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 13
Source File: KubernetesPreviewUrlCommandProvisionerTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldUpdateCommandWhenServiceAndIngressFound() throws InfrastructureException {
  final int PORT = 8080;
  final String SERVICE_PORT_NAME = "service-" + PORT;
  List<CommandImpl> commands =
      Collections.singletonList(
          new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap()));
  KubernetesEnvironment env =
      KubernetesEnvironment.builder().setCommands(new ArrayList<>(commands)).build();

  Mockito.when(mockNamespace.services()).thenReturn(mockServices);
  Service service = new Service();
  ObjectMeta metadata = new ObjectMeta();
  metadata.setName("servicename");
  service.setMetadata(metadata);
  ServiceSpec spec = new ServiceSpec();
  spec.setPorts(
      Collections.singletonList(
          new ServicePort(SERVICE_PORT_NAME, null, PORT, "TCP", new IntOrString(PORT))));
  service.setSpec(spec);
  Mockito.when(mockServices.get()).thenReturn(Collections.singletonList(service));

  Ingress ingress = new Ingress();
  IngressSpec ingressSpec = new IngressSpec();
  IngressRule rule =
      new IngressRule(
          "testhost",
          new HTTPIngressRuleValue(
              Collections.singletonList(
                  new HTTPIngressPath(
                      new IngressBackend("servicename", new IntOrString(SERVICE_PORT_NAME)),
                      null))));
  ingressSpec.setRules(Collections.singletonList(rule));
  ingress.setSpec(ingressSpec);
  Mockito.when(mockNamespace.ingresses()).thenReturn(mockIngresses);
  Mockito.when(mockIngresses.get()).thenReturn(Collections.singletonList(ingress));

  previewUrlCommandProvisioner.provision(env, mockNamespace);

  assertTrue(env.getCommands().get(0).getAttributes().containsKey("previewUrl"));
  assertEquals(env.getCommands().get(0).getAttributes().get("previewUrl"), "testhost");
  assertTrue(env.getWarnings().isEmpty());
}
 
Example 14
Source File: PreviewUrlExposerTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldNotProvisionWhenServiceAndIngressFound()
    throws InternalInfrastructureException {
  final int PORT = 8080;
  final String SERVER_PORT_NAME = "server-" + PORT;

  CommandImpl command =
      new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap());

  Service service = new Service();
  ObjectMeta serviceMeta = new ObjectMeta();
  serviceMeta.setName("servicename");
  service.setMetadata(serviceMeta);
  ServiceSpec serviceSpec = new ServiceSpec();
  serviceSpec.setPorts(
      singletonList(new ServicePort(SERVER_PORT_NAME, null, PORT, "TCP", new IntOrString(PORT))));
  service.setSpec(serviceSpec);

  Ingress ingress = new Ingress();
  ObjectMeta ingressMeta = new ObjectMeta();
  ingressMeta.setName("ingressname");
  ingress.setMetadata(ingressMeta);
  IngressSpec ingressSpec = new IngressSpec();
  IngressRule ingressRule = new IngressRule();
  ingressRule.setHost("ingresshost");
  IngressBackend ingressBackend =
      new IngressBackend("servicename", new IntOrString(SERVER_PORT_NAME));
  ingressRule.setHttp(
      new HTTPIngressRuleValue(singletonList(new HTTPIngressPath(ingressBackend, null))));
  ingressSpec.setRules(singletonList(ingressRule));
  ingress.setSpec(ingressSpec);

  Map<String, Service> services = new HashMap<>();
  services.put("servicename", service);
  Map<String, Ingress> ingresses = new HashMap<>();
  ingresses.put("ingressname", ingress);

  KubernetesEnvironment env =
      KubernetesEnvironment.builder()
          .setCommands(singletonList(new CommandImpl(command)))
          .setServices(services)
          .setIngresses(ingresses)
          .build();

  assertEquals(env.getIngresses().size(), 1);
  previewUrlExposer.expose(env);
  assertEquals(env.getIngresses().size(), 1);
}