Repository: knative-sandbox/eventing-kafka
Pull request #173 (2020-11-06 18:53:45)

[consolidated] Disable KafkaChannel dispatcher leader election
Fixes https://github.com/knative/eventing-contrib/issues/1659

<!-- Please include the 'why' behind your changes if no issue exists -->

## Proposed Changes

-  Disable KafkaChannel dispatcher leader election
-
-

<!--
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
- 🐛 The consolidated KafkaChannel dispatcher does not attempt anymore to acquire a lease
```

**Docs**

<!--
📖 If this change has user-visible impact, link to an issue or PR in
https://github.com/knative/docs.
-->

Must to be backported to 0.17


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

This pull-request has been approved by: *<a href="https://github.com/knative-sandbox/eventing-kafka/pull/173#" title="Author self-approved">lionelvillard</a>*

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

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/eventing-kafka/blob/master/OWNERS)~~ [lionelvillard]

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/eventing-kafka/pull/173?src=pr&el=h1) Report
> Merging [#173](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/173?src=pr&el=desc) (1ea5608) into [master](https://codecov.io/gh/knative-sandbox/eventing-kafka/commit/f0f9fb35cd41a3785465eaa2098ea09a85e51a14?el=desc) (f0f9fb3) will **decrease** coverage by `0.16%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/173/graphs/tree.svg?width=650&height=150&src=pr&token=W765CT93QC)](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/173?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #173      +/-   ##
==========================================
- Coverage   75.56%   75.39%   -0.17%     
==========================================
  Files         112      112              
  Lines        4130     4130              
==========================================
- Hits         3121     3114       -7     
- Misses        816      823       +7     
  Partials      193      193              
```


| [Impacted Files](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/173?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [...el/distributed/dispatcher/dispatcher/dispatcher.go](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/173/diff?src=pr&el=tree#diff-cGtnL2NoYW5uZWwvZGlzdHJpYnV0ZWQvZGlzcGF0Y2hlci9kaXNwYXRjaGVyL2Rpc3BhdGNoZXIuZ28=) | `68.08% <0.00%> (-7.45%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/173?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/173?src=pr&el=footer). Last update [f0f9fb3...c8e26c2](https://codecov.io/gh/knative-sandbox/eventing-kafka/pull/173?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/cmd/channel/consolidated/dispatcher/main.go b/cmd/channel/consolidated/dispatcher/main.go
index 476dd8d0e..3d36c63e7 100644
--- a/cmd/channel/consolidated/dispatcher/main.go
+++ b/cmd/channel/consolidated/dispatcher/main.go
@@ -35,5 +35,8 @@ func main() {
 		ctx = injection.WithNamespaceScope(ctx, ns)
 	}
 
+	// Do not run the dispatcher in leader-election mode
+	ctx = sharedmain.WithHADisabled(ctx)
+
 	sharedmain.MainWithContext(ctx, component, controller.NewController)
 }
diff --git a/config/channel/consolidated/roles/dispatcher-clusterrole.yaml b/config/channel/consolidated/roles/dispatcher-clusterrole.yaml
index 2d2fd231b..bfe23095c 100644
--- a/config/channel/consolidated/roles/dispatcher-clusterrole.yaml
+++ b/config/channel/consolidated/roles/dispatcher-clusterrole.yaml
@@ -68,14 +68,3 @@ rules:
     verbs:
       - create
       - patch
-  - apiGroups:
-      - "coordination.k8s.io"
-    resources:
-      - leases
-    verbs:
-      - get
-      - list
-      - watch
-      - create
-      - patch
-      - update
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",