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)
Exemplo n.º 2
0
    def testAddSecret(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)
        credential_generator = local.CredentialGenerator(lambda: None)
        credential_generator.ModifyDeployment(deployment)
        credential_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"
            - name: GOOGLE_APPLICATION_CREDENTIALS
              value: /etc/local_development_credential/local_development_service_account.json
            ports:
            - containerPort: 8080
            volumeMounts:
            - mountPath: /etc/local_development_credential
              name: local-development-credential
              readOnly: true
          volumes:
          - name: local-development-credential
            secret:
              secretName: local-development-credential
    """)
        self.assertEqual(deployment, yaml.load(expected_yaml_text))
Exemplo n.º 3
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)
Exemplo n.º 4
0
    def testCreateSecret(self):
        credential_generator = local.CredentialGenerator(lambda: 'abcdef')

        configs = credential_generator.CreateConfigs()

        expected = {
            'apiVersion': 'v1',
            'data': {
                'local_development_service_account.json':
                six.ensure_text(base64.b64encode(six.ensure_binary('abcdef')))
            },
            'kind': 'Secret',
            'metadata': {
                'name': 'local-development-credential'
            },
            'type': 'Opaque'
        }
        self.assertEqual(configs, [expected])