GatewayRouterProvisioner implements ConfigurationProvisioner<KubernetesEnvironment> { @Override public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException { for (Entry<String, ConfigMap> configMapEntry : k8sEnv.getConfigMaps().entrySet()) { if (configmapLabels.isGatewayConfig(configMapEntry.getValue())) { ConfigMap gatewayConfigMap = configMapEntry.getValue(); Map<String, ServerConfigImpl> servers = new Annotations.Deserializer(gatewayConfigMap.getMetadata().getAnnotations()).servers(); if (servers.size() != 1) { throw new InfrastructureException( "Expected exactly 1 server in gateway config ConfigMap's '" + gatewayConfigMap.getMetadata().getName() + "' annotations. This is a bug, please report."); } Entry<String, ServerConfigImpl> serverConfigEntry = servers.entrySet().iterator().next(); ServerConfigImpl server = serverConfigEntry.getValue(); if (!server.getAttributes().containsKey(SERVICE_NAME_ATTRIBUTE) || !server.getAttributes().containsKey(SERVICE_PORT_ATTRIBUTE)) { throw new InfrastructureException( "Expected `serviceName` and `servicePort` in gateway config ServerConfig attributes for gateway config Configmap '" + gatewayConfigMap.getMetadata().getName() + "'. This is a bug, please report."); } GatewayRouteConfigGenerator gatewayRouteConfigGenerator = configGeneratorFactory.create(); gatewayRouteConfigGenerator.addRouteConfig(configMapEntry.getKey(), gatewayConfigMap); Map<String, String> gatewayConfiguration = gatewayRouteConfigGenerator.generate(identity.getInfrastructureNamespace()); gatewayConfigMap.setData(gatewayConfiguration); server.getAttributes().remove(SERVICE_NAME_ATTRIBUTE); server.getAttributes().remove(SERVICE_PORT_ATTRIBUTE); gatewayConfigMap .getMetadata() .getAnnotations() .putAll( new Annotations.Serializer() .server(serverConfigEntry.getKey(), server) .annotations()); } } } @Inject  GatewayRouterProvisioner(
      GatewayRouteConfigGeneratorFactory configGeneratorFactory,
      GatewayConfigmapLabels configmapLabels); @Override void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity);  }
@Test(expectedExceptions = InfrastructureException.class) public void testFailWhenNoServersInConfigmapAnnotations() throws InfrastructureException { ConfigMap gatewayRouteConfigMap = new ConfigMapBuilder().withNewMetadata().withName("route").endMetadata().build(); when(env.getConfigMaps()).thenReturn(Collections.singletonMap("route", gatewayRouteConfigMap)); gatewayRouterProvisioner.provision(env, identity); }
@Test(expectedExceptions = InfrastructureException.class) public void testFailWhenMoreThanOneServerInConfigmapAnnotations() throws InfrastructureException { Map<String, String> annotationsWith2Servers = new Annotations.Serializer() .server("s1", serverConfigWithoutAttributes) .server("s2", serverConfigWithoutAttributes) .annotations(); ConfigMap gatewayRouteConfigMap = new ConfigMapBuilder() .withNewMetadata() .withName("route") .withAnnotations(annotationsWith2Servers) .endMetadata() .build(); when(env.getConfigMaps()).thenReturn(Collections.singletonMap("route", gatewayRouteConfigMap)); gatewayRouterProvisioner.provision(env, identity); }
@Test(expectedExceptions = InfrastructureException.class) public void testFailWhenServerHasNotAllNeededAttributes() throws InfrastructureException { Map<String, String> annotationsWith2Servers = new Annotations.Serializer().server("s1", serverConfigWithoutAttributes).annotations(); ConfigMap gatewayRouteConfigMap = new ConfigMapBuilder() .withNewMetadata() .withName("route") .withAnnotations(annotationsWith2Servers) .endMetadata() .build(); when(env.getConfigMaps()).thenReturn(Collections.singletonMap("route", gatewayRouteConfigMap)); gatewayRouterProvisioner.provision(env, identity); }
@Test public void testNoProvisionWhenNoMatchingLabels() throws InfrastructureException { Map<String, String> annotationsWith2Servers = new Annotations.Serializer().server("s1", serverConfig).annotations(); ConfigMap gatewayRouteConfigMap = new ConfigMapBuilder() .withNewMetadata() .withName("route") .withAnnotations(annotationsWith2Servers) .endMetadata() .build(); when(env.getConfigMaps()).thenReturn(Collections.singletonMap("route", gatewayRouteConfigMap)); when(gatewayConfigmapLabels.isGatewayConfig(gatewayRouteConfigMap)).thenReturn(false); gatewayRouterProvisioner.provision(env, identity); verify(configGeneratorFactory, never()).create(); }
@Test public void testProvision() throws InfrastructureException { Map<String, String> annotationsWith2Servers = new Annotations.Serializer().server("s1", serverConfig).annotations(); ConfigMap gatewayRouteConfigMap = new ConfigMapBuilder() .withNewMetadata() .withName("route") .withAnnotations(annotationsWith2Servers) .endMetadata() .build(); when(env.getConfigMaps()).thenReturn(Collections.singletonMap("route", gatewayRouteConfigMap)); Map<String, String> expectedData = Collections.singletonMap("data.yml", "this is for sure generated configuration"); when(gatewayRouteConfigGenerator.generate(NAMESPACE)).thenReturn(expectedData); gatewayRouterProvisioner.provision(env, identity); verify(configGeneratorFactory).create(); verify(gatewayRouteConfigGenerator).addRouteConfig("route", gatewayRouteConfigMap); verify(gatewayRouteConfigGenerator).generate(NAMESPACE); Map<String, ServerConfigImpl> serverConfigsAfterProvisioning = new Annotations.Deserializer(gatewayRouteConfigMap.getMetadata().getAnnotations()) .servers(); assertEquals(serverConfigsAfterProvisioning.size(), 1); ServerConfigImpl server = serverConfigsAfterProvisioning.get( serverConfigsAfterProvisioning.keySet().iterator().next()); assertFalse(server.getAttributes().containsKey(SERVICE_NAME_ATTRIBUTE)); assertFalse(server.getAttributes().containsKey(SERVICE_PORT_ATTRIBUTE)); Map<String, String> actualData = gatewayRouteConfigMap.getData(); assertEquals(actualData, expectedData); }