Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apiserversource defaults conversion #3616

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
4 changes: 3 additions & 1 deletion cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ var ourTypes = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
sourcesv1alpha2.SchemeGroupVersion.WithKind("SinkBinding"): &sourcesv1alpha2.SinkBinding{},
sourcesv1alpha2.SchemeGroupVersion.WithKind("ContainerSource"): &sourcesv1alpha2.ContainerSource{},
// v1beta1
sourcesv1beta1.SchemeGroupVersion.WithKind("ApiServerSource"): &sourcesv1beta1.ApiServerSource{},
sourcesv1beta1.SchemeGroupVersion.WithKind("SinkBinding"): &sourcesv1beta1.SinkBinding{},
sourcesv1beta1.SchemeGroupVersion.WithKind("ContainerSource"): &sourcesv1beta1.ContainerSource{},

Expand Down Expand Up @@ -310,12 +311,13 @@ func NewConversionController(ctx context.Context, cmw configmap.Watcher) *contro
},

// Sources
sourcesv1alpha2.Kind("ApiServerSource"): {
sourcesv1beta1.Kind("ApiServerSource"): {
DefinitionName: sources.ApiServerSourceResource.String(),
HubVersion: sourcesv1alpha1_,
Zygotes: map[string]conversion.ConvertibleObject{
sourcesv1alpha1_: &sourcesv1alpha1.ApiServerSource{},
sourcesv1alpha2_: &sourcesv1alpha2.ApiServerSource{},
sourcesv1beta1_: &sourcesv1beta1.ApiServerSource{},
},
},
sourcesv1alpha2.Kind("PingSource"): {
Expand Down
12 changes: 8 additions & 4 deletions config/core/resources/apiserversource.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,16 @@ spec:
name: v1alpha2
served: true
storage: false
- <<: *version
name: v1beta1
served: true
storage: false
names:
categories:
- all
- knative
- eventing
- sources
- all
- knative
- eventing
- sources
kind: ApiServerSource
plural: apiserversources
scope: Namespaced
Expand Down
9 changes: 4 additions & 5 deletions pkg/apis/sources/v1alpha1/apiserver_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package v1alpha1

import (
"context"
"fmt"
"reflect"

"github.com/google/go-cmp/cmp"
Expand All @@ -32,7 +31,7 @@ import (
)

// ConvertTo implements apis.Convertible.
// Converts source (from v1alpha1.ApiServerSource) into v1alpha2.ApiServerSource
// Converts source (from v1alpha1.ApiServerSource) into into a higher version.
func (source *ApiServerSource) ConvertTo(ctx context.Context, obj apis.Convertible) error {
switch sink := obj.(type) {
case *v1alpha2.ApiServerSource:
Expand Down Expand Up @@ -95,12 +94,12 @@ func (source *ApiServerSource) ConvertTo(ctx context.Context, obj apis.Convertib
source.Status.SourceStatus.DeepCopyInto(&sink.Status.SourceStatus)
return nil
default:
return fmt.Errorf("Unknown conversion, got: %T", sink)
return apis.ConvertToViaProxy(ctx, source, &v1alpha2.ApiServerSource{}, sink)
}
}

// ConvertFrom implements apis.Convertible.
// Converts obj from v1alpha2.ApiServerSource into v1alpha1.ApiServerSource
// Converts obj from a higher version into v1alpha1.ApiServerSource.
func (sink *ApiServerSource) ConvertFrom(ctx context.Context, obj apis.Convertible) error {
switch source := obj.(type) {
case *v1alpha2.ApiServerSource:
Expand Down Expand Up @@ -158,6 +157,6 @@ func (sink *ApiServerSource) ConvertFrom(ctx context.Context, obj apis.Convertib

return nil
default:
return fmt.Errorf("Unknown conversion, got: %T", source)
return apis.ConvertFromViaProxy(ctx, source, &v1alpha2.ApiServerSource{}, sink)
}
}
23 changes: 11 additions & 12 deletions pkg/apis/sources/v1alpha1/apiserver_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1"

"knative.dev/eventing/pkg/apis/sources/v1alpha2"
"knative.dev/eventing/pkg/apis/sources/v1beta1"

"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -46,7 +47,7 @@ func TestApiServerSourceConversionBadType(t *testing.T) {

func TestApiServerSourceConversionRoundTripUp(t *testing.T) {
// Just one for now, just adding the for loop for ease of future changes.
versions := []apis.Convertible{&v1alpha2.ApiServerSource{}}
versions := []apis.Convertible{&v1alpha2.ApiServerSource{}, &v1beta1.ApiServerSource{}}

path, _ := apis.ParseURL("/path")
sink := duckv1beta1.Destination{
Expand Down Expand Up @@ -196,8 +197,6 @@ func TestApiServerSourceConversionRoundTripUp(t *testing.T) {

// This tests round tripping from a higher version -> v1alpha1 and back to the higher version.
func TestApiServerSourceConversionRoundTripDown(t *testing.T) {
// Just one for now, just adding the for loop for ease of future changes.

path, _ := apis.ParseURL("/path")
sink := duckv1.Destination{
Ref: &duckv1.KReference{
Expand All @@ -221,28 +220,28 @@ func TestApiServerSourceConversionRoundTripDown(t *testing.T) {
name string
in apis.Convertible
}{{name: "empty",
in: &v1alpha2.ApiServerSource{
in: &v1beta1.ApiServerSource{
ObjectMeta: metav1.ObjectMeta{
Name: "apiserver-name",
Namespace: "apiserver-ns",
Generation: 17,
},
Spec: v1alpha2.ApiServerSourceSpec{},
Status: v1alpha2.ApiServerSourceStatus{},
Spec: v1beta1.ApiServerSourceSpec{},
Status: v1beta1.ApiServerSourceStatus{},
},
}, {name: "simple configuration",
in: &v1alpha2.ApiServerSource{
in: &v1beta1.ApiServerSource{
ObjectMeta: metav1.ObjectMeta{
Name: "apiserver-name",
Namespace: "apiserver-ns",
Generation: 17,
},
Spec: v1alpha2.ApiServerSourceSpec{
Spec: v1beta1.ApiServerSourceSpec{
SourceSpec: duckv1.SourceSpec{
Sink: sink,
},
},
Status: v1alpha2.ApiServerSourceStatus{
Status: v1beta1.ApiServerSourceStatus{
SourceStatus: duckv1.SourceStatus{
Status: duckv1.Status{
ObservedGeneration: 1,
Expand All @@ -256,19 +255,19 @@ func TestApiServerSourceConversionRoundTripDown(t *testing.T) {
},
},
}, {name: "full",
in: &v1alpha2.ApiServerSource{
in: &v1beta1.ApiServerSource{
ObjectMeta: metav1.ObjectMeta{
Name: "apiserver-name",
Namespace: "apiserver-ns",
Generation: 17,
},
Spec: v1alpha2.ApiServerSourceSpec{
Spec: v1beta1.ApiServerSourceSpec{
SourceSpec: duckv1.SourceSpec{
Sink: sink,
CloudEventOverrides: &ceOverrides,
},
},
Status: v1alpha2.ApiServerSourceStatus{
Status: v1beta1.ApiServerSourceStatus{
SourceStatus: duckv1.SourceStatus{
Status: duckv1.Status{
ObservedGeneration: 1,
Expand Down
113 changes: 108 additions & 5 deletions pkg/apis/sources/v1alpha2/apiserver_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,120 @@ package v1alpha2

import (
"context"
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/eventing/pkg/apis/sources/v1beta1"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
)

// ConvertTo implements apis.Convertible
func (source *ApiServerSource) ConvertTo(ctx context.Context, sink apis.Convertible) error {
return fmt.Errorf("v1alpha2 is the highest known version, got: %T", sink)
// Converts source (from v1alpha2.ApiServerSource) into into a higher version.
func (source *ApiServerSource) ConvertTo(ctx context.Context, obj apis.Convertible) error {
switch sink := obj.(type) {
case *v1beta1.ApiServerSource:
// Meta
sink.ObjectMeta = source.ObjectMeta

// Spec

if len(source.Spec.Resources) > 0 {
sink.Spec.Resources = make([]v1beta1.APIVersionKindSelector, len(source.Spec.Resources))
}
for i, v := range source.Spec.Resources {
sink.Spec.Resources[i] = v1beta1.APIVersionKindSelector{
APIVersion: v.APIVersion,
Kind: v.Kind,
}

if v.LabelSelector != nil {
sink.Spec.Resources[i].LabelSelector = &metav1.LabelSelector{}
v.LabelSelector.DeepCopyInto(sink.Spec.Resources[i].LabelSelector)
}
}

sink.Spec.EventMode = source.Spec.EventMode

// Optional Spec

if source.Spec.ResourceOwner != nil {
sink.Spec.ResourceOwner = &v1beta1.APIVersionKind{
Kind: source.Spec.ResourceOwner.Kind,
APIVersion: source.Spec.ResourceOwner.APIVersion,
}
}

var ref *duckv1.KReference
if source.Spec.Sink.Ref != nil {
ref = &duckv1.KReference{
Kind: source.Spec.Sink.Ref.Kind,
Namespace: source.Spec.Sink.Ref.Namespace,
Name: source.Spec.Sink.Ref.Name,
APIVersion: source.Spec.Sink.Ref.APIVersion,
}
}
sink.Spec.Sink = duckv1.Destination{
Ref: ref,
URI: source.Spec.Sink.URI,
}

if source.Spec.CloudEventOverrides != nil {
sink.Spec.CloudEventOverrides = source.Spec.CloudEventOverrides.DeepCopy()
}

sink.Spec.ServiceAccountName = source.Spec.ServiceAccountName

// Status
source.Status.SourceStatus.DeepCopyInto(&sink.Status.SourceStatus)
return nil
default:
return apis.ConvertToViaProxy(ctx, source, &v1beta1.ApiServerSource{}, sink)
}
}

// ConvertFrom implements apis.Convertible
func (sink *ApiServerSource) ConvertFrom(ctx context.Context, source apis.Convertible) error {
return fmt.Errorf("v1alpha2 is the highest known version, got: %T", source)
// Converts obj from a higher version into v1alpha2.ApiServerSource.
func (sink *ApiServerSource) ConvertFrom(ctx context.Context, obj apis.Convertible) error {
switch source := obj.(type) {
case *v1beta1.ApiServerSource:
// Meta
sink.ObjectMeta = source.ObjectMeta

// Spec
sink.Spec.EventMode = source.Spec.EventMode

sink.Spec.CloudEventOverrides = source.Spec.CloudEventOverrides

sink.Spec.Sink = source.Spec.Sink

if len(source.Spec.Resources) > 0 {
sink.Spec.Resources = make([]APIVersionKindSelector, len(source.Spec.Resources))
}
for i, v := range source.Spec.Resources {
sink.Spec.Resources[i] = APIVersionKindSelector{}
sink.Spec.Resources[i].APIVersion = v.APIVersion
sink.Spec.Resources[i].Kind = v.Kind
if v.LabelSelector != nil {
sink.Spec.Resources[i].LabelSelector = v.LabelSelector.DeepCopy()
}
}

// Spec Optionals

if source.Spec.ResourceOwner != nil {
sink.Spec.ResourceOwner = &APIVersionKind{
Kind: source.Spec.ResourceOwner.Kind,
APIVersion: source.Spec.ResourceOwner.APIVersion,
}
}

sink.Spec.ServiceAccountName = source.Spec.ServiceAccountName

// Status
source.Status.SourceStatus.DeepCopyInto(&sink.Status.SourceStatus)

return nil
default:
return apis.ConvertFromViaProxy(ctx, source, &v1beta1.ApiServerSource{}, sink)
}
}
Loading