Skip to content

Commit 6b75194

Browse files
authored
Add support for config_merger to filter root keys (#162)
1 parent 51976af commit 6b75194

File tree

14 files changed

+228
-7
lines changed

14 files changed

+228
-7
lines changed

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ kubeconfig_location: "{{env(KUBECONFIG)}}"
281281
```
282282

283283

284-
## himl config merger
284+
## himl-config-merger
285285

286286
The `himl-config-merger` script, contains logic of merging a hierarchical config directory and creating the end result YAML files.
287287

@@ -343,6 +343,56 @@ Leveraging HIML, the config-merger script loads the configs tree structure and d
343343
344344
Under each level, there is a mandatory "level key" that is used by config-merger for computing the end result. This key should be present in one of the files under each level. (eg. env.yaml under env).
345345
346+
### Output filtering
347+
348+
Some configs that are specified in the higher levels of the directory tree might not be needed in the end (leaf) result. For this reason, the config-merger script can apply a set of filter rules that are specified via the `--filter-rules-key` parameter. This property must be present in the config and contains rules for removing root level keys from the output. The filter is applied if the selector object matches a subset of the output keys and will keep the keys specified in the `values` list or the keys that match the `regex` pattern.
349+
350+
351+
```yaml
352+
# intermediate config after hierarchical merge
353+
env: dev
354+
cluster: cluster1
355+
region: us-east-1
356+
key1: persisted
357+
key2: dropped
358+
keep_1: persisted
359+
tags:
360+
cost_center: 123
361+
_filters:
362+
- selector:
363+
env: "dev"
364+
keys:
365+
values:
366+
- key1
367+
regex: "keep_.*"
368+
- selector:
369+
cluster:
370+
regex: "cluster1"
371+
keys:
372+
values:
373+
- tags
374+
```
375+
376+
Build the output with filtering:
377+
```sh
378+
himl-config-merger examples/filters --output-dir merged_output --levels env region cluster --leaf-directories cluster --filter-rules-key _filters
379+
```
380+
381+
```yaml
382+
# output after filtering
383+
env: dev
384+
cluster: cluster1
385+
region: us-east-1
386+
key1: persisted
387+
keep_1: persisted
388+
tags:
389+
cost_center: 123
390+
```
391+
#### Filtering limitations
392+
393+
Rule selectors and keys filtering only works at the root level of the config. It is not possible to filter nested keys.
394+
395+
346396
### Extra merger features
347397
348398
Apart from the standard features found in the `PyYaml` library, the `himl-config-merger` component also implements a custom YAML tag called `!include`.

examples/filters/default.yaml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
env: default
2+
region: default
3+
cluster: default
4+
5+
cluster_info:
6+
name: default # this will be overridden by the inner cluster.yaml file
7+
8+
# Interpolation example
9+
description: "This is cluster: {{cluster}}. It is using {{cluster_info.node_type}} instance type."
10+
node_type: c3.2xlarge # default value, which can be overridden by each cluster
11+
cluster_metrics:
12+
- id: 1
13+
metric: cpu
14+
value: 90
15+
- id: 2
16+
metric: memory
17+
value: 90
18+
- id: 3
19+
metric: disk
20+
value: 90
21+
metrics:
22+
- cpu
23+
- memory
24+
- disk
25+
myList:
26+
- id1
27+
- id4
28+
# Fetching the secret value at runtime, from a secrets store (in this case AWS SSM).
29+
# passphrase: "{{ssm.path(/key/coming/from/aws/secrets/store/manager).aws_profile(myprofile)}}"
30+
31+
# Fetching the value at runtime from S3
32+
# my_secret: "{{s3.bucket(my-bucket).path(path/to/file.txt).base64encode(true).aws_profile(myprofile)}}"
33+
34+
35+
_filters:
36+
# Keep _filters key for all outputs. No selector matches all outputs by default.
37+
# - keys:
38+
# values:
39+
# - "_filters"
40+
41+
- selector:
42+
cluster: "cluster.*"
43+
keys:
44+
values:
45+
- persisted_key
46+
# - persisted_key_referenced
47+
# - persisted_key_to_drop
48+
# - persisted_key_to_drop2
49+
# - cluster_persisted_object
50+
# - cluster_persisted_list
51+
52+
- selector:
53+
cluster: cluster1
54+
keys:
55+
values:
56+
- testkey
57+
- home
58+
- cluster_persisted_key
59+
60+
- selector:
61+
cluster: cluster2
62+
keys:
63+
values:
64+
- metrics
65+
- myList
66+
regex: ".*persisted.*"

examples/filters/env=dev/env.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
env: dev
2+
persisted_key: &persisted persisted key
3+
dropped_key: &dropped object will be filtered out
4+
persisted_key_referenced: *persisted
5+
persisted_key_to_drop: *dropped
6+
persisted_key_to_drop2: *dropped
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cluster: cluster1
2+
3+
testkey: |-
4+
# Set to true to log user information returned from LDAP
5+
verbose_logging = true
6+
7+
[[servers]]
8+
# Ldap server host
9+
host = "someaddress"
10+
11+
# Default port is 389 or 636 if use_ssl = true
12+
port = 389
13+
14+
start_tls = true
15+
16+
cluster_persisted_key: this object will be persisted
17+
cluster_filtered_key: this object will be filtered out
18+
cluster_persisted_list: "{{ myList }}"
19+
cluster_persisted_object:
20+
cluster_info: "{{ cluster_info }}"
21+
cluster_list: "{{ myList }}"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
cluster: cluster2
2+
cluster_metrics:
3+
- id: 1
4+
metric: cpu
5+
value: 95
6+
- id: 2
7+
metric: memory
8+
value: 95
9+
- id: 3
10+
metric: disk
11+
remove: True
12+
- metric: exec
13+
value: 5
14+
metrics:
15+
- cpu
16+
- exec
17+
myList:
18+
- id1
19+
- id2
20+
- id3
21+
persisted_key: this object will be persisted
22+
dropped_key: this object will be dropped
23+
another_persisted_key: this object will also be persisted
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
region: us-east-1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cluster: cluster1
2+
home: "{{env(HOME)}}"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
region: us-west-2

examples/filters/env=prod/env.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
env: prod
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cluster: ireland1
2+
3+
file: "{{cwd}}/test.txt"

0 commit comments

Comments
 (0)