def create_or_get(cls, k8s_ns_name): """ Returns a newly created KubernetesNamespace object with the given name, assuming that it does not exist so far. When the namespace already exists, this object is returned instead. In any case, it is ensured that the cluster is in sync accordingly. """ if cls.objects.filter(name=k8s_ns_name).exists(): return cls.objects.get(name=k8s_ns_name) else: new_ns = cls(name=k8s_ns_name) if new_ns.create_in_cluster(): # make sure that new default service account is synced from kubeportal.models.kubernetesserviceaccount import KubernetesServiceAccount KubernetesServiceAccount.create_missing_in_portal() return new_ns else: return None
def sync(request=None): ''' Synchronizes the local shallow copy of Kubernetes data. Returns True on success. ''' try: res1 = KubernetesNamespace.create_missing_in_portal() res2 = KubernetesNamespace.create_missing_in_cluster() res3 = KubernetesServiceAccount.create_missing_in_portal() res4 = KubernetesServiceAccount.create_missing_in_cluster() if request: messages.info(request, "Synchronization finished.") logger.debug("Synchronization finished.") return True == res1 == res2 == res3 == res4 except client.rest.ApiException as e: msg = json.loads(e.body)['message'] logger.error( "API server exception during synchronization: {0}".format(msg)) if request: messages.error( request, "Kubernetes returned an error during synchronization: {0}".format(msg)) return False