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

The following examples show how to use io.fabric8.kubernetes.api.model.PersistentVolumeClaim. 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: CommonPVCStrategyTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testCreatesPVCsWithSubpathsOnPrepare() throws Exception {
  final PersistentVolumeClaim pvc = newPVC(PVC_NAME);
  pvc.getAdditionalProperties()
      .put(format(SUBPATHS_PROPERTY_FMT, WORKSPACE_ID), WORKSPACE_SUBPATHS);
  k8sEnv.getPersistentVolumeClaims().put(PVC_NAME, pvc);
  doNothing()
      .when(pvcSubPathHelper)
      .createDirs(IDENTITY, WORKSPACE_ID, PVC_NAME, emptyMap(), WORKSPACE_SUBPATHS);

  commonPVCStrategy.prepare(k8sEnv, IDENTITY, 100, emptyMap());

  verify(pvcs).get();
  verify(pvcs).create(pvc);
  verify(pvcs).waitBound(PVC_NAME, 100);
  verify(pvcSubPathHelper)
      .createDirs(IDENTITY, WORKSPACE_ID, PVC_NAME, emptyMap(), WORKSPACE_SUBPATHS);
}
 
Example #2
Source File: UniqueWorkspacePVCStrategyTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldProvisionWorkspaceIdLabelToPVCs() throws Exception {
  // given
  PersistentVolumeClaim existingPVC = newPVC("existingPVC");
  when(pvcs.getByLabel(CHE_WORKSPACE_ID_LABEL, WORKSPACE_ID))
      .thenReturn(singletonList(existingPVC));

  // when
  strategy.provision(k8sEnv, IDENTITY);

  // then
  assertEquals(k8sEnv.getPersistentVolumeClaims().size(), 1);
  PersistentVolumeClaim pvc = k8sEnv.getPersistentVolumeClaims().get("existingPVC");
  assertNotNull(pvc);
  assertEquals(pvc.getMetadata().getLabels().get(CHE_WORKSPACE_ID_LABEL), WORKSPACE_ID);
}
 
Example #3
Source File: KafkaAssemblyOperator.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * Deletion of PVCs after the cluster is deleted is handled by owner reference and garbage collection. However,
 * this would not help after scale-downs. Therefore we check if there are any PVCs which should not be present
 * and delete them when they are.
 *
 * This should be called only after the Statefulset reconciliation, rolling update and scale-down when the PVCs
 * are not used anymore by the pods.
 *
 * @return
 */
Future<ReconciliationState> zkPersistentClaimDeletion() {
    Promise<ReconciliationState> resultPromise = Promise.promise();
    Future<List<PersistentVolumeClaim>> futurePvcs = pvcOperations.listAsync(namespace, zkCluster.getSelectorLabels());

    futurePvcs.onComplete(res -> {
        if (res.succeeded() && res.result() != null)    {
            List<String> maybeDeletePvcs = res.result().stream().map(pvc -> pvc.getMetadata().getName()).collect(Collectors.toList());
            List<String> desiredPvcs = zkCluster.generatePersistentVolumeClaims().stream().map(pvc -> pvc.getMetadata().getName()).collect(Collectors.toList());

            persistentClaimDeletion(maybeDeletePvcs, desiredPvcs).onComplete(resultPromise);
        } else {
            resultPromise.fail(res.cause());
        }
    });

    return resultPromise.future();
}
 
Example #4
Source File: CommonPVCStrategyTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testProvisionVolumesIntoKubernetesEnvironment() throws Exception {
  // given
  k8sEnv.getPersistentVolumeClaims().put("pvc1", newPVC("pvc1"));
  k8sEnv.getPersistentVolumeClaims().put("pvc2", newPVC("pvc2"));

  // when
  commonPVCStrategy.provision(k8sEnv, IDENTITY);

  // then
  provisionOrder.verify(volumeConverter).convertCheVolumes(k8sEnv, WORKSPACE_ID);
  provisionOrder.verify(subpathPrefixes).prefixVolumeMountsSubpaths(k8sEnv, WORKSPACE_ID);
  provisionOrder.verify(podsVolumes).replacePVCVolumesWithCommon(k8sEnv.getPodsData(), PVC_NAME);
  assertEquals(k8sEnv.getPersistentVolumeClaims().size(), 1);
  PersistentVolumeClaim commonPVC = k8sEnv.getPersistentVolumeClaims().get(PVC_NAME);
  assertNotNull(commonPVC);
  assertEquals(commonPVC.getMetadata().getName(), PVC_NAME);
  assertEquals(commonPVC.getSpec().getAccessModes(), Collections.singletonList(PVC_ACCESS_MODE));
  assertEquals(
      commonPVC.getSpec().getResources().getRequests().get("storage"),
      new Quantity(PVC_QUANTITY));
}
 
Example #5
Source File: KafkaClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@Test
public void testGeneratePersistentVolumeClaimsEphemeral()    {
    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCm, configuration, emptyMap()))
            .editSpec()
                .editKafka()
                    .withNewEphemeralStorage().endEphemeralStorage()
                .endKafka()
            .endSpec()
            .build();
    KafkaCluster kc = KafkaCluster.fromCrd(kafkaAssembly, VERSIONS);

    // Check Storage annotation on STS
    assertThat(kc.generateStatefulSet(true, ImagePullPolicy.NEVER, null).getMetadata().getAnnotations().get(AbstractModel.ANNO_STRIMZI_IO_STORAGE),
            is(ModelUtils.encodeStorageToJson(kafkaAssembly.getSpec().getKafka().getStorage())));

    // Check PVCs
    List<PersistentVolumeClaim> pvcs = kc.generatePersistentVolumeClaims(kc.getStorage());

    assertThat(pvcs.size(), is(0));
}
 
Example #6
Source File: UniqueWorkspacePVCStrategyTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testCreatesProvisionedPVCsOnPrepareIfWaitIsDisabled() throws Exception {
  strategy =
      new UniqueWorkspacePVCStrategy(
          false, // wait bound PVCs
          factory,
          ephemeralWorkspaceAdapter,
          pvcProvisioner,
          subpathPrefixes);

  final String uniqueName = PVC_NAME_PREFIX + "-3121";
  final PersistentVolumeClaim pvc = newPVC(uniqueName);
  k8sEnv.getPersistentVolumeClaims().clear();
  k8sEnv.getPersistentVolumeClaims().putAll(singletonMap(uniqueName, pvc));
  doReturn(pvc).when(pvcs).create(any());

  strategy.prepare(k8sEnv, IDENTITY, 100, emptyMap());

  verify(pvcs).createIfNotExist(any());
  verify(pvcs, never()).waitBound(anyString(), anyLong());
}
 
Example #7
Source File: KafkaCluster.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
/**
 * Generate the persistent volume claims for the storage It's called recursively on the related inner volumes if the
 * storage is of {@link Storage#TYPE_JBOD} type.
 *
 * @param storage the Storage instance from which building volumes, persistent volume claims and
 *                related volume mount paths.
 * @return The PersistentVolumeClaims.
 */
public List<PersistentVolumeClaim> generatePersistentVolumeClaims(Storage storage) {
    List<PersistentVolumeClaim> pvcs = new ArrayList<>();

    if (storage != null) {
        if (storage instanceof PersistentClaimStorage) {
            Integer id = ((PersistentClaimStorage) storage).getId();
            String pvcBaseName = VolumeUtils.getVolumePrefix(id) + "-" + name;

            for (int i = 0; i < replicas; i++) {
                pvcs.add(createPersistentVolumeClaim(i, pvcBaseName + "-" + i, (PersistentClaimStorage) storage));
            }
        } else if (storage instanceof JbodStorage) {
            for (SingleVolumeStorage volume : ((JbodStorage) storage).getVolumes()) {
                if (volume.getId() == null)
                    throw new InvalidResourceException("Volumes under JBOD storage type have to have 'id' property");
                // it's called recursively for setting the information from the current volume
                pvcs.addAll(generatePersistentVolumeClaims(volume));
            }
        }
    }

    return pvcs;
}
 
Example #8
Source File: VolumeUtils.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
public static List<PersistentVolumeClaim> getDataPersistentVolumeClaims(Storage storage) {
    List<PersistentVolumeClaim> pvcs = new ArrayList<>();

    if (storage != null) {
        if (storage instanceof JbodStorage) {
            for (SingleVolumeStorage volume : ((JbodStorage) storage).getVolumes()) {
                if (volume.getId() == null)
                    throw new InvalidResourceException("Volumes under JBOD storage type have to have 'id' property");
                // it's called recursively for setting the information from the current volume
                pvcs.addAll(getDataPersistentVolumeClaims(volume));
            }
        } else if (storage instanceof PersistentClaimStorage) {
            Integer id = ((PersistentClaimStorage) storage).getId();
            String name = getVolumePrefix(id);
            pvcs.add(createPersistentVolumeClaimTemplate(name, (PersistentClaimStorage) storage));
        }
    }

    return pvcs;
}
 
Example #9
Source File: TektonHandler.java    From dekorate with Apache License 2.0 6 votes vote down vote up
public PersistentVolumeClaim createSourceWorkspacePvc(TektonConfig config) {
  Map<String, Quantity> requests = new HashMap<String, Quantity>() {{
      put("storage", new QuantityBuilder().withAmount(String.valueOf(config.getSourceWorkspaceClaim().getSize())).withFormat(config.getSourceWorkspaceClaim().getUnit()).build());
  }};
  LabelSelector selector = null;


  if (config.getSourceWorkspaceClaim().getMatchLabels().length != 0) {
    selector = new LabelSelectorBuilder()
      .withMatchLabels(Arrays.stream(config.getSourceWorkspaceClaim().getMatchLabels()).collect(Collectors.toMap(l -> l.getKey(), l -> l.getValue())))
      .build();
  }
  return new PersistentVolumeClaimBuilder()
    .withNewMetadata()
    .withName(sourceWorkspaceClaimName(config))
    .endMetadata()
    .withNewSpec()
    .withAccessModes(config.getSourceWorkspaceClaim().getAccessMode().name())
    .withStorageClassName(config.getSourceWorkspaceClaim().getStorageClass())
    .withNewResources().withRequests(requests).endResources()
    .withSelector(selector)
    .endSpec()
    .build();
}
 
Example #10
Source File: Sample9Test.java    From module-ballerina-kubernetes with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void compileSample() throws IOException, InterruptedException {
    Assert.assertEquals(KubernetesTestUtils.compileBallerinaFile(SOURCE_DIR_PATH,
            "hello_world_persistence_volume_k8s.bal"), 0);
    File yamlFile = KUBERNETES_TARGET_PATH.resolve("hello_world_persistence_volume_k8s.yaml").toFile();
    Assert.assertTrue(yamlFile.exists());
    List<HasMetadata> k8sItems = KubernetesTestUtils.loadYaml(yamlFile);
    for (HasMetadata data : k8sItems) {
        switch (data.getKind()) {
            case "Deployment":
                deployment = (Deployment) data;
                break;
            case "PersistentVolumeClaim":
                volumeClaim = (PersistentVolumeClaim) data;
                break;
            default:
                break;
        }
    }
}
 
Example #11
Source File: KafkaAssemblyOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
private Map<String, PersistentVolumeClaim> createPvcs(String namespace, Storage storage, int replicas,
                                               BiFunction<Integer, Integer, String> pvcNameFunction) {

    Map<String, PersistentVolumeClaim> pvcs = new HashMap<>();
    if (storage instanceof PersistentClaimStorage) {

        for (int i = 0; i < replicas; i++) {
            Integer storageId = ((PersistentClaimStorage) storage).getId();
            String pvcName = pvcNameFunction.apply(i, storageId);
            PersistentVolumeClaim pvc =
                    new PersistentVolumeClaimBuilder()
                            .withNewMetadata()
                            .withNamespace(namespace)
                            .withName(pvcName)
                            .endMetadata()
                            .build();
            pvcs.put(pvcName, pvc);
        }

    }
    return pvcs;
}
 
Example #12
Source File: PatchService.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private static EntityPatcher<PersistentVolumeClaim> pvcPatcher() {
    return (KubernetesClient client, String namespace, PersistentVolumeClaim newObj, PersistentVolumeClaim oldObj) -> {
        if (UserConfigurationCompare.configEqual(newObj, oldObj)) {
            return oldObj;
        }
        DoneablePersistentVolumeClaim entity =
            client.persistentVolumeClaims()
                  .inNamespace(namespace)
                  .withName(oldObj.getMetadata().getName())
                  .edit();

        if (!UserConfigurationCompare.configEqual(newObj.getMetadata(), oldObj.getMetadata())) {
            entity.withMetadata(newObj.getMetadata());
        }

        if(!UserConfigurationCompare.configEqual(newObj.getSpec(), oldObj.getSpec())) {
                entity.withSpec(newObj.getSpec());
        }
        return entity.done();
    };
}
 
Example #13
Source File: OpenShiftEnvironmentFactoryTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldCreateOpenShiftEnvironmentWithPVCsFromRecipe() throws Exception {
  // given
  PersistentVolumeClaim pvc1 =
      new PersistentVolumeClaimBuilder().withNewMetadata().withName("pvc1").endMetadata().build();
  PersistentVolumeClaim pvc2 =
      new PersistentVolumeClaimBuilder().withNewMetadata().withName("pvc2").endMetadata().build();
  when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(asList(pvc1, pvc2));

  // when
  OpenShiftEnvironment osEnv = osEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList());

  // then
  assertEquals(osEnv.getPersistentVolumeClaims().size(), 2);
  assertEquals(osEnv.getPersistentVolumeClaims().get("pvc1"), pvc1);
  assertEquals(osEnv.getPersistentVolumeClaims().get("pvc2"), pvc2);
}
 
Example #14
Source File: PerWorkspacePVCStrategyTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldPreparePerWorkspacePVCWithSubPaths() throws Exception {
  // given
  final PersistentVolumeClaim pvc = newPVC(PVC_NAME_PREFIX + "-" + WORKSPACE_ID);
  String perWorkspacePVCName = pvc.getMetadata().getName();

  KubernetesEnvironment k8sEnv = KubernetesEnvironment.builder().build();
  k8sEnv.getPersistentVolumeClaims().put(perWorkspacePVCName, pvc);

  String[] subPaths = {"/projects", "/plugins"};
  pvc.getAdditionalProperties().put(format(SUBPATHS_PROPERTY_FMT, WORKSPACE_ID), subPaths);

  // when
  strategy.prepare(k8sEnv, IDENTITY, 100, emptyMap());

  // then
  verify(pvcs).get();
  verify(pvcs).create(pvc);
  verify(pvcs).waitBound(perWorkspacePVCName, 100);
  verify(pvcSubPathHelper)
      .createDirs(IDENTITY, WORKSPACE_ID, perWorkspacePVCName, emptyMap(), subPaths);
}
 
Example #15
Source File: PersistentVolumeClaimTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
void testDeleteMulti() {
  PersistentVolumeClaim persistentVolumeClaim1 = new PersistentVolumeClaimBuilder().withNewMetadata().withName("persistentvolumeclaim1").withNamespace("test").endMetadata().build();
  PersistentVolumeClaim persistentVolumeClaim2 = new PersistentVolumeClaimBuilder().withNewMetadata().withName("persistentvolumeclaim2").withNamespace("ns1").endMetadata().build();
  PersistentVolumeClaim persistentVolumeClaim3 = new PersistentVolumeClaimBuilder().withNewMetadata().withName("persistentvolumeclaim3").withNamespace("any").endMetadata().build();

  server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/persistentvolumeclaim1").andReturn(200, persistentVolumeClaim1).once();
  server.expect().withPath("/api/v1/namespaces/ns1/persistentvolumeclaims/persistentvolumeclaim2").andReturn(200, persistentVolumeClaim2).once();

  KubernetesClient client = server.getClient();
  Boolean deleted = client.persistentVolumeClaims().inAnyNamespace().delete(persistentVolumeClaim1, persistentVolumeClaim2);
  assertTrue(deleted);

  deleted = client.persistentVolumeClaims().inAnyNamespace().delete(persistentVolumeClaim3);
  assertFalse(deleted);
}
 
Example #16
Source File: PersistentVolumeClaimTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
void testBuild() {
  PersistentVolumeClaim persistentVolumeClaim = new PersistentVolumeClaimBuilder()
    .withNewMetadata().withName("test-pv-claim").withNamespace("test").endMetadata()
    .withNewSpec()
    .withStorageClassName("my-local-storage")
    .withAccessModes("ReadWriteOnce")
    .withNewResources()
    .addToRequests("storage", new Quantity("500Gi"))
    .endResources()
    .endSpec()
    .build();

  server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/test-pv-claim").andReturn(200, persistentVolumeClaim).once();

  KubernetesClient client = server.getClient();
  persistentVolumeClaim = client.persistentVolumeClaims().inNamespace("test").withName("test-pv-claim").get();
  assertNotNull(persistentVolumeClaim);
}
 
Example #17
Source File: PVCProvisionerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testProvisioningPVCsToK8sEnvironment() throws Exception {
  // given
  k8sEnv = KubernetesEnvironment.builder().build();
  Map<String, PersistentVolumeClaim> toProvision = new HashMap<>();
  toProvision.put("appStorage", newPVC("appStorage"));

  Pod pod =
      newPod(POD_1_NAME)
          .withContainers(
              newContainer(CONTAINER_1_NAME)
                  .withVolumeMount("appStorage", "/data", "data")
                  .withVolumeMount("appStorage", "/config", "config")
                  .build())
          .withPVCVolume("appStorage", "appStorage")
          .build();

  k8sEnv.addPod(pod);

  // when
  provisioner.provision(k8sEnv, toProvision);

  // then
  assertEquals(k8sEnv.getPersistentVolumeClaims().size(), 1);
  PersistentVolumeClaim pvcForUserData =
      findPvc("appStorage", k8sEnv.getPersistentVolumeClaims());
  assertNotNull(pvcForUserData);
  assertTrue(pvcForUserData.getMetadata().getName().startsWith(PVC_NAME_PREFIX));

  verify(podsVolumes)
      .changePVCReferences(
          k8sEnv.getPodsData().values(), "appStorage", pvcForUserData.getMetadata().getName());
}
 
Example #18
Source File: KafkaClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testGeneratePersistentVolumeClaimsPersistentWithClaimDeletion() {
    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCm, configuration, emptyMap()))
            .editSpec()
            .editKafka()
            .withNewPersistentClaimStorage().withStorageClass("gp2-ssd").withDeleteClaim(true).withSize("100Gi").endPersistentClaimStorage()
            .endKafka()
            .endSpec()
            .build();
    KafkaCluster kc = KafkaCluster.fromCrd(kafkaAssembly, VERSIONS);

    // Check Storage annotation on STS
    assertThat(kc.generateStatefulSet(true, ImagePullPolicy.NEVER, null).getMetadata().getAnnotations().get(AbstractModel.ANNO_STRIMZI_IO_STORAGE),
            is(ModelUtils.encodeStorageToJson(kafkaAssembly.getSpec().getKafka().getStorage())));

    // Check PVCs
    List<PersistentVolumeClaim> pvcs = kc.generatePersistentVolumeClaims(kc.getStorage());

    assertThat(pvcs.size(), is(3));

    for (PersistentVolumeClaim pvc : pvcs) {
        assertThat(pvc.getSpec().getResources().getRequests().get("storage"), is(new Quantity("100Gi")));
        assertThat(pvc.getSpec().getStorageClassName(), is("gp2-ssd"));
        assertThat(pvc.getMetadata().getName().startsWith(kc.VOLUME_NAME), is(true));
        assertThat(pvc.getMetadata().getOwnerReferences().size(), is(1));
        assertThat(pvc.getMetadata().getAnnotations().get(AbstractModel.ANNO_STRIMZI_IO_DELETE_CLAIM), is("true"));
    }
}
 
Example #19
Source File: KafkaClusterTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testGeneratePersistentVolumeClaimsPersistentWithoutClaimDeletion() {
    Kafka kafkaAssembly = new KafkaBuilder(ResourceUtils.createKafkaCluster(namespace, cluster, replicas,
            image, healthDelay, healthTimeout, metricsCm, configuration, emptyMap()))
            .editSpec()
            .editKafka()
            .withNewPersistentClaimStorage().withStorageClass("gp2-ssd").withDeleteClaim(false).withSize("100Gi").endPersistentClaimStorage()
            .endKafka()
            .endSpec()
            .build();
    KafkaCluster kc = KafkaCluster.fromCrd(kafkaAssembly, VERSIONS);

    // Check Storage annotation on STS
    assertThat(kc.generateStatefulSet(true, ImagePullPolicy.NEVER, null).getMetadata().getAnnotations().get(AbstractModel.ANNO_STRIMZI_IO_STORAGE),
            is(ModelUtils.encodeStorageToJson(kafkaAssembly.getSpec().getKafka().getStorage())));

    // Check PVCs
    List<PersistentVolumeClaim> pvcs = kc.generatePersistentVolumeClaims(kc.getStorage());

    assertThat(pvcs.size(), is(3));

    for (PersistentVolumeClaim pvc : pvcs) {
        assertThat(pvc.getSpec().getResources().getRequests().get("storage"), is(new Quantity("100Gi")));
        assertThat(pvc.getSpec().getStorageClassName(), is("gp2-ssd"));
        assertThat(pvc.getMetadata().getName().startsWith(kc.VOLUME_NAME), is(true));
        assertThat(pvc.getMetadata().getOwnerReferences().size(), is(0));
        assertThat(pvc.getMetadata().getAnnotations().get(AbstractModel.ANNO_STRIMZI_IO_DELETE_CLAIM), is("false"));
    }
}
 
Example #20
Source File: ZookeeperCluster.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
List<PersistentVolumeClaim> getVolumeClaims() {
    List<PersistentVolumeClaim> pvcList = new ArrayList<>(1);
    if (storage instanceof PersistentClaimStorage) {
        pvcList.add(VolumeUtils.createPersistentVolumeClaimTemplate(VOLUME_NAME, (PersistentClaimStorage) storage));
    }
    return pvcList;
}
 
Example #21
Source File: PerWorkspacePVCStrategyTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private static PersistentVolumeClaim newPVC(String name) {
  return new PersistentVolumeClaimBuilder()
      .withNewMetadata()
      .withName(name)
      .endMetadata()
      .withNewSpec()
      .endSpec()
      .build();
}
 
Example #22
Source File: UniqueWorkspacePVCStrategy.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Traced
@Override
public void prepare(
    KubernetesEnvironment k8sEnv,
    RuntimeIdentity identity,
    long timeoutMillis,
    Map<String, String> startOptions)
    throws InfrastructureException {
  String workspaceId = identity.getWorkspaceId();

  TracingTags.WORKSPACE_ID.set(workspaceId);

  if (EphemeralWorkspaceUtility.isEphemeral(k8sEnv.getAttributes())) {
    return;
  }

  if (k8sEnv.getPersistentVolumeClaims().isEmpty()) {
    // no PVCs to prepare
    return;
  }

  final KubernetesPersistentVolumeClaims k8sClaims =
      factory.getOrCreate(identity).persistentVolumeClaims();
  LOG.debug("Creating PVCs for workspace '{}'", workspaceId);
  k8sClaims.createIfNotExist(k8sEnv.getPersistentVolumeClaims().values());

  if (waitBound) {
    LOG.debug("Waiting for PVC(s) of workspace '{}' to be bound", workspaceId);
    for (PersistentVolumeClaim pvc : k8sEnv.getPersistentVolumeClaims().values()) {
      k8sClaims.waitBound(pvc.getMetadata().getName(), timeoutMillis);
    }
  }
  LOG.debug("Preparing PVCs done for workspace '{}'", workspaceId);
}
 
Example #23
Source File: PvcOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
/**
 * Patches the resource with the given namespace and name to match the given desired resource
 * and completes the given future accordingly.
 *
 * PvcOperator needs to patch the volumeName field in spec which is immutable and which should contain the same value as the existing resource.
 *
 * @param namespace Namespace of the pvc
 * @param name      Name of the pvc
 * @param current   Current pvc
 * @param desired   Desired pvc
 *
 * @return  Future with reconciliation result
 */
@Override
protected Future<ReconcileResult<PersistentVolumeClaim>> internalPatch(String namespace, String name, PersistentVolumeClaim current, PersistentVolumeClaim desired) {
    try {
        if (current.getSpec() != null && desired.getSpec() != null)   {
            revertImmutableChanges(current, desired);
        }

        return super.internalPatch(namespace, name, current, desired);
    } catch (Exception e) {
        log.error("Caught exception while patching {} {} in namespace {}", resourceKind, name, namespace, e);
        return Future.failedFuture(e);
    }
}
 
Example #24
Source File: PerWorkspacePVCStrategy.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected PersistentVolumeClaim createCommonPVC(String workspaceId) {
  String pvcName = pvcNamePrefix + '-' + workspaceId;

  PersistentVolumeClaim perWorkspacePVC =
      newPVC(pvcName, pvcAccessMode, pvcQuantity, pvcStorageClassName);
  putLabel(perWorkspacePVC.getMetadata(), CHE_WORKSPACE_ID_LABEL, workspaceId);
  return perWorkspacePVC;
}
 
Example #25
Source File: PersistentVolumeClaimTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
void testEditMissing() {
  server.expect().withPath("/api/v1/namespaces/test/persistentvolumeclaims/persistentvolumeclaim").andReturn(404, "error message from kubernetes").always();
  KubernetesClient client = server.getClient();
  Resource<PersistentVolumeClaim, DoneablePersistentVolumeClaim> pvcResource =  client.persistentVolumeClaims().inNamespace("test").withName("persistentvolumeclaim");
  Assertions.assertThrows(KubernetesClientException.class, pvcResource::edit);
}
 
Example #26
Source File: SubPathPrefixesTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private static PersistentVolumeClaim newPVC(String name, Map<String, String> labels) {
  return new PersistentVolumeClaimBuilder()
      .withNewMetadata()
      .withName(name)
      .withLabels(labels)
      .endMetadata()
      .withNewSpec()
      .endSpec()
      .build();
}
 
Example #27
Source File: KubernetesVolumeClaimGeneratorTests.java    From module-ballerina-kubernetes with Apache License 2.0 5 votes vote down vote up
private void assertGeneratedYAML(File yamlFile) throws IOException {
    PersistentVolumeClaim volumeClaim = Utils.loadYaml(yamlFile);
    Assert.assertEquals(volumeClaimName, volumeClaim.getMetadata().getName());
    Assert.assertEquals(1, volumeClaim.getMetadata().getAnnotations().size());
    Assert.assertTrue(volumeClaim.getMetadata().getAnnotations()
            .containsKey(annotationKey));
    Assert.assertEquals(annotationValue, volumeClaim.getMetadata().getAnnotations()
            .get(annotationKey));
}
 
Example #28
Source File: UniqueWorkspacePVCStrategyTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testCreatesProvisionedPVCsOnPrepare() throws Exception {
  final String uniqueName = PVC_NAME_PREFIX + "-3121";
  final PersistentVolumeClaim pvc = newPVC(uniqueName);
  k8sEnv.getPersistentVolumeClaims().clear();
  k8sEnv.getPersistentVolumeClaims().putAll(singletonMap(uniqueName, pvc));
  doReturn(pvc).when(pvcs).create(any());

  strategy.prepare(k8sEnv, IDENTITY, 100, emptyMap());

  verify(pvcs).createIfNotExist(any());
  verify(pvcs).waitBound(uniqueName, 100);
}
 
Example #29
Source File: KafkaAssemblyOperatorMockTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
private void assertPVCs(VertxTestContext context, String statefulSetName, List<PersistentVolumeClaim> originalPVCs) {
    context.verify(() -> {
        StatefulSet statefulSet = client.apps().statefulSets().inNamespace(NAMESPACE).withName(statefulSetName).get();
        assertThat(statefulSet, is(notNullValue()));
        List<PersistentVolumeClaim> pvcs = statefulSet.getSpec().getVolumeClaimTemplates();
        assertThat(originalPVCs.size(), is(pvcs.size()));
        assertThat(originalPVCs, is(pvcs));
    });

}
 
Example #30
Source File: PVCProvisioner.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Provision the specified PVCs to the environment.
 *
 * <p>Note that:<br>
 * - PVC is not provisioned if environment already contains PVC for corresponding volume;<br>
 * - PVC is provisioned with generated unique name;<br>
 * - corresponding PVC references in Kubernetes Environment are updated during provisioning;<br>
 *
 * @param k8sEnv environment to provision
 * @param toProvision PVCs that should be provisioned to the environment
 */
public void provision(
    KubernetesEnvironment k8sEnv, Map<String, PersistentVolumeClaim> toProvision) {
  final Map<String, PersistentVolumeClaim> volumeName2PVC =
      groupByVolumeName(k8sEnv.getPersistentVolumeClaims().values());

  // process user-defined PVCs according to unique strategy
  final Map<String, PersistentVolumeClaim> envClaims = k8sEnv.getPersistentVolumeClaims();
  for (PersistentVolumeClaim pvc : toProvision.values()) {
    String originalPVCName = pvc.getMetadata().getName();

    PersistentVolumeClaim existingPVC = volumeName2PVC.get(originalPVCName);

    if (existingPVC != null) {
      // Replace pvc in environment with existing. Fix the references in Pods
      podsVolumes.changePVCReferences(
          k8sEnv.getPodsData().values(), originalPVCName, existingPVC.getMetadata().getName());
    } else {
      // there is no the corresponding existing pvc
      // new one should be created with generated name
      putLabel(pvc, CHE_VOLUME_NAME_LABEL, originalPVCName);

      final String uniqueName = Names.generateName(pvcNamePrefix + '-');
      pvc.getMetadata().setName(uniqueName);
      envClaims.put(uniqueName, pvc);

      volumeName2PVC.put(originalPVCName, pvc);
      podsVolumes.changePVCReferences(k8sEnv.getPodsData().values(), originalPVCName, uniqueName);
    }
  }
}