Search in sources :

Example 6 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.

the class ServiceAccountProvisioner method provision.

@Override
@Traced
public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException {
    TracingTags.WORKSPACE_ID.set(identity::getWorkspaceId);
    if (!isNullOrEmpty(serviceAccount)) {
        for (PodData pod : k8sEnv.getPodsData().values()) {
            pod.getSpec().setServiceAccountName(serviceAccount);
            pod.getSpec().setAutomountServiceAccountToken(true);
        }
    }
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Traced(org.eclipse.che.commons.annotation.Traced)

Example 7 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.

the class UniqueNamesProvisioner method rewriteConfigMapNames.

private void rewriteConfigMapNames(PodData pod, Map<String, String> configMapNameTranslation) {
    // First update any env vars that reference configMaps.
    for (Container container : pod.getSpec().getContainers()) {
        // Can set env vars to key/value pairs in configmap
        if (container.getEnv() != null) {
            container.getEnv().stream().filter(env -> env.getValueFrom() != null && env.getValueFrom().getConfigMapKeyRef() != null).forEach(env -> {
                ConfigMapKeySelector configMap = env.getValueFrom().getConfigMapKeyRef();
                String originalName = configMap.getName();
                // null).
                if (configMapNameTranslation.containsKey(originalName)) {
                    configMap.setName(configMapNameTranslation.get(originalName));
                }
            });
        }
        if (container.getEnvFrom() != null) {
            // Can use all entries in configMap as env vars
            container.getEnvFrom().stream().filter(envFrom -> envFrom.getConfigMapRef() != null).forEach(envFrom -> {
                ConfigMapEnvSource configMapRef = envFrom.getConfigMapRef();
                String originalName = configMapRef.getName();
                if (configMapNameTranslation.containsKey(originalName)) {
                    configMapRef.setName(configMapNameTranslation.get(originalName));
                }
            });
        }
    }
    // Next update any mounted configMaps
    List<Volume> volumes = pod.getSpec().getVolumes();
    if (pod.getSpec().getVolumes() != null) {
        volumes.stream().filter(vol -> vol.getConfigMap() != null).forEach(volume -> {
            ConfigMapVolumeSource configMapVolume = volume.getConfigMap();
            String originalName = configMapVolume.getName();
            if (configMapNameTranslation.containsKey(originalName)) {
                configMapVolume.setName(configMapNameTranslation.get(originalName));
            }
        });
    }
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) Traced(org.eclipse.che.commons.annotation.Traced) HashSet(java.util.HashSet) Names(org.eclipse.che.workspace.infrastructure.kubernetes.Names) KubernetesObjectUtil.putLabel(org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesObjectUtil.putLabel) ConfigMapKeySelector(io.fabric8.kubernetes.api.model.ConfigMapKeySelector) Map(java.util.Map) ConfigMapVolumeSource(io.fabric8.kubernetes.api.model.ConfigMapVolumeSource) ConfigMapEnvSource(io.fabric8.kubernetes.api.model.ConfigMapEnvSource) Volume(io.fabric8.kubernetes.api.model.Volume) Constants(org.eclipse.che.workspace.infrastructure.kubernetes.Constants) Collection(java.util.Collection) Set(java.util.Set) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) List(java.util.List) Ingress(io.fabric8.kubernetes.api.model.networking.v1.Ingress) TracingTags(org.eclipse.che.commons.tracing.TracingTags) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) RuntimeIdentity(org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity) PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ConfigMapEnvSource(io.fabric8.kubernetes.api.model.ConfigMapEnvSource) Container(io.fabric8.kubernetes.api.model.Container) Volume(io.fabric8.kubernetes.api.model.Volume) ConfigMapVolumeSource(io.fabric8.kubernetes.api.model.ConfigMapVolumeSource) ConfigMapKeySelector(io.fabric8.kubernetes.api.model.ConfigMapKeySelector)

Example 8 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.

the class KubernetesTrustedCAProvisioner method provision.

/**
 * Propagates additional CA certificates into config map and mounts them into all pods of given
 * namespace
 *
 * @param k8sEnv available objects in the scope
 * @param runtimeID defines namespace into which config map should be provisioned
 * @throws InfrastructureException if failed to CRUD a resource
 */
public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity runtimeID) throws InfrastructureException {
    if (!trustedStoreInitialized) {
        return;
    }
    ConfigMap allCaCertsConfigMap = cheServerClientFactory.create().configMaps().inNamespace(installationLocationNamespace).withName(caBundleConfigMap).get();
    if (allCaCertsConfigMap == null) {
        return;
    }
    k8sEnv.getConfigMaps().put(configMapName, new ConfigMapBuilder().withMetadata(new ObjectMetaBuilder().withName(configMapName).withAnnotations(allCaCertsConfigMap.getMetadata().getAnnotations()).withLabels(configMapLabelKeyValue).build()).withApiVersion(allCaCertsConfigMap.getApiVersion()).withData(allCaCertsConfigMap.getData()).build());
    for (PodData pod : k8sEnv.getPodsData().values()) {
        if (pod.getRole() == PodRole.DEPLOYMENT) {
            if (pod.getSpec().getVolumes().stream().noneMatch(v -> v.getName().equals(CHE_TRUST_STORE_VOLUME))) {
                pod.getSpec().getVolumes().add(new VolumeBuilder().withName(CHE_TRUST_STORE_VOLUME).withConfigMap(new ConfigMapVolumeSourceBuilder().withName(configMapName).build()).build());
            }
        }
        for (Container container : pod.getSpec().getInitContainers()) {
            provisionTrustStoreVolumeMountIfNeeded(container);
        }
        for (Container container : pod.getSpec().getContainers()) {
            provisionTrustStoreVolumeMountIfNeeded(container);
        }
    }
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Container(io.fabric8.kubernetes.api.model.Container) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) ConfigMapVolumeSourceBuilder(io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder)

Example 9 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.

the class OpenShiftUniqueNamesProvisionerTest method provideUniquePodsNames.

@Test
public void provideUniquePodsNames() throws Exception {
    when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID);
    Pod pod = newPod();
    PodData podData = new PodData(pod.getSpec(), pod.getMetadata());
    doReturn(ImmutableMap.of(POD_NAME, podData)).when(osEnv).getPodsData();
    uniqueNamesProvisioner.provision(osEnv, runtimeIdentity);
    ObjectMeta podMetadata = pod.getMetadata();
    assertNotEquals(podMetadata.getName(), POD_NAME);
    assertEquals(podMetadata.getLabels().get(CHE_ORIGINAL_NAME_LABEL), POD_NAME);
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Pod(io.fabric8.kubernetes.api.model.Pod) Test(org.testng.annotations.Test)

Example 10 with PodData

use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.

the class KubernetesComponentToWorkspaceApplier method prepareMachineConfigs.

/**
 * Creates map of machine names and corresponding {@link MachineConfigImpl} with component alias
 * attribute set.
 */
private Map<String, MachineConfigImpl> prepareMachineConfigs(List<PodData> podsData, ComponentImpl component) throws DevfileException {
    Map<String, MachineConfigImpl> machineConfigs = new HashMap<>();
    for (PodData podData : podsData) {
        List<Container> containers = new ArrayList<>();
        containers.addAll(podData.getSpec().getContainers());
        containers.addAll(podData.getSpec().getInitContainers());
        for (Container container : containers) {
            String machineName = machineName(podData, container);
            MachineConfigImpl config = new MachineConfigImpl();
            if (!isNullOrEmpty(component.getAlias())) {
                config.getAttributes().put(DEVFILE_COMPONENT_ALIAS_ATTRIBUTE, component.getAlias());
            }
            provisionVolumes(component, container, config);
            provisionEndpoints(component, config);
            machineConfigs.put(machineName, config);
        }
    }
    return machineConfigs;
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) Container(io.fabric8.kubernetes.api.model.Container) MachineConfigImpl(org.eclipse.che.api.workspace.server.model.impl.MachineConfigImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Aggregations

PodData (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData)156 Test (org.testng.annotations.Test)86 Container (io.fabric8.kubernetes.api.model.Container)62 Pod (io.fabric8.kubernetes.api.model.Pod)56 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)52 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)52 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)40 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)36 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)34 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)30 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)28 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)26 Map (java.util.Map)22 InternalMachineConfig (org.eclipse.che.api.workspace.server.spi.environment.InternalMachineConfig)22 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)20 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)20 KubernetesEnvironment (org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment)20 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)16 Volume (io.fabric8.kubernetes.api.model.Volume)16 HashMap (java.util.HashMap)16