Search in sources :

Example 61 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project che-server by eclipse-che.

the class DevfileIntegrityValidatorTest method shouldRequireAliasWhenKubernetesComponentsHaveSameReference.

@Test(expectedExceptions = DevfileFormatException.class, expectedExceptionsMessageRegExp = "There are multiple components 'list.yaml' of type 'kubernetes' that cannot be" + " uniquely identified. Please add aliases that would distinguish the components.")
public void shouldRequireAliasWhenKubernetesComponentsHaveSameReference() throws Exception {
    // given
    DevfileImpl devfile = new DevfileImpl(initialDevfile);
    ComponentImpl k8s1 = new ComponentImpl();
    k8s1.setType(KUBERNETES_COMPONENT_TYPE);
    k8s1.setReference("list.yaml");
    ComponentImpl k8s2 = new ComponentImpl();
    k8s2.setType(KUBERNETES_COMPONENT_TYPE);
    k8s2.setReference("list.yaml");
    devfile.getComponents().add(k8s1);
    devfile.getComponents().add(k8s2);
    // when
    integrityValidator.validateDevfile(devfile);
// then
// exception is thrown
}
Also used : DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Example 62 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project che-server by eclipse-che.

the class TestObjectGenerator method createDevfile.

private static DevfileImpl createDevfile(String name, String generatedName) {
    String effectiveName = MoreObjects.firstNonNull(name, generatedName);
    SourceImpl source1 = new SourceImpl("type1", "http://location", "branch1", "point1", "tag1", "commit1", "sparseCheckoutDir1");
    ProjectImpl project1 = new ProjectImpl("project1", source1, "path1");
    SourceImpl source2 = new SourceImpl("type2", "http://location", "branch2", "point2", "tag2", "commit2", "sparseCheckoutDir2");
    ProjectImpl project2 = new ProjectImpl("project2", source2, "path2");
    ActionImpl action1 = new ActionImpl("exec1", "component1", "run.sh", "/home/user/1", null, null);
    ActionImpl action2 = new ActionImpl("exec2", "component2", "run.sh", "/home/user/2", null, null);
    CommandImpl command1 = new CommandImpl(effectiveName + "-1", singletonList(action1), singletonMap("attr1", "value1"), null);
    CommandImpl command2 = new CommandImpl(effectiveName + "-2", singletonList(action2), singletonMap("attr2", "value2"), null);
    EntrypointImpl entrypoint1 = new EntrypointImpl("parentName1", singletonMap("parent1", "selector1"), "containerName1", asList("command1", "command2"), asList("arg1", "arg2"));
    EntrypointImpl entrypoint2 = new EntrypointImpl("parentName2", singletonMap("parent2", "selector2"), "containerName2", asList("command3", "command4"), asList("arg3", "arg4"));
    org.eclipse.che.api.workspace.server.model.impl.devfile.VolumeImpl volume1 = new org.eclipse.che.api.workspace.server.model.impl.devfile.VolumeImpl("name1", "path1");
    org.eclipse.che.api.workspace.server.model.impl.devfile.VolumeImpl volume2 = new org.eclipse.che.api.workspace.server.model.impl.devfile.VolumeImpl("name2", "path2");
    EnvImpl env1 = new EnvImpl("name1", "value1");
    EnvImpl env2 = new EnvImpl("name2", "value2");
    EndpointImpl endpoint1 = new EndpointImpl("name1", 1111, singletonMap("key1", "value1"));
    EndpointImpl endpoint2 = new EndpointImpl("name2", 2222, singletonMap("key2", "value2"));
    ComponentImpl component1 = new ComponentImpl("kubernetes", "component1", null, null, null, null, "refcontent1", ImmutableMap.of("app.kubernetes.io/component", "db"), null, null, null, null, null, null, false, false, null, null, null, asList(env1, env2), null);
    component1.setSelector(singletonMap("key1", "value1"));
    ComponentImpl component2 = new ComponentImpl("dockerimage", "component2", null, null, null, null, null, null, null, "image", "256G", null, "3", "180m", false, false, singletonList("command"), singletonList("arg"), asList(volume1, volume2), asList(env1, env2), asList(endpoint1, endpoint2));
    ComponentImpl component3 = new ComponentImpl("chePlugin", "check/terminal-sample/0.0.1", ImmutableMap.of("java.home", "/home/user/jdk11aertwertert", "java.boolean", "true", "java.long", "123444L"));
    MetadataImpl metadata = new MetadataImpl(name);
    metadata.setGenerateName(generatedName);
    DevfileImpl devfile = new DevfileImpl("1.0.0", asList(project1, project2), asList(component1, component2, component3), asList(command1, command2), singletonMap("attribute1", "value1"), metadata);
    return devfile;
}
Also used : CommandImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.CommandImpl) EntrypointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EntrypointImpl) ProjectImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ProjectImpl) EndpointImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl) EnvImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.EnvImpl) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) MetadataImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.MetadataImpl) SourceImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.SourceImpl) UserDevfileImpl(org.eclipse.che.api.devfile.server.model.impl.UserDevfileImpl) DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) ActionImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ActionImpl)

Example 63 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project devspaces-images by redhat-developer.

the class EnvironmentVariableSecretApplier method applySecret.

/**
 * Applies secret as environment variable into workspace containers, respecting automount
 * attribute and optional devfile automount property override.
 *
 * @param env kubernetes environment with workspace containers configuration
 * @param runtimeIdentity identity of current runtime
 * @param secret source secret to apply
 * @throws InfrastructureException on misconfigured secrets or other apply error
 */
@Override
public void applySecret(KubernetesEnvironment env, RuntimeIdentity runtimeIdentity, Secret secret) throws InfrastructureException {
    boolean secretAutomount = Boolean.parseBoolean(secret.getMetadata().getAnnotations().get(ANNOTATION_AUTOMOUNT));
    for (PodData podData : env.getPodsData().values()) {
        if (!podData.getRole().equals(PodRole.DEPLOYMENT)) {
            continue;
        }
        for (Container container : podData.getSpec().getContainers()) {
            Optional<ComponentImpl> component = getComponent(env, container.getName());
            // skip components that explicitly disable automount
            if (component.isPresent() && isComponentAutomountFalse(component.get())) {
                continue;
            }
            // if automount disabled globally and not overridden in component
            if (!secretAutomount && (!component.isPresent() || !isComponentAutomountTrue(component.get()))) {
                continue;
            }
            for (Entry<String, String> secretDataEntry : secret.getData().entrySet()) {
                final String mountEnvName = envName(secret, secretDataEntry.getKey(), runtimeIdentity);
                container.getEnv().add(new EnvVarBuilder().withName(mountEnvName).withValueFrom(new EnvVarSourceBuilder().withSecretKeyRef(new SecretKeySelectorBuilder().withName(secret.getMetadata().getName()).withKey(secretDataEntry.getKey()).build()).build()).build());
            }
        }
    }
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Container(io.fabric8.kubernetes.api.model.Container) EnvVarSourceBuilder(io.fabric8.kubernetes.api.model.EnvVarSourceBuilder) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) EnvVarBuilder(io.fabric8.kubernetes.api.model.EnvVarBuilder) SecretKeySelectorBuilder(io.fabric8.kubernetes.api.model.SecretKeySelectorBuilder)

Example 64 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project devspaces-images by redhat-developer.

the class FileSecretApplier method applySecret.

/**
 * Applies secret as file into workspace containers, respecting automount attribute and optional
 * devfile automount property and/or mount path override.
 *
 * @param env kubernetes environment with workspace containers configuration
 * @param runtimeIdentity identity of current runtime
 * @param secret source secret to apply
 * @throws InfrastructureException on misconfigured secrets or other apply error
 */
@Override
public void applySecret(KubernetesEnvironment env, RuntimeIdentity runtimeIdentity, Secret secret) throws InfrastructureException {
    final String secretMountPath = secret.getMetadata().getAnnotations().get(ANNOTATION_MOUNT_PATH);
    boolean secretAutomount = Boolean.parseBoolean(secret.getMetadata().getAnnotations().get(ANNOTATION_AUTOMOUNT));
    if (secretMountPath == null) {
        throw new InfrastructureException(format("Unable to mount secret '%s': It is configured to be mounted as a file but the mount path was not specified. Please define the '%s' annotation on the secret to specify it.", secret.getMetadata().getName(), ANNOTATION_MOUNT_PATH));
    }
    Volume volumeFromSecret = new VolumeBuilder().withName(secret.getMetadata().getName()).withSecret(new SecretVolumeSourceBuilder().withSecretName(secret.getMetadata().getName()).build()).build();
    for (PodData podData : env.getPodsData().values()) {
        if (!podData.getRole().equals(PodRole.DEPLOYMENT)) {
            continue;
        }
        if (podData.getSpec().getVolumes().stream().anyMatch(v -> v.getName().equals(volumeFromSecret.getName()))) {
            volumeFromSecret.setName(volumeFromSecret.getName() + "_" + NameGenerator.generate("", 6));
        }
        podData.getSpec().getVolumes().add(volumeFromSecret);
        for (Container container : podData.getSpec().getContainers()) {
            Optional<ComponentImpl> component = getComponent(env, container.getName());
            // skip components that explicitly disable automount
            if (component.isPresent() && isComponentAutomountFalse(component.get())) {
                continue;
            }
            // if automount disabled globally and not overridden in component
            if (!secretAutomount && (!component.isPresent() || !isComponentAutomountTrue(component.get()))) {
                continue;
            }
            // find path override if any
            Optional<String> overridePathOptional = Optional.empty();
            if (component.isPresent()) {
                overridePathOptional = getOverridenComponentPath(component.get(), secret.getMetadata().getName());
            }
            final String componentMountPath = overridePathOptional.orElse(secretMountPath);
            // remove the existing mount here to replace it with new one.
            if (k8sVersion.olderThan(1, 13)) {
                LOG.debug("Unable to mount multiple VolumeMounts on same path on this k8s version. Removing conflicting volumes in favor of secret mounts.");
                container.getVolumeMounts().removeIf(vm -> Paths.get(vm.getMountPath()).equals(Paths.get(componentMountPath)));
            }
            container.getVolumeMounts().addAll(secret.getData().keySet().stream().map(secretFile -> buildVolumeMount(volumeFromSecret, componentMountPath, secretFile)).collect(Collectors.toList()));
        }
    }
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) SecretVolumeSourceBuilder(io.fabric8.kubernetes.api.model.SecretVolumeSourceBuilder) Container(io.fabric8.kubernetes.api.model.Container) Volume(io.fabric8.kubernetes.api.model.Volume) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 65 with ComponentImpl

use of org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl in project devspaces-images by redhat-developer.

the class EnvironmentVariableSecretApplierTest method shouldProvisionContainersWithAutomountOverrideTrue.

@Test
public void shouldProvisionContainersWithAutomountOverrideTrue() throws Exception {
    Container container_match1 = new ContainerBuilder().withName("maven").build();
    Container container_match2 = new ContainerBuilder().withName("other").build();
    DevfileImpl mock_defvile = mock(DevfileImpl.class);
    ComponentImpl component = new ComponentImpl();
    component.setAlias("maven");
    component.setAutomountWorkspaceSecrets(true);
    when(podSpec.getContainers()).thenReturn(ImmutableList.of(container_match1, container_match2));
    InternalMachineConfig internalMachineConfig = new InternalMachineConfig();
    internalMachineConfig.getAttributes().put(DEVFILE_COMPONENT_ALIAS_ATTRIBUTE, "maven");
    when(environment.getMachines()).thenReturn(ImmutableMap.of("maven", internalMachineConfig));
    when(environment.getDevfile()).thenReturn(mock_defvile);
    when(mock_defvile.getComponents()).thenReturn(singletonList(component));
    Secret secret = new SecretBuilder().withData(singletonMap("foo", "random")).withMetadata(new ObjectMetaBuilder().withName("test_secret").withAnnotations(ImmutableMap.of(ANNOTATION_ENV_NAME, "MY_FOO", ANNOTATION_MOUNT_AS, "env", ANNOTATION_AUTOMOUNT, "false")).withLabels(emptyMap()).build()).build();
    secretApplier.applySecret(environment, runtimeIdentity, secret);
    // only first container has env set
    assertEquals(container_match1.getEnv().size(), 1);
    EnvVar var = container_match1.getEnv().get(0);
    assertEquals(var.getName(), "MY_FOO");
    assertEquals(var.getValueFrom().getSecretKeyRef().getName(), "test_secret");
    assertEquals(var.getValueFrom().getSecretKeyRef().getKey(), "foo");
    assertEquals(container_match2.getEnv().size(), 0);
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) SecretBuilder(io.fabric8.kubernetes.api.model.SecretBuilder) InternalMachineConfig(org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig) Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) DevfileImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl) EnvVar(io.fabric8.kubernetes.api.model.EnvVar) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) ComponentImpl(org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl) Test(org.testng.annotations.Test)

Aggregations

ComponentImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.ComponentImpl)218 Test (org.testng.annotations.Test)184 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)76 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)56 Container (io.fabric8.kubernetes.api.model.Container)48 MachineConfigImpl (org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl)44 EndpointImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EndpointImpl)42 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)38 WorkspaceConfigImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceConfigImpl)36 CommandImpl (org.eclipse.che.api.workspace.server.model.impl.CommandImpl)26 EntrypointImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EntrypointImpl)26 Map (java.util.Map)24 EnvImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.EnvImpl)24 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)22 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)20 Collections.emptyMap (java.util.Collections.emptyMap)18 DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)18 ImmutableMap (com.google.common.collect.ImmutableMap)16 ArrayList (java.util.ArrayList)14 List (java.util.List)14