def Poll(self, unused_ref): """Overrides. Args: unused_ref: A string representing the operation reference. Currently it must be 'deploy'. Returns: A condition.Conditions object. """ conditions = self.GetConditions() if conditions is None or not conditions.IsFresh(): return None for condition in conditions: message = conditions[condition]['message'] stage = _CONDITION_TO_STAGE[condition] status = conditions[condition]['status'] if message: self._tracker.UpdateStage(stage, message) if status is None: continue elif status and condition not in self._completed_stages: self._completed_stages.append(condition) self._tracker.CompleteStage(stage, message) elif not status and condition not in self._failed_stages: self._failed_stages.append(condition) self._tracker.FailStage( stage, serverless_exceptions.DeploymentFailedError(message), message) return conditions
def _PossiblyFailStage(self, condition, message): """Possibly fail the stage. Args: condition: str, The name of the status whose stage failed. message: str, The detailed message for the condition. Raises: DeploymentFailedError: If the 'Ready' condition failed. """ # Don't fail an already failed stage. if condition in self._failed_stages: return if condition == _READY_CONDITION: raise serverless_exceptions.DeploymentFailedError(message) stage = _CONDITION_TO_STAGE[condition] self._failed_stages.append(condition) self._tracker.FailStage( stage, serverless_exceptions.DeploymentFailedError(message), message)
def Poll(self, unused_ref): """Overrides. Args: unused_ref: A string representing the operation reference. Currently it must be 'deploy'. Returns: A condition.Conditions object. """ conditions = self.GetConditions() if conditions is None or not conditions.IsFresh(): return None ready_message = conditions.DescriptiveMessage() if ready_message: self._tracker.UpdateHeaderMessage(ready_message) for condition in conditions.TerminalSubconditions(): message = conditions[condition]['message'] status = conditions[condition]['status'] self._PossiblyUpdateMessage(condition, message, ready_message) if status is None: continue elif status: self._PossiblyCompleteStage(condition, message, conditions.IsReady()) else: self._PossiblyFailStage(condition, message) if conditions.IsReady(): self._tracker.UpdateHeaderMessage('Done.') # TODO(b/120679874): Should not have to manually call Tick() self._tracker.Tick() elif conditions.IsFailed(): raise serverless_exceptions.DeploymentFailedError(ready_message) return conditions
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))
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: 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])))