use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment in project devspaces-images by redhat-developer.
the class SshKeysProvisioner method doProvisionSshKeys.
private void doProvisionSshKeys(List<SshPairImpl> sshPairs, KubernetesEnvironment k8sEnv, String wsId) {
Map<String, String> data = sshPairs.stream().filter(sshPair -> !isNullOrEmpty(sshPair.getPrivateKey())).collect(toMap(SshPairImpl::getName, sshPair -> Base64.getEncoder().encodeToString(sshPair.getPrivateKey().getBytes())));
Secret secret = new SecretBuilder().addToData(data).withType(SSH_SECRET_TYPE).withNewMetadata().withName(wsId + SSH_SECRET_NAME_SUFFIX).endMetadata().build();
k8sEnv.getSecrets().put(secret.getMetadata().getName(), secret);
k8sEnv.getPodsData().values().forEach(p -> {
mountSshKeySecret(secret.getMetadata().getName(), p.getSpec(), p.getRole() != PodRole.INJECTABLE);
});
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment in project devspaces-images by redhat-developer.
the class PVCProvisioner method addMachineVolumes.
private void addMachineVolumes(String workspaceId, KubernetesEnvironment k8sEnv, Map<String, PersistentVolumeClaim> volumeName2PVC, PodData pod, Container container, Map<String, Volume> volumes) {
if (volumes.isEmpty()) {
return;
}
for (Entry<String, Volume> volumeEntry : volumes.entrySet()) {
final String volumePath = volumeEntry.getValue().getPath();
final String volumeName = LOGS_VOLUME_NAME.equals(volumeEntry.getKey()) ? volumeEntry.getKey() + '-' + pod.getMetadata().getName() : volumeEntry.getKey();
final PersistentVolumeClaim pvc;
// checks whether PVC for given workspace and volume present in environment
if (volumeName2PVC.containsKey(volumeName)) {
pvc = volumeName2PVC.get(volumeName);
} else // when PVC is not found in environment then create new one
{
final String uniqueName = Names.generateName(pvcNamePrefix);
pvc = newPVC(uniqueName, pvcAccessMode, pvcQuantity, pvcStorageClassName);
putLabel(pvc, CHE_WORKSPACE_ID_LABEL, workspaceId);
putLabel(pvc, CHE_VOLUME_NAME_LABEL, volumeName);
k8sEnv.getPersistentVolumeClaims().put(uniqueName, pvc);
volumeName2PVC.put(volumeName, pvc);
}
// binds pvc to pod and container
String pvcName = pvc.getMetadata().getName();
PodSpec podSpec = pod.getSpec();
Optional<io.fabric8.kubernetes.api.model.Volume> volumeOpt = podSpec.getVolumes().stream().filter(volume -> volume.getPersistentVolumeClaim() != null && pvcName.equals(volume.getPersistentVolumeClaim().getClaimName())).findAny();
io.fabric8.kubernetes.api.model.Volume podVolume;
if (volumeOpt.isPresent()) {
podVolume = volumeOpt.get();
} else {
podVolume = newVolume(pvcName, pvcName);
podSpec.getVolumes().add(podVolume);
}
container.getVolumeMounts().add(newVolumeMount(podVolume.getName(), volumePath, ""));
}
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment in project devspaces-images by redhat-developer.
the class EnvVarsConverter method provision.
@Override
@Traced
public void provision(KubernetesEnvironment k8sEnv, RuntimeIdentity identity) throws InfrastructureException {
TracingTags.WORKSPACE_ID.set(identity::getWorkspaceId);
for (PodData pod : k8sEnv.getPodsData().values()) {
for (Container container : pod.getSpec().getContainers()) {
String machineName = Names.machineName(pod, container);
InternalMachineConfig machineConf = k8sEnv.getMachines().get(machineName);
// we need to combine the env vars from the machine config with the variables already
// present in the container. Let's key the variables by name and use the map for merging
Map<String, EnvVar> envVars = machineConf.getEnv().entrySet().stream().map(e -> new EnvVar(e.getKey(), e.getValue(), null)).collect(toMap(EnvVar::getName, identity()));
// the env vars defined in our machine config take precedence over the ones already defined
// in the container, if any
container.getEnv().forEach(v -> envVars.putIfAbsent(v.getName(), v));
// The environment variable expansion only works if a variable that is referenced
// is already defined earlier in the list of environment variables.
// We need to produce a list where variables that reference others always appear later
// in the list.
List<EnvVar> sorted = topoSort.sort(envVars.values());
container.setEnv(sorted);
}
}
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment in project devspaces-images by redhat-developer.
the class SidecarServicesProvisionerTest method shouldAddServiceForEachEndpoint.
@Test
public void shouldAddServiceForEachEndpoint() throws Exception {
List<ChePluginEndpoint> actualEndpoints = asList(new ChePluginEndpoint().name("testE1").targetPort(8080), new ChePluginEndpoint().name("testE2").targetPort(10000));
endpoints.addAll(actualEndpoints);
KubernetesEnvironment kubernetesEnvironment = KubernetesEnvironment.builder().build();
provisioner.provision(kubernetesEnvironment);
assertEquals(kubernetesEnvironment.getServices(), toK8sServices(actualEndpoints));
}
use of org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment in project devspaces-images by redhat-developer.
the class SidecarServicesProvisionerTest method shouldNotAddServiceForNotdDiscoverableEndpoint.
@Test
public void shouldNotAddServiceForNotdDiscoverableEndpoint() throws Exception {
List<ChePluginEndpoint> actualEndpoints = asList(new ChePluginEndpoint().name("testE1").targetPort(8080), new ChePluginEndpoint().name("testE2").targetPort(10000).attributes(ImmutableMap.of("discoverable", "false")));
endpoints.addAll(actualEndpoints);
KubernetesEnvironment kubernetesEnvironment = KubernetesEnvironment.builder().build();
provisioner.provision(kubernetesEnvironment);
assertEquals(kubernetesEnvironment.getServices(), toK8sServices(ImmutableList.of(new ChePluginEndpoint().name("testE1").targetPort(8080))));
}
Aggregations