Kustomize: Ingress serviceName doesn't expand vars

Created on 12 Dec 2019  路  2Comments  路  Source: kubernetes-sigs/kustomize

For example, the following example doesn't seem to expand the vars for serviceName in Ingress resource:

kustomization.yml:

commonLabels:
  app: my-app

imageTags:
  - name: my-app
    newName: localhost/my-app
    newTag: latest

bases:
- app

namePrefix: dev-

vars:
  - name: APP_SERVICE
    objref:
      kind: Service
      name: my-app-service
      apiVersion: v1
    fieldref:
      fieldpath: metadata.name

app/kustomization.yml

resources:
    - deployment.yml
    - ingress.yml

app/deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
  labels:
    app: my-app
spec:
  replicas: 1
  template:
    spec:
      containers:
      - name: my-app
        image: my-app
        ports:
        - containerPort: 808

app/ingress.yml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: my-app
spec:
  tls:
    - hosts:
        - foo.bar.com
      secretName: tls-secret5
  rules:
    - host: foo.bar.com
      http:
        paths:
          - backend:
              serviceName: $(APP_SERVICE)
              servicePort: 8080
---
kind: Service
apiVersion: v1
metadata:
  name: my-app-service
spec:
  type: LoadBalancer
  ports:
  - port: 8080
    name: http
  - port: 8181
    name: http-ssl
  selector:
    app: my-app

However, the APP_SERVICE isn't expanded when I run I get the following warning:

2019/12/11 21:19:46 well-defined vars that were never replaced: APP_SERVICE

The version of kustomization I'm using is:
kustomize version
Version: {Version:3.2.1 GitCommit:d89b448c745937f0cf1936162f26a5aac688f840 BuildDate:2019-10-03T19:25:14+01:00 GoOs:darwin GoArch:amd64}

Most helpful comment

One way to work around this is to add a custom varReference:

cat ./kustomizeconfig/config.yaml

varReference:
- path: spec/rules/http/paths/backend/serviceName
  kind: Ingress

cat ./kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- base/
configurations:
- kustomizeconfig/config.yaml

The defaults are defined here:

https://github.com/kubernetes-sigs/kustomize/blob/master/api/konfig/builtinpluginconsts/varreference.go

All 2 comments

One way to work around this is to add a custom varReference:

cat ./kustomizeconfig/config.yaml

varReference:
- path: spec/rules/http/paths/backend/serviceName
  kind: Ingress

cat ./kustomization.yaml

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- base/
configurations:
- kustomizeconfig/config.yaml

The defaults are defined here:

https://github.com/kubernetes-sigs/kustomize/blob/master/api/konfig/builtinpluginconsts/varreference.go

I was not aware of this technique, thank you, this will work nicely.

Was this page helpful?
0 / 5 - 0 ratings