io.fabric8.kubernetes.api.model.VolumeBuilder Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.VolumeBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UniqueNamesProvisionerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@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);
}
 
Example #2
Source File: PodsVolumesTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldNotChangeNonMatchingVolumesChangePVCReference() {
  // given
  podData
      .getSpec()
      .getVolumes()
      .add(
          new VolumeBuilder()
              .withName("userData")
              .withPersistentVolumeClaim(
                  new PersistentVolumeClaimVolumeSourceBuilder()
                      .withClaimName("nonMatching")
                      .build())
              .build());

  // when
  podsVolumes.changePVCReferences(ImmutableList.of(podData), "userData", "newPVCName");

  // then
  assertEquals(podData.getSpec().getVolumes().size(), 1);
  Volume volume = podData.getSpec().getVolumes().get(0);
  assertEquals(volume.getPersistentVolumeClaim().getClaimName(), "nonMatching");
}
 
Example #3
Source File: PodsVolumesTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldChangePVCReference() {
  // given
  podData
      .getSpec()
      .getVolumes()
      .add(
          new VolumeBuilder()
              .withName("userData")
              .withPersistentVolumeClaim(
                  new PersistentVolumeClaimVolumeSourceBuilder()
                      .withClaimName("userData")
                      .build())
              .build());

  // when
  podsVolumes.changePVCReferences(ImmutableList.of(podData), "userData", "newPVCName");

  // then
  assertEquals(podData.getSpec().getVolumes().size(), 1);
  Volume volume = podData.getSpec().getVolumes().get(0);
  assertEquals(volume.getPersistentVolumeClaim().getClaimName(), "newPVCName");
}
 
Example #4
Source File: GitConfigProvisioner.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
private void mountConfigFile(PodSpec podSpec, String gitConfigMapName, boolean addVolume) {
  if (addVolume) {
    podSpec
        .getVolumes()
        .add(
            new VolumeBuilder()
                .withName(CONFIG_MAP_VOLUME_NAME)
                .withConfigMap(
                    new ConfigMapVolumeSourceBuilder().withName(gitConfigMapName).build())
                .build());
  }

  List<Container> containers = podSpec.getContainers();
  containers.forEach(
      container -> {
        VolumeMount volumeMount =
            new VolumeMountBuilder()
                .withName(CONFIG_MAP_VOLUME_NAME)
                .withMountPath(GIT_CONFIG_PATH)
                .withSubPath(GIT_CONFIG)
                .withReadOnly(false)
                .withNewReadOnly(false)
                .build();
        container.getVolumeMounts().add(volumeMount);
      });
}
 
Example #5
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test(
    expectedExceptions = ValidationException.class,
    expectedExceptionsMessageRegExp =
        "Pods have to have volumes with unique names but there are multiple `volume` volumes")
public void shouldThrownAnExceptionIfVolumeNameCollisionHappened() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder().withVolumes(new VolumeBuilder().withName("volume").build()).build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder().withVolumes(new VolumeBuilder().withName("volume").build()).build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  podMerger.merge(Arrays.asList(podData1, podData2));
}
 
Example #6
Source File: SecretVolume.java    From kubernetes-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public Volume buildVolume(String volumeName) {
    SecretVolumeSource secretVolumeSource = new SecretVolumeSource();
    secretVolumeSource.setSecretName(getSecretName());
    secretVolumeSource.setOptional(getOptional());

    if (StringUtils.isNotBlank(defaultMode)) {
        secretVolumeSource.setDefaultMode(Integer.parseInt(getDefaultMode()));
    }

    return new VolumeBuilder()
            .withName(volumeName)
            .withNewSecretLike(secretVolumeSource)
            .endSecret()
            .build();
}
 
Example #7
Source File: KubernetesEnvironmentPodsValidatorTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test(
    expectedExceptions = ValidationException.class,
    expectedExceptionsMessageRegExp =
        "Pod 'pod1' contains volume 'user-data' with PVC sources that references missing PVC 'non-existing'")
public void shouldThrowExceptionWhenPodHasVolumeThatReferencesMissingPVC() throws Exception {
  // given
  String podName = "pod1";
  Pod pod = createPod("pod1", "main");
  pod.getSpec()
      .getVolumes()
      .add(
          new VolumeBuilder()
              .withName("user-data")
              .withNewPersistentVolumeClaim()
              .withClaimName("non-existing")
              .endPersistentVolumeClaim()
              .build());
  PodData podData = new PodData(pod.getSpec(), pod.getMetadata());
  when(kubernetesEnvironment.getPodsData()).thenReturn(ImmutableMap.of(podName, podData));
  when(kubernetesEnvironment.getMachines())
      .thenReturn(ImmutableMap.of(podName + "/main", mock(InternalMachineConfig.class)));

  // when
  podsValidator.validate(kubernetesEnvironment);
}
 
Example #8
Source File: FlinkConfMountDecoratorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecoratedFlinkPodWithoutLog4jAndLogback() {
	final FlinkPod resultFlinkPod = flinkConfMountDecorator.decorateFlinkPod(baseFlinkPod);

	final List<KeyToPath> expectedKeyToPaths = Collections.singletonList(
		new KeyToPathBuilder()
			.withKey(FLINK_CONF_FILENAME)
			.withPath(FLINK_CONF_FILENAME)
			.build());
	final List<Volume> expectedVolumes = Collections.singletonList(
		new VolumeBuilder()
			.withName(Constants.FLINK_CONF_VOLUME)
			.withNewConfigMap()
				.withName(getFlinkConfConfigMapName(CLUSTER_ID))
				.withItems(expectedKeyToPaths)
				.endConfigMap()
			.build());
	assertEquals(expectedVolumes, resultFlinkPod.getPod().getSpec().getVolumes());

	final List<VolumeMount> expectedVolumeMounts = Collections.singletonList(
		new VolumeMountBuilder()
			.withName(Constants.FLINK_CONF_VOLUME)
			.withMountPath(FLINK_CONF_DIR_IN_POD)
		.build());
	assertEquals(expectedVolumeMounts, resultFlinkPod.getMainContainer().getVolumeMounts());
}
 
Example #9
Source File: VolumeUtils.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * Creates an empty directory volume
 *
 * @param name      Name of the Volume
 * @param sizeLimit Volume size
 * @return The Volume created
 */
public static Volume createEmptyDirVolume(String name, String sizeLimit) {
    String validName = getValidVolumeName(name);

    EmptyDirVolumeSource emptyDirVolumeSource = new EmptyDirVolumeSourceBuilder().build();
    if (sizeLimit != null && !sizeLimit.isEmpty()) {
        emptyDirVolumeSource.setSizeLimit(new Quantity(sizeLimit));
    }

    Volume volume = new VolumeBuilder()
            .withName(validName)
            .withEmptyDir(emptyDirVolumeSource)
            .build();
    log.trace("Created emptyDir Volume named '{}' with sizeLimit '{}'", validName, sizeLimit);
    return volume;
}
 
Example #10
Source File: VolumeUtils.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a secret volume
 *
 * @param name        Name of the Volume
 * @param secretName  Name of the Secret
 * @param isOpenshift true if underlying cluster OpenShift
 * @return The Volume created
 */
public static Volume createSecretVolume(String name, String secretName, boolean isOpenshift) {
    String validName = getValidVolumeName(name);

    int mode = 0444;
    if (isOpenshift) {
        mode = 0440;
    }

    SecretVolumeSource secretVolumeSource = new SecretVolumeSourceBuilder()
            .withDefaultMode(mode)
            .withSecretName(secretName)
            .build();

    Volume volume = new VolumeBuilder()
            .withName(validName)
            .withSecret(secretVolumeSource)
            .build();
    log.trace("Created secret Volume named '{}' with source secret '{}'", validName, secretName);
    return volume;
}
 
Example #11
Source File: FlinkConfMountDecoratorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecoratedFlinkPodWithLog4j() throws IOException {
	KubernetesTestUtils.createTemporyFile("some data", flinkConfDir, "log4j.properties");

	final FlinkPod resultFlinkPod = flinkConfMountDecorator.decorateFlinkPod(baseFlinkPod);

	final List<KeyToPath> expectedKeyToPaths = Arrays.asList(
		new KeyToPathBuilder()
			.withKey("log4j.properties")
			.withPath("log4j.properties")
			.build(),
		new KeyToPathBuilder()
			.withKey(FLINK_CONF_FILENAME)
			.withPath(FLINK_CONF_FILENAME)
			.build());
	final List<Volume> expectedVolumes = Collections.singletonList(
		new VolumeBuilder()
			.withName(Constants.FLINK_CONF_VOLUME)
			.withNewConfigMap()
			.withName(getFlinkConfConfigMapName(CLUSTER_ID))
			.withItems(expectedKeyToPaths)
			.endConfigMap()
			.build());
	assertEquals(expectedVolumes, resultFlinkPod.getPod().getSpec().getVolumes());
}
 
Example #12
Source File: VolumeUtils.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a Kubernetes volume which will map to ConfigMap
 *
 * @param name              Name of the Volume
 * @param configMapName     Name of the ConfigMap
 * @return                  The newly created Volume
 */
public static Volume createConfigMapVolume(String name, String configMapName) {
    String validName = getValidVolumeName(name);

    ConfigMapVolumeSource configMapVolumeSource = new ConfigMapVolumeSourceBuilder()
            .withName(configMapName)
            .build();

    Volume volume = new VolumeBuilder()
            .withName(validName)
            .withConfigMap(configMapVolumeSource)
            .build();

    log.trace("Created configMap Volume named '{}' with source configMap '{}'", validName, configMapName);

    return volume;
}
 
Example #13
Source File: FlinkConfMountDecoratorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testDecoratedFlinkPodWithLogback() throws IOException {
	KubernetesTestUtils.createTemporyFile("some data", flinkConfDir, "logback.xml");

	final FlinkPod resultFlinkPod = flinkConfMountDecorator.decorateFlinkPod(baseFlinkPod);

	final List<KeyToPath> expectedKeyToPaths = Arrays.asList(
		new KeyToPathBuilder()
			.withKey("logback.xml")
			.withPath("logback.xml")
			.build(),
		new KeyToPathBuilder()
			.withKey(FLINK_CONF_FILENAME)
			.withPath(FLINK_CONF_FILENAME)
			.build());
	final List<Volume> expectedVolumes = Collections.singletonList(
		new VolumeBuilder()
			.withName(Constants.FLINK_CONF_VOLUME)
			.withNewConfigMap()
			.withName(getFlinkConfConfigMapName(CLUSTER_ID))
			.withItems(expectedKeyToPaths)
			.endConfigMap()
			.build());
	assertEquals(expectedVolumes, resultFlinkPod.getPod().getSpec().getVolumes());
}
 
Example #14
Source File: UniqueNamesProvisionerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void rewritePodConfigMapVolumes() throws Exception {
  when(runtimeIdentity.getWorkspaceId()).thenReturn(WORKSPACE_ID);

  ConfigMap configMap = newConfigMap();
  doReturn(ImmutableMap.of(CONFIGMAP_NAME, configMap)).when(k8sEnv).getConfigMaps();

  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);

  String newConfigMapName = configMap.getMetadata().getName();
  Volume newVolume = pod.getSpec().getVolumes().iterator().next();
  assertEquals(newVolume.getConfigMap().getName(), newConfigMapName);
}
 
Example #15
Source File: DynamicPVCWorkspaceVolume.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Volume buildVolume(String volumeName, String podName) {
    return new VolumeBuilder()
            .withName(volumeName)
            .withNewPersistentVolumeClaim()
            .withClaimName("pvc-" + podName)
            .withReadOnly(false)
            .and()
            .build();
}
 
Example #16
Source File: PodMergerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldMergeSpecsOfPodsData() throws Exception {
  // given
  PodSpec podSpec1 =
      new PodSpecBuilder()
          .withContainers(new ContainerBuilder().withName("c1").build())
          .withInitContainers(new ContainerBuilder().withName("initC1").build())
          .withVolumes(new VolumeBuilder().withName("v1").build())
          .withImagePullSecrets(new LocalObjectReferenceBuilder().withName("secret1").build())
          .build();
  podSpec1.setAdditionalProperty("add1", 1L);
  PodData podData1 = new PodData(podSpec1, new ObjectMetaBuilder().build());

  PodSpec podSpec2 =
      new PodSpecBuilder()
          .withContainers(new ContainerBuilder().withName("c2").build())
          .withInitContainers(new ContainerBuilder().withName("initC2").build())
          .withVolumes(new VolumeBuilder().withName("v2").build())
          .withImagePullSecrets(new LocalObjectReferenceBuilder().withName("secret2").build())
          .build();
  podSpec2.setAdditionalProperty("add2", 2L);
  PodData podData2 = new PodData(podSpec2, new ObjectMetaBuilder().build());

  // when
  Deployment merged = podMerger.merge(Arrays.asList(podData1, podData2));

  // then
  PodTemplateSpec podTemplate = merged.getSpec().getTemplate();
  verifyContainsAllFrom(podTemplate.getSpec(), podData1.getSpec());
  verifyContainsAllFrom(podTemplate.getSpec(), podData2.getSpec());
}
 
Example #17
Source File: EphemeralWorkspaceAdapterTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testConvertsAllPVCsToEmptyDir() throws Exception {
  // given
  k8sEnv.getPersistentVolumeClaims().put("pvc1", mock(PersistentVolumeClaim.class));
  k8sEnv.getPersistentVolumeClaims().put("pvc2", mock(PersistentVolumeClaim.class));

  io.fabric8.kubernetes.api.model.Volume configMapVolume =
      new VolumeBuilder().withNewConfigMap().withName("configMap").endConfigMap().build();
  io.fabric8.kubernetes.api.model.Volume emptyDirVolume =
      new VolumeBuilder().withNewEmptyDir().endEmptyDir().build();
  io.fabric8.kubernetes.api.model.Volume pvcVolume =
      new VolumeBuilder()
          .withNewPersistentVolumeClaim()
          .withClaimName("pvc1")
          .endPersistentVolumeClaim()
          .build();
  Pod pod =
      new PodBuilder()
          .withNewMetadata()
          .withName(POD_NAME)
          .endMetadata()
          .withNewSpec()
          .withVolumes(
              new VolumeBuilder(pvcVolume).build(),
              new VolumeBuilder(configMapVolume).build(),
              new VolumeBuilder(emptyDirVolume).build())
          .endSpec()
          .build();

  k8sEnv.addPod(pod);

  ephemeralWorkspaceAdapter.provision(k8sEnv, identity);

  assertTrue(k8sEnv.getPersistentVolumeClaims().isEmpty());
  assertNull(pod.getSpec().getVolumes().get(0).getPersistentVolumeClaim());
  assertEquals(pod.getSpec().getVolumes().get(0).getEmptyDir(), new EmptyDirVolumeSource());
  assertEquals(pod.getSpec().getVolumes().get(1), configMapVolume);
  assertEquals(pod.getSpec().getVolumes().get(2), emptyDirVolume);
}
 
Example #18
Source File: TestObjects.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
public TestPodBuilder withPVCVolume(String volumeName, String pvcName) {
  this.volumes.add(
      new VolumeBuilder()
          .withName(volumeName)
          .withNewPersistentVolumeClaim()
          .withClaimName(pvcName)
          .endPersistentVolumeClaim()
          .build());
  return this;
}
 
Example #19
Source File: ConfigMapVolume.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Volume buildVolume(String volumeName) {
    return new VolumeBuilder()
            .withName(volumeName)
            .withNewConfigMap()
                .withName(getConfigMapName())
                .withOptional(getOptional())
            .and()
            .build();
}
 
Example #20
Source File: PersistentVolumeClaimWorkspaceVolume.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Volume buildVolume(String volumeName, String podName) {
    return new VolumeBuilder()
            .withName(volumeName)
            .withNewPersistentVolumeClaim()
                .withClaimName(getClaimName())
                .withReadOnly(getReadOnly())
            .and()
            .build();
}
 
Example #21
Source File: KubernetesArtifactsBrokerApplierTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@BeforeMethod
public void setUp() throws Exception {
  // Workspace env setup
  ObjectMeta workspacePodMeta =
      new ObjectMetaBuilder().withAnnotations(workspacePodAnnotations).build();
  workspacePod = new PodBuilder().withMetadata(workspacePodMeta).withSpec(new PodSpec()).build();
  Map<String, ConfigMap> workspaceConfigMaps = new HashMap<>();
  workspaceEnvironment =
      KubernetesEnvironment.builder()
          .setPods(ImmutableMap.of(WORKSPACE_POD_NAME, workspacePod))
          .setMachines(new HashMap<>())
          .setConfigMaps(workspaceConfigMaps)
          .build();

  // Broker env setup
  ObjectMeta brokerPodMeta =
      new ObjectMetaBuilder().withAnnotations(brokerPodAnnotations).build();
  brokerContainer = new ContainerBuilder().withName(BROKER_CONTAINER_NAME).build();
  brokerVolume = new VolumeBuilder().build();
  Pod brokerPod =
      new PodBuilder()
          .withMetadata(brokerPodMeta)
          .withNewSpec()
          .withContainers(brokerContainer)
          .withVolumes(brokerVolume)
          .endSpec()
          .build();
  brokerConfigMap = new ConfigMapBuilder().addToData(brokerConfigMapData).build();
  KubernetesEnvironment brokerEnvironment =
      KubernetesEnvironment.builder()
          .setPods(ImmutableMap.of(BROKER_POD_NAME, brokerPod))
          .setConfigMaps(ImmutableMap.of(BROKER_CONFIGMAP_NAME, brokerConfigMap))
          .setMachines(ImmutableMap.of(BROKER_MACHINE_NAME, brokerMachine))
          .build();
  doReturn(brokerEnvironment)
      .when(brokerEnvironmentFactory)
      .createForArtifactsBroker(any(), any());

  applier = new KubernetesArtifactsBrokerApplier<>(brokerEnvironmentFactory);
}
 
Example #22
Source File: PersistentVolumeClaim.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Volume buildVolume(String volumeName) {
    return new VolumeBuilder()
            .withName(volumeName)
            .withNewPersistentVolumeClaim()
                .withClaimName(getClaimName())
                .withReadOnly(getReadOnly())
            .and()
            .build();
}
 
Example #23
Source File: PodPresetExamples.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
public static void main(String args[]) {
  String master = "https://192.168.42.193:8443/";
  if (args.length == 1) {
    master = args[0];
  }

  Config config = new ConfigBuilder().withMasterUrl(master).build();
  try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
    String namespace = "default";
    log("namespace", namespace);
    Pod pod = client.pods().inNamespace(namespace).load(PodPresetExamples.class.getResourceAsStream("/pod-preset-example.yml")).get();
    log("Pod created");
    client.pods().inNamespace(namespace).create(pod);

    PodPreset podPreset = new PodPresetBuilder()
      .withNewMetadata().withName("allow-database").endMetadata()
      .withNewSpec()
      .withNewSelector().withMatchLabels(Collections.singletonMap("role", "frontend")).endSelector()
      .withEnv(new EnvVarBuilder().withName("DB_PORT").withValue("6379").build())
      .withVolumeMounts(new VolumeMountBuilder().withMountPath("/cache").withName("cache-volume").build())
      .withVolumes(new VolumeBuilder().withName("cache-volume").withEmptyDir(new EmptyDirVolumeSourceBuilder().build()).build())
      .endSpec()
      .build();

    log("Creating Pod Preset : " + podPreset.getMetadata().getName());
    client.settings().podPresets().inNamespace(namespace).create(podPreset);

    pod = client.pods().inNamespace(namespace).withName(pod.getMetadata().getName()).get();
    log("Updated pod: ");
    log(SerializationUtils.dumpAsYaml(pod));
  } catch (Exception e) {
    log("Exception occurred: ", e.getMessage());
    e.printStackTrace();
  }
}
 
Example #24
Source File: FlinkConfMountDecorator.java    From flink with Apache License 2.0 5 votes vote down vote up
private Pod decoratePod(Pod pod) {
	final List<KeyToPath> keyToPaths = getLocalLogConfFiles().stream()
		.map(file -> new KeyToPathBuilder()
			.withKey(file.getName())
			.withPath(file.getName())
			.build())
		.collect(Collectors.toList());
	keyToPaths.add(new KeyToPathBuilder()
		.withKey(FLINK_CONF_FILENAME)
		.withPath(FLINK_CONF_FILENAME)
		.build());

	final Volume flinkConfVolume = new VolumeBuilder()
		.withName(FLINK_CONF_VOLUME)
		.withNewConfigMap()
			.withName(getFlinkConfConfigMapName(kubernetesComponentConf.getClusterId()))
			.withItems(keyToPaths)
			.endConfigMap()
		.build();

	return new PodBuilder(pod)
		.editSpec()
			.addNewVolumeLike(flinkConfVolume)
			.endVolume()
			.endSpec()
		.build();
}
 
Example #25
Source File: FlinkConfMountDecoratorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecoratedFlinkPodWithLog4jAndLogback() throws IOException {
	KubernetesTestUtils.createTemporyFile("some data", flinkConfDir, "log4j.properties");
	KubernetesTestUtils.createTemporyFile("some data", flinkConfDir, "logback.xml");

	final FlinkPod resultFlinkPod = flinkConfMountDecorator.decorateFlinkPod(baseFlinkPod);

	final List<KeyToPath> expectedKeyToPaths = Arrays.asList(
		new KeyToPathBuilder()
			.withKey("logback.xml")
			.withPath("logback.xml")
			.build(),
		new KeyToPathBuilder()
			.withKey("log4j.properties")
			.withPath("log4j.properties")
			.build(),
		new KeyToPathBuilder()
			.withKey(FLINK_CONF_FILENAME)
			.withPath(FLINK_CONF_FILENAME)
			.build());
	final List<Volume> expectedVolumes = Collections.singletonList(
		new VolumeBuilder()
			.withName(Constants.FLINK_CONF_VOLUME)
			.withNewConfigMap()
			.withName(getFlinkConfConfigMapName(CLUSTER_ID))
			.withItems(expectedKeyToPaths)
			.endConfigMap()
			.build());
	assertEquals(expectedVolumes, resultFlinkPod.getPod().getSpec().getVolumes());
}
 
Example #26
Source File: PodsVolumes.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Replaces all pods PVC sourced volumes with the specified one.
 *
 * @param pods pods to change
 * @param commonPVCName PVC name that should be referenced in all existing PVC sources volumes
 */
public void replacePVCVolumesWithCommon(Map<String, PodData> pods, String commonPVCName) {
  for (PodData pod : pods.values()) {
    Set<String> pvcSourcedVolumes = reducePVCSourcedVolumes(pod.getSpec().getVolumes());

    if (pvcSourcedVolumes.isEmpty()) {
      continue;
    }

    // add common PVC sourced volume instead of removed
    pod.getSpec()
        .getVolumes()
        .add(
            new VolumeBuilder()
                .withName(commonPVCName)
                .withNewPersistentVolumeClaim()
                .withClaimName(commonPVCName)
                .endPersistentVolumeClaim()
                .build());

    Stream.concat(
            pod.getSpec().getContainers().stream(), pod.getSpec().getInitContainers().stream())
        .flatMap(c -> c.getVolumeMounts().stream())
        .filter(vm -> pvcSourcedVolumes.contains(vm.getName()))
        .forEach(vm -> vm.setName(commonPVCName));
  }
}
 
Example #27
Source File: ChePluginsVolumeApplier.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private void addEmptyDirVolumeIfAbsent(PodSpec podSpec, String uniqueVolumeName) {
  if (podSpec
      .getVolumes()
      .stream()
      .noneMatch(volume -> volume.getName().equals(uniqueVolumeName))) {
    podSpec
        .getVolumes()
        .add(
            new VolumeBuilder()
                .withName(uniqueVolumeName)
                .withNewEmptyDir()
                .endEmptyDir()
                .build());
  }
}
 
Example #28
Source File: BrokerEnvironmentFactory.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private Volume newConfigMapVolume(String configMapName, String configMapVolume) {
  return new VolumeBuilder()
      .withName(configMapVolume)
      .withNewConfigMap()
      .withName(configMapName)
      .endConfigMap()
      .build();
}
 
Example #29
Source File: AbstractJwtProxyProvisioner.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private Pod createJwtProxyPod() {
  String containerName = Names.generateName("che-jwtproxy");
  return new PodBuilder()
      .withNewMetadata()
      .withName(JWT_PROXY_POD_NAME)
      .withAnnotations(Names.createMachineNameAnnotations(containerName, JWT_PROXY_MACHINE_NAME))
      .endMetadata()
      .withNewSpec()
      .withContainers(
          new ContainerBuilder()
              .withImagePullPolicy(imagePullPolicy)
              .withName(containerName)
              .withImage(jwtProxyImage)
              .withVolumeMounts(
                  new VolumeMount(
                      JWT_PROXY_CONFIG_FOLDER + "/",
                      null,
                      "che-jwtproxy-config-volume",
                      false,
                      null,
                      null))
              .withArgs("-config", JWT_PROXY_CONFIG_FOLDER + "/" + JWT_PROXY_CONFIG_FILE)
              .addNewEnv()
              .withName("XDG_CONFIG_HOME")
              .withValue(JWT_PROXY_CONFIG_FOLDER)
              .endEnv()
              .build())
      .withVolumes(
          new VolumeBuilder()
              .withName("che-jwtproxy-config-volume")
              .withNewConfigMap()
              .withName(getConfigMapName())
              .endConfigMap()
              .build())
      .endSpec()
      .build();
}
 
Example #30
Source File: VolumeUtils.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a secret volume with given items
 *
 * @param name        Name of the Volume
 * @param secretName  Name of the Secret
 * @param items       contents of the Secret
 * @param isOpenshift true if underlying cluster OpenShift
 * @return The Volume created
 */
public static Volume createSecretVolume(String name, String secretName, Map<String, String> items, boolean isOpenshift) {
    String validName = getValidVolumeName(name);

    int mode = 0444;
    if (isOpenshift) {
        mode = 0440;
    }

    List<KeyToPath> keysPaths = new ArrayList<>();

    for (Map.Entry<String, String> item : items.entrySet()) {
        KeyToPath keyPath = new KeyToPathBuilder()
                .withNewKey(item.getKey())
                .withNewPath(item.getValue())
                .build();

        keysPaths.add(keyPath);
    }

    SecretVolumeSource secretVolumeSource = new SecretVolumeSourceBuilder()
            .withDefaultMode(mode)
            .withSecretName(secretName)
            .withItems(keysPaths)
            .build();

    Volume volume = new VolumeBuilder()
            .withName(validName)
            .withSecret(secretVolumeSource)
            .build();
    log.trace("Created secret Volume named '{}' with source secret '{}'", validName, secretName);
    return volume;
}