def create_volumesnapshotclass(name, deletepolicy): manifest = { 'kind': 'VolumeSnapshotClass', 'apiVersion': 'snapshot.storage.k8s.io/v1beta1', 'metadata': { 'name': name }, 'driver': 'driver.longhorn.io', 'deletionPolicy': deletepolicy } VolumeSnapshotClassFactory.manifests.append(manifest) api = get_custom_object_api_client() manifest_api_version = manifest["apiVersion"] api_group = urlparse(manifest_api_version).path.split("/")[0] api_version = urlparse(manifest_api_version).path.split("/")[1] plural = "volumesnapshotclasses" try: api.create_cluster_custom_object(group=api_group, version=api_version, plural=plural, body=manifest) except ApiException as e: print("exception creating volumesnapshotclass %s\n" % e) return manifest
def volumesnapshotclass(request): class VolumeSnapshotClassFactory(): manifests = [] @staticmethod def create_volumesnapshotclass(name, deletepolicy, snapshot_type=None): manifest = { 'kind': 'VolumeSnapshotClass', 'apiVersion': 'snapshot.storage.k8s.io/v1beta1', 'metadata': { 'name': name }, 'driver': 'driver.longhorn.io', 'deletionPolicy': deletepolicy } if snapshot_type is not None: manifest.update({'parameters': {'type': snapshot_type}}) VolumeSnapshotClassFactory.manifests.append(manifest) api = get_custom_object_api_client() manifest_api_version = manifest["apiVersion"] api_group = urlparse(manifest_api_version).path.split("/")[0] api_version = urlparse(manifest_api_version).path.split("/")[1] plural = "volumesnapshotclasses" try: api.create_cluster_custom_object(group=api_group, version=api_version, plural=plural, body=manifest) except ApiException as e: print("exception creating volumesnapshotclass %s\n" % e) return manifest yield VolumeSnapshotClassFactory.create_volumesnapshotclass api = get_custom_object_api_client() for manifest in VolumeSnapshotClassFactory.manifests: name = manifest["metadata"]["name"] api_group = urlparse(manifest["apiVersion"]).path.split("/")[0] api_version = urlparse(manifest["apiVersion"]).path.split("/")[1] plural = "volumesnapshotclasses" try: api.delete_cluster_custom_object(group=api_group, version=api_version, plural=plural, name=name) except ApiException as e: assert e.status == 404
def create_volumesnapshotcontent(name, volumesnapshotclass_name, delete_policy, snapshot_handle, volumesnapshot_ref_name, volumesnapshot_ref_namespace): manifest = { "apiVersion": "snapshot.storage.k8s.io/v1beta1", "kind": "VolumeSnapshotContent", "metadata": { "name": name, }, "spec": { "volumeSnapshotClassName": volumesnapshotclass_name, "driver": "driver.longhorn.io", "deletionPolicy": delete_policy, "source": { "snapshotHandle": snapshot_handle }, "volumeSnapshotRef": { "name": volumesnapshot_ref_name, "namespace": volumesnapshot_ref_namespace } } } VolumeSnapshotContentFactory.manifests.append(manifest) api = get_custom_object_api_client() api_group = urlparse(manifest["apiVersion"]).path.split("/")[0] api_version = urlparse(manifest["apiVersion"]).path.split("/")[1] name = manifest["metadata"]["name"] plural = "volumesnapshotcontents" try: api.create_cluster_custom_object(group=api_group, version=api_version, plural=plural, body=manifest) except ApiException as e: print("exception create volumesnapshotcontent %s\n" % e) for i in range(RETRY_COUNTS): status = \ api.get_cluster_custom_object_status(group=api_group, version=api_version, plural=plural, name=name) if "status" in status: if status["status"]["readyToUse"] is True: break time.sleep(RETRY_INTERVAL) return status
def delete_volumesnapshot(name, namespace): api = get_custom_object_api_client() api_group = "snapshot.storage.k8s.io" api_version = "v1beta1" plural = "volumesnapshots" try: api.delete_namespaced_custom_object(group=api_group, version=api_version, namespace=namespace, plural=plural, name=name) except ApiException as e: assert e.status == 404
def create_volumesnapshot(name, namespace, volumesnapshotclass_name, source_type, source_name): manifest = { 'apiVersion': 'snapshot.storage.k8s.io/v1beta1', 'kind': 'VolumeSnapshot', 'metadata': { 'name': name, 'namespace': namespace, }, 'spec': { 'volumeSnapshotClassName': volumesnapshotclass_name, 'source': { source_type: source_name } } } VolumeSnapshotFactory.manifests.append(manifest) api = get_custom_object_api_client() api_group = urlparse(manifest["apiVersion"]).path.split("/")[0] api_version = urlparse(manifest["apiVersion"]).path.split("/")[1] name = manifest["metadata"]["name"] plural = "volumesnapshots" try: api.create_namespaced_custom_object(group=api_group, version=api_version, namespace=namespace, plural=plural, body=manifest) except ApiException as e: print("exception create volumesnapshot %s\n" % e) for i in range(RETRY_COUNTS): status = \ api.get_namespaced_custom_object_status( group=api_group, version=api_version, namespace=namespace, plural=plural, name=name) if "status" in status: if "boundVolumeSnapshotContentName" in status["status"]: break time.sleep(RETRY_INTERVAL) return status
def get_volumesnapshotcontent(volumesnapshot_uid): api = get_custom_object_api_client() api_group = "snapshot.storage.k8s.io" api_version = "v1beta1" plural = "volumesnapshotcontents" volumesnapshotcontents = \ api.list_cluster_custom_object(group=api_group, version=api_version, plural=plural) for v in volumesnapshotcontents["items"]: if v["spec"]["volumeSnapshotRef"]["uid"] == volumesnapshot_uid: break return v
def wait_for_volumesnapshot_ready(volumesnapshot_name, namespace): api = get_custom_object_api_client() api_group = "snapshot.storage.k8s.io" api_version = "v1beta1" plural = "volumesnapshots" for i in range(RETRY_COUNTS): v = api.get_namespaced_custom_object_status(group=api_group, version=api_version, namespace=namespace, plural=plural, name=volumesnapshot_name) if v["status"]["readyToUse"] is True: break time.sleep(RETRY_INTERVAL) assert v["status"]["readyToUse"] is True return v