Skip to content

Commit

Permalink
Add ETv1b3 conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com>
  • Loading branch information
pierDipi committed May 22, 2024
1 parent 0c5a8a1 commit f3edb9d
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 4 deletions.
3 changes: 3 additions & 0 deletions cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"k8s.io/client-go/kubernetes/scheme"
configmapinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/configmap/filtered"

eventingv1beta3 "knative.dev/eventing/pkg/apis/eventing/v1beta3"
"knative.dev/eventing/pkg/apis/feature"
"knative.dev/eventing/pkg/auth"
"knative.dev/eventing/pkg/eventingtls"
Expand Down Expand Up @@ -241,6 +242,7 @@ func NewConversionController(ctx context.Context, cmw configmap.Watcher) *contro
sourcesv1_ = sourcesv1.SchemeGroupVersion.Version
eventingv1beta1_ = eventingv1beta1.SchemeGroupVersion.Version
eventingv1beta2_ = eventingv1beta2.SchemeGroupVersion.Version
eventingv1beta3_ = eventingv1beta3.SchemeGroupVersion.Version
)

return conversion.NewConversionController(ctx,
Expand All @@ -265,6 +267,7 @@ func NewConversionController(ctx context.Context, cmw configmap.Watcher) *contro
Zygotes: map[string]conversion.ConvertibleObject{
eventingv1beta1_: &eventingv1beta1.EventType{},
eventingv1beta2_: &eventingv1beta2.EventType{},
eventingv1beta3_: &eventingv1beta3.EventType{},
},
},
},
Expand Down
77 changes: 73 additions & 4 deletions pkg/apis/eventing/v1beta2/eventtype_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,86 @@ package v1beta2

import (
"context"
"fmt"

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

eventing "knative.dev/eventing/pkg/apis/eventing/v1"
"knative.dev/eventing/pkg/apis/eventing/v1beta3"
)

// ConvertTo implements apis.Convertible
// ConvertTo converts the receiver into `to`.
func (source *EventType) ConvertTo(ctx context.Context, to apis.Convertible) error {
return fmt.Errorf("v1beta2 is the highest known version, got: %T", to)
switch sink := to.(type) {
case *v1beta3.EventType:

source.ObjectMeta.DeepCopyInto(&sink.ObjectMeta)
source.Status.Status.DeepCopyInto(&sink.Status.Status)

sink.Spec.Reference = source.Spec.Reference.DeepCopy()
sink.Spec.Description = source.Spec.Description

if source.Spec.Reference == nil && source.Spec.Broker != "" {
source.Spec.Reference = &duckv1.KReference{
Kind: "Broker",
Name: source.Spec.Broker,
APIVersion: eventing.SchemeGroupVersion.String(),
}
}

sink.Spec.Attributes = []v1beta3.EventAttributeDefinition{}
if source.Spec.Type != "" {
sink.Spec.Attributes = append(sink.Spec.Attributes, v1beta3.EventAttributeDefinition{
Name: "type",
Required: true,
Value: source.Spec.Type,
})
}
if source.Spec.Schema != nil {
sink.Spec.Attributes = append(sink.Spec.Attributes, v1beta3.EventAttributeDefinition{
Name: "schemadata",
Required: false,
Value: source.Spec.Schema.String(),
})
}
if source.Spec.Source != nil {
sink.Spec.Attributes = append(sink.Spec.Attributes, v1beta3.EventAttributeDefinition{
Name: "source",
Required: true,
Value: source.Spec.Source.String(),
})
}
return nil
default:
return apis.ConvertToViaProxy(ctx, source, &v1beta3.EventType{}, to)
}

}

// ConvertFrom implements apis.Convertible
func (sink *EventType) ConvertFrom(ctx context.Context, from apis.Convertible) error {
return fmt.Errorf("v1beta2 is the highest known version, got: %T", from)
switch source := from.(type) {
case *v1beta3.EventType:

source.ObjectMeta.DeepCopyInto(&sink.ObjectMeta)
source.Status.Status.DeepCopyInto(&sink.Status.Status)

sink.Spec.Reference = source.Spec.Reference.DeepCopy()
sink.Spec.Description = source.Spec.Description

for _, at := range source.Spec.Attributes {
switch at.Name {
case "source":
sink.Spec.Source, _ = apis.ParseURL(at.Value)
case "type":
sink.Spec.Type = at.Value
case "schemadata":
sink.Spec.Schema, _ = apis.ParseURL(at.Value)
}
}

return nil
default:
return apis.ConvertFromViaProxy(ctx, from, &v1beta3.EventType{}, sink)
}
}
84 changes: 84 additions & 0 deletions pkg/apis/eventing/v1beta2/eventtype_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ package v1beta2
import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"

"knative.dev/eventing/pkg/apis/eventing/v1beta3"
)

func TestEventTypeConversionHighestVersion(t *testing.T) {
Expand All @@ -32,3 +39,80 @@ func TestEventTypeConversionHighestVersion(t *testing.T) {
t.Errorf("ConvertFrom() = %#v, wanted error", good)
}
}

func TestEventTypeConversionV1Beta3(t *testing.T) {
in := &EventType{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: "my-name",
Namespace: "my-ns",
UID: "1234",
},
Spec: EventTypeSpec{
Type: "t1",
Source: &apis.URL{Scheme: "https", Host: "127.0.0.1", Path: "/sources/my-source"},
Schema: &apis.URL{Scheme: "https", Host: "127.0.0.1", Path: "/schemas/my-schema"},
Broker: "",
Reference: &duckv1.KReference{
Kind: "Broker",
Name: "my-broker",
APIVersion: "eventing.knative.dev/v1",
},
Description: "my-description",
},
Status: EventTypeStatus{
Status: duckv1.Status{
ObservedGeneration: 1234,
},
},
}

expected := &v1beta3.EventType{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{
Name: "my-name",
Namespace: "my-ns",
UID: "1234",
},
Spec: v1beta3.EventTypeSpec{
Reference: in.Spec.Reference.DeepCopy(),
Description: in.Spec.Description,
Attributes: []v1beta3.EventAttributeDefinition{
{
Name: "type",
Required: true,
Value: in.Spec.Type,
},
{
Name: "schemadata",
Required: false,
Value: in.Spec.Schema.String(),
},
{
Name: "source",
Required: true,
Value: in.Spec.Source.String(),
},
},
},
Status: v1beta3.EventTypeStatus{
Status: duckv1.Status{
ObservedGeneration: 1234,
},
},
}
got := &v1beta3.EventType{}

if err := in.ConvertTo(context.Background(), got); err != nil {
t.Errorf("ConvertTo() = %#v, wanted no error, got %#v", expected, err)
} else if diff := cmp.Diff(expected, got); diff != "" {
t.Errorf("ConvertTo(), (-want, +got)\n%s", diff)
}

from := &EventType{}
if err := from.ConvertFrom(context.Background(), expected); err != nil {
t.Errorf("ConvertFrom() = %#v, wanted no error %#v", in, err)
} else if diff := cmp.Diff(in, from); diff != "" {
t.Errorf("ConvertFrom(), (-want, +got)\n%s", diff)
}
}

0 comments on commit f3edb9d

Please sign in to comment.