Ingresses { public static Optional<IngressRule> findIngressRuleForServicePort( Collection<Ingress> ingresses, Service service, int port) { Optional<ServicePort> foundPort = Services.findPort(service, port); if (!foundPort.isPresent()) { return Optional.empty(); } for (Ingress ingress : ingresses) { for (IngressRule rule : ingress.getSpec().getRules()) { for (HTTPIngressPath path : rule.getHttp().getPaths()) { IngressBackend backend = path.getBackend(); if (backend.getServiceName().equals(service.getMetadata().getName()) && matchesServicePort(backend.getServicePort(), foundPort.get())) { return Optional.of(rule); } } } } return Optional.empty(); }  static Optional<IngressRule> findIngressRuleForServicePort(
      Collection<Ingress> ingresses, Service service, int port);  }
@Test public void findHostWhenPortDefinedByString() { final String SERVER_PORT_NAME = "server-8080"; final int PORT = 8080; Service service = createService(SERVER_PORT_NAME, PORT); Ingress ingress = createIngress(new IngressBackend("servicename", new IntOrString(PORT))); Optional<IngressRule> foundRule = Ingresses.findIngressRuleForServicePort(singletonList(ingress), service, PORT); assertTrue(foundRule.isPresent()); assertEquals(foundRule.get().getHost(), "ingresshost"); }
@Test public void findHostWhenPortDefinedByInt() { final String SERVER_PORT_NAME = "server-8080"; final int PORT = 8080; Service service = createService(SERVER_PORT_NAME, PORT); Ingress ingress = createIngress(new IngressBackend("servicename", new IntOrString(SERVER_PORT_NAME))); Optional<IngressRule> foundRule = Ingresses.findIngressRuleForServicePort(singletonList(ingress), service, PORT); assertTrue(foundRule.isPresent()); assertEquals(foundRule.get().getHost(), "ingresshost"); }
@Test public void emptyWhenPortByStringAndNotFound() { final String SERVER_PORT_NAME = "server-8080"; final int PORT = 8080; Service service = createService(SERVER_PORT_NAME, PORT); Ingress ingress = createIngress(new IngressBackend("servicename", new IntOrString("does not exist"))); Optional<IngressRule> foundRule = Ingresses.findIngressRuleForServicePort(singletonList(ingress), service, PORT); assertFalse(foundRule.isPresent()); }
@Test public void emptyWhenPortByIntAndNotFound() { final String SERVER_PORT_NAME = "server-8080"; final int PORT = 8080; Service service = createService(SERVER_PORT_NAME, PORT); Ingress ingress = createIngress(new IngressBackend("servicename", new IntOrString(666))); Optional<IngressRule> foundRule = Ingresses.findIngressRuleForServicePort(singletonList(ingress), service, PORT); assertFalse(foundRule.isPresent()); }