Skip to content

Commit

Permalink
feat: support ConfigMap and Secret as an output
Browse files Browse the repository at this point in the history
  • Loading branch information
aabouzaid committed Sep 16, 2023
1 parent c437b21 commit addb97b
Show file tree
Hide file tree
Showing 20 changed files with 340 additions and 108 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/go-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,19 @@ jobs:
with:
path: dist
key: dist-${{ github.run_id }}
- run: find dist
# The upload-artifact action doesn't support multi upload 🤷‍♂️!
- name: Upload Artifacts - AMD
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3
with:
name: merger-${{ matrix.os.id }}-amd
path: |
dist/kustomize-plugin-merger_${{ matrix.os.id }}_amd*/kustomize-plugin-merger*
dist/kustomize-plugin-merger_*_${{ matrix.os.id }}_amd*.{tar.gz,zip}
dist/*checksums.txt*
- name: Upload Artifacts - ARM
uses: actions/upload-artifact@a8a3f3ad30e3422c9c7b888a15615d19a852ae32 # v3
with:
name: merger-${{ matrix.os.id }}-arm
path: |
dist/kustomize-plugin-merger_${{ matrix.os.id }}_arm*/kustomize-plugin-merger*
dist/kustomize-plugin-merger_*_${{ matrix.os.id }}_arm*.{tar.gz,zip}
dist/*checksums.txt*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CONTROLLER_GEN_VERSION := v0.13.0
# Schema targets.
#

.PHONY: schema.install-tools
.PHONY: schema.install-tools
schema.install-tools:
mkdir -p $(CONTROLLER_GEN_HOME);\
export GOBIN=$(CONTROLLER_GEN_HOME);\
Expand Down
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ A Kustomize generator plugin to merge YAML files seamlessly for real-world use c

**Merger** provides schemaless merge with different merge strategies (StrategicMerge).


- [Why](#why)
- [Features](#features)
- [Options](#options)
- [Common use cases](#common-use-cases)
- [1. Generate multiple manifests from a single base](#1-generate-multiple-manifests-from-a-single-base)
- [2. Merge lists in manifests without schema or a unique identifier](#2-merge-lists-in-manifests-without-schema-or-a-unique-identifier)
- [3. Organize long manifests into smaller ones](#3-organize-long-manifests-into-smaller-ones)
- [2. Merge non-manifest files and store them into ConfigMap or Secret](#2-merge-non-manifest-files-and-store-them-into-configmap-or-secret)
- [3. Merge lists in manifests without schema or a unique identifier](#3-merge-lists-in-manifests-without-schema-or-a-unique-identifier)
- [4. Organize long manifests into smaller ones](#4-organize-long-manifests-into-smaller-ones)
- [TO-DO](#to-do)
- [Project status](#project-status)
- [Contributing](#contributing)
Expand All @@ -49,10 +49,10 @@ and for more details on the challenge of providing OpenAPI schema to merge files

- Generate multiple resources/manifests from a single base without copying the resources multiple times.
- Merge any manifests (even CustomResources) without needing their OpenAPI schema.
- Merge applications configuration YAML files into a ConfigMap or Secret.
- Merge manifests with a list of maps without a unique identifier
(when using `x-kubernetes-patch-merge-key` is not possible).
- Merge YAML files with different merge strategies (StrategicMerge).
- Merge applications configuration YAML files into a ConfigMap or Secret (WIP).


## Options
Expand All @@ -74,8 +74,8 @@ metadata:
dst: /mnt
# Exec KRM functions.
# config.kubernetes.io/function: |
# exec:
# path: kustomize-plugin-merger
# exec:
# path: kustomize-plugin-merger
spec:
resources:
- name: example
Expand All @@ -98,7 +98,7 @@ spec:
# - Combine: Maps from source merged with destination, but the lists will be combined together.
strategy: combine
output:
# Available options: raw.
# Available options: raw,configmap,secret
format: raw
```
Expand All @@ -112,29 +112,34 @@ This section shows a couple of use cases where Merger can help.
In this case, you have multiple `CronJobs`, all of which share the same body,
but each has a different command or other config.

[Use case full example](./examples/multiple-manifests-from-single-file/README.md).
[Read the full example](./examples/multiple-manifests-from-single-file/README.md).

### 2. Merge non-manifest files and store them into ConfigMap or Secret

TBA


### 2. Merge lists in manifests without schema or a unique identifier
### 3. Merge lists in manifests without schema or a unique identifier

Currently, in Kustomize, it's not possible to merge resources without a unique identifier, even with Open API schema.

It's possible to do that using the merge strategy `append` in Merger (later on, `combineWithKey` will also be supported).

[Use case full example](./examples/manifest-lists-without-schema/README.md).
[Read the full example](./examples/manifest-lists-without-schema/README.md).

### 3. Organize long manifests into smaller ones
### 4. Organize long manifests into smaller ones

In some use cases (e.g., [Crossplane Compositions](https://docs.crossplane.io/latest/concepts/compositions/)),
you could have a really long YAML manifest, and it's hard to read. You can split that file
and use the Merger `patch` input method to make it a single manifest again.

[Use case full example](./examples/long-omni-manifest/README.md).
[Read the full example](./examples/long-omni-manifest/README.md).


## TO-DO

- Support `ConfigMap` or `Secret` as an output.
- Support `combine` merge strategy with an identifier key (similar to `x-kubernetes-patch-merge-key`).
- Configure the output indentation.
- Provide better docs for Merger options.


Expand Down
4 changes: 2 additions & 2 deletions examples/krm-and-kustomize/merger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ metadata:
dst: /mnt
# Exec KRM functions.
# config.kubernetes.io/function: |
# exec:
# path: kustomize-plugin-merger
# exec:
# path: kustomize-plugin-merger
spec:
resources:
- name: my-envs
Expand Down
55 changes: 38 additions & 17 deletions examples/krm-and-kustomize/resourcelist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,53 @@ functionConfig:
kind: Merger
metadata:
name: merge
annotations:
# Containerized KRM function.
config.kubernetes.io/function: |
container:
image: ghcr.io/aabouzaid/kustomize-generator-merger
mounts:
- type: bind
src: ./
dst: /mnt
# Exec KRM functions.
# config.kubernetes.io/function: |
# exec:
# path: kustomize-plugin-merger
spec:
resources:
- name: my-envs
- name: raw-output-patch
input:
method: patch
files:
sources:
- input/dev.yaml
- input/stage.yaml
destination: input/base.yaml
merge:
strategy: append
output:
format: raw
- name: raw-output-overlay
input:
method: overlay
files:
sources:
- input/dev.yaml
- input/stage.yaml
destination: input/base.yaml
merge:
strategy: append
output:
format: raw
- name: configmap-output
input:
method: overlay
files:
sources:
- input/dev.yaml
- input/stage.yaml
destination: input/base.yaml
merge:
strategy: append
output:
format: configmap
- name: secret-output
input:
method: overlay
files:
# The same as in the KRM container above, omit it if Exec KRM is used.
root: /mnt
sources:
- input/dev.yaml
- input/stage.yaml
destination: input/base.yaml
merge:
strategy: append
output:
format: raw
format: secret
4 changes: 2 additions & 2 deletions examples/long-omni-manifest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ metadata:
dst: /mnt
# Exec KRM functions.
# config.kubernetes.io/function: |
# exec:
# path: kustomize-plugin-merger
# exec:
# path: kustomize-plugin-merger
spec:
resources:
- name: my-eks-composition
Expand Down
4 changes: 2 additions & 2 deletions examples/long-omni-manifest/merger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ metadata:
dst: /mnt
# Exec KRM functions.
# config.kubernetes.io/function: |
# exec:
# path: kustomize-plugin-merger
# exec:
# path: kustomize-plugin-merger
spec:
resources:
- name: my-eks-composition
Expand Down
4 changes: 2 additions & 2 deletions examples/manifest-lists-without-schema/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ metadata:
dst: /mnt
# Exec KRM functions.
# config.kubernetes.io/function: |
# exec:
# path: kustomize-plugin-merger
# exec:
# path: kustomize-plugin-merger
spec:
resources:
- name: my-iam-policy
Expand Down
4 changes: 2 additions & 2 deletions examples/manifest-lists-without-schema/merger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ metadata:
dst: /mnt
# Exec KRM functions.
# config.kubernetes.io/function: |
# exec:
# path: kustomize-plugin-merger
# exec:
# path: kustomize-plugin-merger
spec:
resources:
- name: my-iam-policy
Expand Down
4 changes: 2 additions & 2 deletions examples/multiple-manifests-from-single-file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ metadata:
dst: /mnt
# Exec KRM functions.
# config.kubernetes.io/function: |
# exec:
# path: kustomize-plugin-merger
# exec:
# path: kustomize-plugin-merger
spec:
resources:
- name: my-cronjobs
Expand Down
4 changes: 2 additions & 2 deletions examples/multiple-manifests-from-single-file/merger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ metadata:
dst: /mnt
# Exec KRM functions.
# config.kubernetes.io/function: |
# exec:
# path: kustomize-plugin-merger
# exec:
# path: kustomize-plugin-merger
spec:
resources:
- name: my-cronjobs
Expand Down
124 changes: 124 additions & 0 deletions examples/non-manifest-into-configmap-or-secret/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<!-- omit in toc -->
# Example - Merge non-manifest files and store them into ConfigMap or Secret

- [Use case](#use-case)
- [Input](#input)
- [Manifest](#manifest)
- [Build](#build)
- [Output](#output)

## Use case

No plans from Kustomize to support non-manifest files merge and storing them into ConfigMap
or Secret. Using Merger you can merge any YAML files like application configuration.
In the example, two Prometheus config are merged together and stored as ConfigMap.

## Input

```yaml
# prometheus-base.yaml
global:
scrape_interval: 15s
evaluation_interval: 15s

alerting:
alertmanagers:
- static_configs:
- targets:
- alertmanager:9093

# This will be added via Merger.
rule_files:

scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
```
```yaml
# prometheus.yaml
rule_files:
- "first_rules.yml"
- "second_rules.yml"
```
## Manifest
```yaml
# kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

generators:
- merger.yaml
```
```yaml
# merger.yaml
apiVersion: generators.kustomize.aabouzaid.com/v1alpha1
kind: Merger
metadata:
name: merge
annotations:
# Containerized KRM function.
config.kubernetes.io/function: |
container:
image: ghcr.io/aabouzaid/kustomize-generator-merger
mounts:
- type: bind
src: ./
dst: /mnt
# Exec KRM functions.
# config.kubernetes.io/function: |
# exec:
# path: kustomize-plugin-merger
spec:
resources:
- name: my-prometheus
input:
method: overlay
files:
# The same as in the KRM container above, omit it if Exec KRM is used.
root: /mnt
sources:
- input/prometheus.yaml
destination: input/prometheus-base.yaml
merge:
strategy: combine
output:
format: configmap
```
## Build
```shell
kustomize build --enable-alpha-plugins --as-current-user .
```

## Output

```yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: my-prometheus
data:
prometheus.yaml: |
alerting:
alertmanagers:
- static_configs:
- targets:
- alertmanager:9093
global:
evaluation_interval: 15s
scrape_interval: 15s
rule_files:
- first_rules.yml
- second_rules.yml
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- localhost:9090
```
Loading

0 comments on commit addb97b

Please sign in to comment.