def destroy_namespace(api: client.CoreV1Api, name): if len(api.list_namespace( field_selector=f'metadata.name={name}').items) == 1: logger.info(f'destroying namespace: {name}') api.delete_namespace(name=name) else: logger.info(f'cannot find namespace to destroy: {name}')
def manage_user_namespace(event: Dict[str, Any], api: client.CoreV1Api, userspace_dc: dynamic.DynamicClient) -> None: user_name = cast(str, event.get("user_name")) user_email = cast(str, event.get("user_email")) expected_user_namespaces = cast(Dict[str, str], event.get("expected_user_namespaces")) all_ns_raw = api.list_namespace().to_dict() all_ns = [ item.get("metadata").get("name") for item in all_ns_raw["items"] if item.get("metadata", {}).get("name") and item.get("metadata", {}).get("name").endswith(user_name) ] create_user_namespace( api=api, userspace_dc=userspace_dc, user_name=user_name, user_email=user_email, expected_user_namespaces=expected_user_namespaces, namespaces=all_ns, ) delete_user_namespace( api=api, userspace_dc=userspace_dc, user_name=user_name, expected_user_namespaces=expected_user_namespaces, namespaces=all_ns, )
def ensure_namespace(api: client.CoreV1Api, namespace): if len( api.list_namespace( field_selector=f'metadata.name={namespace}').items) == 0: logger.info(f'creating namespace: {namespace}') body = client.V1Namespace(metadata=V1ObjectMeta(name=namespace)) api.create_namespace(body=body) else: logger.info(f'namespace exists: {namespace}')
def delete_testing_namespaces(v1: CoreV1Api) -> []: """ List and remove all the testing namespaces. Testing namespaces are the ones starting with "test-namespace-" :param v1: CoreV1Api :return: """ namespaces_list = v1.list_namespace() for namespace in list(filter(lambda ns: ns.metadata.name.startswith("test-namespace-"), namespaces_list.items)): delete_namespace(v1, namespace.metadata.name)
def scan_cluster_namespaces(api: client.CoreV1Api): """ Scan the namespaces in the cluster and attribute them tenant_id. If no annotation or label named 'tenant' exist, tenant will be default. :api (client.CoreV1Api) The api client to use to execute the request. """ try: namespace_list = api.list_namespace() except ApiException as exc: raise exc for namespace_obj in namespace_list.items: update_namespace_tenant( namespace_obj.to_dict()['metadata'] )
def to_kubesecret(self, client: CoreV1Api) -> Generator[Tuple[str, V1Secret], None, None]: secret = V1Secret( api_version="v1", data=self.data, kind="Secret", type=self.k8s_type, metadata=V1ObjectMeta(annotations=self.annotations, name=self.k8s_secret_name), ) ns_list = self.k8s_namespaces all_namespaces = [ns.metadata.name for ns in client.list_namespace().items] # Deal with "all namespaces" if "*" in ns_list: ns_list = all_namespaces # TODO handle namespace globs for ns in ns_list: yield ns, secret
def get(client: CoreV1Api, log: BoundLogger, namespace: V1Namespace) -> Optional[V1Namespace]: return common_k8s.get_resource(lambda: client.list_namespace(), log, 'namespace', namespace.metadata.name)