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))
Example #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))
Example #3
0
    def ReleaseService(self,
                       service_ref,
                       config_changes,
                       tracker=None,
                       asyn=False,
                       private_endpoint=None):
        """Change the given service in prod using the given config_changes.

    Ensures a new revision is always created, even if the spec of the revision
    has not changed.

    Arguments:
      service_ref: Resource, the service to release
      config_changes: list, objects that implement AdjustConfiguration().
      tracker: StagedProgressTracker, to report on the progress of releasing.
      asyn: bool, if True, release asyncronously
      private_endpoint:
    """
        if tracker is None:
            tracker = progress_tracker.NoOpStagedProgressTracker(
                stages.ServiceStages(),
                interruptable=True,
                aborted_message='aborted')
        with_code = any(
            isinstance(c, deployable_pkg.Deployable) for c in config_changes)
        self._UpdateOrCreateService(service_ref, config_changes, with_code,
                                    private_endpoint)
        if not asyn:
            getter = functools.partial(self.GetService, service_ref)
            self.WaitForCondition(ServiceConditionPoller(getter, tracker))
  def ReleaseService(self, service_ref, config_changes, tracker=None,
                     asyn=False, private_endpoint=None,
                     allow_unauthenticated=False):
    """Change the given service in prod using the given config_changes.

    Ensures a new revision is always created, even if the spec of the revision
    has not changed.

    Arguments:
      service_ref: Resource, the service to release
      config_changes: list, objects that implement AdjustConfiguration().
      tracker: StagedProgressTracker, to report on the progress of releasing.
      asyn: bool, if True, release asyncronously
      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.
      allow_unauthenticated: bool, True if creating a hosted Cloud Run
        service which should also have its IAM policy set to allow
        unauthenticated access.
    """
    if tracker is None:
      tracker = progress_tracker.NoOpStagedProgressTracker(
          stages.ServiceStages(), interruptable=True, aborted_message='aborted')
    with_code = any(
        isinstance(c, deployable_pkg.Deployable) for c in config_changes)
    self._UpdateOrCreateService(
        service_ref, config_changes, with_code, private_endpoint)
    if allow_unauthenticated:
      self.AddIamPolicyBinding(service_ref, ['allUsers'], 'roles/run.invoker')
    if not asyn:
      getter = functools.partial(self.GetService, service_ref)
      self.WaitForCondition(ServiceConditionPoller(getter, tracker))
    def ReleaseService(self,
                       service_ref,
                       config_changes,
                       tracker=None,
                       asyn=False,
                       allow_unauthenticated=None):
        """Change the given service in prod using the given config_changes.

    Ensures a new revision is always created, even if the spec of the revision
    has not changed.

    Arguments:
      service_ref: Resource, the service to release
      config_changes: list, objects that implement Adjust().
      tracker: StagedProgressTracker, to report on the progress of releasing.
      asyn: bool, if True, release asyncronously
      allow_unauthenticated: bool, True if creating a hosted Cloud Run
        service which should also have its IAM policy set to allow
        unauthenticated access. False if removing the IAM policy to allow
        unauthenticated access from a service.
    """
        if tracker is None:
            tracker = progress_tracker.NoOpStagedProgressTracker(
                stages.ServiceStages(allow_unauthenticated is not None),
                interruptable=True,
                aborted_message='aborted')
        with_image = any(
            isinstance(c, config_changes_mod.ImageChange)
            for c in config_changes)
        self._UpdateOrCreateService(service_ref, config_changes, with_image)

        if allow_unauthenticated is not None:
            try:
                tracker.StartStage(stages.SERVICE_IAM_POLICY_SET)
                tracker.UpdateStage(stages.SERVICE_IAM_POLICY_SET, '')
                self.AddOrRemoveIamPolicyBinding(
                    service_ref, allow_unauthenticated,
                    ALLOW_UNAUTH_POLICY_BINDING_MEMBERS,
                    ALLOW_UNAUTH_POLICY_BINDING_ROLE)
                tracker.CompleteStage(stages.SERVICE_IAM_POLICY_SET)
            except api_exceptions.HttpError:
                warning_message = (
                    'Setting IAM policy failed, try "gcloud beta run services '
                    '{}-iam-policy-binding --region={region} --member=allUsers '
                    '--role=roles/run.invoker {service}"'.format(
                        'add' if allow_unauthenticated else 'remove',
                        region=self._region,
                        service=service_ref.servicesId))
                tracker.CompleteStageWithWarning(
                    stages.SERVICE_IAM_POLICY_SET,
                    warning_message=warning_message)

        if not asyn:
            getter = functools.partial(self.GetService, service_ref)
            poller = ServiceConditionPoller(getter, tracker)
            self.WaitForCondition(poller)
            for msg in run_condition.GetNonTerminalMessages(
                    poller.GetConditions()):
                tracker.AddWarning(msg)
    def ReleaseService(self,
                       service_ref,
                       config_changes,
                       tracker=None,
                       asyn=False,
                       private_endpoint=None,
                       allow_unauthenticated=False):
        """Change the given service in prod using the given config_changes.

    Ensures a new revision is always created, even if the spec of the revision
    has not changed.

    Arguments:
      service_ref: Resource, the service to release
      config_changes: list, objects that implement AdjustConfiguration().
      tracker: StagedProgressTracker, to report on the progress of releasing.
      asyn: bool, if True, release asyncronously
      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.
      allow_unauthenticated: bool, True if creating a hosted Cloud Run
        service which should also have its IAM policy set to allow
        unauthenticated access.
    """
        if tracker is None:
            tracker = progress_tracker.NoOpStagedProgressTracker(
                stages.ServiceStages(allow_unauthenticated),
                interruptable=True,
                aborted_message='aborted')
        with_image = any(
            isinstance(c, config_changes_mod.ImageChange)
            for c in config_changes)
        self._UpdateOrCreateService(service_ref, config_changes, with_image,
                                    private_endpoint)

        if allow_unauthenticated:
            try:
                tracker.StartStage(stages.SERVICE_IAM_POLICY_SET)
                tracker.UpdateStage(stages.SERVICE_IAM_POLICY_SET, '')
                self.AddIamPolicyBinding(service_ref, ['allUsers'],
                                         'roles/run.invoker')
                tracker.CompleteStage(stages.SERVICE_IAM_POLICY_SET)
            except api_exceptions.HttpError:
                warning_message = (
                    'Setting IAM policy failed, try "gcloud beta run services '
                    'add-iam-policy-binding --region=%s --member=allUsers '
                    '--role=roles/run.invoker %s"') % (self._region,
                                                       service_ref.servicesId)
                tracker.CompleteStageWithWarning(
                    stages.SERVICE_IAM_POLICY_SET,
                    warning_message=warning_message)

        if not asyn:
            getter = functools.partial(self.GetService, service_ref)
            self.WaitForCondition(ServiceConditionPoller(getter, tracker))
  def ReleaseService(self,
                     service_ref,
                     config_changes,
                     tracker=None,
                     asyn=False,
                     allow_unauthenticated=None,
                     for_replace=False,
                     prefetch=False,
                     build_op_ref=None,
                     build_log_url=None):
    """Change the given service in prod using the given config_changes.

    Ensures a new revision is always created, even if the spec of the revision
    has not changed.

    Arguments:
      service_ref: Resource, the service to release.
      config_changes: list, objects that implement Adjust().
      tracker: StagedProgressTracker, to report on the progress of releasing.
      asyn: bool, if True, return without waiting for the service to be updated.
      allow_unauthenticated: bool, True if creating a hosted Cloud Run
        service which should also have its IAM policy set to allow
        unauthenticated access. False if removing the IAM policy to allow
        unauthenticated access from a service.
      for_replace: bool, If the change is for a replacing the service from a
        YAML specification.
      prefetch: the service, pre-fetched for ReleaseService. `False` indicates
        the caller did not perform a prefetch; `None` indicates a nonexistant
        service.
      build_op_ref: The reference to the build.
      build_log_url: The log url of the build result.
    """
    if tracker is None:
      tracker = progress_tracker.NoOpStagedProgressTracker(
          stages.ServiceStages(allow_unauthenticated is not None),
          interruptable=True, aborted_message='aborted')
    if build_op_ref is not None:
      tracker.StartStage(stages.BUILD_READY)
      tracker.UpdateHeaderMessage('Building Container.')
      tracker.UpdateStage(
          stages.BUILD_READY, 'Logs are available at [{build_log_url}].'.format(
              build_log_url=build_log_url))
      client = cloudbuild_util.GetClientInstance()
      poller = waiter.CloudOperationPoller(client.projects_builds,
                                           client.operations)
      operation = waiter.PollUntilDone(poller, build_op_ref)
      response_dict = encoding.MessageToPyValue(operation.response)
      if response_dict and response_dict['status'] != 'SUCCESS':
        tracker.FailStage(
            stages.BUILD_READY, None,
            message='Container build failed and '
            'logs are available at [{build_log_url}].'.format(
                build_log_url=build_log_url))
        return
      else:
        tracker.CompleteStage(stages.BUILD_READY)
    if prefetch is None:
      serv = None
    else:
      serv = prefetch or self.GetService(service_ref)
    if for_replace:
      with_image = True
    else:
      with_image = any(
          isinstance(c, config_changes_mod.ImageChange) for c in config_changes)
      self._AddRevisionForcingChange(serv, config_changes)
      if serv and not with_image:
        # Avoid changing the running code by making the new revision by digest
        self._EnsureImageDigest(serv, config_changes)
    config_changes = [_SetClientNameAndVersion()] + config_changes

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

    if allow_unauthenticated is not None:
      try:
        tracker.StartStage(stages.SERVICE_IAM_POLICY_SET)
        tracker.UpdateStage(stages.SERVICE_IAM_POLICY_SET, '')
        self.AddOrRemoveIamPolicyBinding(service_ref, allow_unauthenticated,
                                         ALLOW_UNAUTH_POLICY_BINDING_MEMBER,
                                         ALLOW_UNAUTH_POLICY_BINDING_ROLE)
        tracker.CompleteStage(stages.SERVICE_IAM_POLICY_SET)
      except api_exceptions.HttpError:
        warning_message = (
            'Setting IAM policy failed, try "gcloud beta run services '
            '{}-iam-policy-binding --region={region} --member=allUsers '
            '--role=roles/run.invoker {service}"'.format(
                'add' if allow_unauthenticated else 'remove',
                region=self._region,
                service=service_ref.servicesId))
        tracker.CompleteStageWithWarning(
            stages.SERVICE_IAM_POLICY_SET, warning_message=warning_message)

    if not asyn:
      getter = functools.partial(self.GetService, service_ref)
      poller = ServiceConditionPoller(
          getter,
          tracker,
          dependencies=stages.ServiceDependencies(),
          serv=serv)
      self.WaitForCondition(poller)
      for msg in run_condition.GetNonTerminalMessages(poller.GetConditions()):
        tracker.AddWarning(msg)