HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: //snap/google-cloud-cli/current/lib/googlecloudsdk/command_lib/run/printers/revision_printer.py
# -*- coding: utf-8 -*- #
# Copyright 2020 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Revision-specific printer."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from googlecloudsdk.api_lib.run import container_resource
from googlecloudsdk.api_lib.run import revision
from googlecloudsdk.command_lib.run.printers import container_and_volume_printer_util as container_util
from googlecloudsdk.command_lib.run.printers import k8s_object_printer_util as k8s_util
from googlecloudsdk.core.resource import custom_printer_base as cp

REVISION_PRINTER_FORMAT = 'revision'
CPU_ALWAYS_ALLOCATED_MESSAGE = 'CPU is always allocated'
CPU_THROTTLED_MESSAGE = 'CPU is only allocated during request processing'
HTTP2_PORT_NAME = 'h2c'
EXECUTION_ENV_VALS = {'gen1': 'First Generation', 'gen2': 'Second Generation'}


class RevisionPrinter(cp.CustomPrinterBase):
  """Prints the run Revision in a custom human-readable format.

  Format specific to Cloud Run revisions. Only available on Cloud Run commands
  that print revisions.
  """

  def Transform(self, record):
    """Transform a service into the output structure of marker classes."""
    fmt = cp.Lines([
        k8s_util.BuildHeader(record),
        k8s_util.GetLabels(record.labels),
        ' ',
        self.TransformSpec(record),
        k8s_util.FormatReadyMessage(record),
        RevisionPrinter.CurrentMinInstances(record),
    ])
    return fmt

  @staticmethod
  def GetTimeout(record):
    if record.timeout is not None:
      return '{}s'.format(record.timeout)
    return None

  @staticmethod
  def GetMinInstances(record):
    return record.annotations.get(revision.MIN_SCALE_ANNOTATION, '')

  @staticmethod
  def GetMaxInstances(record):
    return record.annotations.get(revision.MAX_SCALE_ANNOTATION, '')

  @staticmethod
  def GetCMEK(record):
    cmek_key = record.annotations.get(container_resource.CMEK_KEY_ANNOTATION)
    if not cmek_key:
      return ''
    cmek_name = cmek_key.split('/')[-1]
    return cmek_name

  @staticmethod
  def GetCpuAllocation(record):
    cpu_throttled = record.annotations.get(
        container_resource.CPU_THROTTLE_ANNOTATION
    )
    if not cpu_throttled:
      return ''
    elif cpu_throttled.lower() == 'false':
      return CPU_ALWAYS_ALLOCATED_MESSAGE
    else:
      return CPU_THROTTLED_MESSAGE

  @staticmethod
  def GetHttp2Enabled(record):
    for port in record.container.ports:
      if port.name == HTTP2_PORT_NAME:
        return 'Enabled'
    return ''

  @staticmethod
  def GetExecutionEnv(record):
    execution_env_value = k8s_util.GetExecutionEnvironment(record)
    if execution_env_value in EXECUTION_ENV_VALS:
      return EXECUTION_ENV_VALS[execution_env_value]
    return execution_env_value

  @staticmethod
  def GetSessionAffinity(record):
    return record.annotations.get(revision.SESSION_AFFINITY_ANNOTATION, '')

  @staticmethod
  def GetThreatDetectionEnabled(record):
    return k8s_util.GetThreatDetectionEnabled(record)

  @staticmethod
  def TransformSpec(
      record: revision.Revision, manual_scaling_enabled=False
  ) -> cp.Lines:
    labels = [
        ('Service account', record.spec.serviceAccountName),
        ('Concurrency', record.concurrency)]
    if not manual_scaling_enabled:
      labels.extend([
          ('Min instances', RevisionPrinter.GetMinInstances(record)),
          ('Max instances', RevisionPrinter.GetMaxInstances(record)),
      ])
    labels.extend([
        (
            'SQL connections',
            k8s_util.GetCloudSqlInstances(record.annotations),
        ),
        ('Timeout', RevisionPrinter.GetTimeout(record)),
        ('VPC access', k8s_util.GetVpcNetwork(record.annotations)),
        ('CMEK', RevisionPrinter.GetCMEK(record)),
        ('HTTP/2 Enabled', RevisionPrinter.GetHttp2Enabled(record)),
        ('CPU Allocation', RevisionPrinter.GetCpuAllocation(record)),
        (
            'Execution Environment',
            RevisionPrinter.GetExecutionEnv(record),
        ),
        (
            'Session Affinity',
            RevisionPrinter.GetSessionAffinity(record),
        ),
        ('Volumes', container_util.GetVolumes(record)),
        ('Threat Detection', RevisionPrinter.GetThreatDetectionEnabled(record))
    ])
    return cp.Lines([container_util.GetContainers(record), cp.Labeled(labels)])

  @staticmethod
  def CurrentMinInstances(record):
    return cp.Labeled([
        (
            'Current Min Instances',
            getattr(record.status, 'desiredReplicas', None),
        ),
    ])