Skip to content

Commit b5eb476

Browse files
matzewpierDipi
andauthored
Add EventType v1beta3 conversion (knative#7938) (#981)
Add ETv1b3 conversion Signed-off-by: Pierangelo Di Pilato <[email protected]> Co-authored-by: Pierangelo Di Pilato <[email protected]>
1 parent faf2f11 commit b5eb476

File tree

3 files changed

+160
-4
lines changed

3 files changed

+160
-4
lines changed

cmd/webhook/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"k8s.io/client-go/kubernetes/scheme"
2626
configmapinformer "knative.dev/pkg/client/injection/kube/informers/core/v1/configmap/filtered"
2727

28+
eventingv1beta3 "knative.dev/eventing/pkg/apis/eventing/v1beta3"
2829
"knative.dev/eventing/pkg/apis/feature"
2930
"knative.dev/eventing/pkg/auth"
3031
"knative.dev/eventing/pkg/eventingtls"
@@ -241,6 +242,7 @@ func NewConversionController(ctx context.Context, cmw configmap.Watcher) *contro
241242
sourcesv1_ = sourcesv1.SchemeGroupVersion.Version
242243
eventingv1beta1_ = eventingv1beta1.SchemeGroupVersion.Version
243244
eventingv1beta2_ = eventingv1beta2.SchemeGroupVersion.Version
245+
eventingv1beta3_ = eventingv1beta3.SchemeGroupVersion.Version
244246
)
245247

246248
return conversion.NewConversionController(ctx,
@@ -265,6 +267,7 @@ func NewConversionController(ctx context.Context, cmw configmap.Watcher) *contro
265267
Zygotes: map[string]conversion.ConvertibleObject{
266268
eventingv1beta1_: &eventingv1beta1.EventType{},
267269
eventingv1beta2_: &eventingv1beta2.EventType{},
270+
eventingv1beta3_: &eventingv1beta3.EventType{},
268271
},
269272
},
270273
},

pkg/apis/eventing/v1beta2/eventtype_conversion.go

Lines changed: 73 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,86 @@ package v1beta2
1818

1919
import (
2020
"context"
21-
"fmt"
2221

2322
"knative.dev/pkg/apis"
23+
duckv1 "knative.dev/pkg/apis/duck/v1"
24+
25+
eventing "knative.dev/eventing/pkg/apis/eventing/v1"
26+
"knative.dev/eventing/pkg/apis/eventing/v1beta3"
2427
)
2528

26-
// ConvertTo implements apis.Convertible
29+
// ConvertTo converts the receiver into `to`.
2730
func (source *EventType) ConvertTo(ctx context.Context, to apis.Convertible) error {
28-
return fmt.Errorf("v1beta2 is the highest known version, got: %T", to)
31+
switch sink := to.(type) {
32+
case *v1beta3.EventType:
33+
34+
source.ObjectMeta.DeepCopyInto(&sink.ObjectMeta)
35+
source.Status.Status.DeepCopyInto(&sink.Status.Status)
36+
37+
sink.Spec.Reference = source.Spec.Reference.DeepCopy()
38+
sink.Spec.Description = source.Spec.Description
39+
40+
if source.Spec.Reference == nil && source.Spec.Broker != "" {
41+
source.Spec.Reference = &duckv1.KReference{
42+
Kind: "Broker",
43+
Name: source.Spec.Broker,
44+
APIVersion: eventing.SchemeGroupVersion.String(),
45+
}
46+
}
47+
48+
sink.Spec.Attributes = []v1beta3.EventAttributeDefinition{}
49+
if source.Spec.Type != "" {
50+
sink.Spec.Attributes = append(sink.Spec.Attributes, v1beta3.EventAttributeDefinition{
51+
Name: "type",
52+
Required: true,
53+
Value: source.Spec.Type,
54+
})
55+
}
56+
if source.Spec.Schema != nil {
57+
sink.Spec.Attributes = append(sink.Spec.Attributes, v1beta3.EventAttributeDefinition{
58+
Name: "schemadata",
59+
Required: false,
60+
Value: source.Spec.Schema.String(),
61+
})
62+
}
63+
if source.Spec.Source != nil {
64+
sink.Spec.Attributes = append(sink.Spec.Attributes, v1beta3.EventAttributeDefinition{
65+
Name: "source",
66+
Required: true,
67+
Value: source.Spec.Source.String(),
68+
})
69+
}
70+
return nil
71+
default:
72+
return apis.ConvertToViaProxy(ctx, source, &v1beta3.EventType{}, to)
73+
}
74+
2975
}
3076

3177
// ConvertFrom implements apis.Convertible
3278
func (sink *EventType) ConvertFrom(ctx context.Context, from apis.Convertible) error {
33-
return fmt.Errorf("v1beta2 is the highest known version, got: %T", from)
79+
switch source := from.(type) {
80+
case *v1beta3.EventType:
81+
82+
source.ObjectMeta.DeepCopyInto(&sink.ObjectMeta)
83+
source.Status.Status.DeepCopyInto(&sink.Status.Status)
84+
85+
sink.Spec.Reference = source.Spec.Reference.DeepCopy()
86+
sink.Spec.Description = source.Spec.Description
87+
88+
for _, at := range source.Spec.Attributes {
89+
switch at.Name {
90+
case "source":
91+
sink.Spec.Source, _ = apis.ParseURL(at.Value)
92+
case "type":
93+
sink.Spec.Type = at.Value
94+
case "schemadata":
95+
sink.Spec.Schema, _ = apis.ParseURL(at.Value)
96+
}
97+
}
98+
99+
return nil
100+
default:
101+
return apis.ConvertFromViaProxy(ctx, from, &v1beta3.EventType{}, sink)
102+
}
34103
}

pkg/apis/eventing/v1beta2/eventtype_conversion_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ package v1beta2
1919
import (
2020
"context"
2121
"testing"
22+
23+
"github.com/google/go-cmp/cmp"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"knative.dev/pkg/apis"
26+
duckv1 "knative.dev/pkg/apis/duck/v1"
27+
28+
"knative.dev/eventing/pkg/apis/eventing/v1beta3"
2229
)
2330

2431
func TestEventTypeConversionHighestVersion(t *testing.T) {
@@ -32,3 +39,80 @@ func TestEventTypeConversionHighestVersion(t *testing.T) {
3239
t.Errorf("ConvertFrom() = %#v, wanted error", good)
3340
}
3441
}
42+
43+
func TestEventTypeConversionV1Beta3(t *testing.T) {
44+
in := &EventType{
45+
TypeMeta: metav1.TypeMeta{},
46+
ObjectMeta: metav1.ObjectMeta{
47+
Name: "my-name",
48+
Namespace: "my-ns",
49+
UID: "1234",
50+
},
51+
Spec: EventTypeSpec{
52+
Type: "t1",
53+
Source: &apis.URL{Scheme: "https", Host: "127.0.0.1", Path: "/sources/my-source"},
54+
Schema: &apis.URL{Scheme: "https", Host: "127.0.0.1", Path: "/schemas/my-schema"},
55+
Broker: "",
56+
Reference: &duckv1.KReference{
57+
Kind: "Broker",
58+
Name: "my-broker",
59+
APIVersion: "eventing.knative.dev/v1",
60+
},
61+
Description: "my-description",
62+
},
63+
Status: EventTypeStatus{
64+
Status: duckv1.Status{
65+
ObservedGeneration: 1234,
66+
},
67+
},
68+
}
69+
70+
expected := &v1beta3.EventType{
71+
TypeMeta: metav1.TypeMeta{},
72+
ObjectMeta: metav1.ObjectMeta{
73+
Name: "my-name",
74+
Namespace: "my-ns",
75+
UID: "1234",
76+
},
77+
Spec: v1beta3.EventTypeSpec{
78+
Reference: in.Spec.Reference.DeepCopy(),
79+
Description: in.Spec.Description,
80+
Attributes: []v1beta3.EventAttributeDefinition{
81+
{
82+
Name: "type",
83+
Required: true,
84+
Value: in.Spec.Type,
85+
},
86+
{
87+
Name: "schemadata",
88+
Required: false,
89+
Value: in.Spec.Schema.String(),
90+
},
91+
{
92+
Name: "source",
93+
Required: true,
94+
Value: in.Spec.Source.String(),
95+
},
96+
},
97+
},
98+
Status: v1beta3.EventTypeStatus{
99+
Status: duckv1.Status{
100+
ObservedGeneration: 1234,
101+
},
102+
},
103+
}
104+
got := &v1beta3.EventType{}
105+
106+
if err := in.ConvertTo(context.Background(), got); err != nil {
107+
t.Errorf("ConvertTo() = %#v, wanted no error, got %#v", expected, err)
108+
} else if diff := cmp.Diff(expected, got); diff != "" {
109+
t.Errorf("ConvertTo(), (-want, +got)\n%s", diff)
110+
}
111+
112+
from := &EventType{}
113+
if err := from.ConvertFrom(context.Background(), expected); err != nil {
114+
t.Errorf("ConvertFrom() = %#v, wanted no error %#v", in, err)
115+
} else if diff := cmp.Diff(in, from); diff != "" {
116+
t.Errorf("ConvertFrom(), (-want, +got)\n%s", diff)
117+
}
118+
}

0 commit comments

Comments
 (0)