def discover_accessible_namespace()

in src/hyperpod_cli/service/discover_namespaces.py [0:0]


    def discover_accessible_namespace(self, resource_attributes_template, only_sm_managed=True):
        """
        Discover the accessible namespaces
        """
        k8s_client = KubernetesClient()
        context_namespace = k8s_client.get_current_context_namespace()

        # If the namespace is explicitly set by user, take the namespace from the config instead
        # of discovering automatically 
        if context_namespace is not None:
            return context_namespace
        
        try:
            if only_sm_managed:
                namespaces = GetNamespaces().get_sagemaker_managed_namespaces()
            else:
                namespaces = GetNamespaces().get_namespaces()

            discovered_namespaces = self.get_namespaces_by_checking_access_permission(
                namespaces,
                resource_attributes_template,
            )

            if len(discovered_namespaces) == 0:
                logger.error("Found no accessible namespaces. Please ask for cluster admin for assistance or specify value of namespace explicitly in the command.")
                sys.exit(1)
            if len(discovered_namespaces) > 1:
                logger.error(f"Found more than 1 accessible namespaces {discovered_namespaces}. Please specify value of namespace explicitly in the command.")
                sys.exit(1)
            
            if len(discovered_namespaces) == 1:
                logger.info(f"Found accessible namespace: {discovered_namespaces}")
            return discovered_namespaces[0]
        except ApiException as e:
            if e.status == 403:
                logger.error("Got access denied error when discovering accessible namespaces, please specify the '--namespace' parameter in the command and try again.")
                sys.exit(1)