diff --git a/charts/manifests/Chart.yaml b/charts/manifests/Chart.yaml index 18aa4b9f..45fcc6fb 100644 --- a/charts/manifests/Chart.yaml +++ b/charts/manifests/Chart.yaml @@ -2,7 +2,7 @@ apiVersion: v2 name: manifests description: "Bedag's Manifest chart. Library full of basic kubernetes manifests." type: library -version: 0.5.3 +version: 0.6.0 icon: "https://www.bedag.ch/wGlobal/wGlobal/layout/images/logo.svg" keywords: - Bedag @@ -22,4 +22,5 @@ annotations: artifacthub.io/prerelease: "false" artifacthub.io/license: Apache-2.0 artifacthub.io/changes: | - - "[Changed]: Updated Documentation" + - "[Added]: Content File Template (Template & Documentation)" + - "[Added]: Content Template (Template & Documentation)" \ No newline at end of file diff --git a/charts/manifests/README.md b/charts/manifests/README.md index f77113f7..59e6d87d 100644 --- a/charts/manifests/README.md +++ b/charts/manifests/README.md @@ -1,6 +1,6 @@ # Manifests Library -![Version: 0.5.3](https://img.shields.io/badge/Version-0.5.3-informational?style=flat-square) ![Type: library](https://img.shields.io/badge/Type-library-informational?style=flat-square) +![Version: 0.6.0](https://img.shields.io/badge/Version-0.6.0-informational?style=flat-square) ![Type: library](https://img.shields.io/badge/Type-library-informational?style=flat-square) This library's purpose is to have more flexibility as chart author but at the same time have kubernetes manifests managed in a central library. This way you can avoid big surprises when Kubernetes has breaking changes in any of their APIs. Currently we support a base set of resources. Resources may be added as soon as we see or get a request that there's a need for it. This chart is still under development and testing, since it's rather complex. Feel free to use it. Our goal is to get it as reliable as possible. diff --git a/charts/manifests/templates/utils/README.md b/charts/manifests/templates/utils/README.md index 64ca0520..f330949b 100644 --- a/charts/manifests/templates/utils/README.md +++ b/charts/manifests/templates/utils/README.md @@ -10,6 +10,9 @@ Description and Definition of all available Go Sprig Templates. Base functionali * [Fullname](#fullname) * [serviceAccountName](#serviceaccountname) * [mergedValues](#mergedvalues) +* **[Configs](#configs)** + * [content](#content) + * [files](#files) * **[Helpers](#helpers)** * [javaProxies](#javaproxies) * **[Environment](#environment)** @@ -91,6 +94,210 @@ String, YAML Structure {{- $values := include "bedag-lib.utils.common.mergedValues" (dict "type" "serviceAccount" "root" .) }} ``` +## [Configs](./_configs_.tpl) + +### Content +--- + +Renders Config content based on your needs + +#### Arguments + +If an as required marked argument is missing, the template engine will intentionally. + + * `.config` - Config Structure [e.g. .config_files.application_properties ](Required) + * `.format` - Define the format when including the template. The format per config (`.config.format`) value takes precedence, if present. + * `.context` - Inherited Root Context (Required). + + +#### Structure/Example + +This template supports the following key structure: + + +**values.yaml** +``` +config_files: + + # Config Structure: Here we define a config file we want to render with a custom format + application_properties: + + ## Format allows you to define your own pattern on how to format the key value pairs. + ## You can use $.loop.key as placeholder for your key values and $.loop.value for the value placeholder. + ## This only works, when the content is kind map, otherwise it will be dumped to yaml without applying the format (Optional) + # + ## In the following example we want to use = ase key value indicator, therefor we use the following format: + format*: "{{ $.loop.key }}={{ tpl $.loop.value $ | quote }}" + + ## For the format to apply, the content needs to be a map, otherwise it's just templated to yaml (Required) + content: + spring.datasource.jdbcUrl: "jdbc:postgresql://postgresql:5342/postgres" + spring.datasource.driver-class-name: "org.postgresql.Driver" + spring.datasource.username: "${DB_USER}" + spring.datasource.password: "${DB_PASSWORD}" + api.auth.openid.authorizationUrlForSwagger: "{{ $.Values.config.properties.oauth.url }}" + + # Renders the given content just as yaml and allows templating + application_environment: + content: | + some.environment = { + debug: false, + apiUrl: "{{ $.Values.config.properties.oauth.url }}", + + openIdConnect: { + redirectUri: window.location.origin + '/index.html', + requireHttps: false + } + } + +## Custom Config Structure we access from the configurations +config: + properties: + oauth: + url: "https://oauth.company.com" +``` + +**configmap.yaml** +``` +apiVersion: v1 +kind: ConfigMap +metadata: + name: application-configuration +data: + application.properties: | {{- include "bedag-lib.utils.configs.content" (dict "context" $ "config" $.Values.config_files.application_properties ) | nindent 4 }} + + environment: | {{- include "bedag-lib.utils.configs.content" (dict "context" $ "config" $.Values.config_files.application_environment ) | nindent 4 }} +``` + +Results in this: + +``` +apiVersion: v1 +kind: ConfigMap +metadata: + name: application-configuration +data: + application.properties: | + + api.auth.openid.authorizationUrlForSwagger="https://oauth.company.com" + spring.datasource.driver-class-name="org.postgresql.Driver" + spring.datasource.jdbcUrl="jdbc:postgresql://postgresql:5342/postgres" + spring.datasource.password="${DB_PASSWORD}" + spring.datasource.username="${DB_USER}" + + environment: | + + some.environment = { + debug: false, + apiUrl: "https://oauth.company.com", + + openIdConnect: { + redirectUri: window.location.origin + '/index.html', + requireHttps: false + } + } +``` + +#### Returns + +String, YAML Structure + +#### Usage + +``` +{{- include "bedag-lib.utils.configs.content" (dict "context" $ "config" $.Values.config_files.application_environment "format" "{{ $.loop.key }}={{ tpl $.loop.value $ | quote }}") | nindent 0 }} +``` + +### Files +--- + +Wrapper for the [Content Template](#template) but allows to specify a file name. Click the link to get more information on content rendering. + +#### Arguments + +If an as required marked argument is missing, the template engine will intentionally. + + * `.config` - Config Structure [e.g. .config_files.application_properties ](Required) + * `.name` - Define the name for the file when including the template. The format per config (`.config.name`) value takes precedence, if present. Defaults to `config.yml` (Optional) + * `.format` - Define the format when including the template. The format per config (`.config.format`) value takes precedence, if present. + * `.context` - Inherited Root Context (Required). + + +#### Structure/Example + +This template supports the following key structure: + + +**values.yaml** +``` +config_files: + + # Config Structure: Here we define a config file we want to render with a custom format + application_properties: + + ## Define the file name + name: "application.properties" + + ## Format allows you to define your own pattern on how to format the key value pairs. + ## You can use $.loop.key as placeholder for your key values and $.loop.value for the value placeholder. + ## This only works, when the content is kind map, otherwise it will be dumped to yaml without applying the format (Optional) + # + ## In the following example we want to use = ase key value indicator, therefor we use the following format: + format*: "{{ $.loop.key }}={{ tpl $.loop.value $ | quote }}" + + ## For the format to apply, the content needs to be a map, otherwise it's just templated to yaml (Required) + content: + spring.datasource.jdbcUrl: "jdbc:postgresql://postgresql:5342/postgres" + spring.datasource.driver-class-name: "org.postgresql.Driver" + spring.datasource.username: "${DB_USER}" + spring.datasource.password: "${DB_PASSWORD}" + api.auth.openid.authorizationUrlForSwagger: "{{ $.Values.config.properties.oauth.url }}" + +## Custom Config Structure we access from the configurations +config: + properties: + oauth: + url: "https://oauth.company.com" +``` + +**configmap.yaml** +``` +apiVersion: v1 +kind: ConfigMap +metadata: + name: application-configuration +data: + {{- include "bedag-lib.utils.configs.file" (dict "context" $ "config" $.Values.config_files.application_properties ) | nindent 2 }} +``` + +Results in this: + +``` +apiVersion: v1 +kind: ConfigMap +metadata: + name: application-configuration +data: + + application.properties: |- + + api.auth.openid.authorizationUrlForSwagger="https://oauth.company.com" + spring.datasource.driver-class-name="org.postgresql.Driver" + spring.datasource.jdbcUrl="jdbc:postgresql://postgresql:5342/postgres" + spring.datasource.password="${DB_PASSWORD}" + spring.datasource.username="${DB_USER}" +``` + +#### Returns + +String, YAML Structure + +#### Usage + +``` +{{- include "bedag-lib.utils.configs.file" (dict "context" $ "config" $.Values.config_files.application_environment "format" "{{ $.loop.key }}={{ tpl $.loop.value $ | quote }}" "name" "application.properties") | nindent 0 }} +``` + ## [Helpers](./_helpers.tpl) ### JavaProxies diff --git a/charts/manifests/templates/utils/_configs.tpl b/charts/manifests/templates/utils/_configs.tpl new file mode 100644 index 00000000..e69c1f96 --- /dev/null +++ b/charts/manifests/templates/utils/_configs.tpl @@ -0,0 +1,44 @@ +/* + +Copyright © 2021 Bedag Informatik AG + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +*/ +{{/* + Sprig Template - File +*/}} +{{- define "bedag-lib.utils.configs.file" -}} +{{- $config := (required "Configuration reference requried" $.config) -}} +{{- default (default "config.yml" .name) $config.name | nindent 0 }}: |- {{- include "bedag-lib.utils.configs.content" $ | nindent 2 }} +{{- end -}} + + +{{/* + Sprig Template - Content +*/}} +{{- define "bedag-lib.utils.configs.content" -}} +{{- $c := (required "Context required" $.context) -}} +{{- $config := (required "Configuration reference requried" $.config) -}} + {{- if $config.content -}} + {{- $format := (default $.format $config.format) -}} + {{- if and $format (kindIs "map" $config.content) }} + {{- range $key, $value := $config.content }} + {{- $scope := (dict "key" $key "value" $value) -}} + {{- include "lib.utils.strings.template" (dict "value" $format "context" $c "extraValuesKey" "loop" "extraValues" $scope) | nindent 0 }} + {{- end }} + {{- else }} + {{- include "lib.utils.strings.template" (dict "value" $config.content "context" $c) | nindent 0 }} + {{- end }} + {{- end }} +{{- end -}} \ No newline at end of file