Skip to content
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 2 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/mtchannel_broker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
"knative.dev/pkg/injection/sharedmain"

"knative.dev/eventing/pkg/reconciler/mtbroker"
"knative.dev/eventing/pkg/reconciler/mtnamespace"
)

func main() {
sharedmain.Main("mt-broker-controller",
mtnamespace.NewController,
mtbroker.NewController,
)
}
70 changes: 70 additions & 0 deletions pkg/reconciler/mtnamespace/controller.go
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
}
38 changes: 38 additions & 0 deletions pkg/reconciler/mtnamespace/controller_test.go
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")
}
}
135 changes: 135 additions & 0 deletions pkg/reconciler/mtnamespace/namespace.go
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 {
Copy link
Contributor

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?

Copy link
Member Author

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
}
Loading