Skip to content

Commit

Permalink
changed: tidy up the main example
Browse files Browse the repository at this point in the history
  • Loading branch information
aabouzaid committed Sep 10, 2023
1 parent 77df539 commit c70ff5e
Show file tree
Hide file tree
Showing 16 changed files with 144 additions and 37 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*.dll
*.so
*.dylib
dist/*

# Test binary, built with `go test -c`
*.test
Expand Down
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CONTROLLER_GEN_HOME := /tmp/controller-gen
CONTROLLER_GEN_VERSION := v0.13.0

#
# Schema targets.
#

.PHONY: schema.install-tools
schema.install-tools:
mkdir -p $(CONTROLLER_GEN_HOME);\
Expand All @@ -16,6 +20,14 @@ schema.generate:
-e 's|apiextensions.k8s.io/v1|config.kubernetes.io/v1alpha1|g' \
-i pkg/merger/schema/generators.kustomize.aabouzaid.com_mergers.yaml

#
# Golang targets.
#

.PHONY: go.build
go.build:
go build -o 'dist' .

.PHONY: go.format
go.format:
gofumpt -l -w .
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ Kustomize's default patch strategy for the lists (arrays) is `replace`, and to c
for the Kubernetes `Custom Resource`, you must provide the OpenAPI schema of that custom resource,
which is only helpful if the lists of that CR have a unique id.

For more details, please read:
Many people are asking for such functionalities as [easy merging CRs](https://stackoverflow.com/q/73655002/4547221), [creating a ConfigMap from arbitrary YAML files](https://stackoverflow.com/q/74547569/4547221),
and for more details on the challenge of providing OpenAPI schema to merge files, please read the following post:
[Set OpenAPI patch strategy for Kubernetes Custom Resources](https://tech.aabouzaid.com/2022/11/set-openapi-patch-strategy-for-kubernetes-custom-resources-kustomize.html).


Expand All @@ -57,6 +58,19 @@ 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: example
Expand All @@ -66,6 +80,8 @@ spec:
# - Patch: Produce a single output by merging all sources together then with the destination.
method: overlay
files:
# The same as in the KRM container above.
root: /mnt
sources:
- src01.yaml
- src02.yaml
Expand Down Expand Up @@ -113,6 +129,7 @@ and use the Merger `patch` input method to make it a single manifest again.

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


## Project status
Expand Down
8 changes: 8 additions & 0 deletions examples/krm-and-kustomize/input/base.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Pod
metadata:
name: base
spec:
containers:
- name: sidecar
image: sidecar
8 changes: 8 additions & 0 deletions examples/krm-and-kustomize/input/dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
metadata:
name: dev
spec:
containers:
- name: dev
image: dev:1.0.0
ports:
- containerPort: 80
8 changes: 8 additions & 0 deletions examples/krm-and-kustomize/input/stage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
metadata:
name: stage
spec:
containers:
- name: stage
image: stage:1.0.0
ports:
- containerPort: 80
5 changes: 5 additions & 0 deletions examples/krm-and-kustomize/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

generators:
- merger.yaml
34 changes: 34 additions & 0 deletions examples/krm-and-kustomize/merger.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
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-envs
input:
method: overlay
files:
# The same as in the KRM container above.
root: /mnt
sources:
- input/dev.yaml
- input/stage.yaml
destination: input/base.yaml
merge:
strategy: append
output:
format: raw
39 changes: 39 additions & 0 deletions examples/krm-and-kustomize/resourcelist.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
apiVersion: config.kubernetes.io/v1
kind: ResourceList
metadata:
name: krm-function-input
functionConfig:
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-envs
input:
method: overlay
files:
# The same as in the KRM container above.
root: /mnt
sources:
- input/dev.yaml
- input/stage.yaml
destination: input/base.yaml
merge:
strategy: append
output:
format: raw
3 changes: 0 additions & 3 deletions examples/krm-without-kustomize/input/common.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions examples/krm-without-kustomize/input/dev.yaml

This file was deleted.

3 changes: 0 additions & 3 deletions examples/krm-without-kustomize/input/stage.yaml

This file was deleted.

24 changes: 0 additions & 24 deletions examples/krm-without-kustomize/resourcelist.yaml

This file was deleted.

7 changes: 5 additions & 2 deletions pkg/merger/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ func (r *mergerResource) setInputFilesOverlay() {
for _, inputFileSource := range r.Input.Files.Sources {
r.Input.items = append(r.Input.items,
resourceInputFiles{
Sources: []string{inputFileSource},
Destination: r.Input.Files.Destination,
Sources: []string{r.Input.Files.Root + inputFileSource},
Destination: r.Input.Files.Root + r.Input.Files.Destination,
})
}
}

func (r *mergerResource) setInputFilesPatch() {
for index, inputFileSource := range r.Input.Files.Sources {
r.Input.Files.Sources[index] = r.Input.Files.Root + inputFileSource
}
r.Input.items = append(r.Input.items, r.Input.Files)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ spec:
- name: v1alpha1
schema:
openAPIV3Schema:
description: Merger manifest.
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
Expand All @@ -44,6 +45,8 @@ spec:
properties:
destination:
type: string
root:
type: string
sources:
items:
type: string
Expand Down
4 changes: 3 additions & 1 deletion pkg/merger/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
ResourceKind string = "Merger"
)

// Merger manifest body.
// Merger manifest.
type Merger struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata"`
Expand Down Expand Up @@ -49,6 +49,8 @@ type mergerResource struct {
//

type resourceInputFiles struct {
// +optional
Root string `yaml:"root" json:"root"`
Sources []string `yaml:"sources" json:"sources"`
Destination string `yaml:"destination" json:"destination"`
}
Expand Down

0 comments on commit c70ff5e

Please sign in to comment.