Skip to content

Commit

Permalink
added: use case - Generate multiple manifests from a single file
Browse files Browse the repository at this point in the history
  • Loading branch information
aabouzaid committed Sep 10, 2023
1 parent c4583df commit a367cd8
Show file tree
Hide file tree
Showing 11 changed files with 347 additions and 6 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/aabouzaid/kustomize-plugin-merger/pulls)

<!-- omit in toc -->
# Merger
# 🔀 Merger 🔀

A Kustomize generator plugin to merge YAML files seamlessly for real-world use cases.

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


- [Why](#why)
Expand Down Expand Up @@ -106,10 +106,10 @@ This section shows a couple of use cases where Merger can help.
### 1. Generate multiple manifests from a single base
In this case, you have multiple `CronJobs`, all of them share the same body,
but each one has a different command or other config.
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/generate-multiple-manifests/README.md).
[Use case full example](./examples/generate-multiple-manifests-from-single-file/README.md).

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

Expand Down
228 changes: 228 additions & 0 deletions examples/generate-multiple-manifests-from-single-file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<!-- omit in toc -->
# Example - Generate multiple manifests from a single file

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

## Use case

Currently, in Kustomize, you can use a single patch to override multiple resources. However, the vice versa
doesn't work in the same path (using a single resource as a template and generating multiple resources from it).

## Input

```yaml
# Base.
apiVersion: batch/v1
kind: CronJob
metadata:
name: this-will-be-applied-to-multiple-cronjobs
spec:
failedJobsHistoryLimit: 2
jobTemplate:
spec:
template:
spec:
containers:
- name: my-app
image: my-app
# The command will be updated via Merger.
command: []
imagePullPolicy: Always
restartPolicy: Never
imagePullSecrets:
- name: docker-registry
# The schedule will be updated via Merger.
schedule: ""
successfulJobsHistoryLimit: 0

```

```yaml
# Overlay 01.
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob01
spec:
schedule: '0 1 * * *'
jobTemplate:
spec:
template:
spec:
containers:
- name: my-app
command:
- echo
- cronjob01
```
```yaml
# Overlay 02.
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob02
spec:
schedule: '0 2 * * *'
jobTemplate:
spec:
template:
spec:
containers:
- name: my-app
command:
- echo
- cronjob02
```
```yaml
# Overlay 03.
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob03
spec:
schedule: '0 3 * * *'
jobTemplate:
spec:
template:
spec:
containers:
- name: my-app
command:
- echo
- cronjob03
```
## 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-cronjobs
input:
method: overlay
files:
# The same as in the KRM container above.
root: /mnt
sources:
- input/cronjob-01.yaml
- input/cronjob-02.yaml
- input/cronjob-03.yaml
destination: input/cronjob-common.yaml
merge:
strategy: combine
output:
format: raw
```
## Build
```shell
kustomize build --enable-alpha-plugins --as-current-user .
```

## Output

```yaml
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob01
spec:
failedJobsHistoryLimit: 2
jobTemplate:
spec:
template:
spec:
containers:
- command:
- echo
- cronjob01
image: my-app
imagePullPolicy: Always
name: my-app
imagePullSecrets:
- name: docker-registry
restartPolicy: Never
schedule: 0 1 * * *
successfulJobsHistoryLimit: 0
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob02
spec:
failedJobsHistoryLimit: 2
jobTemplate:
spec:
template:
spec:
containers:
- command:
- echo
- cronjob02
image: my-app
imagePullPolicy: Always
name: my-app
imagePullSecrets:
- name: docker-registry
restartPolicy: Never
schedule: 0 2 * * *
successfulJobsHistoryLimit: 0
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob03
spec:
failedJobsHistoryLimit: 2
jobTemplate:
spec:
template:
spec:
containers:
- command:
- echo
- cronjob03
image: my-app
imagePullPolicy: Always
name: my-app
imagePullSecrets:
- name: docker-registry
restartPolicy: Never
schedule: 0 3 * * *
successfulJobsHistoryLimit: 0

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob01
spec:
schedule: '0 1 * * *'
jobTemplate:
spec:
template:
spec:
containers:
- name: my-app
command:
- echo
- cronjob01
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob02
spec:
schedule: '0 2 * * *'
jobTemplate:
spec:
template:
spec:
containers:
- name: my-app
command:
- echo
- cronjob02
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: cronjob03
spec:
schedule: '0 3 * * *'
jobTemplate:
spec:
template:
spec:
containers:
- name: my-app
command:
- echo
- cronjob03
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: this-will-be-applied-to-multiple-cronjobs
spec:
failedJobsHistoryLimit: 2
jobTemplate:
spec:
template:
spec:
containers:
- name: my-app
image: my-app
# The command will be updated via Merger.
command: []
imagePullPolicy: Always
restartPolicy: Never
imagePullSecrets:
- name: docker-registry
# The schedule will be updated via Merger.
schedule: ""
successfulJobsHistoryLimit: 0
35 changes: 35 additions & 0 deletions examples/generate-multiple-manifests-from-single-file/merger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
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-cronjobs
input:
method: overlay
files:
# The same as in the KRM container above.
root: /mnt
sources:
- input/cronjob-01.yaml
- input/cronjob-02.yaml
- input/cronjob-03.yaml
destination: input/cronjob-common.yaml
merge:
strategy: combine
output:
format: raw
Empty file.
7 changes: 7 additions & 0 deletions examples/merge-lists-without-schema/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<!-- omit in toc -->
# Example - Append CustomResources lists without schema

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

## Use case

Currently, in Kustomize, it's not possible to append CustomResources without providing an Open API schema
Expand Down
2 changes: 1 addition & 1 deletion examples/merge-lists-without-schema/merger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ metadata:
# Exec KRM functions.
# config.kubernetes.io/function: |
# exec:
# path: ./kustomize-plugin-merger
# path: kustomize-plugin-merger
spec:
resources:
- name: my-iam-policy
Expand Down

0 comments on commit a367cd8

Please sign in to comment.