def get(cls, project, name, safe=True, from_cache=True): resp = None if from_cache: resp = cache_client.get(cls.name_plural() + "_" + project.name + "_" + name) if resp is None: crd_api = client.CustomObjectsApi() try: resp = crd_api.get_namespaced_custom_object( GROUP, cls.version(), "sandwich-" + project.name, cls.name_plural(), name) if resp is None: return None o = cls(resp) cache_client.set( cls.name_plural() + "_" + o.project_name + "_" + o.name, o._raw) except ApiException as e: if e.status == 404: cache_client.delete(cls.name_plural() + "_" + project.name + "_" + name) if safe: return None raise else: o = cls(resp) return o
def get(cls, name, safe=True, from_cache=True): resp = None if from_cache: resp = cache_client.get("project_" + name) if resp is None: core_api = client.CoreV1Api() try: resp = core_api.read_namespace("sandwich-" + name).to_dict() if resp is None: return None if resp['metadata']['labels'] is None: return None if PROJECT_LABEL not in resp['metadata']['labels']: return None o = cls(resp) if o.state != 'Terminating': # Only cache if not terminating cache_client.set('project_' + o.name, o._raw) except ApiException as e: if e.status == 404: cache_client.delete('project_' + name) if safe: return None raise else: o = cls(resp) return o
def save(self): core_api = client.CoreV1Api() try: self._raw = core_api.replace_namespace( self._raw['metadata']['name'], self._raw) cache_client.set('project_' + self.name, self._raw) except ApiException: cache_client.delete('project_' + self.name) raise
def save(self, ignore=False): crd_api = client.CustomObjectsApi() try: self.updated_at = arrow.now('UTC') self._raw = crd_api.replace_cluster_custom_object( GROUP, self.version(), self.name_plural(), self.name, self._raw) cache_client.set(self.name_plural() + "_" + self.name, self._raw) except ApiException as e: cache_client.delete(self.name_plural() + "_" + self.name) if e.status == 409: if ignore: return raise e
def delete(self, force=False): if force: if 'delete.sandwichcloud.com' in self._raw['metadata'][ 'finalizers']: self._raw['metadata']['finalizers'].remove( 'delete.sandwichcloud.com') self.save() cache_client.delete(self.name_plural() + "_" + self.name) crd_api = client.CustomObjectsApi() try: crd_api.delete_cluster_custom_object(GROUP, self.version(), self.name_plural(), self.name, V1DeleteOptions()) cache_client.delete(self.name_plural() + "_" + self.name) except ApiException as e: if e.status != 404: raise else: self.state = ResourceState.ToDelete self.save()
def delete(self): core_api = client.CoreV1Api() core_api.delete_namespace(self._raw['metadata']['name'], V1DeleteOptions()) # TODO: Does this cause any side effects since the k8s object isn't deleted right away cache_client.delete('project_' + self.name)