Darkwind
Darkwind

Reputation: 345

How to merge two yaml files in one configmap via Kustomize?

I got APP whitch use config in yaml format Config is:

application:
    web-server:
      init-timeout: 2m
      shutdown-timeout: 1m
      sites-сonfig: ./app/sites/prod
kerberos:
  principal: [email protected]
  keytab: /kerberos/keytab/test.keytab

I want to change settings of this config via Kustomize

Now I create in ./base catalog kustomization.yaml contains:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
  - deployment.yaml

configMapGenerator:
- name: config-app
  files:
  - config.yml

Next I create in ./base catalog config.yml contains my application config:

application:
    web-server:
      init-timeout: 2m
      shutdown-timeout: 1m
      sites-сonfig: ./app/sites/prod
kerberos:
  principal: [email protected]
  keytab: /kerberos/keytab/test.keytab

And I create env catalog for my stage & test environment

for example catalog ./env/test contains kustomization.yaml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

resources:
- ../../base



patches:
- replica_count.yml

configMapGenerator:
  - name: config-app
    behavior: merge
    files:
      - config.yml
    options:
        disableNameSuffixHash: true      

File config.yml in directory ./env/test contains:

application:
kerberos:
  principal: [email protected]
  keytab: /kerberos/keytab/test_user.keytab

When I run kustomize build | kubectl apply --prune -f , I see this config-app in configMaps:

application:
    web-server:
      init-timeout: 2m
      shutdown-timeout: 1m
      sites-сonfig: ./app/sites/prod
kerberos:
  principal: [email protected]
  keytab: /kerberos/keytab/test.keytab

How I can merge some settings from yaml in ./env/test to main config in ./base ? I don't want to duplicate full yaml config to ./env/test

Upvotes: 1

Views: 2600

Answers (2)

Ahmed AbouZaid
Ahmed AbouZaid

Reputation: 2888

I developed Merger, a Kustomize that makes merging much easier (using a schemaless StrategicMerge).

I've added your exact example to the common use cases.

Here is how to do it with Merger using your case (also Secret output is supported).

Files:

Assuming that your files config.yml and replica_count.yml are already created in the same dir.

# kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization

generators:
- merger.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
spec:
  resources:
  - name: config-app
    input:
      method: overlay
      files:
        root: /mnt
        # The source overrides the destination.
        sources:
        - config.yml # replica_count.yml
        destination: config-base.yml
    merge:
      strategy: combine
    output:
      format: configmap

Build:

kustomize build --enable-alpha-plugins --as-current-user .

Output:

kind: ConfigMap
metadata:
  name: config-app
apiVersion: v1
data:
  config.yml: |
    application:
        web-server:
            init-timeout: 2m
            shutdown-timeout: 1m
            sites-сonfig: ./app/sites/prod
    kerberos:
        keytab: /kerberos/keytab/test_user.keytab
        principal: [email protected]

If you have other use cases, feel free to create an issue in the project repo.

Upvotes: 0

Johnny White
Johnny White

Reputation: 61

The ConfigMapGenerator is not planned to support this type of merging functionality. He is overriding files with same names. Same user issue https://github.com/kubernetes-sigs/kustomize/issues/370. Its sad) I think to solve this problem you may use profiles:

config.yml common config
config-test.yml additional test env config.

And your application reading files in right order

Upvotes: 1

Related Questions