Question: How can I to source the Kubernetes deployment/job name that spawned the current pod from within the pod?

Answer: 
In many cases the hostname of the Pod equals to the name of the Pod (you can access that by the HOSTNAME environment variable). However that's not a reliable method of determining the Pod's identity.

You will want to you use the [Downward API](https://kubernetes.io/docs/user-guide/downward-api/) which allows you to expose metadata as environment variables and/or files on a volume.

The name and namespace of a Pod can be exposed as environment variables (fields: `metadata.name` and `metadata.namespace`) but the information about the creator of a Pod (which is the *annotation* `kubernetes.io/created-by`) can only be exposed as a file.

Example:

```
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: busybox
  labels: {app: busybox}
spec:
  selector: {matchLabels: {app: busybox}}
  template:
    metadata: {labels: {app: busybox}}
    spec:
      containers:
      - name: busybox
        image: busybox
        command:
        - "sh"
        - "-c"
        - |
          echo "I am $MY_POD_NAME in the namespace $MY_POD_NAMESPACE"
          echo
          grep ".*" /etc/podinfo/*
          while :; do sleep 3600; done
        env:
        - name: MY_POD_NAME
          valueFrom: {fieldRef: {fieldPath: metadata.name}}
        - name: MY_POD_NAMESPACE
          valueFrom: {fieldRef: {fieldPath: metadata.namespace}}
        volumeMounts:
        - name: podinfo
          mountPath: /etc/podinfo/
      volumes:
        - name: podinfo
          downwardAPI:
            items:
              - path: "labels"
                fieldRef: {fieldPath: metadata.labels}
              - path: "annotations"
                fieldRef: {fieldPath: metadata.annotations}

```

Too see the output:

```
$ kubectl logs `kubectl get pod -l app=busybox -o name | cut -d / -f2`

```

Output:

```
I am busybox-1704453464-m1b9h in the namespace default

/etc/podinfo/annotations:kubernetes.io/config.seen="2017-02-16T16:46:57.831347234Z"
/etc/podinfo/annotations:kubernetes.io/config.source="api"
/etc/podinfo/annotations:kubernetes.io/created-by="{\"kind\":\"SerializedReference\",\"apiVersion\":\"v1\",\"reference\":{\"kind\":\"ReplicaSet\",\"namespace\":\"default\",\"name\":\"busybox-1704453464\",\"uid\":\"87b86370-f467-11e6-8d47-525400247352\",\"apiVersion\":\"extensions\",\"resourceVersion\":\"191157\"}}\n"
/etc/podinfo/annotations:kubernetes.io/limit-ranger="LimitRanger plugin set: cpu request for container busybox"
/etc/podinfo/labels:app="busybox"
/etc/podinfo/labels:pod-template-hash="1704453464"

```
If you are using the Downwards API to get deployment name from inside the pod, and you want to avoid using the volume mount way - there is one opinionated way to get deployment info, exposed to pod as **environment variables**.

Template labels specified in a Deployment spec are added as pod labels to each pod of that deployment.
Example : the `app` label below will be added to all pods of this deployment

```yaml
...
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
...

```

It is a *commonly followed* (again, not necessarily true for your case) convention for deployments, to keep the `app` label value same as the the deployment name, as shown in the above example. If your deployments follow this convention (mine did), you can expose this label's value (essentially, the name of deployment) as an environment variable to the pod, using the downwards API

Continuing on above example :

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
        env:
          - name: NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: DEPLOYMENT_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.labels['app']

```

Again, clarifying that this is not a guaranteed solution for your problem as it still **does not give the deployment name** in env vars. It is just an opinionated way which I found useful and thought would be good to share.

In my case, there were a lot of deployments (>20) and I didn't want to add the deployment name manually as an env variable, for each of the deployment config. As my deployments already followed the above convention, I just copied the bit of yaml specifying NAMESPACE and DEPLOYMENT\_NAME variable to each deployment config

references :

* <https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#capabilities-of-the-downward-api>