-
Notifications
You must be signed in to change notification settings - Fork 592
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
Add a namespace controller for the MT Broker. #2778
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,70 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
package mtnamespace | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/client-go/tools/cache" | ||
"knative.dev/pkg/configmap" | ||
"knative.dev/pkg/controller" | ||
|
||
"knative.dev/eventing/pkg/reconciler" | ||
|
||
"knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta1/broker" | ||
"knative.dev/pkg/client/injection/kube/informers/core/v1/namespace" | ||
) | ||
|
||
const ( | ||
// ReconcilerName is the name of the reconciler | ||
ReconcilerName = "Namespace" // TODO: Namespace is not a very good name for this controller. | ||
|
||
// controllerAgentName is the string used by this controller to identify | ||
// itself when creating events. | ||
controllerAgentName = "knative-eventing-namespace-controller" | ||
) | ||
|
||
// NewController initializes the controller and is called by the generated code | ||
// Registers event handlers to enqueue events | ||
func NewController( | ||
ctx context.Context, | ||
cmw configmap.Watcher, | ||
) *controller.Impl { | ||
|
||
namespaceInformer := namespace.Get(ctx) | ||
brokerInformer := broker.Get(ctx) | ||
|
||
r := &Reconciler{ | ||
Base: reconciler.NewBase(ctx, controllerAgentName, cmw), | ||
namespaceLister: namespaceInformer.Lister(), | ||
brokerLister: brokerInformer.Lister(), | ||
} | ||
|
||
impl := controller.NewImpl(r, r.Logger, ReconcilerName) | ||
// TODO: filter label selector: on InjectionEnabledLabels() | ||
|
||
r.Logger.Info("Setting up event handlers") | ||
namespaceInformer.Informer().AddEventHandler(controller.HandleAll(impl.Enqueue)) | ||
brokerInformer.Informer().AddEventHandler( | ||
cache.FilteringResourceEventHandler{ | ||
FilterFunc: controller.FilterGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Namespace")), | ||
Handler: controller.HandleAll(impl.EnqueueControllerOf), | ||
}) | ||
|
||
return impl | ||
} |
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,38 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
package mtnamespace | ||
|
||
import ( | ||
"testing" | ||
|
||
"knative.dev/pkg/configmap" | ||
. "knative.dev/pkg/reconciler/testing" | ||
|
||
// Fake injection informers | ||
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta1/broker/fake" | ||
_ "knative.dev/pkg/client/injection/kube/informers/core/v1/namespace/fake" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
ctx, _ := SetupFakeContext(t) | ||
|
||
c := NewController(ctx, configmap.NewStaticWatcher()) | ||
|
||
if c == nil { | ||
t.Fatal("Expected NewController to return a non-nil value") | ||
} | ||
} |
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,135 @@ | ||
/* | ||
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. | ||
*/ | ||
|
||
package mtnamespace | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"k8s.io/client-go/tools/cache" | ||
"knative.dev/eventing/pkg/reconciler/mtnamespace/resources" | ||
|
||
corev1listers "k8s.io/client-go/listers/core/v1" | ||
eventinglisters "knative.dev/eventing/pkg/client/listers/eventing/v1beta1" | ||
|
||
"go.uber.org/zap" | ||
corev1 "k8s.io/api/core/v1" | ||
apierrs "k8s.io/apimachinery/pkg/api/errors" | ||
k8serrors "k8s.io/apimachinery/pkg/api/errors" | ||
"knative.dev/eventing/pkg/apis/eventing/v1alpha1" | ||
"knative.dev/eventing/pkg/apis/eventing/v1beta1" | ||
"knative.dev/eventing/pkg/logging" | ||
"knative.dev/eventing/pkg/reconciler" | ||
"knative.dev/pkg/controller" | ||
) | ||
|
||
const ( | ||
namespaceReconciled = "NamespaceReconciled" | ||
namespaceReconcileFailure = "NamespaceReconcileFailure" | ||
|
||
// Name of the corev1.Events emitted from the reconciliation process. | ||
brokerCreated = "BrokerCreated" | ||
) | ||
|
||
var ( | ||
brokerGVK = v1alpha1.SchemeGroupVersion.WithKind("Broker") | ||
) | ||
|
||
type Reconciler struct { | ||
*reconciler.Base | ||
|
||
// listers index properties about resources | ||
namespaceLister corev1listers.NamespaceLister | ||
brokerLister eventinglisters.BrokerLister | ||
} | ||
|
||
// Check that our Reconciler implements controller.Reconciler | ||
var _ controller.Reconciler = (*Reconciler)(nil) | ||
|
||
// Reconcile compares the actual state with the desired, and attempts to | ||
// converge the two. It then updates the Status block of the Namespace resource | ||
// with the current status of the resource. | ||
func (r *Reconciler) Reconcile(ctx context.Context, key string) error { | ||
_, name, err := cache.SplitMetaNamespaceKey(key) | ||
if err != nil { | ||
logging.FromContext(ctx).Error("invalid resource key") | ||
return nil | ||
} | ||
|
||
// Get the namespace resource with this namespace/name | ||
original, err := r.namespaceLister.Get(name) | ||
if apierrs.IsNotFound(err) { | ||
// The resource may no longer exist, in which case we stop processing. | ||
logging.FromContext(ctx).Error("namespace key in work queue no longer exists") | ||
return nil | ||
} else if err != nil { | ||
return err | ||
} | ||
|
||
if original.Labels[resources.InjectionLabelKey] == resources.InjectionDisabledLabelValue { | ||
logging.FromContext(ctx).Debug("Not reconciling Namespace") | ||
return nil | ||
} | ||
|
||
// Don't modify the informers copy | ||
ns := original.DeepCopy() | ||
|
||
// Reconcile this copy of the Namespace. | ||
reconcileErr := r.reconcile(ctx, ns) | ||
if reconcileErr != nil { | ||
logging.FromContext(ctx).Error("Error reconciling Namespace", zap.Error(reconcileErr)) | ||
r.Recorder.Eventf(ns, corev1.EventTypeWarning, namespaceReconcileFailure, "Failed to reconcile Namespace: %v", reconcileErr) | ||
} else { | ||
logging.FromContext(ctx).Debug("Namespace reconciled") | ||
r.Recorder.Eventf(ns, corev1.EventTypeNormal, namespaceReconciled, "Namespace reconciled: %q", ns.Name) | ||
} | ||
|
||
return reconcileErr | ||
} | ||
|
||
func (r *Reconciler) reconcile(ctx context.Context, ns *corev1.Namespace) error { | ||
if ns.DeletionTimestamp != nil { | ||
return nil | ||
} | ||
|
||
if _, err := r.reconcileBroker(ctx, ns); err != nil { | ||
return fmt.Errorf("broker: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// reconcileBroker reconciles the default Broker for the Namespace 'ns'. | ||
func (r *Reconciler) reconcileBroker(ctx context.Context, ns *corev1.Namespace) (*v1beta1.Broker, error) { | ||
current, err := r.brokerLister.Brokers(ns.Name).Get(resources.DefaultBrokerName) | ||
|
||
// If the resource doesn't exist, we'll create it. | ||
if k8serrors.IsNotFound(err) { | ||
b := resources.MakeBroker(ns) | ||
b, err = r.EventingClientSet.EventingV1beta1().Brokers(ns.Name).Create(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
r.Recorder.Event(ns, corev1.EventTypeNormal, brokerCreated, | ||
"Default eventing.knative.dev Broker created.") | ||
return b, nil | ||
} else if err != nil { | ||
return nil, err | ||
} | ||
// Don't update anything that is already present. | ||
return current, 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.
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.
So, this means that to opt-out, you have to create the namespace with this label in it which means you can't just do:
kubectl create ns foobar
but have to create the object (or is there a way to do this?)
I'm fine with getting this in as is for now, but perhaps we should use the configmap to control this behaviour for the opt-out as a follow on?
Also, could you add an issue for adding the docs for this feature?
Thoughts?
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.
Yeah, I don't see an option to label namespaces in
kubectl create ns foo
. I think if we were going to go the ConfigMap route we'd want consistency with ST Broker. Env vars seem to be popular for configuring this.How about making the default behavior configurable via
DEFAULT_BROKER_INJECTION
and having the two reconcilers default it differently?re: docs
I'm happy to follow up with a PR after this lands. I was thinking we'd fold the "enable" tab into the ST Broker instructions, and combine this with the MT Broker instructions. I'll start on that and we can discuss there?
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.
knative/docs#2325