use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.
the class ServersConverter method provision.
@Override
@Traced
public void provision(T k8sEnv, RuntimeIdentity identity) throws InfrastructureException {
TracingTags.WORKSPACE_ID.set(identity::getWorkspaceId);
SecureServerExposer<T> secureServerExposer = secureServerExposerFactoryProvider.get(k8sEnv).create(identity);
for (PodData podConfig : k8sEnv.getPodsData().values()) {
final PodSpec podSpec = podConfig.getSpec();
for (Container containerConfig : podSpec.getContainers()) {
String machineName = Names.machineName(podConfig, containerConfig);
InternalMachineConfig machineConfig = k8sEnv.getMachines().get(machineName);
if (!machineConfig.getServers().isEmpty()) {
KubernetesServerExposer kubernetesServerExposer = new KubernetesServerExposer<>(externalServerExposer, secureServerExposer, machineName, podConfig, containerConfig, k8sEnv);
kubernetesServerExposer.expose(machineConfig.getServers());
}
}
}
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.
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()));
}
}
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.
the class UniqueNamesProvisionerTest method rewritePodConfigMapEnv.
@Test
public void rewritePodConfigMapEnv() throws Exception {
when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID);
ConfigMap configMap = newConfigMap();
doReturn(ImmutableMap.of(CONFIGMAP_NAME, configMap)).when(k8sEnv).getConfigMaps();
EnvVar envVar = new EnvVarBuilder().withNewValueFrom().withNewConfigMapKeyRef().withName(CONFIGMAP_NAME).withKey(CONFIGMAP_KEY).endConfigMapKeyRef().endValueFrom().build();
Container container = new ContainerBuilder().withEnv(envVar).build();
Pod pod = newPod();
pod.getSpec().setContainers(ImmutableList.of(container));
PodData podData = new PodData(pod.getSpec(), pod.getMetadata());
doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData();
uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity);
String newConfigMapName = configMap.getMetadata().getName();
EnvVar newEnvVar = container.getEnv().iterator().next();
assertEquals(newEnvVar.getValueFrom().getConfigMapKeyRef().getName(), newConfigMapName);
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.
the class UniqueNamesProvisionerTest method doesNotRewritePodConfigMapEnvWhenNoConfigMap.
@Test
public void doesNotRewritePodConfigMapEnvWhenNoConfigMap() throws Exception {
when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID);
EnvVar envVar = new EnvVarBuilder().withNewValueFrom().withNewConfigMapKeyRef().withName(CONFIGMAP_NAME).withKey(CONFIGMAP_KEY).endConfigMapKeyRef().endValueFrom().build();
Container container = new ContainerBuilder().withEnv(envVar).build();
Pod pod = newPod();
pod.getSpec().setContainers(ImmutableList.of(container));
PodData podData = new PodData(pod.getSpec(), pod.getMetadata());
doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData();
uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity);
EnvVar newEnvVar = container.getEnv().iterator().next();
assertEquals(newEnvVar.getValueFrom().getConfigMapKeyRef().getName(), CONFIGMAP_NAME);
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData in project che-server by eclipse-che.
the class UniqueNamesProvisionerTest method doesNotRewritePodConfigMapVolumesWhenNoConfigMap.
@Test
public void doesNotRewritePodConfigMapVolumesWhenNoConfigMap() throws Exception {
when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID);
Volume volume = new VolumeBuilder().withNewConfigMap().withName(CONFIGMAP_NAME).endConfigMap().build();
Pod pod = newPod();
pod.getSpec().setVolumes(ImmutableList.of(volume));
PodData podData = new PodData(pod.getSpec(), pod.getMetadata());
doReturn(ImmutableMap.of(POD_NAME, podData)).when(k8sEnv).getPodsData();
uniqueNamesProvisioner.provision(k8sEnv, runtimeIdentity);
Volume newVolume = pod.getSpec().getVolumes().iterator().next();
assertEquals(newVolume.getConfigMap().getName(), CONFIGMAP_NAME);
}
Aggregations