def KubernetesConfig(self):
    """Create a kubernetes config file.

    Returns:
      Text of a kubernetes config file.
    """
    if self._settings.cpu:
      if isinstance(self._settings.cpu, six.text_type):
        if not self._settings.cpu.endswith('m'):
          raise ValueError('cpu limit must be defined as an integer or as '
                           'millicpus')
        user_cpu = int(self._settings.cpu[:-1]) / 1000.0
      else:
        user_cpu = self._settings.cpu
      cpu_request = min(0.1, user_cpu)
    else:
      cpu_request = None

    code_generators = [
        local.AppContainerGenerator(
            self._settings.service_name, self._settings.image,
            self._settings.env_vars, self._settings.env_vars_secrets,
            self._settings.memory, self._settings.cpu, cpu_request,
            self._settings.readiness_probe),
        local.SecretsGenerator(self._settings.service_name,
                               self._settings.env_vars_secrets,
                               self._settings.volumes_secrets,
                               self._settings.namespace,
                               self._settings.allow_secret_manager)
    ]

    credential_generator = None
    if isinstance(self._settings.credential, local.ServiceAccountSetting):
      credential_generator = local.CredentialGenerator(
          functools.partial(local.GetServiceAccountSecret,
                            self._settings.credential.name))
      code_generators.append(credential_generator)
    elif isinstance(self._settings.credential,
                    local.ApplicationDefaultCredentialSetting):
      credential_generator = local.CredentialGenerator(local.GetUserCredential)
      code_generators.append(credential_generator)

    if self._settings.cloudsql_instances:
      if not credential_generator:
        raise ValueError('A credential generator must be defined when cloudsql '
                         'instances are defined.')
      cloudsql_proxy = local.CloudSqlProxyGenerator(
          self._settings.cloudsql_instances, credential_generator.GetInfo())
      code_generators.append(cloudsql_proxy)

    return _GenerateKubeConfigs(code_generators)
Example #2
0
    def KubernetesConfig(self):
        """Create a kubernetes config file.

    Returns:
      Text of a kubernetes config file.
    """
        if self._settings.cpu:
            cpu_request = min(0.1, self._settings.cpu)
        else:
            cpu_request = None

        code_generators = [
            local.AppContainerGenerator(self._settings.service_name,
                                        self._settings.image,
                                        self._settings.env_vars,
                                        self._settings.memory,
                                        self._settings.cpu, cpu_request,
                                        self._settings.readiness_probe)
        ]

        credential_generator = None
        if isinstance(self._settings.credential, local.ServiceAccountSetting):
            credential_generator = local.CredentialGenerator(
                functools.partial(local.GetServiceAccountSecret,
                                  self._settings.credential.name))
            code_generators.append(credential_generator)
        elif isinstance(self._settings.credential,
                        local.ApplicationDefaultCredentialSetting):
            credential_generator = local.CredentialGenerator(
                local.GetUserCredential)
            code_generators.append(credential_generator)

        if self._settings.cloudsql_instances:
            if not credential_generator:
                raise ValueError(
                    'A credential generator must be defined when cloudsql '
                    'instances are defined.')
            cloudsql_proxy = local.CloudSqlProxyGenerator(
                self._settings.cloudsql_instances,
                credential_generator.GetInfo())
            code_generators.append(cloudsql_proxy)

        return _GenerateKubeConfigs(code_generators)
Example #3
0
  def KubernetesConfig(self):
    """Create a kubernetes config file.

    Returns:
      Text of a kubernetes config file.
    """
    code_generators = [
        local.AppContainerGenerator(self._settings.service_name,
                                    self._settings.image_name,
                                    self._settings.env_vars)
    ]

    if self._settings.service_account:
      secret_generator = local.SecretGenerator(self._settings.service_account)
      code_generators.append(secret_generator)

    if self._settings.cloudsql_instances:
      cloudsql_proxy = local.CloudSqlProxyGenerator(
          self._settings.cloudsql_instances, secret_generator.GetInfo())
      code_generators.append(cloudsql_proxy)

    return _GenerateKubeConfigs(code_generators)
Example #4
0
    def testAddCloudSqlSidecar(self):
        yaml_text = textwrap.dedent("""\
    apiVersion: v1
    kind: Deployment
    metadata:
      name: my-service
    labels:
      service: my-service
    spec:
      template:
        spec:
          containers:
          - name: my-service-container
            image: image-name
            env:
            - name: PORT
              value: "8080"
            ports:
            - containerPort: 8080
    """)
        deployment = yaml.load(yaml_text)

        code_generator = local.CloudSqlProxyGenerator(
            ['my-sql-instance', 'your-sql-instance'], local.SecretInfo())
        code_generator.ModifyDeployment(deployment)
        code_generator.ModifyContainer(
            deployment['spec']['template']['spec']['containers'][0])

        expected_yaml_text = textwrap.dedent("""\
    apiVersion: v1
    kind: Deployment
    metadata:
      name: my-service
    labels:
      service: my-service
    spec:
      template:
        spec:
          containers:
          - name: my-service-container
            image: image-name
            env:
            - name: PORT
              value: "8080"
            ports:
            - containerPort: 8080
            volumeMounts:
            - name: cloudsql
              mountPath: /cloudsql
              readOnly: true
          - name: cloud-sql-proxy
            image: 'gcr.io/cloudsql-docker/gce-proxy:1.16'
            command:
            - '/cloud_sql_proxy'
            args:
            - '-dir=/cloudsql'
            - '-instances=my-sql-instance,your-sql-instance'
            - '-credential_file=/etc/local_development_credential/local_development_service_account.json'
            volumeMounts:
            - name: cloudsql
              mountPath: /cloudsql
          volumes:
          - name: cloudsql
            emptyDir: {}
    """)
        self.assertEqual(deployment, yaml.load(expected_yaml_text))