Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow users to merge json envs #290

Merged
merged 6 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# MustacheTemplate

[![Build Status](https://travis-ci.com/razee-io/MustacheTemplate.svg?branch=master)](https://travis-ci.com/razee-io/MustacheTemplate)
[![Dependabot Status](https://api.dependabot.com/badges/status?host=github&repo=razee-io/MustacheTemplate)](https://dependabot.com)
![GitHub](https://img.shields.io/github/license/razee-io/MustacheTemplate.svg?color=success)

MustacheTemplate is the next step of complexity when working with Razee. With
Expand Down Expand Up @@ -42,11 +41,26 @@ spec:
- name: app-label
value: "deployment 1"
- name: desired-replicas
optional: true
default: 3
valueFrom:
configMapKeyRef:
name: nginx-config
key: replicas
type: number
- name: json-config
valueFrom:
configMapKeyRef:
name: nginx-config-globals
key: my-app-config
type: json
- name: json-config
overrideStrategy: merge
valueFrom:
configMapKeyRef:
name: nginx-config-dev
key: my-app-config-dev-overrides
type: json
templates:
- apiVersion: v1
kind: ConfigMap
Expand Down Expand Up @@ -204,7 +218,7 @@ and CRDs with a high level `.data` section can be pulled in by using the
`genericMapRef` key. The keys pulled from the resource are what you would use
to match values into your templates.

**Note:**: values are loaded in from `.spec.envFrom` before `.spec.env`, and
**Note:** values are loaded in from `.spec.envFrom` before `.spec.env`, and
top down. Any values with the same key/name will be overwritten, last in wins.

**Schema:**
Expand Down Expand Up @@ -283,6 +297,7 @@ the value will be treated as a normal string.

**Note:** values are loaded in from `.spec.envFrom` before `.spec.env`, and
top down. Any values with the same key/name will be overwritten, last in wins.
If you want to have json values merged, specify [`overrideStrategy: merge`](#Env-OverrideStrategy)

**Schema:**

Expand Down Expand Up @@ -325,6 +340,9 @@ env:
x-kubernetes-int-or-string: true
name:
type: string
overrideStrategy:
type: string
pattern: "^merge$|^replace$"
value:
x-kubernetes-int-or-string: true
valueFrom:
Expand Down Expand Up @@ -410,6 +428,28 @@ default:
x-kubernetes-int-or-string: true
```

#### Env OverrideStrategy

**Path:** `.spec.env[].overrideStrategy`

**Description:** If you are loading envs as json, and you want to allow overrided
values to merge instead of just replacing, specify `overrideStrategy: merge`.

**Note:** If either env defined is not a json object when merge is specified, the
behavior will revert to replace instead of merge (ie. a json object is loaded first,
then a jsonString is loaded second with `overrideStrategy: merge` specified. the
jsonString will replace the first json object instead of trying to merge with it.)

**Schema:**

```yaml
overrideStrategy:
type: string
pattern: "^merge$|^replace$"
```

**Default:** `replace`

### Managed Resource Labels

#### Reconcile
Expand Down
3 changes: 3 additions & 0 deletions kubernetes/MustacheTemplate/resource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ items:
x-kubernetes-int-or-string: true
name:
type: string
overrideStrategy:
type: string
pattern: "^merge$|^replace$"
value:
x-kubernetes-int-or-string: true
valueFrom:
Expand Down
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"license": "Apache-2.0",
"dependencies": {
"@razee/kubernetes-util": "^0.2.5",
"@razee/razeedeploy-core": "^0.12.0",
"@razee/razeedeploy-core": "^0.13.0",
"handlebars": "^4.7.7",
"js-yaml": "^4.1.0",
"mustache": "^4.2.0",
Expand All @@ -54,7 +54,7 @@
"eslint": "^7.32.0",
"markdownlint-cli": "^0.28.1",
"mocha": "^9.1.0",
"nock": "^13.1.2",
"nock": "^13.1.3",
"npm-check-updates": "^11.8.3",
"npm-run-all": "^4.1.5",
"nyc": "^15.1.0",
Expand Down
13 changes: 13 additions & 0 deletions src/handlebar-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ const helpers = {
arr.pop();
let flat = arr.flat();
return ''.concat(...flat);
},
base64: function (data) {
if (typeof data !== 'string' && !Array.isArray(data)) {
return '';
}
return Buffer.from(data).toString('base64');
},
jsonStringify: function (data) {
return JSON.stringify(data);
},
jsonDoubleStringify: function (data) {
// if you want to use this, you must use our strTemplate, and you must not put quotes around your template (ie. `my-field: {{ jsonStringify my-json }}` is valid but `my-field: "{{ jsonStringify my-json }}"` is not)
return JSON.stringify(JSON.stringify(data));
}
};

Expand Down
Loading