def _create_pvc(api: CoreV1Api, name: str, namespace: str,
                pv: V1PersistentVolume) -> V1PersistentVolumeClaim:
    logger.info(f"creating pvc: {name}")
    try:
        return api.create_namespaced_persistent_volume_claim(
            namespace=namespace,
            body=V1PersistentVolumeClaim(
                api_version="v1",
                kind="PersistentVolumeClaim",
                metadata=V1ObjectMeta(name=name, namespace=namespace),
                spec=V1PersistentVolumeClaimSpec(
                    access_modes=["ReadWriteOnce"],
                    resources=V1ResourceRequirements(
                        requests=pv.spec.capacity),
                    storage_class_name=pv.spec.storage_class_name,
                    volume_name=pv.metadata.name,
                ),
            ),
        )
    except ApiException as e:
        if e.reason == CONFLICT and json.loads(
                e.body)["reason"] == ALREADY_EXISTS:
            logger.info(f"using existing pvc: {name}")
            return api.read_namespaced_persistent_volume_claim(name, namespace)
        raise
Example #2
0
 def check_pvc(self, namespace, name, service_api: CoreV1Api = None):
     try:
         claim = service_api.read_namespaced_persistent_volume_claim(
             name, namespace)
     except Exception as e:
         logger.error(e)
         return False
     return True