enableServiceLinks=false doesn't disable Kubernetes clusterIP default service environment variables to be injected into pod

Viewed 4248

In pod specification, there is the option enableServiceLinks. When set to false, environment variables related to services running at the moment of pod creation will not be injected into pod.

The problem is that I expected this to also happen with kubernetes clusterIp service on default namespace:

NAME                        TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
kubernetes                  ClusterIP   10.116.0.1     <none>        443/TCP    27d

But it is injecting environment variables into pod as follows:

KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.116.0.1:443
NODE_VERSION=12.18.0
HOSTNAME=static-web
YARN_VERSION=1.22.4
SHLVL=1
HOME=/root
test_value=8585
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.116.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.116.0.1:443
KUBERNETES_SERVICE_HOST=10.116.0.1
PWD=/indecision-app

Deployment file used for deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: indecision-app-deployment
  labels:
    app: indecision-app
spec:
  selector:
    matchLabels:
      app: indecision-app
  template:
    metadata:
      labels:
        app: indecision-app
    spec:
      enableServiceLinks: false
      containers:
      - name: indecision-app
        image: hleal18/indecision-app:latest
        ports:
        - containerPort: 8080

Is this the correct behavior? Is there an API-supported way of avoiding these service environment variables to be injected?

1 Answers

According to the source code:

We always want to add environment variabled for master services from the master service namespace, even if enableServiceLinks is false.

Which basically means that you will and should not prevent those specific environment variables from being injected to the pods.

I hope this solves your issue.

Related