def create_pv_and_pvc_and_pod(self, workspace, size, namespace, ontap_cluster_data_lif): ''' Create PV, PVC and pod enabled to use volume 'vol_name' ''' kb_clone_name = workspace['kb_clone_name'] statuses = self.create_pv_and_pvc( kb_clone_name, size, namespace, ontap_cluster_data_lif) body = self.create_pod_config(workspace) service_body = self.create_service_config(workspace) try: self.api.create_namespaced_pod(namespace, body) self.api.create_namespaced_service(namespace, service_body) pod_status = OntapService.set_status( 201, "Pod", body['metadata']['name']) except ApiException as exc: if self.parse_exception(exc) == "AlreadyExists": pod_status = OntapService.set_status( 200, "Pod", body['metadata']['name']) else: error_message = "Exception when calling CoreV1Api->create_namespaced_pod: %s\n" % exc pod_status = OntapService.set_status( 400, "Pod", body['metadata']['name'], error_message) statuses.append(pod_status) return statuses
def create_pvc_clone(self, pvc_clone_name, pvc_source, size, namespace, storage_class): ''' Create a PVC clone from a source PVC. For use with Trident where Trident creates an ONTAP clone and a k8s PV and maps it to the PVC :param pvc_clone: PVC clone name :param pvc_source: PVC source name to clone from :param size: size of the clone PVC in MB :param namespace: Kube namespace :param storage_class: Storage class (should match with Trident) :return: Status of creation ''' body = self.create_pvc_clone_config(pvc_clone_name, pvc_source, size, storage_class) try: self.api.create_namespaced_persistent_volume_claim(namespace, body) status = OntapService.set_status( 201, "PVC", body['metadata']['name']) except ApiException as exc: if self.parse_exception(exc) == "AlreadyExists": status = OntapService.set_status( 200, "PVC", body['metadata']['name']) else: err = "Exception calling CoreV1Api->create_namespaced_persistent_volume_claim: %s\n" % exc status = OntapService.set_status( 400, "PVC", body['metadata']['name'], err) return status
def create_pvc(self, vol_name, pvc_size, namespace): ''' Create PVC with name 'vol_name' and size 'pvc_size' ''' body = self.create_pvc_config(vol_name, pvc_size) try: self.api.create_namespaced_persistent_volume_claim(namespace, body) status = OntapService.set_status( 201, "PVC", body['metadata']['name']) except ApiException as exc: if self.parse_exception(exc) == "AlreadyExists": status = OntapService.set_status( 200, "PVC", body['metadata']['name']) else: err = "Exception calling CoreV1Api->create_namespaced_persistent_volume_claim: %s\n" % exc status = OntapService.set_status( 400, "PVC", body['metadata']['name'], err) return status
def create_pv(self, vol_name, pv_size, ontap_cluster_data_lif): ''' Create PV with name 'vol_name' and size 'pv_size' ''' body = self.create_pv_config( vol_name, pv_size, ontap_cluster_data_lif) # V1PersistentVolume try: # api_response = self.api.create_persistent_volume(body) self.api.create_persistent_volume(body) status = OntapService.set_status( 201, "PV", body['metadata']['name']) except ApiException as exc: if self.parse_exception(exc) == "AlreadyExists": status = OntapService.set_status( 200, "PV", body['metadata']['name']) else: error_message = "Exception when calling CoreV1Api->create_persistent_volume: %s\n" % exc status = OntapService.set_status( 400, "PV", body['metadata']['name'], error_message) return status
def test_set_status_for_failure(self): '''Test helper to create status dictionary''' expected_status = { 'resource': 'Volume', 'resource_name': 'test-vol', 'code': 400, 'status': 'FAILED', 'message': '', 'error_message': 'Error creating PV' } attempted_status = ontap.set_status(400, 'Volume', 'test-vol', 'Error creating PV') self.assertEqual(expected_status, attempted_status)
def test_set_status(self): '''Test helper to create status dictionary''' expected_status = { 'resource': 'Volume', 'resource_name': 'test-vol', 'code': 200, 'status': 'SUCCESS', 'message': 'Volume test-vol already exists', 'error_message': '' } attempted_status = ontap.set_status(200, 'Volume', 'test-vol') self.assertEqual(expected_status, attempted_status)
def create_pvc_clone_and_pod(self, workspace, merge=False): """ Create a Kube PVC (clone), Pod and a service representing the user workspace Once PVC clone is created, Trident assigns an ONTAP clone and a PV :param workspace: workspace details dict() :return: status of PVC and Pod creation """ logging.debug("Received workspace details:: %s" % str(workspace)) workspace['pvc'] = self.get_kube_resource_name(workspace['name'], 'pvc') workspace['source_pvc'] = self.get_kube_resource_name(workspace['build_name'], 'pvc') workspace['pipeline_pvc'] = self.get_kube_resource_name(workspace['pipeline'], 'pvc') workspace['pod'] = self.get_kube_resource_name(workspace['name'], 'pod') workspace['service'] = self.get_kube_resource_name(workspace['name'], 'service') logging.debug("KUBE workspace PVC:: %s" % workspace['pvc']) logging.debug("KUBE workspace POD:: %s" % workspace['pod']) logging.debug("KUBE workspace SERVICE:: %s" % workspace['service']) logging.debug("KUBE workspace PIPELINE PVC:: %s" % workspace['pipeline_pvc']) logging.debug("KUBE workspace SOURCE (BUILD) PVC:: %s" % workspace['source_pvc']) clone_response = self.create_pvc_clone_resource(clone=workspace['pvc'], source=workspace['source_pvc']) workspace['clone_name'] = self.get_volume_name_from_pvc(workspace['pvc']) workspace['pv_name'] = self.get_pv_name_from_pvc(workspace['pvc']) if merge: workspace['source_workspace_pvc'] = self.get_kube_resource_name(workspace['source_workspace_name'], 'pvc') workspace['source_workspace_pv'] = self.get_pv_name_from_pvc(workspace['source_workspace_pvc']) logging.debug("KUBE source workspace PVC:: %s" % workspace['source_workspace_pvc']) logging.debug("KUBE source workspace PV:: %s" % workspace['source_workspace_pv']) workspace['temp_pod_name'] = 'temp-pod-for-uid-gid' + workspace['name'] temp_pod = self.create_temporary_pod_to_change_uid_gid(workspace) body = self.create_pod_config(workspace) service_body = self.create_service_config(workspace) logging.debug("WORKSPACE DETAILS:::: %s" % str(workspace)) try: # create a temporary pod to set UID GID For workspace self.api.create_namespaced_pod(self.namespace, temp_pod) logging.info("Changing UID and GID for the workspace clone volume") sleep(10) # TODO: Change this to wait on pod status # delete the temp pod self.delete_pod(workspace['temp_pod_name']) self.api.create_namespaced_pod(self.namespace, body) self.api.create_namespaced_service(self.namespace, service_body) # TODO: move set_status to helper? pod_status = OntapService.set_status(201, "Pod", body['metadata']['name']) service_status = OntapService.set_status(201, "Service", body['metadata']['name']) except ApiException as exc: if self.parse_exception(exc) == "AlreadyExists": pod_status = OntapService.set_status(200, "Pod", body['metadata']['name']) service_status = OntapService.set_status(200, "Service", body['metadata']['name']) else: error_message = "Exception when calling create_namespaced_pod or create_namespaced_service: %s\n" % exc pod_status = OntapService.set_status(400, "Pod", body['metadata']['name'], error_message) service_status = OntapService.set_status(400, "Service", body['metadata']['name'], error_message) return [clone_response, pod_status, service_status]
def create_pvc_and_pod(self, workspace, merge=False, namespace='default'): """ Create a Kube PVC (clone), Pod and a service representing the user workspace Once PVC clone is created, Trident assigns an ONTAP clone and a PV :param workspace: workspace details dict() :param namespace: Kube namespace :return: status of PVC and Pod creation """ logging.debug("Received workspace details:: %s" % str(workspace)) workspace['pvc'] = self.get_kube_resource_name(workspace['name'], 'pvc') workspace['source_pvc'] = self.get_kube_resource_name(workspace['build_name'], 'pvc') workspace['pod'] = self.get_kube_resource_name(workspace['name'], 'pod') workspace['service'] = self.get_kube_resource_name(workspace['name'], 'service') logging.debug("KUBE workspace PVC:: %s" % workspace['pvc']) logging.debug("KUBE workspace POD:: %s" % workspace['pod']) logging.debug("KUBE workspace SERVICE:: %s" % workspace['service']) logging.debug("KUBE workspace SOURCE PVC:: %s" % workspace['source_pvc']) clone_response = self.create_pvc_clone_resource(clone=workspace['pvc'], source=workspace['source_pvc'], namespace=namespace) workspace['clone_name'] = self.get_volume_name_from_pvc(workspace['pvc']) workspace['pv_name'] = self.get_pv_name_from_pvc(workspace['pvc']) if merge: workspace['source_workspace_pvc'] = self.get_kube_resource_name(workspace['source_workspace_name'], 'pvc') workspace['source_workspace_pv'] = self.get_pv_name_from_pvc(workspace['source_workspace_pvc']) logging.debug("KUBE source workspace PVC:: %s" % workspace['source_workspace_pvc']) logging.debug("KUBE source workspace PV:: %s" % workspace['source_workspace_pv']) body = self.create_pod_config(workspace) service_body = self.create_service_config(workspace) logging.debug("WORKSPACE DETAILS:::: %s" % str(workspace)) try: self.api.create_namespaced_pod(namespace, body) self.api.create_namespaced_service(namespace, service_body) # TODO: move set_status to helper? pod_status = OntapService.set_status(201, "Pod", body['metadata']['name']) service_status = OntapService.set_status(201, "Service", body['metadata']['name']) except ApiException as exc: if self.parse_exception(exc) == "AlreadyExists": pod_status = OntapService.set_status(200, "Pod", body['metadata']['name']) service_status = OntapService.set_status(200, "Service", body['metadata']['name']) else: error_message = "Exception when calling create_namespaced_pod or create_namespaced_service: %s\n" % exc pod_status = OntapService.set_status(400, "Pod", body['metadata']['name'], error_message) service_status = OntapService.set_status(400, "Service", body['metadata']['name'], error_message) return [clone_response, pod_status, service_status]