Ejemplo n.º 1
0
 async def func_wrapper(request):
     ctx = await func(request)
     if isinstance(ctx, dict):
         namespaces = await kubernetes.get_list(Namespace.objects(api))
         ctx["namespaces"] = namespaces
         ctx["rel_url"] = request.rel_url
     return ctx
Ejemplo n.º 2
0
async def get_cluster(request):
    cluster = request.match_info["cluster"]
    namespaces = await kubernetes.get_list(Namespace.objects(api))
    return {
        "cluster": cluster,
        "namespace": None,
        "namespaces": namespaces,
        "resource_types": cluster_resource_types,
    }
 def fetch_namespaces(self):
     self.namespace_list = Namespace.objects(self.api).all().response["items"]
     print("Currently there are " + str(len(self.namespace_list)) + " namespaces in the cluster.")
     if len(self.namespace_list) <= 0:
         return False
     self.namespace_names = []
     for ns in self.namespace_list:
         self.namespace_names.append(ns["metadata"]["name"])
     return True
Ejemplo n.º 4
0
 def tenant_namespace(self) -> Namespace:
     namespaces = Namespace.objects(self.api).filter(
         selector={"k8spin.cloud/org": self.org.name, "k8spin.cloud/type": "tenant"})
     for namespace in namespaces:
         if any([owner.get("name") == self.name
                 for owner in namespace.metadata.get("ownerReferences", list())]):
             return namespace
     # TODO CHANGE WITH K8SPIN EXCEPTIONS
     raise Exception("Tenant namespace not found")
Ejemplo n.º 5
0
 def fetch_namespaces(self):
     self.namespace_list = Namespace.objects(self.api).all().response["items"]
     print("Currently there are " + str(len(self.namespace_list)) + " namespaces in the cluster.")
     if len(self.namespace_list) <= 0:
         return False
     self.namespace_names = []
     for ns in self.namespace_list:
         self.namespace_names.append(ns["metadata"]["name"])
     return True
Ejemplo n.º 6
0
 def build_namespace(self, name):
     return Namespace(self.api,
     {
         "metadata":
         {
             "name": name
         },
         "spec": {}
     })
 def delete_namespace_byname(self, name, wait_for_delete=False):
     print("Deleting " + name + " namespace...")
     try:
         self.build_namespace(name).delete()
     except Exception as ex:
         print("Ignoring potential error with delete: " + ex)
     if wait_for_delete:
         print("Waiting for " + name + " to terminate...")
     while wait_for_delete:
         wait_for_delete = len(Namespace.objects(self.api).filter(selector={"name": "openshift-deploy"}).response["items"]) != 0
Ejemplo n.º 8
0
 def delete_namespace_byname(self, name, wait_for_delete=False):
     print("Deleting " + name + " namespace...")
     try:
         self.build_namespace(name).delete()
     except Exception as ex:
         print("Ignoring potential error with delete: " + ex)
     if wait_for_delete:
         print("Waiting for " + name + " to terminate...")
     while wait_for_delete:
         wait_for_delete = len(Namespace.objects(self.api).filter(selector={"name": "openshift-deploy"}).response["items"]) != 0
def test_example_hook():
    namespace = Namespace(None, {"metadata": {"name": "my-ns"}})
    hook = kube_janitor.example_hooks.random_dice
    cache = {}
    context = get_resource_context(namespace, hook, cache)
    value = context["random_dice"]
    assert 1 <= value <= 6

    # check that cache is used
    new_context = get_resource_context(namespace, hook, cache)
    assert new_context["random_dice"] == value
Ejemplo n.º 10
0
 async def func_wrapper(request):
     session = await get_session(request)
     ctx = await func(request, session)
     if isinstance(ctx, dict) and ctx.get("cluster"):
         clusters, is_all_clusters = get_clusters(
             request, ctx["cluster"])
         if not is_all_clusters and len(clusters) == 1:
             cluster = clusters[0]
             namespaces = await kubernetes.get_list(
                 wrap_query(Namespace.objects(cluster.api), request,
                            session))
             ctx["namespaces"] = namespaces
     ctx["rel_url"] = request.rel_url
     return ctx
Ejemplo n.º 11
0
async def get_cluster(request, session):
    cluster = request.app[CLUSTER_MANAGER].get(request.match_info["cluster"])
    namespaces = await kubernetes.get_list(
        wrap_query(Namespace.objects(cluster.api), request, session))
    resource_types = await cluster.resource_registry.cluster_resource_types
    return {
        "cluster":
        cluster.name,
        "cluster_obj":
        cluster,
        "namespace":
        None,
        "namespaces":
        namespaces,
        "resource_types":
        sorted(resource_types, key=lambda t: (t.kind, t.version)),
    }
Ejemplo n.º 12
0
 def tenant(self) -> Tenant:
     namespace = Namespace.objects(self.api).get(name=self.namespace)
     tenant_name = namespace.labels["k8spin.cloud/tenant"]
     tenant_namespace = self.org.organization_namespace
     return Tenant.objects(self.api,
                           tenant_namespace.name).get(name=tenant_name)
Ejemplo n.º 13
0
 def org(self) -> Organization:
     namespace = Namespace.objects(self.api).get(name=self.namespace)
     org_name = namespace.labels["k8spin.cloud/org"]
     return Organization.objects(self.api).get(name=org_name)
Ejemplo n.º 14
0
 def organization_namespace(self) -> Namespace:
     return Namespace.objects(self.api).get(name="org-" + self.name)