def create(self, name, spec): body = client.V1PersistentVolume() body.spec = spec body.metadata = self.create_metadata(name) return self.v1api.create_persistent_volume(body)
def create(existing_volumes, create_volumes, namespace_name, labels): for i in range(existing_volumes, create_volumes): # creating a instance of class Namespace body = client.V1PersistentVolume() labels['type'] = "local" # giving name for the namespace as given in the function call body.metadata = client.V1ObjectMeta(name="cassandra-data-" + str(i), namespace=namespace_name, labels=labels) host_path = client.V1HostPathVolumeSource( path="/tmp/data/cassandra-data-" + str(i)) #creating volume Spec spec = client.V1PersistentVolumeSpec( access_modes=["ReadWriteOnce"], capacity={"storage": "5Gi"}, host_path=host_path, persistent_volume_reclaim_policy="Delete") body.spec = spec v1.create_persistent_volume(body=body) return "persistent volume created"
def fake_persistent_volume_error(): return client.V1PersistentVolume( api_version='v1', kind='PersistentVolume', metadata=client.V1ObjectMeta(name='curry-test001', namespace='curryns'), status=client.V1PersistentVolumeStatus(phase='UnBound', ))
def create_pv(username, namespace, path, storage_size): safe_chars = set(string.ascii_lowercase + string.digits) # Need to format the username that same way jupyterhub does. username = escapism.escape(username, safe=safe_chars, escape_char='-').lower() name = 'gpfs-{!s}'.format(username) claim_name = 'claim-{!s}'.format(username) path = os.path.join(path, username) metadata = client.V1ObjectMeta(name=name, namespace=namespace) claim_ref = client.V1ObjectReference(namespace=namespace, name=claim_name) host_path = client.V1HostPathVolumeSource(path, 'DirectoryOrCreate') spec = client.V1PersistentVolumeSpec( access_modes=[ 'ReadWriteOnce', ], capacity={ 'storage': storage_size, }, claim_ref=claim_ref, host_path=host_path, storage_class_name='gpfs', persistent_volume_reclaim_policy='Retain', volume_mode='Filesystem') pv = client.V1PersistentVolume('v1', 'PersistentVolume', metadata, spec) return pv, path
def create(self): spec = client.V1PersistentVolumeSpec( access_modes=[self.access_mode], capacity={"storage": "10Pi"}, volume_mode="Filesystem", persistent_volume_reclaim_policy="Delete", mount_options=self.mount_options, csi=client.V1CSIPersistentVolumeSource( driver="csi.juicefs.com", fs_type="juicefs", volume_handle=self.volume_handle, node_publish_secret_ref=client.V1SecretReference( name=self.secret_name, namespace=self.secret_namespace ), volume_attributes=self.parameters, ) ) pv = client.V1PersistentVolume( api_version="v1", kind="PersistentVolume", metadata=client.V1ObjectMeta(name=self.name, labels={"pv": self.name}), spec=spec ) client.CoreV1Api().create_persistent_volume(body=pv) PVs.append(self)
def pv_create(core_v1_api, pv_name): core_v1_api = core_v1_api body = client.V1PersistentVolume( api_version="v1", kind="PersistentVolume", metadata=client.V1ObjectMeta(name=pv_name, labels={"key": "localpvs"}), spec=client.V1PersistentVolumeSpec( capacity={"storage": "0.5Gi"}, volume_mode="Filesystem", access_modes=["ReadWriteOnce"], persistent_volume_reclaim_policy="Recycle", local={ "path": "/home/damu/Documents/kubernet/project/CDN_project/volumes/{_name}" .format(_name=pv_name) }, node_affinity=client.V1VolumeNodeAffinity( required=client.V1NodeSelector([ client.V1NodeSelectorTerm(match_expressions=[ client.V1NodeSelectorRequirement( key="kubernetes.io/hostname", operator="In", values=["minikube"]) ]) ])))) core_v1_api.create_persistent_volume(body=body)
def create_pv_object(name, **kwargs): for k, v in kwargs.items(): print('Optional key: %s value: %s' % (k, v)) capacity = kwargs['capacity'] accessModes = kwargs['accessModes'] reclaimPolicy = kwargs['reclaimPolicy'] storage_class_name = kwargs['storage_class_name'] nfs = kwargs['nfs'] # current_app.logger.debug("nfs: {}".format(nfs)) nfs_path = nfs['path'] nfs_server = nfs['server'] readonly = nfs['readonly'] nfs_readonly = False if readonly == 'true': nfs_readonly = True elif readonly == 'false': nfs_readonly == False else: pass spec = client.V1PersistentVolumeSpec( access_modes=[accessModes], capacity={"storage": capacity}, persistent_volume_reclaim_policy=reclaimPolicy, nfs=client.V1NFSVolumeSource(path=nfs_path, server=nfs_server, read_only=nfs_readonly), storage_class_name=storage_class_name, ) # print(spec) pv = client.V1PersistentVolume(api_version="v1", kind="PersistentVolume", metadata=client.V1ObjectMeta(name=name), spec=spec) return pv
def create_storage(self, name, capacity, storageClassName, infrastructure_id, properties): v1 = self.coreV1Api() logger.debug('storageClassName=' + storageClassName) if (storageClassName == 'hostpath'): hostpath = properties.get('hostpath', None) if (hostpath is None): raise ValueError("Hostpath property must be provided") spec = client.V1PersistentVolumeSpec( capacity={'storage': capacity}, access_modes=['ReadWriteOnce'], host_path=client.V1HostPathVolumeSource(path=hostpath, type='')) storage = client.V1PersistentVolume( api_version='v1', kind='PersistentVolume', metadata=client.V1ObjectMeta( name=name, labels={"infrastructure_id": infrastructure_id}), spec=spec) logger.debug("Creating storage %s" % str(storage)) api_response = v1.create_persistent_volume(storage) logger.debug("Storage created. status='%s'" % str(api_response.status)) else: # the storage provisioner will create the persistent volume in this case pass
def create_pv_from_current_pv(kubeconfig_path, pv): config.load_kube_config(kubeconfig_path) v1 = client.CoreV1Api() newpv = client.V1PersistentVolume() newpv.kind = "PersistentVolume" newpv.api_version = "v1" newpv.metadata = client.V1ObjectMeta() newpv.metadata.name = pv.metadata.name newpv.spec = client.V1PersistentVolumeSpec() newpv.spec.capacity = pv.spec.capacity newpv.spec.access_modes = pv.spec.access_modes newpv.spec.azure_disk = pv.spec.azure_disk newpv.spec.azure_disk.disk_uri = pv.target_disk_uri if pv.spec.azure_disk.kind.lower() == 'managed': newpv.spec.azure_disk.disk_name = pv.target_disk_name else: newpv.spec.azure_disk.disk_uri = pv.target_disk_uri try: v1.create_persistent_volume(newpv) except ApiException: return False return True
def create_k8s_nfs_resources(self) -> bool: """ Create NFS resources such as PV and PVC in Kubernetes. """ from kubernetes import client as k8sclient pv_name = "nfs-ckpt-pv-{}".format(uuid.uuid4()) persistent_volume = k8sclient.V1PersistentVolume( api_version="v1", kind="PersistentVolume", metadata=k8sclient.V1ObjectMeta( name=pv_name, labels={'app': pv_name} ), spec=k8sclient.V1PersistentVolumeSpec( access_modes=["ReadWriteMany"], nfs=k8sclient.V1NFSVolumeSource( path=self.params.path, server=self.params.server ), capacity={'storage': '10Gi'}, storage_class_name="" ) ) k8s_api_client = k8sclient.CoreV1Api() try: k8s_api_client.create_persistent_volume(persistent_volume) self.params.pv_name = pv_name except k8sclient.rest.ApiException as e: print("Got exception: %s\n while creating the NFS PV", e) return False pvc_name = "nfs-ckpt-pvc-{}".format(uuid.uuid4()) persistent_volume_claim = k8sclient.V1PersistentVolumeClaim( api_version="v1", kind="PersistentVolumeClaim", metadata=k8sclient.V1ObjectMeta( name=pvc_name ), spec=k8sclient.V1PersistentVolumeClaimSpec( access_modes=["ReadWriteMany"], resources=k8sclient.V1ResourceRequirements( requests={'storage': '10Gi'} ), selector=k8sclient.V1LabelSelector( match_labels={'app': self.params.pv_name} ), storage_class_name="" ) ) try: k8s_api_client.create_namespaced_persistent_volume_claim(self.params.namespace, persistent_volume_claim) self.params.pvc_name = pvc_name except k8sclient.rest.ApiException as e: print("Got exception: %s\n while creating the NFS PVC", e) return False return True
def get_persistent_volume(namespace, volume, run_type): metadata = client.V1ObjectMeta(name=volume, labels=get_labels(volume), namespace=namespace) spec = get_persistent_volume_spec(namespace=namespace, volume=volume, run_type=run_type) return client.V1PersistentVolume(api_version=k8s_constants.K8S_API_VERSION_V1, kind=k8s_constants.K8S_PERSISTENT_VOLUME_KIND, metadata=metadata, spec=spec)
def _get_pv_body(self, name, path, storage, namespace): spec = self._get_pv_spec(name, path, storage) if namespace != "default": name = name + "-" + namespace return client.V1PersistentVolume( api_version="v1", kind="PersistentVolume", metadata=client.V1ObjectMeta(name=name), spec=spec)
def get(self): return client.V1PersistentVolume( kind='PersistentVolume', api_version='v1', metadata=client.V1ObjectMeta(name='postgres-pv', labels={'pv': 'postgres'}), spec=client.V1PersistentVolumeSpec( storage_class_name='manual', capacity={'storage': '2Gi'}, access_modes=['ReadWriteOnce'], host_path=client.V1HostPathVolumeSource( path='/data/postgres_storage')))
def create_pv(pv_values, pv_name, created_objects, sc_name=""): """ creates persistent volume Args: param1: pv_values - values required for creation of pv param2: pv_name - name of pv to be created param3: sc_name - name of storage class pv associated with Returns: None Raises: Raises an exception on kubernetes client api failure and asserts """ api_instance = client.CoreV1Api() pv_metadata = client.V1ObjectMeta(name=pv_name) pv_csi = client.V1CSIPersistentVolumeSource( driver="spectrumscale.csi.ibm.com", volume_handle=pv_values["volumeHandle"]) if pv_values["reclaim_policy"] == "Default": pv_spec = client.V1PersistentVolumeSpec( access_modes=[pv_values["access_modes"]], capacity={"storage": pv_values["storage"]}, csi=pv_csi, storage_class_name=sc_name) else: pv_spec = client.V1PersistentVolumeSpec( access_modes=[pv_values["access_modes"]], capacity={"storage": pv_values["storage"]}, csi=pv_csi, persistent_volume_reclaim_policy=pv_values["reclaim_policy"], storage_class_name=sc_name) pv_body = client.V1PersistentVolume(api_version="v1", kind="PersistentVolume", metadata=pv_metadata, spec=pv_spec) try: LOGGER.info( f'PV Create : Creating PV {pv_name} with {pv_values} parameter') api_response = api_instance.create_persistent_volume(body=pv_body, pretty=True) LOGGER.debug(str(api_response)) created_objects["pv"].append(pv_name) except ApiException as e: LOGGER.error(f'PV {pv_name} creation failed hence failing test case ') LOGGER.error( f"Exception when calling CoreV1Api->create_persistent_volume: {e}") cleanup.clean_with_created_objects(created_objects) assert False
def _replicate_nfs_pv_with_suffix(self, vol, suffix): # A Note on Namespaces # # PersistentVolumes binds are exclusive, # and since PersistentVolumeClaims are namespaced objects, # mounting claims with “Many” modes (ROX, RWX) is only # possible within one namespace. # # (https://kubernetes.io/docs/concepts/storage/persistent-volumes) # # The way we do this at LSST is that an NFS PV is statically created # with the name suffixed with the same namespace as the Hub # (e.g. "projects-gkenublado") # # Then when a new user namespace is created, we duplicate the NFS # PV to one with the new namespace appended # (e.g. "projects-gkenublado-athornton") # # Then we can bind PVCs to the new (effectively, namespaced) PVs # and everything works. if not suffix: self.log.warning("Cannot create namespaced PV without suffix.") return None pname = vol.metadata.name mtkey = "volume.beta.kubernetes.io/mount-options" mtopts = None if vol.metadata.annotations: mtopts = vol.metadata.annotations.get(mtkey) ns_name = pname + "-" + suffix anno = {} if mtopts: anno[mtkey] = mtopts pv = client.V1PersistentVolume(spec=vol.spec, metadata=client.V1ObjectMeta( annotations=anno, name=ns_name, labels={"name": ns_name})) # It is new, therefore unclaimed. pv.spec.claim_ref = None self.log.info("Creating PV '{}'.".format(ns_name)) try: self.api.create_persistent_volume(pv) except ApiException as e: if e.status != 409: self.log.exception("Create PV '%s' " % ns_name + "failed: %s" % str(e)) raise else: self.log.info("PV '%s' already exists." % ns_name) return pv
def persistent_volume(self, name, storage, accessModes=["ReadWriteOnce"], host_path=True, patch=False): """Create persistent volume by default on host.""" ps_vol = client.V1PersistentVolume(kind="PersistentVolume", api_version="v1") ps_vol.metadata = client.V1ObjectMeta(name=name) spec = client.V1PersistentVolumeSpec(capacity={"storage": storage}, access_modes=accessModes, storage_class_name=name) if host_path: spec.host_path = client.V1HostPathVolumeSource(path=f'/mnt/data/{name}') ps_vol.spec = spec try: api_core.create_persistent_volume(body=ps_vol) LOG.info(f'Persistent Volume: {name} created.') except ApiException as e: if e.status == 409 and patch: api_core.patch_persistent_volume(name=name, body=ps_vol) LOG.info(f'PeVolume: {name} patched.') else: LOG.error(f'Exception message: {e}')
def post(self, request): data = self.request.POST name = str.lower(data.get('name', None)) capacity = data.get('capacity', None) access_mode = data.get('access_mode', None) storage_class = data.get('storage_class', None) if storage_class == 'nfs': storage_class_name = 'nfs-storageclass-provisioner' else: storage_class_name = None server_ip = data.get('server_ip', None) mount_path = str.lower(data.get('mount_path', name)) k8s.load_auth(request=self.request) core_api = client.CoreV1Api() body = client.V1PersistentVolume( api_version='v1', kind='PersistentVolume', metadata=client.V1ObjectMeta(name=name), spec=client.V1PersistentVolumeSpec( capacity={'storage': capacity}, access_modes=[access_mode], storage_class_name=storage_class_name, nfs=client.V1NFSVolumeSource( server=server_ip, path='/ifs/kubernetes/{}'.format(mount_path)))) try: core_api.create_persistent_volume(body=body) except client.exceptions.ApiException as e: code = e.status if e.status == 403: msg = '没有创建权限!' elif e.status == 409: msg = 'PV名称冲突' elif e.status == 422: e = json.loads(e.body) msg = e.get('message') else: print(e) msg = '创建失败!' else: code, msg = 0, '创建{}成功!'.format(name) result = {'code': code, 'msg': msg} return JsonResponse(result)
def get_pv(request): user = user_get(request) hostname = request.GET.get('hostname') auth_config(user, hostname) core_api = client.CoreV1Api() if request.method == "GET": pageIndex = request.GET.get("pageIndex") pageSize = request.GET.get("pageSize") list = [] res = [] try: for pv in core_api.list_persistent_volume().items: dict = {} dict["name"] = pv.metadata.name dict["capacity"] = pv.spec.capacity["storage"] dict["access_modes"] = pv.spec.access_modes dict[ "reclaim_policy"] = pv.spec.persistent_volume_reclaim_policy dict["status"] = pv.status.phase if pv.spec.claim_ref is not None: pvc_ns = pv.spec.claim_ref.namespace pvc_name = pv.spec.claim_ref.name dict["pvc"] = "%s / %s" % (pvc_ns, pvc_name) else: dict["pvc"] = "未绑定" dict["tsorage_class"] = pv.spec.storage_class_name dict["create_time"] = dt_format(pv.metadata.creation_timestamp) list.append(dict) pageInator = Paginator(list, pageSize) context = pageInator.page(pageIndex) for item in context: res.append(item) data = { "code": 0, "msg": "ok", "DataCount": len(list), "data": res } except Exception as e: data = error(e) return JsonResponse(data) elif request.method == "DELETE": request_data = QueryDict(request.body) name = request_data.get("name") try: core_api.delete_persistent_volume(name) data = {"code": 0, "msg": "删除成功."} except Exception as e: data = error(e) return JsonResponse(data) elif request.method == "POST": name = request.POST.get("name", None) capacity = request.POST.get("capacity", None) access_mode = request.POST.get("access_mode", None) storage_type = request.POST.get("storage_type", None) server_ip = request.POST.get("server_ip", None) mount_path = request.POST.get("mount_path", None) body = client.V1PersistentVolume( api_version="v1", kind="PersistentVolume", metadata=client.V1ObjectMeta(name=name), spec=client.V1PersistentVolumeSpec( capacity={'storage': capacity}, access_modes=[access_mode], nfs=client.V1NFSVolumeSource(server=server_ip, path="/ifs/kubernetes/%s" % mount_path))) try: core_api.create_persistent_volume(body=body) data = {"code": 0, "msg": "创建成功"} except Exception as e: data = error(e) return JsonResponse(data)