def AddServiceAccountSecret(configs):
  """Add a service account secret and mounts to kubernetes configs.

  Args:
    configs: List of kubernetes yaml configurations as dictionaries.
  """
  pods = (config for config in configs if config['kind'] == 'Pod')
  for pod in pods:
    volumes = yaml_helper.GetOrCreate(pod, ('spec', 'volumes'), list)
    _AddSecretVolume(volumes, _CREDENTIAL_SECRET_NAME)
    for container in yaml_helper.GetOrCreate(pod, ('spec', 'containers'), list):
      mounts = yaml_helper.GetOrCreate(container, ('volumeMounts',), list)
      _AddVolumeMount(mounts, _CREDENTIAL_SECRET_NAME)
      envs = yaml_helper.GetOrCreate(container, ('env',), list)
      _AddSecretEnvVar(envs, _CREDENTIAL_SECRET_NAME)
示例#2
0
  def SkaffoldConfig(self, kubernetes_file_path):
    """Create a skaffold yaml file.

    Args:
      kubernetes_file_path: Path to the kubernetes config file.

    Returns:
      Text of the skaffold yaml file.
    """
    skaffold_yaml_text = _SKAFFOLD_TEMPLATE.format(
        image_name=self._settings.image_name,
        context_path=self._settings.build_context_directory or
        os.path.dirname(self._settings.dockerfile) or '.')
    skaffold_yaml = yaml.load(skaffold_yaml_text)
    manifests = yaml_helper.GetOrCreate(
        skaffold_yaml, ('deploy', 'kubectl', 'manifests'), constructor=list)
    manifests.append(kubernetes_file_path)
    artifact = {'image': self._settings.image_name}
    if self._settings.builder:
      artifact['buildpack'] = {'builder': self._settings.builder}
    else:
      artifact['context'] = (
          self._settings.build_context_directory or
          os.path.dirname(self._settings.dockerfile) or '.')
    artifacts = yaml_helper.GetOrCreate(
        skaffold_yaml, ('build', 'artifacts'), constructor=list)
    artifacts.append(artifact)

    if self._settings.local_port:
      skaffold_yaml['portForward'] = [{
          'resourceType': 'service',
          'resourceName': self._settings.service_name,
          'port': 8080,
          'localPort': self._settings.local_port
      }]

    return yaml.dump(skaffold_yaml)
示例#3
0
  def Run(self, args):
    project_name = properties.VALUES.core.project.Get(required=True)

    if not args.IsSpecified('service_name'):
      dir_name = os.path.basename(
          os.path.dirname(os.path.join(files.GetCWD(), args.dockerfile)))
      service_name = console_io.PromptWithDefault(
          message='Service name', default=dir_name)
    else:
      service_name = args.service_name

    if not args.IsSpecified('image_name'):
      default_image_name = 'gcr.io/{project}/{service}'.format(
          project=project_name, service=service_name)
      image_name = console_io.PromptWithDefault(
          message='Docker image tag', default=default_image_name)
    else:
      image_name = args.image_name

    kubernetes_yaml_paths = []
    kubernetes_configs = local.CreatePodAndService(service_name, image_name)
    if args.service_account:
      service_account = local.CreateDevelopmentServiceAccount(
          args.service_account)
      private_key_json = local.CreateServiceAccountKey(service_account)
      secret_yaml = local.LocalDevelopmentSecretSpec(private_key_json)
      kubernetes_configs.append(secret_yaml)
      local.AddServiceAccountSecret(kubernetes_configs)

    with files.FileWriter(args.kubernetes_file) as output:
      yaml.dump_all(kubernetes_configs, output)
    kubernetes_yaml_paths.append(args.kubernetes_file)

    skaffold_yaml_text = _SKAFFOLD_TEMPLATE.format(
        image_name=image_name,
        context_path=args.build_context_directory or
        os.path.dirname(args.dockerfile) or '.')
    skaffold_yaml = yaml.load(skaffold_yaml_text)
    manifests = yaml_helper.GetOrCreate(
        skaffold_yaml, ('deploy', 'kubectl', 'manifests'), constructor=list)
    manifests.extend(kubernetes_yaml_paths)

    with files.FileWriter(args.skaffold_file) as output:
      yaml.dump(skaffold_yaml, output)
  def SkaffoldConfig(self, kubernetes_file_path):
    """Create a skaffold yaml file.

    Args:
      kubernetes_file_path: Path to the kubernetes config file.

    Returns:
      Text of the skaffold yaml file.
    """
    skaffold_yaml_text = _SKAFFOLD_TEMPLATE.format(
        image_name=self._image_name,
        context_path=self._build_context_directory or
        os.path.dirname(self._dockerfile) or '.')
    skaffold_yaml = yaml.load(skaffold_yaml_text)
    manifests = yaml_helper.GetOrCreate(
        skaffold_yaml, ('deploy', 'kubectl', 'manifests'), constructor=list)
    manifests.append(kubernetes_file_path)

    return yaml.dump(skaffold_yaml)