2

In my kustomization.yaml I have:

configMapGenerator:
  - name: nginx-config
    files:
      - nginx.conf
vars:
  - name: PHP_FPM
    objref:
      kind: Service
      name: app-service
      apiVersion: v1
    fieldref:
      fieldpath: metadata.name

And then in nginx.conf I have:

    location = /index.php {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass $(PHP_FPM):9000;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /app/index.php;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

When I run kustomize the var isn't replaced:

❯ kustomize build kubernetes/overlays/staging | grep fastcgi_pass
2020/06/07 18:25:05 well-defined vars that were never replaced: PHP_FPM
            fastcgi_pass $(PHP_FPM):9000;

How can I get my service name into nginx.conf?

mpen
  • 313
  • 5
  • 14
  • 1
    Kustomize can be customized to allow vars in `ConfigMap.data`. It's not well-documented, but you can find it buried in some Github issue which I don't feel like digging up right now. – mpen Jul 31 '20 at 18:22
  • See varReference configurations: https://github.com/innoq/advanced-kustomize-examples/tree/main/var-reference-configuration-example – Joe Bowbeer Aug 13 '22 at 04:33

1 Answers1

2

According to the fact that Nginx doesn't support variables in the config file, the file should be adjusted before it's applied to the Nginx container.

Kustomize vars allowed only in particular places and ConfigMap.data is not one of them at this moment.

The error tells you that PHP_FPM variable is defined in kustomization.yaml but never used for replacement. Link1 Link2

Nevertheless, the problem could be solved by adjusting nginx.conf content using the Init Container and then mount it to the Main container.

Example:

kustomization.yaml:

resources:
- cm-init-pod.yaml
- svc.yaml

patches:
- patch.yaml

configMapGenerator:
  - name: mymap
    files:
      - nginx.conf
vars:
  - name: PHP_FPM
    objref:
      kind: Service
      name: app-service
      apiVersion: v1
    fieldref:
      fieldpath: metadata.name

cm-init-pod.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: cm-vol-pod 
  labels:
    app: nginx
spec:
  containers:
    - name: nginx 
      image: nginx

nginx.conf:

blalb-a
bla-bla
backend ##PHP_FPM##
bl-abla
bla-bla

svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: app-service
spec:
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx

patch.yaml

apiVersion: v1
kind: Pod
metadata:
  name: cm-vol-pod 
spec:
  containers:
    - name: nginx 
      image: nginx
      command: [ "bash", "-c", "nginx -g \"daemon off;\""  ]
      volumeMounts:
      - name: ed
# this is not a real nginx.conf so I've put it next to the original one
        mountPath: /etc/nginx/nginx.conf1 
        subPath: nginx.conf
  initContainers:
    - name: init
      image: nginx
      command: [ "/bin/sh", "-c", "cp /mnt/cm/nginx.conf /mnt/ed/nginx.conf && sed -i 's/##PHP_FPM##/$(PHP_FPM)/' /mnt/ed/nginx.conf" ]
      volumeMounts:
      - name: cm
        mountPath: /mnt/cm
      - name: ed
        mountPath: /mnt/ed
  volumes:
    - name: cm
      configMap:
        name: mymap
    - name: ed
      emptyDir: {}

Results:

$ kustomize build | kubectl apply -f -
# or kubectl apply -k ./

configmap/mymap-k2hbfmf776 created
service/app-service created
pod/cm-vol-pod created

$ kubectl exec -it cm-vol-pod -- bash

root@cm-vol-pod:/# cat /etc/nginx/nginx.conf1

blalb-a
bla-bla
backend app-service
bl-abla
bla-bla

Let's clean up everything

$ kustomize build | kubectl delete -f -

# or

$ kubectl delete -k ./

configmap "mymap-k2hbfmf776" deleted
service "app-service" deleted
pod "cm-vol-pod" deleted

VAS
  • 370
  • 1
  • 9
  • Seems like a pretty heavy solution to replace one var, but I guess it works :-) – mpen Jul 31 '20 at 18:25
  • I just wanted to keep everything important that you mentioned to ensure that it fits the proposed flow. Eventually, You can end-up with just one well-tailored InitContainer. – VAS Jul 31 '20 at 18:30
  • https://stackoverflow.com/questions/61375369/openshift-or-kubernate-environment-variables-from-file – VAS Jul 31 '20 at 18:34