From 27c1eda2a682be87a8301ddea2dadb6773803dbd Mon Sep 17 00:00:00 2001 From: Zheng Xi Zhou Date: Mon, 24 May 2021 00:41:21 +0800 Subject: [PATCH] Add service-binding trait to chart (#1692) * Add service-binding trait to chart Also add docs for the trait. * Update charts/vela-core/templates/defwithtemplate/service-binding.yaml Co-authored-by: Jianbo Sun * address comments Co-authored-by: Jianbo Sun --- .../defwithtemplate/service-binding.yaml | 46 +++++++++++++++++++ docs/en/end-user/traits/service-binding.md | 18 +++++++- hack/vela-templates/cue/service-binding.cue | 30 ++++++++++++ .../definitions/service-binding.yaml | 14 ++++++ 4 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 charts/vela-core/templates/defwithtemplate/service-binding.yaml create mode 100644 hack/vela-templates/cue/service-binding.cue create mode 100644 hack/vela-templates/definitions/service-binding.yaml diff --git a/charts/vela-core/templates/defwithtemplate/service-binding.yaml b/charts/vela-core/templates/defwithtemplate/service-binding.yaml new file mode 100644 index 000000000..4d8669138 --- /dev/null +++ b/charts/vela-core/templates/defwithtemplate/service-binding.yaml @@ -0,0 +1,46 @@ +# Code generated by KubeVela templates. DO NOT EDIT. +apiVersion: core.oam.dev/v1beta1 +kind: TraitDefinition +metadata: + annotations: + definition.oam.dev/description: "Binding secrets of cloud resources to component env" + name: service-binding + namespace: {{.Values.systemDefinitionNamespace}} +spec: + appliesToWorkloads: + - webservice + - worker + schematic: + cue: + template: | + patch: { + spec: template: spec: { + // +patchKey=name + containers: [{ + name: context.name + // +patchKey=name + env: [ + for envName, v in parameter.envMappings { + name: envName + valueFrom: { + secretKeyRef: { + name: v.secret + if v["key"] != _|_ { + key: v.key + } + if v["key"] == _|_ { + key: envName + } + } + } + }, + ] + }] + } + } + + parameter: { + // +usage=The mapping of environment variables to secret + envMappings: [string]: [string]: string + } + diff --git a/docs/en/end-user/traits/service-binding.md b/docs/en/end-user/traits/service-binding.md index 9084e68dc..cae7065ba 100644 --- a/docs/en/end-user/traits/service-binding.md +++ b/docs/en/end-user/traits/service-binding.md @@ -2,7 +2,10 @@ title: Service Binding --- -Service binding trait will bind data from Kubernetes `Secret` to the application container's ENV. Here is an example. +# Description +Service binding trait will bind data from Kubernetes `Secret` to the application container's ENV. + +## Sample ```yaml apiVersion: core.oam.dev/v1beta1 @@ -40,3 +43,16 @@ spec: secretName: db-conn ``` + +For more detailed samples, please reference to [cloud resource](../components/cloud-services) + +## Specification + +```console +# Properties ++-------------+------------------------------------------------+------------------+----------+---------+ +| NAME | DESCRIPTION | TYPE | REQUIRED | DEFAULT | ++-------------+------------------------------------------------+------------------+----------+---------+ +| envMappings | The mapping of environment variables to secret | map[string]{...} | true | | ++-------------+------------------------------------------------+------------------+----------+---------+ +``` \ No newline at end of file diff --git a/hack/vela-templates/cue/service-binding.cue b/hack/vela-templates/cue/service-binding.cue new file mode 100644 index 000000000..3c723ff75 --- /dev/null +++ b/hack/vela-templates/cue/service-binding.cue @@ -0,0 +1,30 @@ +patch: { + spec: template: spec: { + // +patchKey=name + containers: [{ + name: context.name + // +patchKey=name + env: [ + for envName, v in parameter.envMappings { + name: envName + valueFrom: { + secretKeyRef: { + name: v.secret + if v["key"] != _|_ { + key: v.key + } + if v["key"] == _|_ { + key: envName + } + } + } + }, + ] + }] + } +} + +parameter: { + // +usage=The mapping of environment variables to secret + envMappings: [string]: [string]: string +} diff --git a/hack/vela-templates/definitions/service-binding.yaml b/hack/vela-templates/definitions/service-binding.yaml new file mode 100644 index 000000000..8e87b8e86 --- /dev/null +++ b/hack/vela-templates/definitions/service-binding.yaml @@ -0,0 +1,14 @@ +apiVersion: core.oam.dev/v1beta1 +kind: TraitDefinition +metadata: + annotations: + definition.oam.dev/description: "Binding secrets of cloud resources to component env" + name: service-binding + namespace: {{.Values.systemDefinitionNamespace}} +spec: + appliesToWorkloads: + - webservice + - worker + schematic: + cue: + template: |