5

I have a GKE/GCE ingress file which has 2 hosts.

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host: myfirstdomain # <-------- override this via kustomize
    http:
      paths:
      - path: /abc
        backend:
          serviceName: abc
          servicePort: abc
      - path: /def
        backend:
          serviceName: def
          servicePort: def
      - path: /ghi
        backend:
          serviceName: ghi
          servicePort: ghi
  - host: myseconddomain # <-------- override this via kustomize
    http:
      paths:
      - backend:
          serviceName: xyz
          servicePort: xyz

I want to declare the host values via kustomize so that i can specify different host values for different environments.

My overlay patch file for dev environment looks like this:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host: dev1.example.com
  - host: dev2.example.com

However, the output of running kustomize build overlays/dev looks like this:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: my-ingress
spec:
  rules:
  - host: dev1.example.com
  - host: dev2.example.com

All other specs are lost.

It ends up replacing the entire host blocks instead of replacing just the host lines.

How can i solve this? I am using:

  • kustomize version v4.1.2
  • kubectl version v1.21.0
Rakib
  • 12,376
  • 16
  • 77
  • 113

1 Answers1

9

You can not do that using strategic merge since the spec.rules is an array of items. You have to use json merge by providing the exact paths to be impacted.

You can create a separate patch file for json merge like below:

# overlays/dev/ingress-json-patch.yaml

- op: replace
  path: /spec/rules/0/host
  value: dev1.example.com
- op: replace
  path: /spec/rules/1/host
  value: dev2.example.com

Then refer to that patch file by adding the following into your kustomization.yaml:

# overlays/dev/kustomization.yaml

patchesJson6902:
- target:
    group: extensions
    version: v1beta1
    kind: Ingress
    name: my-ingress
  path: overlays/dev/ingress-json-patch.yaml
Rakib
  • 12,376
  • 16
  • 77
  • 113
Munim
  • 2,626
  • 1
  • 19
  • 28