def get_version(self, reraise=False): try: return self.k8s_version_api.get_code().to_dict() except ApiException as e: logger.error("K8S error: {}".format(e)) if reraise: raise PolyaxonK8SError(e)
def get_ingress(self, name, reraise=False): try: return self.k8s_beta_api.read_namespaced_ingress(name, self.namespace) except ApiException as e: if reraise: raise PolyaxonK8SError(e) return None
def create_or_update_custom_object(self, name, group, version, plural, body, reraise=False): try: return ( self.create_custom_object(name=name, group=group, version=version, plural=plural, body=body), True, ) except ApiException: try: return ( self.update_custom_object( name=name, group=group, version=version, plural=plural, body=body, ), False, ) except ApiException as e: if reraise: raise PolyaxonK8SError(e) else: logger.error("K8S error: {}".format(e))
def get_config_map(self, name, reraise=False): try: return self.k8s_api.read_namespaced_config_map(name, self.namespace) except ApiException as e: if reraise: raise PolyaxonK8SError(e) return None
def get_job(self, name, reraise=False): try: return self.k8s_batch_api.read_namespaced_job( name=name, namespace=self.namespace) except ApiException as e: if reraise: raise PolyaxonK8SError(e) return None
def update_node_labels(self, node, labels, reraise=False): body = {"metadata": {"labels": labels}, "namespace": self.namespace} try: return self.k8s_api.patch_node(name=node, body=body) except ApiException as e: logger.error("K8S error: {}".format(e)) if reraise: raise PolyaxonK8SError(e)
def list_nodes(self, reraise=False): try: res = self.k8s_api.list_node() return [p for p in res.items] except ApiException as e: logger.error("K8S error: {}".format(e)) if reraise: raise PolyaxonK8SError(e)
def _list_namespace_resource(self, labels, resource_api, reraise=False, **kwargs): try: res = resource_api(self.namespace, label_selector=labels, **kwargs) return [p for p in res.items] except ApiException as e: logger.error("K8S error: {}".format(e)) if reraise: raise PolyaxonK8SError(e) return []
def create_or_update_ingress(self, name, body, reraise=False): try: return self.create_ingress(name=name, body=body), True except ApiException: try: return self.update_ingress(name=name, body=body), False except ApiException as e: if reraise: raise PolyaxonK8SError(e) else: logger.error("K8S error: {}".format(e))
def delete_service(self, name, reraise=False): found = False try: self.k8s_api.read_namespaced_service(name, self.namespace) found = True self.k8s_api.delete_namespaced_service(name, self.namespace) logger.debug('Service `{}` deleted'.format(name)) except ApiException as e: if found: logger.error('Could not delete service `{}`'.format(name)) if reraise: raise PolyaxonK8SError(e) else: logger.debug('Service `{}` was not found'.format(name))
def delete_volume(self, name, reraise=False): found = False try: self.k8s_api.read_persistent_volume(name) found = True self.k8s_api.delete_persistent_volume( name, client.V1DeleteOptions(api_version=constants.K8S_API_VERSION_V1)) logger.debug('Volume `{}` Deleted'.format(name)) except ApiException as e: if found: logger.error('Could not delete volume `{}`'.format(name)) if reraise: raise PolyaxonK8SError(e) else: logger.debug('Volume `{}` was not found'.format(name))
def delete_config_map(self, name, reraise=False): found = False try: self.k8s_api.read_namespaced_config_map(name, self.namespace) found = True self.k8s_api.delete_namespaced_config_map( name, self.namespace, client.V1DeleteOptions(api_version=constants.K8S_API_VERSION_V1)) logger.debug('Config map `{}` Deleted'.format(name)) except ApiException as e: if found: logger.error('Could not delete config map `{}`'.format(name)) if reraise: raise PolyaxonK8SError(e) else: logger.debug('Config map `{}` was not found'.format(name))
def create_or_update_config_map(self, name, body, reraise=False): found = False try: self.k8s_api.read_namespaced_config_map(name, self.namespace) found = True logger.debug('A config map with name `{}` was found'.format(name)) resp = self.k8s_api.patch_namespaced_config_map(name, self.namespace, body) logger.debug('Config map `{}` was patched'.format(name)) except ApiException as e: if found: # Config map was found but could not update, we need to raise logger.error("K8S error: {}".format(e)) if reraise: raise PolyaxonK8SError(e) resp = None else: resp = self.k8s_api.create_namespaced_config_map(self.namespace, body) logger.debug('Config map `{}` was created'.format(name)) return resp, not found
def delete_ingress(self, name, reraise=False): found = False try: self.k8s_beta_api.read_namespaced_ingress(name, self.namespace) found = True self.k8s_beta_api.delete_namespaced_ingress( name, self.namespace, client.V1DeleteOptions(api_version=constants.K8S_API_VERSION_V1_BETA1, propagation_policy='Foreground')) logger.debug('Ingress `{}` deleted'.format(name)) except ApiException as e: if found: logger.error('Could not delete Ingress `{}`'.format(name)) if reraise: raise PolyaxonK8SError(e) else: logger.debug('Ingress `{}` was not found'.format(name))
def create_or_update_ingress(self, name, data, reraise=False): found = False try: self.k8s_beta_api.read_namespaced_ingress(name, self.namespace) found = True logger.debug('An ingress with name `{}` was found'.format(name)) resp = self.k8s_beta_api.patch_namespaced_ingress(name, self.namespace, data) logger.debug('Ingress `{}` was patched'.format(name)) except ApiException as e: if found: logger.error('Could not create ingress `{}`'.format(name)) if reraise: raise PolyaxonK8SError(e) resp = None else: resp = self.k8s_beta_api.create_namespaced_ingress(self.namespace, data) logger.debug('ingress `{}` was created'.format(name)) return resp, not found
def create_or_update_volume(self, name, data, reraise=False): found = False try: self.k8s_api.read_persistent_volume(name) found = True logger.debug('A Persistent volume with name `{}` was found'.format(name)) resp = self.k8s_api.patch_persistent_volume(name, data) logger.debug('Persistent volume `{}` was patched'.format(name)) except ApiException as e: if found: logger.error('Could not create volume `{}`'.format(name)) if reraise: raise PolyaxonK8SError(e) resp = None else: resp = self.k8s_api.create_persistent_volume(data) logger.debug('Persistent volume `{}` was created'.format(name)) return resp, not found