21

I've got this ingress.yaml base configuration:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  labels:
    sia: aza
    app: asap-ingress-internal
  name: asap-ingress-internal
  annotations:
    kubernetes.io/ingress.class: "nginx-external"
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
    - host: the-host-value
      http:
        paths:
          - path: /asap-srv-template/(.*)
            backend:
              serviceName: asap-srv-template
              servicePort: 8080

And want to replace the spec.rules.host value only (and keep all http.paths as is.

So I create a env-var.yaml like this :

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: asap-ingress-internal
spec:
  rules:
    - host: the.real.hostname

But the result is the following:

$ kustomize build
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx-external
    nginx.ingress.kubernetes.io/use-regex: "true"
  labels:
    app: asap-ingress-internal
    env: dev
    sia: aza
  name: asap-ingress-internal
  namespace: aza-72461-dev
spec:
  rules:
  - host: the.real.hostname

I have lost all http.paths configuration and I can't find out how to do.

I tried with patches: or patchesStrategicMerge in kustomization.yaml but the result is always the same.

Any help would be greatly appreciated

Orlando Osorio
  • 3,116
  • 7
  • 29
  • 52
jmcollin92
  • 2,896
  • 6
  • 27
  • 49

3 Answers3

21

You can use a json patch for this, below is an example.

Here is an example kustomization.yaml. It will call out a patch in the patches section:

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

resources:
- ../../base/app1

patches:
- target:
    kind: Ingress
    name: my-ingress
  path: ingress-patch.json  

Here would be an example ingress-patch.json:

[
    { 
        "op": "replace", 
        "path": "/spec/rules/0/host", 
        "value": "the.real.hostname"
    }
]
mroma
  • 328
  • 1
  • 5
  • Thanks @mroma i will try this – jmcollin92 May 12 '21 at 13:41
  • I already test with the patch but the result is the same: the entire spec is replaced and not patched or merged. I will try your example (and not mine) – jmcollin92 May 12 '21 at 13:56
  • i think in this case its less error prone an easier if one just generate indiviudal ingress yaml files – Fabian Börner Jul 07 '21 at 22:45
  • This solution worked for me, I am still using api version v1beta1. Just one thing to keep in mind is that in the kustomization.yml file the 'name' directive under 'patches' needs to match the name of the ingress resource; otherwise the kustomize build won't fail but the patch won't be applied. – yaach Jan 11 '22 at 14:48
14

Another option is to do inline patch. It's the same approach mroma offered, but without the file. I find it simpler.

# kustomization.yaml

resources:
  - ingress.yaml
patches:
  - target:
      kind: Ingress
      name: asap-ingress-internal
    patch: |-
      - op: replace
        path: /spec/rules/0/host
        value: the.real.hostname
Chen A.
  • 10,140
  • 3
  • 42
  • 61
-3

If you are on a recent version of kubernetes (I think starting form 18) the Ingres api version is no longer the beta one apiVersion: extensions/v1beta1 it is now apiVersion: networking.k8s.io/v1.

I have tested bellow sample and it works :

ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: asap-ingress-internal
  annotations:
    kubernetes.io/ingress.class: "nginx-external"
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
    - host: the-host-value
      http:
        paths:
          - path: /asap-srv-template/(.*)
            backend:
              serviceName: asap-srv-template
              servicePort: 8080

ingress-patch.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: asap-ingress-internal
spec:
  rules:
    - host: the.real.hostname

kustomization.yaml

resources:
- ingress.yaml
patchesStrategicMerge:
- ingress-patch.yaml

Tested with both kubectl kustomize (version of kubectl is v1.19.7) and kustomize build (version of standalone kustomize is v3.5.4 )

Ovidiu Buligan
  • 2,784
  • 1
  • 28
  • 37
  • 9
    It ends up replacing the entire `spec` with just the `rules.host` attributes. All other attributes like `http.paths` etc are gone. Tried with kubectl `v1.21.0` and kustomize `v4.1.2` – Rakib Apr 25 '21 at 20:03
  • @ovidiu-buligan Thanks for the tip about the beta extension. Like said above by Rakib when I test this, the entire spec is replaced. I will check kubectl and kustomize release. To be sure can you please, post the result of the kustomization ? – jmcollin92 May 12 '21 at 13:46
  • There is [an open issue for changing this via patchesStrategicMerge](https://github.com/kubernetes-sigs/kustomize/issues/347) – iamolegga Feb 22 '22 at 19:00