Repository: knative-sandbox/kn-plugin-func
Pull request #660 (2021-11-15 19:40:53)

feat: add POD_NAME as an environment variable

# Changes

:gift: This change adds POD_NAME as an environment variable for a deployed
function using the Kubernetes downward API. An alternative means of exposing
this information would be through the filesystem. For example, a file at
`/etc/func/pod_name` would contain the pod's name. I chose to use the env
as the place to surface this because it was easier to implement, and likely
easier to consume for a function developer.

The motivation for this change is that the Node.js and TypeScript invocation
runtime will be exposing telemetry/metrics through Prometheus at a `/metrics`
endpoint (see: https://github.com/boson-project/faas-js-runtime/pull/109).
The OpenTelemetry specification has some experimental labels for function
runtime environments which don't specifically mention pod name (the spec
is not unique to Kubernetes), but do imply the need for a unique identifier
such as this.

Ref: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/semantic_conventions/faas.md

/kind enhancement



knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative-sandbox/kn-plugin-func/pull/660#" title="Author self-approved">lance</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative-sandbox%2Fkn-plugin-func).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/knative-sandbox/kn-plugin-func/blob/main/OWNERS)~~ [lance]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
codecov[bot]: # [Codecov](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/660?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) Report
> Merging [#660](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/660?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) (59a9536) into [main](https://codecov.io/gh/knative-sandbox/kn-plugin-func/commit/597195bab81aa5f69540f8524924529b22611c99?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) (597195b) will **increase** coverage by `0.17%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/660/graphs/tree.svg?width=650&height=150&src=pr&token=da1B9bMsQ9&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox)](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/660?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox)

```diff
@@            Coverage Diff             @@
##             main     #660      +/-   ##
==========================================
+ Coverage   37.52%   37.69%   +0.17%     
==========================================
  Files          38       38              
  Lines        3891     3897       +6     
==========================================
+ Hits         1460     1469       +9     
+ Misses       2238     2235       -3     
  Partials      193      193              
```


| [Impacted Files](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/660?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox) | Coverage Δ | |
|---|---|---|
| [knative/deployer.go](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/660/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox#diff-a25hdGl2ZS9kZXBsb3llci5nbw==) | `7.71% <100.00%> (+2.08%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/660?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/660?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox). Last update [597195b...59a9536](https://codecov.io/gh/knative-sandbox/kn-plugin-func/pull/660?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=knative-sandbox).

knative-prow-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/knative-sandbox/kn-plugin-func/pull/660#" title="Author self-approved">lance</a>*, *<a href="https://github.com/knative-sandbox/kn-plugin-func/pull/660#pullrequestreview-806499874" title="LGTM">lkingland</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=knative-sandbox%2Fkn-plugin-func).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/knative-sandbox/kn-plugin-func/blob/main/OWNERS)~~ [lance,lkingland]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/knative/deployer.go b/knative/deployer.go
index 6b0d71c6..b0ff0fcd 100644
--- a/knative/deployer.go
+++ b/knative/deployer.go
@@ -392,7 +392,14 @@ func processLabels(f fn.Function) (map[string]string, error) {
 //   - value: {{ configMap:configMapName }}      # all key-pair values from ConfigMap are set as ENV
 func processEnvs(envs []fn.Env, referencedSecrets, referencedConfigMaps *sets.String) ([]corev1.EnvVar, []corev1.EnvFromSource, error) {
 
-	envVars := []corev1.EnvVar{{Name: "BUILT", Value: time.Now().Format("20060102T150405")}}
+	envVars := []corev1.EnvVar{
+		{Name: "BUILT", Value: time.Now().Format("20060102T150405")},
+		{Name: "POD_NAME", ValueFrom: &corev1.EnvVarSource{
+			FieldRef: &corev1.ObjectFieldSelector{
+				FieldPath: "metadata.name",
+			},
+		}},
+	}
 	envFrom := []corev1.EnvFromSource{}
 
 	for _, env := range envs {


Test patch:
diff --git a/knative/deployer_test.go b/knative/deployer_test.go
index 43065e42..dc8e9771 100644
--- a/knative/deployer_test.go
+++ b/knative/deployer_test.go
@@ -11,6 +11,28 @@ import (
 	fn "knative.dev/kn-plugin-func"
 )
 
+// Tests that the deployer sets an environment variable POD_NAME
+// with a reference to the Pod's metadata using the downward API
+// https://kubernetes.io/docs/tasks/inject-data-application/environment-variable-expose-pod-information/
+func Test_setsPodNameAsEnvVar(t *testing.T) {
+	f := fn.Function{
+		Name: "testing",
+	}
+	envs, _, err := processEnvs(f.Envs, nil, nil)
+	if err != nil {
+		t.Error(err)
+	}
+	// TODO: is this too fragile, knowing that it should be
+	// the second value in the array?
+	env := envs[1]
+	if env.Name != "POD_NAME" {
+		t.Errorf("Expected 'POD_NAME' got '%s'", env.Name)
+	}
+	if env.ValueFrom.FieldRef.FieldPath != "metadata.name" {
+		t.Errorf("Expected 'metadata.name' got '%s'\n", env.ValueFrom.FieldRef.FieldPath)
+	}
+}
+
 func Test_setHealthEndpoints(t *testing.T) {
 	f := fn.Function{
 		Name: "testing",