This repository has been archived by the owner on Jun 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
Fix Kafka Channel dispatcher ownerRef #1536
Merged
knative-prow-robot
merged 7 commits into
knative:master
from
aliok:fix-kafka-ch-dispatcher-ownerref2
Sep 10, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
09e6273
Fix Kafka Channel dispatcher ownerRef
aliok 811ad7a
Fix Kafka Channel dispatcher ownerRef
aliok 9bb36be
Fix Kafka Channel dispatcher ownerRef
aliok 59e3a1f
Addressing comments
aliok 08a4156
Make more readable
aliok 38aba44
Unit tests for scale up
aliok e655f90
Unit tests for `findContainer`
aliok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Copyright 2020 The Knative Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: kafka-ch-dispatcher | ||
namespace: knative-eventing | ||
labels: | ||
eventing.knative.dev/release: devel | ||
spec: | ||
# this deployment is going to be scaled up by the | ||
# controller when the very first KafkaChannel is created | ||
replicas: 0 | ||
selector: | ||
matchLabels: | ||
messaging.knative.dev/channel: kafka-channel | ||
messaging.knative.dev/role: dispatcher | ||
template: | ||
metadata: | ||
labels: | ||
messaging.knative.dev/channel: kafka-channel | ||
messaging.knative.dev/role: dispatcher | ||
eventing.knative.dev/release: devel | ||
spec: | ||
containers: | ||
- name: dispatcher | ||
image: ko://knative.dev/eventing-contrib/kafka/channel/cmd/channel_dispatcher | ||
env: | ||
- name: SYSTEM_NAMESPACE | ||
valueFrom: | ||
fieldRef: | ||
apiVersion: v1 | ||
fieldPath: metadata.namespace | ||
- name: METRICS_DOMAIN | ||
value: "knative.dev/eventing" | ||
- name: CONFIG_LOGGING_NAME | ||
value: "config-logging" | ||
- name: CONFIG_LEADERELECTION_NAME | ||
value: "config-leader-election-kafka" | ||
ports: | ||
- containerPort: 9090 | ||
name: metrics | ||
protocol: TCP | ||
volumeMounts: | ||
- name: config-kafka | ||
mountPath: /etc/config-kafka | ||
serviceAccountName: kafka-ch-dispatcher | ||
volumes: | ||
- name: config-kafka | ||
configMap: | ||
name: config-kafka | ||
|
||
--- | ||
|
||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
labels: | ||
messaging.knative.dev/channel: kafka-channel | ||
messaging.knative.dev/role: dispatcher | ||
name: kafka-ch-dispatcher | ||
namespace: knative-eventing | ||
spec: | ||
ports: | ||
- name: http-dispatcher | ||
port: 80 | ||
protocol: TCP | ||
targetPort: 8080 | ||
selector: | ||
messaging.knative.dev/channel: kafka-channel | ||
messaging.knative.dev/role: dispatcher |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,8 @@ import ( | |
"context" | ||
"errors" | ||
"fmt" | ||
"reflect" | ||
|
||
"k8s.io/utils/pointer" | ||
|
||
"github.com/Shopify/sarama" | ||
|
||
|
@@ -274,6 +275,7 @@ func (r *Reconciler) reconcileDispatcher(ctx context.Context, scope string, disp | |
DispatcherScope: scope, | ||
DispatcherNamespace: dispatcherNamespace, | ||
Image: r.dispatcherImage, | ||
Replicas: 1, | ||
} | ||
|
||
expected := resources.MakeDispatcher(args) | ||
|
@@ -294,17 +296,51 @@ func (r *Reconciler) reconcileDispatcher(ctx context.Context, scope string, disp | |
logging.FromContext(ctx).Errorw("Unable to get the dispatcher deployment", zap.Error(err)) | ||
kc.Status.MarkDispatcherUnknown("DispatcherDeploymentFailed", "Failed to get dispatcher deployment: %v", err) | ||
return nil, err | ||
} else if !reflect.DeepEqual(expected.Spec.Template.Spec.Containers[0].Image, d.Spec.Template.Spec.Containers[0].Image) { | ||
logging.FromContext(ctx).Infof("Deployment image is not what we expect it to be, updating Deployment Got: %q Expect: %q", expected.Spec.Template.Spec.Containers[0].Image, d.Spec.Template.Spec.Containers[0].Image) | ||
d, err := r.KubeClientSet.AppsV1().Deployments(dispatcherNamespace).Update(expected) | ||
if err == nil { | ||
controller.GetEventRecorder(ctx).Event(kc, corev1.EventTypeNormal, dispatcherDeploymentUpdated, "Dispatcher deployment updated") | ||
kc.Status.PropagateDispatcherStatus(&d.Status) | ||
return d, nil | ||
} else { | ||
kc.Status.MarkServiceFailed("DispatcherDeploymentUpdateFailed", "Failed to update the dispatcher deployment: %v", err) | ||
} else { | ||
existing := utils.FindContainer(d, resources.DispatcherContainerName) | ||
if existing == nil { | ||
logging.FromContext(ctx).Errorw("Container %s does not exist in existing dispatcher deployment. Updating the deployment", resources.DispatcherContainerName) | ||
d, err := r.KubeClientSet.AppsV1().Deployments(dispatcherNamespace).Update(expected) | ||
if err == nil { | ||
controller.GetEventRecorder(ctx).Event(kc, corev1.EventTypeNormal, dispatcherDeploymentUpdated, "Dispatcher deployment updated") | ||
kc.Status.PropagateDispatcherStatus(&d.Status) | ||
return d, nil | ||
} else { | ||
kc.Status.MarkServiceFailed("DispatcherDeploymentUpdateFailed", "Failed to update the dispatcher deployment: %v", err) | ||
} | ||
return d, newDeploymentWarn(err) | ||
} | ||
|
||
expectedContainer := utils.FindContainer(expected, resources.DispatcherContainerName) | ||
if expectedContainer == nil { | ||
return nil, errors.New(fmt.Sprintf("Container %s does not exist in expected dispatcher deployment. Cannot check if the deployment needs an update.", resources.DispatcherContainerName)) | ||
} | ||
|
||
needsUpdate := false | ||
|
||
if existing.Image != expectedContainer.Image { | ||
logging.FromContext(ctx).Infof("Dispatcher deployment image is not what we expect it to be, updating Deployment Got: %q Expect: %q", expected.Spec.Template.Spec.Containers[0].Image, d.Spec.Template.Spec.Containers[0].Image) | ||
existing.Image = expectedContainer.Image | ||
needsUpdate = true | ||
} | ||
|
||
if *d.Spec.Replicas == 0 { | ||
logging.FromContext(ctx).Infof("Dispatcher deployment has 0 replicas. Scaling up deployment to 1 replicas") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: replicas -> replica, both instances. |
||
d.Spec.Replicas = pointer.Int32Ptr(1) | ||
needsUpdate = true | ||
} | ||
|
||
if needsUpdate { | ||
d, err := r.KubeClientSet.AppsV1().Deployments(dispatcherNamespace).Update(d) | ||
if err == nil { | ||
controller.GetEventRecorder(ctx).Event(kc, corev1.EventTypeNormal, dispatcherDeploymentUpdated, "Dispatcher deployment updated") | ||
kc.Status.PropagateDispatcherStatus(&d.Status) | ||
return d, nil | ||
} else { | ||
kc.Status.MarkServiceFailed("DispatcherDeploymentUpdateFailed", "Failed to update the dispatcher deployment: %v", err) | ||
return d, newDeploymentWarn(err) | ||
} | ||
} | ||
return d, newDeploymentWarn(err) | ||
} | ||
|
||
kc.Status.PropagateDispatcherStatus(&d.Status) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that like: https://github.com/knative/eventing-contrib/pull/1536/files#diff-c0164358c6e61a5abb38a4914a1dc150R332-R341
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed a new commit and made it easier to understand now.
Looks more similar to old code: https://github.com/knative/eventing-contrib/blob/master/kafka/channel/pkg/reconciler/controller/kafkachannel.go#L307