Skip to content

Commit

Permalink
feat: autocreate v1beta3 eventtypes (#8276)
Browse files Browse the repository at this point in the history
* feat: eventtype autocreate creates v1beta3 ets

Signed-off-by: Cali0707 <calumramurray@gmail.com>

* fix(test): et tests pass with v1beta3 autocreate

Signed-off-by: Cali0707 <calumramurray@gmail.com>

* fix(build): components use correct v1beta3 interfaces when making the et autocreate handler

Signed-off-by: Cali0707 <calumramurray@gmail.com>

---------

Signed-off-by: Cali0707 <calumramurray@gmail.com>
  • Loading branch information
Cali0707 authored Oct 25, 2024
1 parent 291c0a8 commit 03ba8f4
Show file tree
Hide file tree
Showing 10 changed files with 456 additions and 34 deletions.
4 changes: 2 additions & 2 deletions cmd/broker/filter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import (
eventingclient "knative.dev/eventing/pkg/client/injection/client"
brokerinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1/broker"
triggerinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1/trigger"
eventtypeinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta2/eventtype"
eventtypeinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta3/eventtype"
subscriptioninformer "knative.dev/eventing/pkg/client/injection/informers/messaging/v1/subscription"
"knative.dev/eventing/pkg/eventingtls"
"knative.dev/eventing/pkg/eventtype"
Expand Down Expand Up @@ -130,7 +130,7 @@ func main() {
if featureFlags.IsEnabled(feature.EvenTypeAutoCreate) && featureStore != nil && handler != nil {
autoCreate := &eventtype.EventTypeAutoHandler{
EventTypeLister: eventtypeinformer.Get(ctx).Lister(),
EventingClient: eventingclient.Get(ctx).EventingV1beta2(),
EventingClient: eventingclient.Get(ctx).EventingV1beta3(),
FeatureStore: featureStore,
Logger: logger,
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/broker/ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import (
eventingclient "knative.dev/eventing/pkg/client/injection/client"
brokerinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1/broker"
eventpolicyinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1alpha1/eventpolicy"
eventtypeinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta2/eventtype"
eventtypeinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta3/eventtype"
"knative.dev/eventing/pkg/eventingtls"
"knative.dev/eventing/pkg/eventtype"
"knative.dev/eventing/pkg/reconciler/names"
Expand Down Expand Up @@ -153,7 +153,7 @@ func main() {
if featureFlags.IsEnabled(feature.EvenTypeAutoCreate) && featureStore != nil && handler != nil {
autoCreate := &eventtype.EventTypeAutoHandler{
EventTypeLister: eventtypeinformer.Get(ctx).Lister(),
EventingClient: eventingclient.Get(ctx).EventingV1beta2(),
EventingClient: eventingclient.Get(ctx).EventingV1beta3(),
FeatureStore: featureStore,
Logger: logger,
}
Expand Down Expand Up @@ -190,7 +190,7 @@ func main() {
if featureStore.IsEnabled(feature.EvenTypeAutoCreate) {
autoCreate := &eventtype.EventTypeAutoHandler{
EventTypeLister: eventtypeinformer.Get(ctx).Lister(),
EventingClient: eventingclient.Get(ctx).EventingV1beta2(),
EventingClient: eventingclient.Get(ctx).EventingV1beta3(),
FeatureStore: featureStore,
Logger: logger,
}
Expand Down
25 changes: 14 additions & 11 deletions pkg/eventtype/eventtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"

"knative.dev/eventing/pkg/apis/eventing/v1beta2"
"knative.dev/eventing/pkg/apis/eventing/v1beta3"
"knative.dev/eventing/pkg/apis/feature"
eventingv1beta2 "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1beta2"
v1beta22 "knative.dev/eventing/pkg/client/listers/eventing/v1beta2"
eventingv1beta3 "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1beta3"
v1beta33 "knative.dev/eventing/pkg/client/listers/eventing/v1beta3"
"knative.dev/eventing/pkg/utils"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
)

type EventTypeAutoHandler struct {
EventTypeLister v1beta22.EventTypeLister
EventingClient eventingv1beta2.EventingV1beta2Interface
EventTypeLister v1beta33.EventTypeLister
EventingClient eventingv1beta3.EventingV1beta3Interface
FeatureStore *feature.Store
Logger *zap.Logger
}
Expand Down Expand Up @@ -80,7 +80,7 @@ func (h *EventTypeAutoHandler) AutoCreateEventType(ctx context.Context, event *e
source, _ := apis.ParseURL(event.Source())
schema, _ := apis.ParseURL(event.DataSchema())

et := &v1beta2.EventType{
et := &v1beta3.EventType{
ObjectMeta: metav1.ObjectMeta{
Name: eventTypeName,
Namespace: addressable.Namespace,
Expand All @@ -93,11 +93,14 @@ func (h *EventTypeAutoHandler) AutoCreateEventType(ctx context.Context, event *e
},
},
},
Spec: v1beta2.EventTypeSpec{
Type: event.Type(),
Source: source,
Schema: schema,
SchemaData: event.DataSchema(),
Spec: v1beta3.EventTypeSpec{
Attributes: []v1beta3.EventAttributeDefinition{
{Name: "type", Value: event.Type(), Required: true},
{Name: "source", Value: source.String(), Required: true},
{Name: "schemadata", Value: schema.String(), Required: true},
{Name: "specversion", Value: event.SpecVersion(), Required: true},
{Name: "id", Required: false},
},
Reference: addressable,
Description: "Event Type auto-created by controller",
},
Expand Down
22 changes: 11 additions & 11 deletions pkg/eventtype/eventtypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"knative.dev/eventing/pkg/apis/eventing/v1beta2"
"knative.dev/eventing/pkg/apis/eventing/v1beta3"
"knative.dev/eventing/pkg/apis/feature"
fakeeventingclientset "knative.dev/eventing/pkg/client/clientset/versioned/fake"
reconcilertestingv1beta2 "knative.dev/eventing/pkg/reconciler/testing/v1beta2"
reconcilertestingv1beta3 "knative.dev/eventing/pkg/reconciler/testing/v1beta3"
"knative.dev/eventing/test/lib/resources"
duckv1 "knative.dev/pkg/apis/duck/v1"
logtesting "knative.dev/pkg/logging/testing"
Expand All @@ -43,7 +43,7 @@ func TestEventTypeAutoHandler_AutoCreateEventType(t *testing.T) {
featureFlag string
addressable *duckv1.KReference
events []v2.Event
expectedEventType []v1beta2.EventType
expectedEventType []v1beta3.EventType
expectedError error
}{
{
Expand All @@ -55,7 +55,7 @@ func TestEventTypeAutoHandler_AutoCreateEventType(t *testing.T) {
Namespace: "default",
Name: "broker"},
events: []v2.Event{initEvent("")},
expectedEventType: []v1beta2.EventType{
expectedEventType: []v1beta3.EventType{
initEventTypeObject(),
initEventTypeObject()},
expectedError: nil,
Expand All @@ -71,7 +71,7 @@ func TestEventTypeAutoHandler_AutoCreateEventType(t *testing.T) {
events: []v2.Event{
initEvent("foo.type"),
initEvent("bar.type")},
expectedEventType: []v1beta2.EventType{
expectedEventType: []v1beta3.EventType{
initEventTypeObject(),
initEventTypeObject()},
expectedError: nil,
Expand All @@ -80,13 +80,13 @@ func TestEventTypeAutoHandler_AutoCreateEventType(t *testing.T) {
for _, tc := range testCases {
ctx := context.TODO()
eventtypes := make([]runtime.Object, 0, 10)
listers := reconcilertestingv1beta2.NewListers(eventtypes)
listers := reconcilertestingv1beta3.NewListers(eventtypes)
eventingClient := fakeeventingclientset.NewSimpleClientset()
logger := zap.NewNop()

handler := &EventTypeAutoHandler{
EventTypeLister: listers.GetEventTypeLister(),
EventingClient: eventingClient.EventingV1beta2(),
EventingClient: eventingClient.EventingV1beta3(),
FeatureStore: initFeatureStore(t, tc.featureFlag),
Logger: logger,
}
Expand All @@ -99,7 +99,7 @@ func TestEventTypeAutoHandler_AutoCreateEventType(t *testing.T) {
time.Sleep(time.Millisecond * 500) // autocreate runs in a different goroutine, need to wait for it to finish

etName := generateEventTypeName(tc.addressable.Name, tc.addressable.Namespace, event.Type(), event.Source())
et, err := eventingClient.EventingV1beta2().EventTypes(tc.addressable.Namespace).Get(ctx, etName, metav1.GetOptions{})
et, err := eventingClient.EventingV1beta3().EventTypes(tc.addressable.Namespace).Get(ctx, etName, metav1.GetOptions{})
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -272,9 +272,9 @@ func initEvent(eventType string) v2.Event {
return e
}

func initEventTypeObject() v1beta2.EventType {
return v1beta2.EventType{
Spec: v1beta2.EventTypeSpec{
func initEventTypeObject() v1beta3.EventType {
return v1beta3.EventType{
Spec: v1beta3.EventTypeSpec{
Reference: &duckv1.KReference{
APIVersion: "eventing.knative.dev/v1",
Kind: "Broker",
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/inmemorychannel/dispatcher/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import (
"knative.dev/eventing/pkg/channel"
eventingclient "knative.dev/eventing/pkg/client/injection/client"
eventpolicyinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1alpha1/eventpolicy"
eventtypeinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta2/eventtype"
eventtypeinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta3/eventtype"
inmemorychannelinformer "knative.dev/eventing/pkg/client/injection/informers/messaging/v1/inmemorychannel"
inmemorychannelreconciler "knative.dev/eventing/pkg/client/injection/reconciler/messaging/v1/inmemorychannel"
"knative.dev/eventing/pkg/inmemorychannel"
Expand Down Expand Up @@ -143,7 +143,7 @@ func NewController(
multiChannelEventHandler: sh,
reporter: reporter,
messagingClientSet: eventingclient.Get(ctx).MessagingV1(),
eventingClient: eventingclient.Get(ctx).EventingV1beta2(),
eventingClient: eventingclient.Get(ctx).EventingV1beta3(),
eventTypeLister: eventtypeinformer.Get(ctx).Lister(),
eventDispatcher: kncloudevents.NewDispatcher(clientConfig, oidcTokenProvider),
authVerifier: auth.NewVerifier(ctx, eventpolicyinformer.Get(ctx).Lister(), trustBundleConfigMapLister, cmw),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
_ "knative.dev/pkg/injection/clients/namespacedkube/informers/core/v1/secret/fake"

_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1alpha1/eventpolicy/fake"
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta2/eventtype/fake"
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta3/eventtype/fake"
_ "knative.dev/eventing/pkg/client/injection/informers/messaging/v1/inmemorychannel/fake"
)

Expand Down
8 changes: 4 additions & 4 deletions pkg/reconciler/inmemorychannel/dispatcher/inmemorychannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ import (
"knative.dev/eventing/pkg/channel"
"knative.dev/eventing/pkg/channel/fanout"
"knative.dev/eventing/pkg/channel/multichannelfanout"
eventingv1beta2 "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1beta2"
eventingv1beta3 "knative.dev/eventing/pkg/client/clientset/versioned/typed/eventing/v1beta3"
messagingv1 "knative.dev/eventing/pkg/client/clientset/versioned/typed/messaging/v1"
reconcilerv1 "knative.dev/eventing/pkg/client/injection/reconciler/messaging/v1/inmemorychannel"
"knative.dev/eventing/pkg/client/listers/eventing/v1beta2"
"knative.dev/eventing/pkg/client/listers/eventing/v1beta3"
"knative.dev/eventing/pkg/eventingtls"
"knative.dev/eventing/pkg/eventtype"
"knative.dev/eventing/pkg/kncloudevents"
Expand All @@ -56,9 +56,9 @@ type Reconciler struct {
multiChannelEventHandler multichannelfanout.MultiChannelEventHandler
reporter channel.StatsReporter
messagingClientSet messagingv1.MessagingV1Interface
eventTypeLister v1beta2.EventTypeLister
eventTypeLister v1beta3.EventTypeLister
inMemoryChannelLister listers.InMemoryChannelLister
eventingClient eventingv1beta2.EventingV1beta2Interface
eventingClient eventingv1beta3.EventingV1beta3Interface
featureStore *feature.Store
eventDispatcher *kncloudevents.Dispatcher

Expand Down
127 changes: 127 additions & 0 deletions pkg/reconciler/testing/v1beta3/eventtype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
Copyright 2023 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 testing

import (
"context"
"time"

duckv1 "knative.dev/pkg/apis/duck/v1"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/eventing/pkg/apis/eventing/v1beta3"
"knative.dev/pkg/apis"
)

// EventTypeOption enables further configuration of an EventType.
type EventTypeOption func(*v1beta3.EventType)

// NewEventType creates a EventType with EventTypeOptions.
func NewEventType(name, namespace string, o ...EventTypeOption) *v1beta3.EventType {
et := &v1beta3.EventType{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
},
}
for _, opt := range o {
opt(et)
}
et.SetDefaults(context.Background())
return et
}

// WithInitEventTypeConditions initializes the EventType's conditions.
func WithInitEventTypeConditions(et *v1beta3.EventType) {
et.Status.InitializeConditions()
}

func WithEventTypeSource(source *apis.URL) EventTypeOption {
return func(et *v1beta3.EventType) {
if et.Spec.Attributes == nil {
et.Spec.Attributes = make([]v1beta3.EventAttributeDefinition, 0)
}
et.Spec.Attributes = append(et.Spec.Attributes, v1beta3.EventAttributeDefinition{Name: "source", Value: source.String(), Required: true})
}
}

func WithEventTypeType(t string) EventTypeOption {
return func(et *v1beta3.EventType) {
if et.Spec.Attributes == nil {
et.Spec.Attributes = make([]v1beta3.EventAttributeDefinition, 0)
}
et.Spec.Attributes = append(et.Spec.Attributes, v1beta3.EventAttributeDefinition{Name: "type", Value: t, Required: true})
}
}

func WithEventTypeSpecVersion(specVersion string) EventTypeOption {
return func(et *v1beta3.EventType) {
if et.Spec.Attributes == nil {
et.Spec.Attributes = make([]v1beta3.EventAttributeDefinition, 0)
}
et.Spec.Attributes = append(et.Spec.Attributes, v1beta3.EventAttributeDefinition{Name: "specversion", Value: specVersion, Required: true})
}
}

func WithEventTypeId() EventTypeOption {
return func(et *v1beta3.EventType) {
if et.Spec.Attributes == nil {
et.Spec.Attributes = make([]v1beta3.EventAttributeDefinition, 0)
}
et.Spec.Attributes = append(et.Spec.Attributes, v1beta3.EventAttributeDefinition{Name: "id", Required: true})
}
}
func WithEventTypeReference(ref *duckv1.KReference) EventTypeOption {
return func(et *v1beta3.EventType) {
et.Spec.Reference = ref
}
}

func WithEventTypeDescription(description string) EventTypeOption {
return func(et *v1beta3.EventType) {
et.Spec.Description = description
}
}

func WithEventTypeLabels(labels map[string]string) EventTypeOption {
return func(et *v1beta3.EventType) {
et.ObjectMeta.Labels = labels
}
}

func WithEventTypeOwnerReference(ownerRef metav1.OwnerReference) EventTypeOption {
return func(et *v1beta3.EventType) {
et.ObjectMeta.OwnerReferences = []metav1.OwnerReference{
ownerRef,
}
}
}

func WithEventTypeDeletionTimestamp(et *v1beta3.EventType) {
t := metav1.NewTime(time.Unix(1e9, 0))
et.ObjectMeta.SetDeletionTimestamp(&t)
}

// WithEventTypeResourceDoesNotExist calls .Status.MarkFilterFailed on the EventType.
func WithEventTypeResourceDoesNotExist(et *v1beta3.EventType) {
et.Status.MarkReferenceDoesNotExist()
}

// WithEventTypeResourceExists calls .Status.MarkReferenceExists on the EventType.
func WithEventTypeResourceExists(et *v1beta3.EventType) {
et.Status.MarkReferenceExists()
}
Loading

0 comments on commit 03ba8f4

Please sign in to comment.