Search in sources :

Example 6 with Annotations

use of org.eclipse.che.workspace.infrastructure.kubernetes.Annotations in project che-server by eclipse-che.

the class GatewayRouterProvisionerTest method testFailWhenMoreThanOneServerInConfigmapAnnotations.

@Test(expectedExceptions = InfrastructureException.class)
public void testFailWhenMoreThanOneServerInConfigmapAnnotations() throws InfrastructureException {
    // given
    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));
    // when
    gatewayRouterProvisioner.provision(env, identity);
// then exception
}
Also used : Annotations(org.eclipse.che.workspace.infrastructure.kubernetes.Annotations) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) Test(org.testng.annotations.Test)

Example 7 with Annotations

use of org.eclipse.che.workspace.infrastructure.kubernetes.Annotations in project che-server by eclipse-che.

the class GatewayRouterProvisioner method provision.

@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.");
            }
            // We're now creating only 1 gateway route configuration per ConfigMap, so we need to create
            // generator in each loop iteration.
            GatewayRouteConfigGenerator gatewayRouteConfigGenerator = configGeneratorFactory.create();
            gatewayRouteConfigGenerator.addRouteConfig(configMapEntry.getKey(), gatewayConfigMap);
            Map<String, String> gatewayConfiguration = gatewayRouteConfigGenerator.generate(identity.getInfrastructureNamespace());
            gatewayConfigMap.setData(gatewayConfiguration);
            // Configuration is now generated, so remove these internal attributes
            server.getAttributes().remove(SERVICE_NAME_ATTRIBUTE);
            server.getAttributes().remove(SERVICE_PORT_ATTRIBUTE);
            gatewayConfigMap.getMetadata().getAnnotations().putAll(new Annotations.Serializer().server(serverConfigEntry.getKey(), server).annotations());
        }
    }
}
Also used : Annotations(org.eclipse.che.workspace.infrastructure.kubernetes.Annotations) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) GatewayRouteConfigGenerator(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.GatewayRouteConfigGenerator) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 8 with Annotations

use of org.eclipse.che.workspace.infrastructure.kubernetes.Annotations in project devspaces-images by redhat-developer.

the class GatewayRouterProvisionerTest method testProvision.

@Test
public void testProvision() throws InfrastructureException {
    // given
    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);
    // when
    gatewayRouterProvisioner.provision(env, identity);
    // then
    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());
    // verify that provisioner removes the internal attributes
    assertFalse(server.getAttributes().containsKey(SERVICE_NAME_ATTRIBUTE));
    assertFalse(server.getAttributes().containsKey(SERVICE_PORT_ATTRIBUTE));
    // verify that provisioner included the data info configmap
    Map<String, String> actualData = gatewayRouteConfigMap.getData();
    assertEquals(actualData, expectedData);
}
Also used : Annotations(org.eclipse.che.workspace.infrastructure.kubernetes.Annotations) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) Test(org.testng.annotations.Test)

Example 9 with Annotations

use of org.eclipse.che.workspace.infrastructure.kubernetes.Annotations in project devspaces-images by redhat-developer.

the class GatewayServerExposerTest method testExposeServiceWithGatewayConfigmap.

@Test
public void testExposeServiceWithGatewayConfigmap() {
    // given
    KubernetesEnvironment k8sEnv = KubernetesEnvironment.builder().build();
    // when
    serverExposer.expose(k8sEnv, machineName, serviceName, serverId, servicePort, servers);
    // then
    Map<String, ConfigMap> configMaps = k8sEnv.getConfigMaps();
    assertTrue(configMaps.containsKey(serviceName + "-" + serverId));
    ConfigMap serverConfigMap = configMaps.get("service-server");
    // data should be empty at this point
    assertTrue(serverConfigMap.getData() == null || serverConfigMap.getData().isEmpty());
    assertEquals(serverConfigMap.getMetadata().getLabels(), GATEWAY_CONFIGMAP_LABELS);
    Map<String, String> annotations = serverConfigMap.getMetadata().getAnnotations();
    Annotations.Deserializer deserializer = Annotations.newDeserializer(annotations);
    assertEquals(deserializer.machineName(), machineName);
    Map<String, ServerConfigImpl> exposedServers = deserializer.servers();
    assertTrue(exposedServers.containsKey("serverOne"));
    ServerConfig s1 = exposedServers.get("serverOne");
    assertEquals(s1.getAttributes().get(s1attrs.keySet().iterator().next()), s1attrs.values().iterator().next());
    assertEquals(s1.getAttributes().get(ServerConfigImpl.SERVICE_NAME_ATTRIBUTE), "service");
    assertEquals(s1.getAttributes().get(ServerConfigImpl.SERVICE_PORT_ATTRIBUTE), "1234");
    assertEquals(s1.getPort(), "1111");
    assertEquals(s1.getProtocol(), "ws");
    assertNull(s1.getPath());
    assertEquals(s1.getEndpointOrigin(), "/service/server/");
}
Also used : ServerConfig(org.eclipse.che.api.core.model.workspace.config.ServerConfig) Annotations(org.eclipse.che.workspace.infrastructure.kubernetes.Annotations) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) Test(org.testng.annotations.Test)

Example 10 with Annotations

use of org.eclipse.che.workspace.infrastructure.kubernetes.Annotations in project devspaces-images by redhat-developer.

the class GatewayRouterProvisioner method provision.

@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.");
            }
            // We're now creating only 1 gateway route configuration per ConfigMap, so we need to create
            // generator in each loop iteration.
            GatewayRouteConfigGenerator gatewayRouteConfigGenerator = configGeneratorFactory.create();
            gatewayRouteConfigGenerator.addRouteConfig(configMapEntry.getKey(), gatewayConfigMap);
            Map<String, String> gatewayConfiguration = gatewayRouteConfigGenerator.generate(identity.getInfrastructureNamespace());
            gatewayConfigMap.setData(gatewayConfiguration);
            // Configuration is now generated, so remove these internal attributes
            server.getAttributes().remove(SERVICE_NAME_ATTRIBUTE);
            server.getAttributes().remove(SERVICE_PORT_ATTRIBUTE);
            gatewayConfigMap.getMetadata().getAnnotations().putAll(new Annotations.Serializer().server(serverConfigEntry.getKey(), server).annotations());
        }
    }
}
Also used : Annotations(org.eclipse.che.workspace.infrastructure.kubernetes.Annotations) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) GatewayRouteConfigGenerator(org.eclipse.che.workspace.infrastructure.kubernetes.server.external.GatewayRouteConfigGenerator) ServerConfigImpl(org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Aggregations

ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)10 Annotations (org.eclipse.che.workspace.infrastructure.kubernetes.Annotations)10 ServerConfigImpl (org.eclipse.che.api.workspace.server.model.impl.ServerConfigImpl)8 Test (org.testng.annotations.Test)8 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)6 IntOrString (io.fabric8.kubernetes.api.model.IntOrString)2 ServerConfig (org.eclipse.che.api.core.model.workspace.config.ServerConfig)2 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)2 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)2 GatewayRouteConfigGenerator (org.eclipse.che.workspace.infrastructure.kubernetes.server.external.GatewayRouteConfigGenerator)2