I am using Traefik ingress controller for my kubernetes cluster.
I have defined a IngressRoute of Traefik :
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: my-app-route
spec:
entryPoints:
- web
routes:
- match: Host(``) # I want to fill in the value of host in kustomization.yml
kind: Rule
services:
- name: my-app
port: 8000
I am using Kustomize to manage my k8s manifests.
I would like to replace or fill-in the value of
Host(``)
to something like:
Host(`www.alpha.app.com`)
I tried in my kustomization.yml:
# generate config map for the host value
configMapGenerator:
- name: my-config
literals:
- host=www.alpha.app.com
# replace the value
replacements:
- source:
kind: ConfigMap
name: my-config
fieldPath: data.host
targets:
- select:
kind: IngressRoute
name: my-app-route
fieldPaths:
- spec.routes.match
I know it won't work since the value contains string Host(``), but that is how far I can make. I want to keep the configMap data as it is, how to replace the Host(``) to Host(`www.alpha.app.com`) using Kustomize?
A possible workaround would be to set a placeholder value in your
IngressRouteYAML where the host should be. For example, you might use__HOST_PLACEHOLDER__:Then use the
replacementsfield in yourkustomization.ymlto replace the entirespec.routes.matchfield (which contains the placeholder) with the value from the ConfigMap.Note that
kubernetes-sigs/kustomizeissue 4012 discusses a similar use case, but it is stale (closed by k8s-ci-robot).The discussion highlights that the
ReplacementTransformerin Kustomize, at the time of that issue, had limitations when dealing with scenarios where only a part of a field's value (like a placeholder within a string) needs to be replaced. That is precisely the challenge you are facing with replacing__HOST_PLACEHOLDER__.The issue references this Stack Overflow answer, which could involve defining the
Hostvalue as a shell variable: That will be used to create the patch file with the correct host value.Then create an
IngressRoutepatch file, which will contain theIngressRouteconfiguration with the host value dynamically inserted.And include the patch in
kustomization.yaml, to be included as a strategic merge patch to apply the changes.Kustomize will apply the
ingressroute-patch.ymlas a strategic merge patch to the originalIngressRouteresource, effectively replacing the host part of thematchfield with the desired value.However,
patchesStrategicMergeis deprecated in Kustomize v5.0.0+, and use thepatchesfield.So that would be:
And:
That workaround would bypass the limitation of Kustomize regarding partial string replacements within a field. By pre-generating a patch file with the correct host value and including it in the
kustomization.yaml, you could dynamically set the host in theIngressRoute.