def SetTraffic(self, service_ref, config_changes, tracker, asyn,
                   is_managed):
        """Set traffic splits for service."""
        if tracker is None:
            tracker = progress_tracker.NoOpStagedProgressTracker(
                stages.ServiceStages(False),
                interruptable=True,
                abortde_message='aborted')
        serv = self.GetService(service_ref)
        if not serv:
            raise serverless_exceptions.ServiceNotFoundError(
                'Service [{}] could not be found.'.format(
                    service_ref.servicesId))

        if not serv.spec.template:
            if is_managed:
                raise serverless_exceptions.UnsupportedOperationError(
                    'Your provider does not support setting traffic for this service.'
                )
            else:
                raise serverless_exceptions.UnsupportedOperationError(
                    'You must upgrade your cluster to version 0.61 or greater '
                    'to set traffic.')
        self._UpdateOrCreateService(service_ref, config_changes, False, serv)

        if not asyn:
            getter = functools.partial(self.GetService, service_ref)
            self.WaitForCondition(ServiceConditionPoller(getter, tracker))
Ejemplo n.º 2
0
    def UpdateTraffic(self, service_ref, config_changes, tracker, asyn):
        """Update traffic splits for service."""
        if tracker is None:
            tracker = progress_tracker.NoOpStagedProgressTracker(
                stages.UpdateTrafficStages(),
                interruptable=True,
                aborted_message='aborted')
        serv = self.GetService(service_ref)
        if not serv:
            raise serverless_exceptions.ServiceNotFoundError(
                'Service [{}] could not be found.'.format(
                    service_ref.servicesId))

        if serv.configuration:
            raise serverless_exceptions.UnsupportedOperationError(
                'This service is using an old version of Cloud Run for Anthos '
                'that does not support traffic features. Please upgrade to 0.8 '
                'or later.')

        self._UpdateOrCreateService(service_ref, config_changes, False, serv)

        if not asyn:
            getter = functools.partial(self.GetService, service_ref)
            self.WaitForCondition(
                ServiceConditionPoller(getter, tracker, serv=serv))
    def CheckCloudRunServices(self, service_names):
        """Check for existence of Cloud Run services.

    Args:
      service_names: array, list of service to check.

    Raises:
      exceptions.ServiceNotFoundError: when a Cloud Run service doesn't exist.
    """
        conn_context = connection_context.GetConnectionContext(
            {'region', self._region}, run_flags.Product.RUN)
        with serverless_operations.Connect(conn_context) as client:
            for name in service_names:
                service_ref = self.GetServiceRef(name)
                service = client.GetService(service_ref)
                if not service:
                    raise exceptions.ServiceNotFoundError(
                        'Service [{}] could not be found.'.format(name))
Ejemplo n.º 4
0
    def DeleteService(self, service_ref):
        """Delete the provided Service.

    Args:
      service_ref: Resource, a reference to the Service to delete

    Raises:
      ServiceNotFoundError: if provided service is not found.
    """
        messages = self._messages_module
        service_name = service_ref.RelativeName()
        service_delete_request = messages.RunNamespacesServicesDeleteRequest(
            name=service_name, )

        try:
            with metrics.RecordDuration(metric_names.DELETE_SERVICE):
                self._client.namespaces_services.Delete(service_delete_request)
        except api_exceptions.HttpNotFoundError:
            raise serverless_exceptions.ServiceNotFoundError(
                'Service [{}] could not be found.'.format(
                    service_ref.servicesId))
Ejemplo n.º 5
0
    def _UpdateOrCreateService(self,
                               service_ref,
                               config_changes,
                               with_code,
                               private_endpoint=None):
        """Apply config_changes to the service. Create it if necessary.

    Arguments:
      service_ref: Reference to the service to create or update
      config_changes: list of ConfigChanger to modify the service with
      with_code: bool, True if the config_changes contains code to deploy.
        We can't create the service if we're not deploying code.
      private_endpoint: bool, True if creating a new Service for
        Cloud Run on GKE that should only be addressable from within the
        cluster. False if it should be publicly addressable. None if
        its existing visibility should remain unchanged.

    Returns:
      The Service object we created or modified.
    """
        nonce = _Nonce()
        config_changes = [_NewRevisionForcingChange(nonce)] + config_changes
        messages = self._messages_module
        # GET the Service
        serv = self.GetService(service_ref)
        try:
            if serv:
                if not with_code:
                    # Avoid changing the running code by making the new revision by digest
                    self._EnsureImageDigest(serv, config_changes)

                if private_endpoint is None:
                    # Don't change the existing service visibility
                    pass
                elif private_endpoint:
                    serv.labels[
                        service.ENDPOINT_VISIBILITY] = service.CLUSTER_LOCAL
                else:
                    del serv.labels[service.ENDPOINT_VISIBILITY]

                # PUT the changed Service
                for config_change in config_changes:
                    config_change.AdjustConfiguration(serv.configuration,
                                                      serv.metadata)
                serv_name = service_ref.RelativeName()
                serv_update_req = (
                    messages.RunNamespacesServicesReplaceServiceRequest(
                        service=serv.Message(), name=serv_name))
                with metrics.RecordDuration(metric_names.UPDATE_SERVICE):
                    updated = self._client.namespaces_services.ReplaceService(
                        serv_update_req)
                return service.Service(updated, messages)

            else:
                if not with_code:
                    raise serverless_exceptions.ServiceNotFoundError(
                        'Service [{}] could not be found.'.format(
                            service_ref.servicesId))
                # POST a new Service
                new_serv = service.Service.New(self._client,
                                               service_ref.namespacesId,
                                               private_endpoint)
                new_serv.name = service_ref.servicesId
                parent = service_ref.Parent().RelativeName()
                for config_change in config_changes:
                    config_change.AdjustConfiguration(new_serv.configuration,
                                                      new_serv.metadata)
                serv_create_req = (messages.RunNamespacesServicesCreateRequest(
                    service=new_serv.Message(), parent=parent))
                with metrics.RecordDuration(metric_names.CREATE_SERVICE):
                    raw_service = self._client.namespaces_services.Create(
                        serv_create_req)
                return service.Service(raw_service, messages)
        except api_exceptions.HttpBadRequestError as e:
            error_payload = exceptions_util.HttpErrorPayload(e)
            if error_payload.field_violations:
                if (serverless_exceptions.BadImageError.IMAGE_ERROR_FIELD
                        in error_payload.field_violations):
                    exceptions.reraise(serverless_exceptions.BadImageError(e))
            exceptions.reraise(e)
        except api_exceptions.HttpNotFoundError as e:
            error_msg = 'Deployment endpoint was not found.'
            if not self._region:
                all_clusters = global_methods.ListClusters()
                clusters = [
                    '* {} in {}'.format(c.name, c.zone) for c in all_clusters
                ]
                error_msg += (
                    ' Perhaps the provided cluster was invalid or '
                    'does not have Cloud Run enabled. Pass the '
                    '`--cluster` and `--cluster-location` flags or set the '
                    '`run/cluster` and `run/cluster_location` properties to '
                    'a valid cluster and zone and retry.'
                    '\nAvailable clusters:\n{}'.format('\n'.join(clusters)))
            else:
                all_regions = global_methods.ListRegions(self._op_client)
                if self._region not in all_regions:
                    regions = ['* {}'.format(r) for r in all_regions]
                    error_msg += (
                        ' The provided region was invalid. '
                        'Pass the `--region` flag or set the '
                        '`run/region` property to a valid region and retry.'
                        '\nAvailable regions:\n{}'.format('\n'.join(regions)))
            raise serverless_exceptions.DeploymentFailedError(error_msg)
  def _UpdateOrCreateService(
      self, service_ref, config_changes, with_code, serv):
    """Apply config_changes to the service. Create it if necessary.

    Arguments:
      service_ref: Reference to the service to create or update
      config_changes: list of ConfigChanger to modify the service with
      with_code: bool, True if the config_changes contains code to deploy.
        We can't create the service if we're not deploying code.
      serv: service.Service, For update the Service to update and for
        create None.

    Returns:
      The Service object we created or modified.
    """
    messages = self.messages_module
    try:
      if serv:
        # PUT the changed Service
        for config_change in config_changes:
          serv = config_change.Adjust(serv)
        serv_name = service_ref.RelativeName()
        serv_update_req = (
            messages.RunNamespacesServicesReplaceServiceRequest(
                service=serv.Message(),
                name=serv_name))
        with metrics.RecordDuration(metric_names.UPDATE_SERVICE):
          updated = self._client.namespaces_services.ReplaceService(
              serv_update_req)
        return service.Service(updated, messages)

      else:
        if not with_code:
          raise serverless_exceptions.ServiceNotFoundError(
              'Service [{}] could not be found.'.format(service_ref.servicesId))
        # POST a new Service
        new_serv = service.Service.New(self._client, service_ref.namespacesId)
        new_serv.name = service_ref.servicesId
        parent = service_ref.Parent().RelativeName()
        for config_change in config_changes:
          new_serv = config_change.Adjust(new_serv)
        serv_create_req = (
            messages.RunNamespacesServicesCreateRequest(
                service=new_serv.Message(),
                parent=parent))
        with metrics.RecordDuration(metric_names.CREATE_SERVICE):
          raw_service = self._client.namespaces_services.Create(
              serv_create_req)
        return service.Service(raw_service, messages)
    except api_exceptions.HttpBadRequestError as e:
      exceptions.reraise(serverless_exceptions.HttpError(e))
    except api_exceptions.HttpNotFoundError as e:
      platform = properties.VALUES.run.platform.Get()
      error_msg = 'Deployment endpoint was not found.'
      if platform == 'gke':
        all_clusters = global_methods.ListClusters()
        clusters = ['* {} in {}'.format(c.name, c.zone) for c in all_clusters]
        error_msg += (' Perhaps the provided cluster was invalid or '
                      'does not have Cloud Run enabled. Pass the '
                      '`--cluster` and `--cluster-location` flags or set the '
                      '`run/cluster` and `run/cluster_location` properties to '
                      'a valid cluster and zone and retry.'
                      '\nAvailable clusters:\n{}'.format('\n'.join(clusters)))
      elif platform == 'managed':
        all_regions = global_methods.ListRegions(self._op_client)
        if self._region not in all_regions:
          regions = ['* {}'.format(r) for r in all_regions]
          error_msg += (' The provided region was invalid. '
                        'Pass the `--region` flag or set the '
                        '`run/region` property to a valid region and retry.'
                        '\nAvailable regions:\n{}'.format('\n'.join(regions)))
      elif platform == 'kubernetes':
        error_msg += (' Perhaps the provided cluster was invalid or '
                      'does not have Cloud Run enabled. Ensure in your '
                      'kubeconfig file that the cluster referenced in '
                      'the current context or the specified context '
                      'is a valid cluster and retry.')
      raise serverless_exceptions.DeploymentFailedError(error_msg)
    except api_exceptions.HttpError as e:
      platform = properties.VALUES.run.platform.Get()
      if platform == 'managed':
        exceptions.reraise(e)
      k8s_error = serverless_exceptions.KubernetesExceptionParser(e)
      causes = '\n\n'.join([c['message'] for c in k8s_error.causes])
      if not causes:
        causes = k8s_error.error
      raise serverless_exceptions.KubernetesError('Error{}:\n{}\n'.format(
          's' if len(k8s_error.causes) > 1 else '', causes))
Ejemplo n.º 7
0
  def _UpdateOrCreateService(self, service_ref, config_changes, with_code,
                             private_endpoint=None):
    """Apply config_changes to the service. Create it if necessary.

    Arguments:
      service_ref: Reference to the service to create or update
      config_changes: list of ConfigChanger to modify the service with
      with_code: bool, True if the config_changes contains code to deploy.
        We can't create the service if we're not deploying code.
      private_endpoint: bool, True if creating a new Service for
        Cloud Run on GKE that should only be addressable from within the
        cluster. False if it should be publicly addressable. None if
        its existing visibility should remain unchanged.

    Returns:
      The Service object we created or modified.
    """
    nonce = _Nonce()
    config_changes = [_NewRevisionForcingChange(nonce)] + config_changes
    messages = self._messages_module
    # GET the Service
    serv = self.GetService(service_ref)
    try:
      if serv:
        if not with_code:
          # Avoid changing the running code by making the new revision by digest
          self._EnsureImageDigest(serv, config_changes)

        if private_endpoint is None:
          # Don't change the existing service visibility
          pass
        elif private_endpoint:
          serv.labels[service.ENDPOINT_VISIBILITY] = service.CLUSTER_LOCAL
        else:
          del serv.labels[service.ENDPOINT_VISIBILITY]

        # PUT the changed Service
        for config_change in config_changes:
          config_change.AdjustConfiguration(serv.configuration, serv.metadata)
        serv_name = service_ref.RelativeName()
        serv_update_req = (
            messages.RunNamespacesServicesReplaceServiceRequest(
                service=serv.Message(),
                name=serv_name))
        with metrics.record_duration(metrics.UPDATE_SERVICE):
          updated = self._client.namespaces_services.ReplaceService(
              serv_update_req)
        return service.Service(updated, messages)

      else:
        if not with_code:
          raise serverless_exceptions.ServiceNotFoundError(
              'Service [{}] could not be found.'.format(service_ref.servicesId))
        # POST a new Service
        new_serv = service.Service.New(self._client, service_ref.namespacesId,
                                       private_endpoint)
        new_serv.name = service_ref.servicesId
        pretty_print.Info('Creating new service [{bold}{service}{reset}]',
                          service=new_serv.name)
        parent = service_ref.Parent().RelativeName()
        for config_change in config_changes:
          config_change.AdjustConfiguration(new_serv.configuration,
                                            new_serv.metadata)
        serv_create_req = (
            messages.RunNamespacesServicesCreateRequest(
                service=new_serv.Message(),
                parent=parent))
        with metrics.record_duration(metrics.CREATE_SERVICE):
          raw_service = self._client.namespaces_services.Create(
              serv_create_req)
        return service.Service(raw_service, messages)
    except api_exceptions.HttpBadRequestError as e:
      error_payload = exceptions_util.HttpErrorPayload(e)
      if error_payload.field_violations:
        if (serverless_exceptions.BadImageError.IMAGE_ERROR_FIELD
            in error_payload.field_violations):
          exceptions.reraise(serverless_exceptions.BadImageError(e))
      exceptions.reraise(e)
    except api_exceptions.HttpNotFoundError as e:
      # TODO(b/118339293): List available regions to check whether provided
      # region is invalid or not.
      raise serverless_exceptions.DeploymentFailedError(
          'Deployment endpoint was not found. Perhaps the provided '
          'region was invalid. Set the `run/region` property to a valid '
          'region and retry. Ex: `gcloud config set run/region us-central1`')
    def _UpdateOrCreateService(self, service_ref, config_changes, with_code):
        """Apply config_changes to the service. Create it if necessary.

    Arguments:
      service_ref: Reference to the service to create or update
      config_changes: list of ConfigChanger to modify the service with
      with_code: boolean, True if the config_changes contains code to deploy.
        We can't create the service if we're not deploying code.

    Returns:
      The Service object we created or modified.
    """
        nonce = _Nonce()
        config_changes = [_NewRevisionForcingChange(nonce)] + config_changes
        messages = self._messages_module
        # GET the Service
        serv = self.GetService(service_ref)
        try:
            if serv:
                if not with_code:
                    # Avoid changing the running code by making the new revision by digest
                    self._EnsureImageDigest(serv, config_changes)
                # PUT the changed Service
                for config_change in config_changes:
                    config_change.AdjustConfiguration(serv.configuration,
                                                      serv.metadata)
                serv_name = service_ref.RelativeName()
                serv_update_req = (
                    messages.ServerlessNamespacesServicesReplaceServiceRequest(
                        service=serv.Message(), name=serv_name))
                with metrics.record_duration(metrics.UPDATE_SERVICE):
                    updated = self._client.namespaces_services.ReplaceService(
                        serv_update_req)
                return service.Service(updated, messages)

            else:
                if not with_code:
                    raise serverless_exceptions.ServiceNotFoundError(
                        'Service [{}] could not be found.'.format(
                            service_ref.servicesId))
                # POST a new Service
                new_serv = service.Service.New(self._client,
                                               service_ref.namespacesId)
                new_serv.name = service_ref.servicesId
                pretty_print.Info(
                    'Creating new service [{bold}{service}{reset}]',
                    service=new_serv.name)
                parent = service_ref.Parent().RelativeName()
                for config_change in config_changes:
                    config_change.AdjustConfiguration(new_serv.configuration,
                                                      new_serv.metadata)
                serv_create_req = (
                    messages.ServerlessNamespacesServicesCreateRequest(
                        service=new_serv.Message(), parent=parent))
                with metrics.record_duration(metrics.CREATE_SERVICE):
                    raw_service = self._client.namespaces_services.Create(
                        serv_create_req)
                return service.Service(raw_service, messages)
        except api_exceptions.HttpBadRequestError as e:
            error_payload = exceptions_util.HttpErrorPayload(e)
            if error_payload.field_violations:
                if (serverless_exceptions.BadImageError.IMAGE_ERROR_FIELD
                        in error_payload.field_violations):
                    exceptions.reraise(serverless_exceptions.BadImageError(e))
            exceptions.reraise(e)
    def _UpdateOrCreateService(self, service_ref, config_changes, with_code):
        """Apply config_changes to the service. Create it if necessary.

    Arguments:
      service_ref: Reference to the service to create or update
      config_changes: list of ConfigChanger to modify the service with
      with_code: bool, True if the config_changes contains code to deploy.
        We can't create the service if we're not deploying code.

    Returns:
      The Service object we created or modified.
    """
        nonce = _Nonce()
        config_changes = [
            _NewRevisionForcingChange(nonce),
            _SetClientNameAndVersion()
        ] + config_changes
        messages = self._messages_module
        # GET the Service
        serv = self.GetService(service_ref)
        try:
            if serv:
                if not with_code:
                    # Avoid changing the running code by making the new revision by digest
                    self._EnsureImageDigest(serv, config_changes)

                # Revision names must be unique across the namespace.
                # To prevent the revision name being unchanged from the last revision,
                # we reset the value so the default naming scheme will be used instead.
                serv.template.name = None

                # PUT the changed Service
                for config_change in config_changes:
                    config_change.Adjust(serv)
                serv_name = service_ref.RelativeName()
                serv_update_req = (
                    messages.RunNamespacesServicesReplaceServiceRequest(
                        service=serv.Message(), name=serv_name))
                with metrics.RecordDuration(metric_names.UPDATE_SERVICE):
                    updated = self._client.namespaces_services.ReplaceService(
                        serv_update_req)
                return service.Service(updated, messages)

            else:
                if not with_code:
                    raise serverless_exceptions.ServiceNotFoundError(
                        'Service [{}] could not be found.'.format(
                            service_ref.servicesId))
                # POST a new Service
                new_serv = service.Service.New(self._client,
                                               service_ref.namespacesId)
                new_serv.name = service_ref.servicesId
                parent = service_ref.Parent().RelativeName()
                for config_change in config_changes:
                    config_change.Adjust(new_serv)
                serv_create_req = (messages.RunNamespacesServicesCreateRequest(
                    service=new_serv.Message(), parent=parent))
                with metrics.RecordDuration(metric_names.CREATE_SERVICE):
                    raw_service = self._client.namespaces_services.Create(
                        serv_create_req)
                return service.Service(raw_service, messages)
        except api_exceptions.HttpBadRequestError as e:
            error_payload = exceptions_util.HttpErrorPayload(e)
            if error_payload.field_violations:
                if (serverless_exceptions.BadImageError.IMAGE_ERROR_FIELD
                        in error_payload.field_violations):
                    exceptions.reraise(serverless_exceptions.BadImageError(e))
                elif (serverless_exceptions.MalformedLabelError.
                      LABEL_ERROR_FIELD in error_payload.field_violations):
                    exceptions.reraise(
                        serverless_exceptions.MalformedLabelError(e))
            exceptions.reraise(e)
        except api_exceptions.HttpNotFoundError as e:
            platform = properties.VALUES.run.platform.Get()
            error_msg = 'Deployment endpoint was not found.'
            if platform == 'gke':
                all_clusters = global_methods.ListClusters()
                clusters = [
                    '* {} in {}'.format(c.name, c.zone) for c in all_clusters
                ]
                error_msg += (
                    ' Perhaps the provided cluster was invalid or '
                    'does not have Cloud Run enabled. Pass the '
                    '`--cluster` and `--cluster-location` flags or set the '
                    '`run/cluster` and `run/cluster_location` properties to '
                    'a valid cluster and zone and retry.'
                    '\nAvailable clusters:\n{}'.format('\n'.join(clusters)))
            elif platform == 'managed':
                all_regions = global_methods.ListRegions(self._op_client)
                if self._region not in all_regions:
                    regions = ['* {}'.format(r) for r in all_regions]
                    error_msg += (
                        ' The provided region was invalid. '
                        'Pass the `--region` flag or set the '
                        '`run/region` property to a valid region and retry.'
                        '\nAvailable regions:\n{}'.format('\n'.join(regions)))
            elif platform == 'kubernetes':
                error_msg += (
                    ' Perhaps the provided cluster was invalid or '
                    'does not have Cloud Run enabled. Ensure in your '
                    'kubeconfig file that the cluster referenced in '
                    'the current context or the specified context '
                    'is a valid cluster and retry.')
            raise serverless_exceptions.DeploymentFailedError(error_msg)
        except api_exceptions.HttpError as e:
            k8s_error = serverless_exceptions.KubernetesExceptionParser(e)
            raise serverless_exceptions.KubernetesError(
                'Error{}:\n{}\n'.format(
                    's' if len(k8s_error.causes) > 1 else '',
                    '\n\n'.join([c['message'] for c in k8s_error.causes])))