def Run(self, args): if not args.IsSpecified('service_name'): dir_name = os.path.basename(os.path.dirname(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=properties.VALUES.core.project.Get(required=True), service=service_name) image_name = console_io.PromptWithDefault( message='Docker image tag', default=default_image_name) else: image_name = args.image_name kubernetes_configs = local.CreatePodAndService(service_name, image_name) with files.FileWriter(args.kubernetes_file) as output: yaml.dump_all(kubernetes_configs, output) skaffold_yaml_text = _SKAFFOLD_TEMPLATE.format( image_name=image_name, context_path=args.build_context_directory or os.path.dirname(args.dockerfile) or '.') with files.FileWriter(args.skaffold_file) as output: output.write(skaffold_yaml_text)
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)
def test_import_multidoc(self): trigger1 = copy.deepcopy(self.base_trigger) trigger1.name = 'name1' trigger2 = copy.deepcopy(self.base_trigger) trigger2.name = 'name2' triggers = [trigger1, trigger2] encoded = [encoding.MessageToDict(trigger) for trigger in triggers] path = self.Touch('.', 'trigger.yaml', yaml.dump_all(encoded)) wants = [] for trigger in triggers: want = copy.deepcopy(trigger) want.id = 'id' wants.append(want) self.mocked_cloudbuild_v1.projects_triggers.Patch.Expect( self.msg.CloudbuildProjectsTriggersPatchRequest( projectId='my-project', triggerId=trigger.name, buildTrigger=trigger), exception=apitools_exceptions.HttpNotFoundError('', '', '')) self.mocked_cloudbuild_v1.projects_triggers.Create.Expect( self.msg.CloudbuildProjectsTriggersCreateRequest( projectId='my-project', buildTrigger=trigger), response=want) properties.VALUES.core.user_output_enabled.Set(False) resp = self.Run(['alpha', 'builds', 'triggers', 'import', '--source', path]) self.assertEqual(wants, resp)
def KubernetesConfig(self): """Create a kubernetes config file. Returns: Text of a kubernetes config file. """ deployment = local.CreateDeployment(self._settings.service_name, self._settings.image_name) if self._settings.env_vars: local.AddEnvironmentVariables( deployment, self._settings.service_name + '-container', self._settings.env_vars) kubernetes_configs = [ deployment, local.CreateService(self._settings.service_name) ] if self._settings.service_account: service_account = local.CreateDevelopmentServiceAccount( self._settings.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) return yaml.dump_all(kubernetes_configs)
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 SerializeAll(self, resources, path=None, scope_field=None): if path: if not os.path.isdir(path): raise client_base.ClientException( 'Error executing export: "{}" must be an existing directory when --all is specified.' .format(path)) for resource in resources: file_name = resource['metadata']['name'] if scope_field: scope = resource['spec'][scope_field] file_name = '{}_{}'.format(scope, file_name) file_path = os.path.join(path, '{}.yaml'.format(file_name)) try: with files.FileWriter(file_path) as stream: yaml.dump(resource, stream=stream) except (EnvironmentError, yaml.Error) as e: raise client_base.ClientException( 'Error while serializing resource to file [{}]: "{}"'. format(file_path, e)) else: yaml.dump_all(documents=resources, stream=sys.stdout)
def _MakeTempConfigFiles(self): """Copy fixture data files to temp directories so they can be modified.""" fixture_file_dir = self.Resource('tests', 'unit', 'command_lib', 'anthos', 'testdata') config_v2_path = self.Resource(fixture_file_dir, 'auth-config-v2alpha1.yaml') config_v2_multi_path = self.Resource( fixture_file_dir, 'auth-config-multiple-v2alpha1.yaml') config_v1_multi_path = self.Resource( fixture_file_dir, 'auth-config-multiple-v1alpha1.yaml') config_v1_path = self.Resource(fixture_file_dir, 'auth-config-v1alpha1.yaml') config_v2_missing_providers = self.Resource( fixture_file_dir, 'auth-config-v2alpha1-missing-providers.yaml') config_v2_1p = self.Resource(fixture_file_dir, 'auth-config-v2alpha1-1p.yaml') config_v2_1p_ldap = self.Resource(fixture_file_dir, 'auth-config-v2alpha1-1p-ldap.yaml') self.v2_ex1_path = self.Touch(self.temp_path, 'config_v2_ex1.yaml', contents=yaml.dump_all( yaml.load_all_path(config_v2_path))) self.v2_ex1_contents = files.ReadFileContents(self.v2_ex1_path) self.v2_ex2_multi_path = self.Touch( self.temp_path, 'config_v2_ex2.yaml', contents=yaml.dump_all(yaml.load_all_path(config_v2_multi_path))) self.v2_ex2_multi_contents = files.ReadFileContents( self.v2_ex2_multi_path) self.v1_ex1_path = self.Touch( self.temp_path, 'config_v1_multi_path.yaml', contents=yaml.dump_all(yaml.load_all_path(config_v1_multi_path))) self.v1_ex2_multi_path = self.Touch( self.temp_path, 'config_v1_multi_path.yaml', contents=yaml.dump_all(yaml.load_all_path(config_v1_path))) self.v2_ex2_missing_providers = self.Touch( self.temp_path, 'config-v2alpha1-missing-providers.yaml', contents=yaml.dump_all( yaml.load_all_path(config_v2_missing_providers))) self.v2_ex3_1p = self.Touch(self.temp_path, 'config-v2alpha1-1p.yaml', contents=yaml.dump_all( yaml.load_all_path(config_v2_1p))) self.v2_ex4_1p_ldap = self.Touch( self.temp_path, 'config-v2alpha1-1p-ldap.yaml', contents=yaml.dump_all(yaml.load_all_path(config_v2_1p_ldap)))
def KubernetesConfig(self): """Create a kubernetes config file. Returns: Text of a kubernetes config file. """ kubernetes_configs = local.CreatePodAndService(self._settings.service_name, self._settings.image_name) if self._settings.service_account: service_account = local.CreateDevelopmentServiceAccount( self._settings.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) return yaml.dump_all(kubernetes_configs)
def WriteToDisk(self): with files.FileWriter(self.file_path) as f: yaml.dump_all([x.content for x in self.data], f)