Skip to content

Commit

Permalink
[manifests]: Content Rendering Functions (#50)
Browse files Browse the repository at this point in the history
* Release Crowd Helm Chart

Signed-off-by: Oliver Bähler <[email protected]>

* Add Sonatype Chart

Signed-off-by: Oliver Bähler <[email protected]>

* Add license

Signed-off-by: Oliver Bähler <[email protected]>

* Content Templates

Signed-off-by: Oliver Bähler <[email protected]>

* Helm-Docs

Signed-off-by: Oliver Bähler <[email protected]>
  • Loading branch information
oliverbaehler authored May 11, 2021
1 parent a65cf19 commit 0edf057
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 3 deletions.
5 changes: 3 additions & 2 deletions charts/manifests/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)"
2 changes: 1 addition & 1 deletion charts/manifests/README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
207 changes: 207 additions & 0 deletions charts/manifests/templates/utils/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)**
Expand Down Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions charts/manifests/templates/utils/_configs.tpl
Original file line number Diff line number Diff line change
@@ -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 -}}

0 comments on commit 0edf057

Please sign in to comment.