Repository: knative-sandbox/eventing-kafka (go)

Issue:
[consolidated] Namespaced KafkaChannel is broken
**Describe the bug**
When creating a namespaced KafkaChannel, the dispatcher crashes with this error:

```
{"level":"fatal","ts":"2020-11-06T16:50:58.731Z","logger":"kafkachannel-dispatcher","caller":"dispatcher/kafkachannel.go:106","msg":"Unable to create kafka dispatcher","error":"missing required environment variable 'POD_NAME'","stacktrace":"knative.dev/eventing-kafka/pkg/channel/consolidated/reconciler/dispatcher.NewController\n\tknative.dev/eventing-kafka/pkg/channel/consolidated/reconciler/dispatcher/kafkachannel.go:106\nknative.dev/pkg/injection/sharedmain.ControllersAndWebhooksFromCtors\n\tknative.dev/pkg@v0.0.0-20201103163404-5514ab0c1fdf/injection/sharedmain/main.go:364\nknative.dev/pkg/injection/sharedmain.MainWithConfig\n\tknative.dev/pkg@v0.0.0-20201103163404-5514ab0c1fdf/injection/sharedmain/main.go:199\nknative.dev/pkg/injection/sharedmain.MainWithContext\n\tknative.dev/pkg@v0.0.0-20201103163404-5514ab0c1fdf/injection/sharedmain/main.go:142\nmain.main\n\tknative.dev/eventing-kafka/cmd/channel/consolidated/dispatcher/main.go:38\nruntime.main\n\truntime/proc.go:203"}
```

 **Expected behavior**
A clear and concise description of what you expected to happen.

 **To Reproduce**
Steps to reproduce the behavior.
Same steps as https://github.com/knative/eventing-contrib/issues/1659

 **Knative release version**
0.18.4, master

 **Additional context**
Add any other context about the problem here such as proposed priority


Pull request:
fix namespaced kafka dispacher crash
Fixes #171

<!-- Please include the 'why' behind your changes if no issue exists -->

## Proposed Changes

- Add missing env vars
-
-

<!--
If this change has user-visible impact, follow the instructions below.
Examples include:

- 🎁 Add new feature
- 🐛 Fix bug
- 🧽 Update or clean up current behavior
- 🗑️ Remove feature or internal logic

Otherwise delete the rest of this template.
-->

**Release Note**

<!--
🗒️ If this change has user-visible impact, write a release note in the block
below. Include the string "action required" if additional action is required of
users switching to the new release, for example in case of a breaking change.

Write as if you are speaking to users, not other Knative contributors. If this
change has no user-visible impact, no release-note is needed.
-->

```release-note
- 🐛 Fix crash in the consolidated KafkaChannel namespaced dispatcher
```

**Docs**

<!--
📖 If this change has user-visible impact, link to an issue or PR in
https://github.com/knative/docs.
-->
 

Patch:
diff --git a/pkg/channel/consolidated/reconciler/controller/resources/dispatcher.go b/pkg/channel/consolidated/reconciler/controller/resources/dispatcher.go
index b19f53b75..cae363d23 100644
--- a/pkg/channel/consolidated/reconciler/controller/resources/dispatcher.go
+++ b/pkg/channel/consolidated/reconciler/controller/resources/dispatcher.go
@@ -20,7 +20,6 @@ import (
 	v1 "k8s.io/api/apps/v1"
 	corev1 "k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
-
 	"knative.dev/pkg/system"
 )
 
@@ -124,6 +123,16 @@ func makeEnv(args DispatcherArgs) []corev1.EnvVar {
 					FieldPath: "metadata.namespace",
 				},
 			},
+		}, corev1.EnvVar{
+			Name: "POD_NAME",
+			ValueFrom: &corev1.EnvVarSource{
+				FieldRef: &corev1.ObjectFieldSelector{
+					FieldPath: "metadata.name",
+				},
+			},
+		}, corev1.EnvVar{
+			Name:  "CONTAINER_NAME",
+			Value: "dispatcher",
 		})
 	}
 


Test patch:
diff --git a/pkg/channel/consolidated/reconciler/controller/resources/dispatcher_test.go b/pkg/channel/consolidated/reconciler/controller/resources/dispatcher_test.go
index 2d8c330b9..ee4bbef33 100644
--- a/pkg/channel/consolidated/reconciler/controller/resources/dispatcher_test.go
+++ b/pkg/channel/consolidated/reconciler/controller/resources/dispatcher_test.go
@@ -78,8 +78,7 @@ func TestNewDispatcher(t *testing.T) {
 							}, {
 								Name:  "CONFIG_LEADERELECTION_NAME",
 								Value: "config-leader-election-kafka",
-							},
-							},
+							}},
 							Ports: []corev1.ContainerPort{{
 								Name:          "metrics",
 								ContainerPort: 9090,
@@ -168,6 +167,16 @@ func TestNewNamespaceDispatcher(t *testing.T) {
 										FieldPath: "metadata.namespace",
 									},
 								},
+							}, {
+								Name: "POD_NAME",
+								ValueFrom: &corev1.EnvVarSource{
+									FieldRef: &corev1.ObjectFieldSelector{
+										FieldPath: "metadata.name",
+									},
+								},
+							}, {
+								Name:  "CONTAINER_NAME",
+								Value: "dispatcher",
 							}},
 							Ports: []corev1.ContainerPort{{
 								Name:          "metrics",