예제 #1
0
def EventsConnectionContext(args):
  """Returns the appropriate cluster connection context based on args.

  Unless the user has configured cluster connectivity options, calling this
  will result in the user being prompted to select a GKE cluster.

  Args:
    args: A parsed argument context

  Returns:
    googlecloudsdk.command_lib.run.connection_context.ConnectionInfo

  Raises:
    flags.ConfigurationError when the user has not specified a cluster
    connection method and can't be prompted.
  """
  api_name = _CLUSTER_EVENTS_API_NAME
  api_version = _CLUSTER_EVENTS_API_VERSION

  connection = flags.ClusterConnectionMethod(args)
  if connection == flags.CONNECTION_KUBECONFIG:
    kubeconfig_path, context = flags.KubeconfigPathAndContext(args)
    kubeconfig = run_flags.GetKubeconfig(kubeconfig_path)
    return run_context.KubeconfigConnectionContext(
        kubeconfig, api_name, api_version, context)

  elif connection == flags.CONNECTION_GKE:
    cluster_ref = flags.ParseClusterRefOrPromptUser(args)
    return run_context.GKEConnectionContext(cluster_ref, api_name, api_version)

  else:
    raise exceptions.Error('Unable to determine cluster connection method')
예제 #2
0
    def _Prompt(self, parsed_args):
        """Fallthrough to reading the cluster name from an interactive prompt.

    Only prompt for cluster name if the user has not opted to connect to the
    cluster via kubeconfig.

    Args:
      parsed_args: Namespace, the args namespace.

    Returns:
      A cluster name string
    """
        if flags.ClusterConnectionMethod(parsed_args) != flags.CONNECTION_GKE:
            return

        project = properties.VALUES.core.project.Get(required=True)
        cluster_location = (getattr(parsed_args, 'cluster_location', None) or
                            properties.VALUES.kuberun.cluster_location.Get())
        cluster_location_msg = ' in [{}]'.format(
            cluster_location) if cluster_location else ''

        cluster_refs = global_methods.MultiTenantClustersForProject(
            project, cluster_location)
        if not cluster_refs:
            raise flags.ConfigurationError(
                'No compatible clusters found{}. '
                'Ensure your cluster has Cloud Run enabled.'.format(
                    cluster_location_msg))

        cluster_refs_descs = [
            self._GetClusterDescription(c, cluster_location, project)
            for c in cluster_refs
        ]
        idx = console_io.PromptChoice(
            cluster_refs_descs,
            message='GKE cluster{}:'.format(cluster_location_msg),
            cancel_option=True)

        cluster_ref = cluster_refs[idx]

        if cluster_location:
            location_help_text = ''
        else:
            location_help_text = (
                ' && gcloud config set kuberun/cluster_location {}'.format(
                    cluster_ref.zone))

        cluster_name = cluster_ref.Name()

        if cluster_ref.projectId != project:
            cluster_name = cluster_ref.RelativeName()
            location_help_text = ''

        log.status.Print('To make this the default cluster, run '
                         '`gcloud config set kuberun/cluster {cluster}'
                         '{location}`.\n'.format(cluster=cluster_name,
                                                 location=location_help_text))
        return cluster_ref.SelfLink()
예제 #3
0
    def _Prompt(self, parsed_args):
        """Fallthrough to reading the cluster location from an interactive prompt.

    Only prompt for cluster location if the user has not opted to connect to the
    cluster via kubeconfig and a cluster name is already defined.

    Args:
      parsed_args: Namespace, the args namespace.

    Returns:
      A cluster location string
    """
        cluster_name = (getattr(parsed_args, 'cluster', None)
                        or properties.VALUES.kuberun.cluster.Get())
        if (flags.ClusterConnectionMethod(parsed_args) == flags.CONNECTION_GKE
                and cluster_name):
            clusters = [
                c for c in global_methods.ListClusters()
                if c.name == cluster_name
            ]
            if not clusters:
                raise flags.ConfigurationError(
                    'No cluster locations found for cluster [{}]. '
                    'Ensure your clusters have Cloud Run enabled.'.format(
                        cluster_name))
            cluster_locations = [c.zone for c in clusters]
            idx = console_io.PromptChoice(
                cluster_locations,
                message='GKE cluster location for [{}]:'.format(cluster_name),
                cancel_option=True)
            location = cluster_locations[idx]
            log.status.Print(
                'To make this the default cluster location, run '
                '`gcloud config set kuberun/cluster_location {}`.\n'.format(
                    location))
            return location