Skip to content

Commit

Permalink
feat: reconcile eventtype v1beta3 resources (#8087)
Browse files Browse the repository at this point in the history
* feat: reconcile eventtype v1beta3

Signed-off-by: Calum Murray <cmurray@redhat.com>

* serve v1beta3 CRD

Signed-off-by: Calum Murray <cmurray@redhat.com>

---------

Signed-off-by: Calum Murray <cmurray@redhat.com>
  • Loading branch information
Cali0707 authored Jul 11, 2024
1 parent 57b52ea commit ea08684
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 31 deletions.
2 changes: 1 addition & 1 deletion config/core/resources/eventtype.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ spec:
group: eventing.knative.dev
versions:
- name: v1beta3
served: false
served: true
storage: false
subresources:
status: {}
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/eventing/v1beta3/eventtype_lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func (et *EventTypeStatus) MarkReferenceDoesNotExist() {
eventTypeCondSet.Manage(et).MarkFalse(EventTypeConditionReferenceExists, "ResourceDoesNotExist", "Resource in spec.reference does not exist")
}

func (et *EventTypeStatus) MarkReferenceNotSet() {
eventTypeCondSet.Manage(et).MarkTrueWithReason(EventTypeConditionReferenceExists, "ReferenceNotSet", "spec.reference is not set")
}

func (et *EventTypeStatus) MarkReferenceExistsUnknown(reason, messageFormat string, messageA ...interface{}) {
eventTypeCondSet.Manage(et).MarkUnknown(EventTypeConditionReferenceExists, reason, messageFormat, messageA...)
}
4 changes: 2 additions & 2 deletions pkg/reconciler/eventtype/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"knative.dev/pkg/configmap"
"knative.dev/pkg/controller"

eventtypeinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta2/eventtype"
eventtypereconciler "knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1beta2/eventtype"
eventtypeinformer "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta3/eventtype"
eventtypereconciler "knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1beta3/eventtype"
)

// NewController initializes the controller and is called by the generated code
Expand Down
2 changes: 1 addition & 1 deletion pkg/reconciler/eventtype/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (

// Fake injection informers
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1/broker/fake"
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta2/eventtype/fake"
_ "knative.dev/eventing/pkg/client/injection/informers/eventing/v1beta3/eventtype/fake"
)

func TestNew(t *testing.T) {
Expand Down
20 changes: 16 additions & 4 deletions pkg/reconciler/eventtype/eventtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import (
"go.uber.org/zap"
apierrs "k8s.io/apimachinery/pkg/api/errors"

"knative.dev/eventing/pkg/apis/eventing/v1beta2"
eventtypereconciler "knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1beta2/eventtype"
"knative.dev/eventing/pkg/apis/eventing/v1beta3"
eventtypereconciler "knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1beta3/eventtype"
duckv1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/logging"
pkgreconciler "knative.dev/pkg/reconciler"
)
Expand All @@ -38,8 +39,15 @@ type Reconciler struct {
var _ eventtypereconciler.Interface = (*Reconciler)(nil)

// ReconcileKind implements Interface.ReconcileKind.
// 1. Verify the Reference exists.
func (r *Reconciler) ReconcileKind(ctx context.Context, et *v1beta2.EventType) pkgreconciler.Event {
// 1. Check if there is a reference
// a) if not, reconcile to true
// b) if yes, continue reconciling
// 2. Verify the Reference exist
func (r *Reconciler) ReconcileKind(ctx context.Context, et *v1beta3.EventType) pkgreconciler.Event {
if et.Spec.Reference == nil || isEmptyReference(et.Spec.Reference) {
et.Status.MarkReferenceNotSet()
return nil
}

_, err := r.kReferenceResolver.Resolve(ctx, et.Spec.Reference, et)
if err != nil {
Expand All @@ -55,3 +63,7 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, et *v1beta2.EventType) p
et.Status.MarkReferenceExists()
return nil
}

func isEmptyReference(ref *duckv1.KReference) bool {
return ref.Kind == "" && ref.Group == "" && ref.Name == "" && ref.APIVersion == "" && ref.Namespace == "" && (ref.Address == nil || *ref.Address == "")
}
42 changes: 39 additions & 3 deletions pkg/reconciler/eventtype/eventtype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
clientgotesting "k8s.io/client-go/testing"
fakeeventingclient "knative.dev/eventing/pkg/client/injection/client/fake"
"knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1beta2/eventtype"
"knative.dev/eventing/pkg/client/injection/reconciler/eventing/v1beta3/eventtype"
. "knative.dev/eventing/pkg/reconciler/testing/v1"
"knative.dev/pkg/apis"
"knative.dev/pkg/configmap"
Expand Down Expand Up @@ -87,13 +87,17 @@ func TestReconcile(t *testing.T) {
NewEventType(eventTypeName, testNS,
WithEventTypeType(eventTypeType),
WithEventTypeSource(eventTypeSource),
WithEventTypeSpecV1(),
WithEventTypeEmptyID(),
WithEventTypeReference(brokerReference(eventTypeBroker)),
),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: NewEventType(eventTypeName, testNS,
WithEventTypeType(eventTypeType),
WithEventTypeSource(eventTypeSource),
WithEventTypeSpecV1(),
WithEventTypeEmptyID(),
WithEventTypeReference(brokerReference(eventTypeBroker)),
WithInitEventTypeConditions,
WithEventTypeResourceDoesNotExist,
Expand All @@ -112,13 +116,17 @@ func TestReconcile(t *testing.T) {
NewEventType(eventTypeName, testNS,
WithEventTypeType(eventTypeType),
WithEventTypeSource(eventTypeSource),
WithEventTypeSpecV1(),
WithEventTypeEmptyID(),
WithEventTypeReference(channelReference(eventTypeChannel)),
),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: NewEventType(eventTypeName, testNS,
WithEventTypeType(eventTypeType),
WithEventTypeSource(eventTypeSource),
WithEventTypeSpecV1(),
WithEventTypeEmptyID(),
WithInitEventTypeConditions,
WithEventTypeReference(channelReference(eventTypeChannel)),
WithEventTypeResourceDoesNotExist,
Expand All @@ -136,6 +144,8 @@ func TestReconcile(t *testing.T) {
NewEventType(eventTypeName, testNS,
WithEventTypeType(eventTypeType),
WithEventTypeSource(eventTypeSource),
WithEventTypeSpecV1(),
WithEventTypeEmptyID(),
WithEventTypeReference(brokerReference(eventTypeBroker)),
),
resources.MakeBroker(testNS, eventTypeBroker),
Expand All @@ -144,6 +154,8 @@ func TestReconcile(t *testing.T) {
Object: NewEventType(eventTypeName, testNS,
WithEventTypeType(eventTypeType),
WithEventTypeSource(eventTypeSource),
WithEventTypeSpecV1(),
WithEventTypeEmptyID(),
WithInitEventTypeConditions,
WithEventTypeReference(brokerReference(eventTypeBroker)),
WithEventTypeResourceExists,
Expand All @@ -158,6 +170,8 @@ func TestReconcile(t *testing.T) {
NewEventType(eventTypeName, testNS,
WithEventTypeType(eventTypeType),
WithEventTypeSource(eventTypeSource),
WithEventTypeSpecV1(),
WithEventTypeEmptyID(),
WithEventTypeReference(channelReference(eventTypeChannel)),
),
NewInMemoryChannel(eventTypeChannel, testNS),
Expand All @@ -166,14 +180,36 @@ func TestReconcile(t *testing.T) {
Object: NewEventType(eventTypeName, testNS,
WithEventTypeType(eventTypeType),
WithEventTypeSource(eventTypeSource),
WithEventTypeSpecV1(),
WithEventTypeEmptyID(),
WithInitEventTypeConditions,
WithEventTypeReference(channelReference(eventTypeChannel)),
WithEventTypeResourceExists,
),
}},
WantErr: false,
},
}
}, {
Name: "No reference set",
Key: testKey,
Objects: []runtime.Object{
NewEventType(eventTypeName, testNS,
WithEventTypeType(eventTypeType),
WithEventTypeSource(eventTypeSource),
WithEventTypeSpecV1(),
WithEventTypeEmptyID(),
),
},
WantStatusUpdates: []clientgotesting.UpdateActionImpl{{
Object: NewEventType(eventTypeName, testNS,
WithInitEventTypeConditions,
WithEventTypeType(eventTypeType),
WithEventTypeSource(eventTypeSource),
WithEventTypeSpecV1(),
WithEventTypeEmptyID(),
WithEventTypeReferenceNotSet),
}},
WantErr: false,
}}

logger := logtesting.TestLogger(t)
table.Test(t, MakeFactory(func(ctx context.Context, listers *Listers, cmw configmap.Watcher) controller.Reconciler {
Expand Down
79 changes: 63 additions & 16 deletions pkg/reconciler/testing/v1/eventtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ import (
duckv1 "knative.dev/pkg/apis/duck/v1"

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

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

// NewEventType creates a EventType with EventTypeOptions.
func NewEventType(name, namespace string, o ...EventTypeOption) *v1beta2.EventType {
et := &v1beta2.EventType{
func NewEventType(name, namespace string, o ...EventTypeOption) *v1beta3.EventType {
et := &v1beta3.EventType{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: name,
Expand All @@ -46,59 +46,106 @@ func NewEventType(name, namespace string, o ...EventTypeOption) *v1beta2.EventTy
}

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

func WithEventTypeSource(source *apis.URL) EventTypeOption {
return func(et *v1beta2.EventType) {
et.Spec.Source = source
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",
Required: true,
Value: source.String(),
})
}
}

func WithEventTypeType(t string) EventTypeOption {
return func(et *v1beta2.EventType) {
et.Spec.Type = t
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",
Required: true,
Value: t,
})
}
}

func WithEventTypeSpecV1() 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",
Required: true,
Value: "V1",
})
}
}

func WithEventTypeEmptyID() 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 *v1beta2.EventType) {
return func(et *v1beta3.EventType) {
et.Spec.Reference = ref
}
}

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

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

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

func WithEventTypeDeletionTimestamp(et *v1beta2.EventType) {
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 *v1beta2.EventType) {
func WithEventTypeResourceDoesNotExist(et *v1beta3.EventType) {
et.Status.MarkReferenceDoesNotExist()
}

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

func WithEventTypeReferenceNotSet(et *v1beta3.EventType) {
et.Status.MarkReferenceNotSet()
}
8 changes: 4 additions & 4 deletions pkg/reconciler/testing/v1/listers.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ import (
eventingduck "knative.dev/eventing/pkg/apis/duck/v1"
eventingv1 "knative.dev/eventing/pkg/apis/eventing/v1"
eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1"
eventingv1beta2 "knative.dev/eventing/pkg/apis/eventing/v1beta2"
eventingv1beta3 "knative.dev/eventing/pkg/apis/eventing/v1beta3"
flowsv1 "knative.dev/eventing/pkg/apis/flows/v1"
messagingv1 "knative.dev/eventing/pkg/apis/messaging/v1"
sinksv1alpha1 "knative.dev/eventing/pkg/apis/sinks/v1alpha1"
sourcesv1 "knative.dev/eventing/pkg/apis/sources/v1"
fakeeventingclientset "knative.dev/eventing/pkg/client/clientset/versioned/fake"
eventinglisters "knative.dev/eventing/pkg/client/listers/eventing/v1"
eventingv1alpha1listers "knative.dev/eventing/pkg/client/listers/eventing/v1alpha1"
eventingv1beta2listers "knative.dev/eventing/pkg/client/listers/eventing/v1beta2"
eventingv1beta3listers "knative.dev/eventing/pkg/client/listers/eventing/v1beta3"
flowslisters "knative.dev/eventing/pkg/client/listers/flows/v1"
messaginglisters "knative.dev/eventing/pkg/client/listers/messaging/v1"
sinkslisters "knative.dev/eventing/pkg/client/listers/sinks/v1alpha1"
Expand Down Expand Up @@ -114,8 +114,8 @@ func (l *Listers) GetAllObjects() []runtime.Object {
return all
}

func (l *Listers) GetEventTypeLister() eventingv1beta2listers.EventTypeLister {
return eventingv1beta2listers.NewEventTypeLister(l.indexerFor(&eventingv1beta2.EventType{}))
func (l *Listers) GetEventTypeLister() eventingv1beta3listers.EventTypeLister {
return eventingv1beta3listers.NewEventTypeLister(l.indexerFor(&eventingv1beta3.EventType{}))
}

func (l *Listers) GetEventPolicyLister() eventingv1alpha1listers.EventPolicyLister {
Expand Down

0 comments on commit ea08684

Please sign in to comment.