Search in sources :

Example 1 with V1PersistentVolumeClaimBuilder

use of io.kubernetes.client.openapi.models.V1PersistentVolumeClaimBuilder in project heron by twitter.

the class V1Controller method createPersistentVolumeClaims.

/**
 * Generates <code>Persistent Volume Claims Templates</code> from a mapping of <code>Volumes</code>
 * to <code>key-value</code> pairs of configuration options and values.
 * @param mapOfOpts <code>Volume</code> to configuration <code>key-value</code> mappings.
 * @return Fully populated list of only dynamically backed <code>Persistent Volume Claims</code>.
 */
@VisibleForTesting
protected List<V1PersistentVolumeClaim> createPersistentVolumeClaims(final Map<String, Map<KubernetesConstants.VolumeConfigKeys, String>> mapOfOpts) {
    List<V1PersistentVolumeClaim> listOfPVCs = new LinkedList<>();
    // Iterate over all the PVC Volumes.
    for (Map.Entry<String, Map<KubernetesConstants.VolumeConfigKeys, String>> pvc : mapOfOpts.entrySet()) {
        // Only create claims for `OnDemand` volumes.
        final String claimName = pvc.getValue().get(KubernetesConstants.VolumeConfigKeys.claimName);
        if (claimName != null && !KubernetesConstants.LABEL_ON_DEMAND.equalsIgnoreCase(claimName)) {
            continue;
        }
        V1PersistentVolumeClaim claim = new V1PersistentVolumeClaimBuilder().withNewMetadata().withName(pvc.getKey()).withLabels(getPersistentVolumeClaimLabels(getTopologyName())).endMetadata().withNewSpec().withStorageClassName("").endSpec().build();
        // Populate PVC options.
        for (Map.Entry<KubernetesConstants.VolumeConfigKeys, String> option : pvc.getValue().entrySet()) {
            String optionValue = option.getValue();
            switch(option.getKey()) {
                case storageClassName:
                    claim.getSpec().setStorageClassName(optionValue);
                    break;
                case sizeLimit:
                    claim.getSpec().setResources(new V1ResourceRequirements().putRequestsItem("storage", new Quantity(optionValue)));
                    break;
                case accessModes:
                    claim.getSpec().setAccessModes(Arrays.asList(optionValue.split(",")));
                    break;
                case volumeMode:
                    claim.getSpec().setVolumeMode(optionValue);
                    break;
                // Valid ignored options not used in a PVC.
                default:
                    break;
            }
        }
        listOfPVCs.add(claim);
    }
    return listOfPVCs;
}
Also used : V1PersistentVolumeClaim(io.kubernetes.client.openapi.models.V1PersistentVolumeClaim) Quantity(io.kubernetes.client.custom.Quantity) Map(java.util.Map) HashMap(java.util.HashMap) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) LinkedList(java.util.LinkedList) V1PersistentVolumeClaimBuilder(io.kubernetes.client.openapi.models.V1PersistentVolumeClaimBuilder) V1ResourceRequirements(io.kubernetes.client.openapi.models.V1ResourceRequirements) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with V1PersistentVolumeClaimBuilder

use of io.kubernetes.client.openapi.models.V1PersistentVolumeClaimBuilder in project heron by twitter.

the class V1ControllerTest method testCreatePersistentVolumeClaims.

@Test
public void testCreatePersistentVolumeClaims() {
    final String topologyName = "topology-name";
    final String volumeNameOne = "volume-name-one";
    final String volumeNameTwo = "volume-name-two";
    final String volumeNameStatic = "volume-name-static";
    final String claimNameOne = "OnDemand";
    final String claimNameTwo = "claim-name-two";
    final String claimNameStatic = "OnDEmaND";
    final String storageClassName = "storage-class-name";
    final String sizeLimit = "555Gi";
    final String accessModesList = "ReadWriteOnce,ReadOnlyMany,ReadWriteMany";
    final String accessModes = "ReadOnlyMany";
    final String volumeMode = "VolumeMode";
    final String path = "/path/to/mount/";
    final String subPath = "/sub/path/to/mount/";
    final Map<String, Map<VolumeConfigKeys, String>> mapPVCOpts = ImmutableMap.of(volumeNameOne, new HashMap<VolumeConfigKeys, String>() {

        {
            put(VolumeConfigKeys.claimName, claimNameOne);
            put(VolumeConfigKeys.storageClassName, storageClassName);
            put(VolumeConfigKeys.sizeLimit, sizeLimit);
            put(VolumeConfigKeys.accessModes, accessModesList);
            put(VolumeConfigKeys.volumeMode, volumeMode);
            put(VolumeConfigKeys.path, path);
        }
    }, volumeNameTwo, new HashMap<VolumeConfigKeys, String>() {

        {
            put(VolumeConfigKeys.claimName, claimNameTwo);
            put(VolumeConfigKeys.storageClassName, storageClassName);
            put(VolumeConfigKeys.sizeLimit, sizeLimit);
            put(VolumeConfigKeys.accessModes, accessModes);
            put(VolumeConfigKeys.volumeMode, volumeMode);
            put(VolumeConfigKeys.path, path);
            put(VolumeConfigKeys.subPath, subPath);
        }
    }, volumeNameStatic, new HashMap<VolumeConfigKeys, String>() {

        {
            put(VolumeConfigKeys.claimName, claimNameStatic);
            put(VolumeConfigKeys.sizeLimit, sizeLimit);
            put(VolumeConfigKeys.accessModes, accessModes);
            put(VolumeConfigKeys.volumeMode, volumeMode);
            put(VolumeConfigKeys.path, path);
            put(VolumeConfigKeys.subPath, subPath);
        }
    });
    final V1PersistentVolumeClaim claimOne = new V1PersistentVolumeClaimBuilder().withNewMetadata().withName(volumeNameOne).withLabels(V1Controller.getPersistentVolumeClaimLabels(topologyName)).endMetadata().withNewSpec().withStorageClassName(storageClassName).withAccessModes(Arrays.asList(accessModesList.split(","))).withVolumeMode(volumeMode).withNewResources().addToRequests("storage", new Quantity(sizeLimit)).endResources().endSpec().build();
    final V1PersistentVolumeClaim claimStatic = new V1PersistentVolumeClaimBuilder().withNewMetadata().withName(volumeNameStatic).withLabels(V1Controller.getPersistentVolumeClaimLabels(topologyName)).endMetadata().withNewSpec().withStorageClassName("").withAccessModes(Collections.singletonList(accessModes)).withVolumeMode(volumeMode).withNewResources().addToRequests("storage", new Quantity(sizeLimit)).endResources().endSpec().build();
    final List<V1PersistentVolumeClaim> expectedClaims = new LinkedList<>(Arrays.asList(claimOne, claimStatic));
    final List<V1PersistentVolumeClaim> actualClaims = v1ControllerWithPodTemplate.createPersistentVolumeClaims(mapPVCOpts);
    Assert.assertTrue(expectedClaims.containsAll(actualClaims));
}
Also used : VolumeConfigKeys(org.apache.heron.scheduler.kubernetes.KubernetesConstants.VolumeConfigKeys) V1PersistentVolumeClaim(io.kubernetes.client.openapi.models.V1PersistentVolumeClaim) Quantity(io.kubernetes.client.custom.Quantity) Matchers.anyString(org.mockito.Matchers.anyString) HashMap(java.util.HashMap) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) V1PersistentVolumeClaimBuilder(io.kubernetes.client.openapi.models.V1PersistentVolumeClaimBuilder) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 3 with V1PersistentVolumeClaimBuilder

use of io.kubernetes.client.openapi.models.V1PersistentVolumeClaimBuilder in project twister2 by DSC-SPIDAL.

the class RequestObjectBuilder method createPersistentVolumeClaimObject.

public static V1PersistentVolumeClaim createPersistentVolumeClaimObject(int numberOfWorkers) {
    String pvcName = jobID;
    // set labels for V1PersistentVolumeClaim
    HashMap<String, String> labels = KubernetesUtils.createJobLabels(jobID);
    String storageClass = KubernetesContext.persistentStorageClass(config);
    String accessMode = KubernetesContext.storageAccessMode(config);
    V1ResourceRequirements resources = new V1ResourceRequirements();
    double storageSize = SchedulerContext.persistentVolumePerWorker(config) * numberOfWorkers;
    if (!JobMasterContext.jobMasterRunsInClient(config)) {
        storageSize += JobMasterContext.persistentVolumeSize(config);
    }
    resources.putRequestsItem("storage", new Quantity(storageSize + "Gi"));
    V1PersistentVolumeClaim pvc = new V1PersistentVolumeClaimBuilder().withApiVersion("v1").withNewMetadata().withName(pvcName).withLabels(labels).endMetadata().withNewSpec().withStorageClassName(storageClass).withAccessModes(Arrays.asList(accessMode)).withResources(resources).endSpec().build();
    return pvc;
}
Also used : V1PersistentVolumeClaim(io.kubernetes.client.openapi.models.V1PersistentVolumeClaim) Quantity(io.kubernetes.client.custom.Quantity) V1ResourceRequirements(io.kubernetes.client.openapi.models.V1ResourceRequirements) V1PersistentVolumeClaimBuilder(io.kubernetes.client.openapi.models.V1PersistentVolumeClaimBuilder)

Aggregations

Quantity (io.kubernetes.client.custom.Quantity)3 V1PersistentVolumeClaim (io.kubernetes.client.openapi.models.V1PersistentVolumeClaim)3 V1PersistentVolumeClaimBuilder (io.kubernetes.client.openapi.models.V1PersistentVolumeClaimBuilder)3 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)2 V1ResourceRequirements (io.kubernetes.client.openapi.models.V1ResourceRequirements)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 VolumeConfigKeys (org.apache.heron.scheduler.kubernetes.KubernetesConstants.VolumeConfigKeys)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1