def GetConnectionContext(args):
    """Gets the regional, kubeconfig, or GKE connection context.

  Args:
    args: Namespace, the args namespace.

  Raises:
    ArgumentError if region or cluster is not specified.

  Returns:
    A GKE or regional ConnectionInfo object.
  """
    if flags.IsKubernetes(args):
        kubeconfig = flags.GetKubeconfig(args)
        return _KubeconfigConnectionContext(kubeconfig, args.context)

    if flags.IsGKE(args):
        cluster_ref = args.CONCEPTS.cluster.Parse()
        if not cluster_ref:
            raise flags.ArgumentError(
                'You must specify a cluster in a given location. '
                'Either use the `--cluster` and `--cluster-location` flags '
                'or set the run/cluster and run/cluster_location properties.')
        return _GKEConnectionContext(cluster_ref)

    if flags.IsManaged(args):
        region = flags.GetRegion(args, prompt=True)
        if not region:
            raise flags.ArgumentError(
                'You must specify a region. Either use the `--region` flag '
                'or set the run/region property.')
        return _RegionalConnectionContext(region)
Exemple #2
0
    def _Prompt(self, parsed_args):
        """Fallthrough to reading the cluster location from an interactive prompt.

    Only prompt for cluster location if the user-specified platform is GKE
    and if 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.run.cluster.Get())
        if flags.IsGKE(parsed_args) and cluster_name:
            clusters = [
                c for c in global_methods.ListClusters()
                if c.name == cluster_name
            ]
            if not clusters:
                raise exceptions.ConfigurationError(
                    'No cluster locations found for cluster [{}]. '
                    'Ensure your clusters have Cloud Run on GKE 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 run/cluster_location {}`.\n'.format(
                    location))
            return location
Exemple #3
0
  def _Prompt(self, parsed_args):
    """Fallthrough to reading the cluster name from an interactive prompt.

    Only prompt for cluster name if the user-specified platform is GKE.

    Args:
      parsed_args: Namespace, the args namespace.

    Returns:
      A cluster name string
    """
    if flags.IsGKE(parsed_args):
      cluster_location = (
          getattr(parsed_args, 'cluster_location', None) or
          properties.VALUES.run.cluster_location.Get())
      cluster_location_msg = ' in [{}]'.format(
          cluster_location) if cluster_location else ''

      clusters = global_methods.ListClusters(cluster_location)
      if not clusters:
        raise exceptions.ConfigurationError(
            'No compatible clusters found{}. '
            'Ensure your cluster has Cloud Run on GKE enabled.'.format(
                cluster_location_msg))

      def _GetClusterDescription(cluster):
        """Description of cluster for prompt."""
        if cluster_location:
          return cluster.name
        return '{} in {}'.format(cluster.name, cluster.zone)

      cluster_descs = [_GetClusterDescription(c) for c in clusters]

      idx = console_io.PromptChoice(
          cluster_descs,
          message='GKE cluster{}:'.format(cluster_location_msg),
          cancel_option=True)
      cluster = clusters[idx]

      if cluster_location:
        cluster_result = cluster.name
        location_help_text = ''
      else:
        cluster_ref = flags.GetClusterRef(cluster)
        cluster_result = cluster_ref.SelfLink()
        location_help_text = (
            ' && gcloud config set run/cluster_location {}'.format(
                cluster.zone))
      log.status.Print(
          'To make this the default cluster, run '
          '`gcloud config set run/cluster {cluster}'
          '{location}`.\n'.format(
              cluster=cluster.name,
              location=location_help_text))
      return cluster_result
Exemple #4
0
 def _Call(self, parsed_args):
     if flags.IsGKE(parsed_args) or flags.IsKubernetes(parsed_args):
         return 'default'
     elif not (getattr(parsed_args, 'project', None)
               or properties.VALUES.core.project.Get()):
         # HACK: Compensate for how "namespace" is actually "project" in Cloud Run
         # by providing an error message explicitly early here.
         raise flags.ArgumentError(
             'The [project] resource is not properly specified. '
             'Please specify the argument [--project] on the command line or '
             'set the property [core/project].')
     return None
Exemple #5
0
 def Run(self, args):
     """List available services."""
     if args.uri:
         raise flags.ArgumentError(
             '--uri flag is not supported for this resource')
     if not flags.IsGKE(args) and not getattr(args, 'region', None):
         locations_ref = args.CONCEPTS.region.Parse()
         return global_methods.ListServices(locations_ref.RelativeName())
     else:
         conn_context = connection_context.GetConnectionContext(args)
         namespace_ref = args.CONCEPTS.namespace.Parse()
         with serverless_operations.Connect(conn_context) as client:
             return client.ListServices(namespace_ref)
def GetConnectionContext(args):
    """Gets the regional or GKE connection context.

  Args:
    args: Namespace, the args namespace.

  Raises:
    ConfigurationError if cluster is specified without a location.

  Returns:
    A GKE or regional ConnectionInfo object.
  """

    if flags.IsGKE(args):
        cluster_ref = args.CONCEPTS.cluster.Parse()
        return _GKEConnectionContext(cluster_ref)

    flags.ValidateClusterArgs(args)
    region = flags.GetRegion(args, prompt=True)
    if not region:
        raise flags.ArgumentError(
            'You must specify either a cluster or a region.')
    return _RegionalConnectionContext(region)
def GetConnectionContext(args, track=base.ReleaseTrack.BETA):
    """Gets the regional, kubeconfig, or GKE connection context.

  Args:
    args: Namespace, the args namespace.
    track: ReleaseTrack, the release track.

  Raises:
    ArgumentError if region or cluster is not specified.

  Returns:
    A GKE or regional ConnectionInfo object.
  """
    if flags.IsKubernetes(args):
        kubeconfig = flags.GetKubeconfig(args)
        return _KubeconfigConnectionContext(kubeconfig, args.context)

    if flags.IsGKE(args):
        cluster_ref = args.CONCEPTS.cluster.Parse()
        if not cluster_ref:
            raise flags.ArgumentError(
                'You must specify a cluster in a given location. '
                'Either use the `--cluster` and `--cluster-location` flags '
                'or set the run/cluster and run/cluster_location properties.')
        return _GKEConnectionContext(cluster_ref)

    if flags.IsManaged(args):
        region = flags.GetRegion(args, prompt=True)
        if not region:
            raise flags.ArgumentError(
                'You must specify a region. Either use the `--region` flag '
                'or set the run/region property.')
        if track == base.ReleaseTrack.ALPHA:
            version = 'v1'
        else:
            version = global_methods.SERVERLESS_API_VERSION
        return _RegionalConnectionContext(region, version)