-
Notifications
You must be signed in to change notification settings - Fork 630
Defaultcontroller #843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Defaultcontroller #843
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4546b0e
Creating a default channel controller that will watch every channel
akashrv ba80a2e
Removed commetned lines
akashrv 8813fc8
Fixed some typos
akashrv c5e8bf2
Merge branch 'master' of github.com:knative/eventing into defaultcont…
akashrv 9b790f5
Changes based on comments on code review (pull request)
akashrv fbbdcc0
Moved the code to set ChannelConditionProvisionerInstalled=True to In…
akashrv 671056b
Changed all %v to %s when logging strings
akashrv 6720918
Updating some formatting issues called out in PR
akashrv 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,141 @@ | ||
| /* | ||
| Copyright 2019 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. | ||
| */ | ||
|
|
||
| package channel | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| corev1 "k8s.io/api/core/v1" | ||
| "sigs.k8s.io/controller-runtime/pkg/event" | ||
| "sigs.k8s.io/controller-runtime/pkg/predicate" | ||
|
|
||
| "k8s.io/client-go/rest" | ||
| "k8s.io/client-go/tools/record" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller" | ||
| "sigs.k8s.io/controller-runtime/pkg/handler" | ||
| "sigs.k8s.io/controller-runtime/pkg/manager" | ||
| "sigs.k8s.io/controller-runtime/pkg/source" | ||
|
|
||
| "github.com/golang/glog" | ||
| "github.com/knative/eventing/pkg/apis/eventing/v1alpha1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| "k8s.io/client-go/dynamic" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
| ) | ||
|
|
||
| const ( | ||
| // controllerAgentName is the string used by this controller to identify | ||
| // itself when creating events. | ||
| controllerAgentName = "channel-default-controller" | ||
| channelReconciled = "ChannelReconciled" | ||
| channelUpdateStatusFailed = "ChannelUpdateStatusFailed" | ||
| ) | ||
|
|
||
| type reconciler struct { | ||
| client client.Client | ||
| restConfig *rest.Config | ||
| dynamicClient dynamic.Interface | ||
| recorder record.EventRecorder | ||
| } | ||
|
|
||
| // Verify the struct implements reconcile.Reconciler | ||
| var _ reconcile.Reconciler = &reconciler{} | ||
|
|
||
| // ProvideController returns a Channel controller. | ||
| // This Channel controller is a default controller for channels of all provisioner kinds | ||
| func ProvideController(mgr manager.Manager) (controller.Controller, error) { | ||
| // Setup a new controller to Reconcile channel | ||
| c, err := controller.New(controllerAgentName, mgr, controller.Options{ | ||
| Reconciler: &reconciler{ | ||
| recorder: mgr.GetRecorder(controllerAgentName), | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // Watch channel events | ||
| // This controller is no-op when Channels are deleted | ||
| if err := c.Watch( | ||
| &source.Kind{Type: &v1alpha1.Channel{}}, | ||
| &handler.EnqueueRequestForObject{}, | ||
| predicate.Funcs{ | ||
| DeleteFunc: func(event.DeleteEvent) bool { | ||
| return false | ||
| }, | ||
| }); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return c, nil | ||
| } | ||
|
|
||
| // Reconcile will check if the channel is being watched by provisioner's channel controller | ||
| // This will improve UX. See https://github.com/knative/eventing/issues/779 | ||
| func (r *reconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) { | ||
| glog.Infof("Reconciling channel %s", request) | ||
|
akashrv marked this conversation as resolved.
|
||
| ch := &v1alpha1.Channel{} | ||
|
|
||
| // Controller-runtime client Get() always deep copies the object. Hence no need to again deep copy it | ||
| err := r.client.Get(context.TODO(), request.NamespacedName, ch) | ||
|
|
||
| if errors.IsNotFound(err) { | ||
| glog.Errorf("could not find channel %s\n", request) | ||
| return reconcile.Result{}, nil | ||
| } | ||
|
|
||
| if err != nil { | ||
| glog.Errorf("could not fetch channel %s: %s\n", request, err) | ||
| return reconcile.Result{}, err | ||
| } | ||
|
|
||
| err = r.reconcile(ch) | ||
|
|
||
| if err != nil { | ||
| glog.Warningf("Error reconciling channel %s: %s. Will retry.", request, err) | ||
| r.recorder.Eventf(ch, corev1.EventTypeWarning, channelUpdateStatusFailed, "Failed to update channel status: %s", request) | ||
| return reconcile.Result{Requeue: true}, err | ||
| } | ||
| glog.Infof("Successfully reconciled channel %s", request) | ||
| r.recorder.Eventf(ch, corev1.EventTypeNormal, channelReconciled, "Channel reconciled: %s", request) | ||
| return reconcile.Result{Requeue: false}, nil | ||
| } | ||
|
|
||
| func (r *reconciler) reconcile(ch *v1alpha1.Channel) error { | ||
| // Do not Initialize() Status in channel-default-controller. It will set ChannelConditionProvisionerInstalled=True | ||
| // Directly call GetCondition(). If the Status was never initialized then GetCondition() will return nil and | ||
| // IsUnknown() will return true | ||
| c := ch.Status.GetCondition(v1alpha1.ChannelConditionProvisionerInstalled) | ||
|
|
||
| if c.IsUnknown() { | ||
| ch.Status.MarkProvisionerNotInstalled( | ||
| "Provisioner not found.", | ||
| "Specified provisioner [Name:%s Kind:%s] is not installed or not controlling the channel.", | ||
| ch.Spec.Provisioner.Name, | ||
| ch.Spec.Provisioner.Kind, | ||
| ) | ||
| err := r.client.Status().Update(context.TODO(), ch) | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (r *reconciler) InjectClient(c client.Client) error { | ||
| r.client = c | ||
| return nil | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.