Kustomize how to replace only the host in Ingress configuration Kustomize how to replace only the host in Ingress configuration kubernetes kubernetes

Kustomize how to replace only the host in Ingress configuration


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/v1beta1kind: Kustomizationresources:- ../../base/app1patches:- 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"    }]


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/v1kind: Ingressmetadata:  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/v1kind: Ingressmetadata:  name: asap-ingress-internalspec:  rules:    - host: the.real.hostname

kustomization.yaml

resources:- ingress.yamlpatchesStrategicMerge:- 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 )