From be42e35f3bacd32ffd7c356a2baa75327b9d74d2 Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Fri, 2 Feb 2024 17:08:11 +0000 Subject: [PATCH] feat: support for k8s ServiceAccount --- .../output/kube_serviceaccount.py | 59 +++++++++++++++++++ setup.py | 2 +- .../integration/kube_basic/deploy/config.yml | 11 ++++ ...rviceaccount-001-test-serviceaccount1.yaml | 4 ++ ...rviceaccount-002-test-serviceaccount2.yaml | 8 +++ 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 deploy_config_generator/output/kube_serviceaccount.py create mode 100644 tests/integration/kube_basic/expected_output/kube_serviceaccount-001-test-serviceaccount1.yaml create mode 100644 tests/integration/kube_basic/expected_output/kube_serviceaccount-002-test-serviceaccount2.yaml diff --git a/deploy_config_generator/output/kube_serviceaccount.py b/deploy_config_generator/output/kube_serviceaccount.py new file mode 100644 index 0000000..1625847 --- /dev/null +++ b/deploy_config_generator/output/kube_serviceaccount.py @@ -0,0 +1,59 @@ +import copy + +from deploy_config_generator.utils import yaml_dump, underscore_to_camelcase +from deploy_config_generator.output import kube_common + + +class OutputPlugin(kube_common.OutputPlugin): + + NAME = 'kube_serviceaccount' + DESCR = 'Kubernetes service account output plugin' + FILE_EXT = '.yaml' + + DEFAULT_CONFIG = { + 'fields': { + 'kube_serviceaccounts': dict( + metadata=dict( + type='dict', + required=True, + fields=copy.deepcopy(kube_common.METADATA_FIELD_SPEC), + ), + automount_service_account_token=dict( + type='bool', + ), + secrets=dict( + type='list', + subtype='dict', + fields=dict( + name=dict( + type='str', + ), + ), + ), + image_pull_secrets=dict( + type='list', + subtype='dict', + fields=dict( + name=dict( + type='str', + ), + ), + ), + ), + } + } + + def generate_output(self, app_vars): + # Basic structure + data = { + 'apiVersion': 'v1', + 'kind': 'ServiceAccount', + } + data['metadata'] = self.build_metadata(app_vars['APP']['metadata']) + for field in ('automount_service_account_token', 'secrets', 'image_pull_secrets'): + if app_vars['APP'].get(field, None) is not None and app_vars['APP'][field]: + data[underscore_to_camelcase(field)] = app_vars['APP'][field] + + data = self._template.render_template(data, app_vars) + output = yaml_dump(data) + return (output, self.get_output_filename_suffix(data)) diff --git a/setup.py b/setup.py index 4aa27f4..f57d7da 100644 --- a/setup.py +++ b/setup.py @@ -53,7 +53,7 @@ def run(self): setup( name='deploy-config-generator', - version='2.26.0', + version='2.27.0', url='https://github.com/ApplauseOSS/deploy-config-generator', license='MIT', description='Utility to generate service deploy configurations', diff --git a/tests/integration/kube_basic/deploy/config.yml b/tests/integration/kube_basic/deploy/config.yml index 57ee4a0..02bce02 100644 --- a/tests/integration/kube_basic/deploy/config.yml +++ b/tests/integration/kube_basic/deploy/config.yml @@ -180,3 +180,14 @@ kube_configmaps: test1 test2 test3 + +kube_serviceaccounts: + - metadata: + name: test-serviceaccount1 + - metadata: + name: test-serviceaccount2 + automount_service_account_token: false + secrets: + - name: foo-bar + image_pull_secrets: + - name: bar-baz diff --git a/tests/integration/kube_basic/expected_output/kube_serviceaccount-001-test-serviceaccount1.yaml b/tests/integration/kube_basic/expected_output/kube_serviceaccount-001-test-serviceaccount1.yaml new file mode 100644 index 0000000..e93d95e --- /dev/null +++ b/tests/integration/kube_basic/expected_output/kube_serviceaccount-001-test-serviceaccount1.yaml @@ -0,0 +1,4 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: test-serviceaccount1 diff --git a/tests/integration/kube_basic/expected_output/kube_serviceaccount-002-test-serviceaccount2.yaml b/tests/integration/kube_basic/expected_output/kube_serviceaccount-002-test-serviceaccount2.yaml new file mode 100644 index 0000000..3835725 --- /dev/null +++ b/tests/integration/kube_basic/expected_output/kube_serviceaccount-002-test-serviceaccount2.yaml @@ -0,0 +1,8 @@ +apiVersion: v1 +imagePullSecrets: +- name: bar-baz +kind: ServiceAccount +metadata: + name: test-serviceaccount2 +secrets: +- name: foo-bar