Example #1
0
def _GenerateKubeConfigs(code_generators):
  """Generate Kubernetes yaml configs.

  Args:
    code_generators: Iterable of KubeConfigGenerator.

  Returns:
    Iterable of dictionaries representing kubernetes yaml configs.
  """
  kube_configs = []
  for code_generator in code_generators:
    kube_configs.extend(code_generator.CreateConfigs())

  deployments = [
      config for config in kube_configs if config['kind'] == 'Deployment'
  ]
  for deployment, code_generator in itertools.product(deployments,
                                                      code_generators):
    code_generator.ModifyDeployment(deployment)

  for deployment in deployments:
    containers = yaml_helper.GetAll(deployment,
                                    ('spec', 'template', 'spec', 'containers'))

    for container, code_generator in itertools.product(containers,
                                                       code_generators):
      code_generator.ModifyContainer(container)

  return yaml.dump_all(kube_configs)
Example #2
0
    def testCreateServiceAccountCredential(self):
        refresh_token = e2e_base.RefreshTokenAuth()
        local_credential_variable = EnvironmentVariable(
            'LOCAL_CREDENTIAL_PATH', _LOCAL_CREDENTIAL_FILE_PATH)

        pod_and_services_path = os.path.join(_LOCAL_DEVELOPMENT_DIR,
                                             'pods_and_services.yaml')
        with refresh_token as auth, local_credential_variable as _:
            command = (
                'code export --project {0} --kubernetes-file={1} '
                '--skaffold-file={2} --service-account={3} --dockerfile={4}'
            ).format(auth.Project(), pod_and_services_path,
                     _SKAFFOLD_FILE_PATH, self.local_account_email,
                     self.docker_file)
            self.Run(command)
            self.WriteInput('y')

        with open(pod_and_services_path) as pods_and_services_file:
            pods_and_services = list(yaml.load_all(pods_and_services_file))

        pod_specs = [
            spec for spec in pods_and_services if spec['kind'] == 'Deployment'
        ]
        self.assertGreaterEqual(len(pod_specs), 1)
        for spec in pod_specs:
            env_vars = yaml_helper.GetAll(spec,
                                          path=('spec', 'template', 'spec',
                                                'containers', 'env'))
            credential_vars = (
                var['value'] for var in env_vars
                if var['name'] == 'GOOGLE_APPLICATION_CREDENTIALS')
            env_var_path = next(credential_vars, None)
            self.assertEqual(
                env_var_path, '/etc/local_development_credential/'
                'local_development_service_account.json')

        secret_specs = [
            spec for spec in pods_and_services if spec['kind'] == 'Secret'
        ]
        self.assertEqual(len(secret_specs), 1)
        self.assertEqual(secret_specs[0]['metadata']['name'],
                         'local-development-credential')
 def testFinalList(self):
     obj = {'A': {'B': {'C': [1, 2, 3]}}}
     six.assertCountEqual(self, yaml_helper.GetAll(obj, ('A', 'B', 'C')),
                          (1, 2, 3))
 def testNotAListOrObj(self):
     obj = {'A': {'B': 3}}
     with self.assertRaises(ValueError):
         list(yaml_helper.GetAll(obj, ('A', 'B', 'C')))
 def testIntermediateList(self):
     obj = {'A': {'B': {'C': [{'D': 1}, {'D': 2}, {'D': 3}]}}}
     six.assertCountEqual(self, yaml_helper.GetAll(obj,
                                                   ('A', 'B', 'C', 'D')),
                          (1, 2, 3))