Skip to content
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
8 changes: 8 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pkg/apis/sources/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import (
const (
GroupName = "sources.knative.dev"

// SourceDuckAnnotationKey is the annotation key to indicate
// SourceDuckLabelKey is the label key to indicate
// whether the CRD is a Source duck type.
// Valid values: "true" or "false"
SourceDuckAnnotationKey = duck.GroupName + "/source"
SourceDuckLabelKey = duck.GroupName + "/source"

// SourceDuckAnnotationValue is the annotation value to indicate
// SourceDuckLabelValue is the label value to indicate
// the CRD is a Source duck type.
SourceDuckAnnotationValue = "true"
SourceDuckLabelValue = "true"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/source/crd/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewController(

logger.Info("Setting up event handlers")
crdInformer.Informer().AddEventHandler(cache.FilteringResourceEventHandler{
FilterFunc: pkgreconciler.LabelFilterFunc(sources.SourceDuckAnnotationKey, sources.SourceDuckAnnotationValue, false),
FilterFunc: pkgreconciler.LabelFilterFunc(sources.SourceDuckLabelKey, sources.SourceDuckLabelValue, false),
Handler: controller.HandleAll(impl.Enqueue),
})

Expand Down
37 changes: 37 additions & 0 deletions pkg/reconciler/source/crd/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
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 crd

import (
"testing"

"knative.dev/pkg/configmap"
. "knative.dev/pkg/reconciler/testing"

// Fake injection informers
_ "knative.dev/pkg/client/injection/apiextensions/informers/apiextensions/v1beta1/customresourcedefinition/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")
}
}
104 changes: 104 additions & 0 deletions pkg/reconciler/source/crd/crd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
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 crd

import (
"context"
"testing"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"knative.dev/eventing/pkg/apis/sources"
"knative.dev/pkg/client/injection/ducks/duck/v1/source"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
logtesting "knative.dev/pkg/logging/testing"

. "knative.dev/eventing/pkg/reconciler/testing"
. "knative.dev/pkg/reconciler/testing"

// Fake injection informers
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1alpha1/eventtype/fake"
_ "knative.dev/pkg/client/injection/apiextensions/informers/apiextensions/v1beta1/customresourcedefinition/fake"
_ "knative.dev/pkg/client/injection/ducks/duck/v1/source/fake"
)

const (
crdName = "test-crd"
crdGroup = "testing.sources.knative.dev"
crdKind = "TestSource"
crdPlural = "testsources"
crdVersionServed = "v1alpha1"
)

func TestAllCases(t *testing.T) {
table := TableTest{{
Name: "bad workqueue key",
// Make sure Reconcile handles bad keys.
Key: "too/many/parts",
}, {
Name: "key not found",
// Make sure Reconcile handles good keys that don't exist.
Key: "not-found",
}, {
Name: "reconcile failed, cannot find GVR or GVK",
Objects: []runtime.Object{
NewCustomResourceDefinition(crdName,
WithCustomResourceDefinitionLabels(map[string]string{
sources.SourceDuckLabelKey: sources.SourceDuckLabelValue,
})),
},
Key: crdName,
WantErr: true,
WantEvents: []string{
Eventf(corev1.EventTypeWarning, "SourceCRDReconcileFailed", "Source CRD reconciliation failed: unable to find GVR or GVK for %s", crdName),
},
},
//}, {
// TODO uncomment the following once we figure out why the eventtype informer is missing from the duck.controller.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@n3wscott I'm having trouble with this one.
It seems that the context we pass to the Source duck controller doesn't have eventtype:

sd := sdc(sdctx, r.ogcmw)
, although I'm importing the fake...
I might be missing something, OR there is a bug in
sdctx, cancel := context.WithCancel(r.ogctx)
...
Any ideas?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is the error that is thrown?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

does it not have the kind EventType? I need more info to try to help

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sorrry, was AFK...
The problem that the informer is not registered: Unable to fetch knative.dev/eventing/pkg/client/informers/externalversions/eventing/v1alpha1.EventTypeInformer from context

updated the codegen stuff... will be back in a few mins

//Name: "reconcile succeeded",
//Objects: []runtime.Object{
// NewCustomResourceDefinition(crdName,
// WithCustomResourceDefinitionLabels(map[string]string{
// sources.SourceDuckLabelKey: sources.SourceDuckLabelValue,
// }),
// WithCustomResourceDefinitionGroup(crdGroup),
// WithCustomResourceDefinitionNames(apiextensionsv1beta1.CustomResourceDefinitionNames{
// Kind: crdKind,
// Plural: crdPlural,
// }),
// WithCustomResourceDefinitionVersions([]apiextensionsv1beta1.CustomResourceDefinitionVersion{{
// Name: crdVersionServed,
// Served: true,
// }})),
//},
//Key: crdName,
}

logger := logtesting.TestLogger(t)
table.Test(t, MakeFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher) controller.Reconciler {
ctx = source.WithDuck(ctx)
return &Reconciler{
crdLister: listers.GetCustomResourceDefinitionLister(),
ogctx: ctx,
ogcmw: cmw,
recorder: controller.GetEventRecorder(ctx),
}
},
false, logger,
))
}
17 changes: 4 additions & 13 deletions pkg/reconciler/source/duck/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ import (
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/cache"

"knative.dev/pkg/apis/duck"
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"
"knative.dev/pkg/injection"
"knative.dev/pkg/injection/clients/dynamicclient"
"knative.dev/pkg/logging"

eventingclient "knative.dev/eventing/pkg/client/injection/client"
eventtypeinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1alpha1/eventtype"
duckv1 "knative.dev/pkg/apis/duck/v1"
crdinfomer "knative.dev/pkg/client/injection/apiextensions/informers/apiextensions/v1beta1/customresourcedefinition"
sourceinformer "knative.dev/pkg/client/injection/ducks/duck/v1/source"
)

const (
Expand All @@ -50,28 +48,21 @@ func NewController(crd string, gvr schema.GroupVersionResource, gvk schema.Group
logger := logging.FromContext(ctx)
eventTypeInformer := eventtypeinformer.Get(ctx)
crdInformer := crdinfomer.Get(ctx)
sourceduckInformer := sourceinformer.Get(ctx)

// Create a duck TypedInformer for duckv1.Source resources.
sourceinformer := &duck.TypedInformerFactory{
Client: dynamicclient.Get(ctx),
Type: &duckv1.Source{},
ResyncPeriod: controller.DefaultResyncPeriod,
StopChannel: ctx.Done(),
}

sourceInformer, sourceLister, err := sourceinformer.Get(gvr)
sourceInformer, sourceLister, err := sourceduckInformer.Get(gvr)
if err != nil {
logger.Errorw("Error getting source informer", zap.String("GVR", gvr.String()), zap.Error(err))
return nil
}

r := &Reconciler{
eventingClientSet: eventingclient.Get(ctx),
eventTypeLister: eventTypeInformer.Lister(),
crdLister: crdInformer.Lister(),
sourceLister: sourceLister,
gvr: gvr,
crdName: crd,
eventingClientSet: eventingclient.Get(ctx),
}
impl := controller.NewImpl(r, logger, ReconcilerName)

Expand Down
51 changes: 51 additions & 0 deletions pkg/reconciler/source/duck/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
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 duck

import (
"testing"

"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/configmap"
. "knative.dev/pkg/reconciler/testing"

// Fake injection informers
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1alpha1/eventtype/fake"
_ "knative.dev/pkg/client/injection/apiextensions/informers/apiextensions/v1beta1/customresourcedefinition/fake"
_ "knative.dev/pkg/client/injection/ducks/duck/v1/source/fake"
)

func TestNew(t *testing.T) {
ctx, _ := SetupFakeContext(t)

gvr := schema.GroupVersionResource{
Group: "testing.sources.knative.dev",
Version: "v1alpha1",
Resource: "testsources",
}
gvk := schema.GroupVersionKind{
Group: "testing.sources.knative.dev",
Version: "v1alpha1",
Kind: "TestSource",
}
crd := "testsources.testing.sources.knative.dev"

c := NewController(crd, gvr, gvk)(ctx, configmap.NewStaticWatcher())
if c == nil {
t.Fatal("Expected NewController to return a non-nil value")
}
}
Loading