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

Trigger conversion function (v1alpha1 <-> v1beta1) #2508

Merged
merged 3 commits into from
Feb 5, 2020
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
82 changes: 82 additions & 0 deletions pkg/apis/eventing/v1alpha1/trigger_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2020 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 v1alpha1

import (
"context"
"fmt"

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

// ConvertUp implements apis.Convertible.
// Converts source (from v1alpha1.Trigger) into v1beta1.Trigger
func (source *Trigger) ConvertUp(ctx context.Context, obj apis.Convertible) error {
switch sink := obj.(type) {
case *v1beta1.Trigger:
sink.ObjectMeta = source.ObjectMeta
sink.Spec.Broker = source.Spec.Broker
sink.Spec.Subscriber = source.Spec.Subscriber
if source.Spec.Filter != nil {
sink.Spec.Filter = &v1beta1.TriggerFilter{
Attributes: make(v1beta1.TriggerFilterAttributes, 0),
}
if source.Spec.Filter.Attributes != nil {
for k, v := range *source.Spec.Filter.Attributes {
sink.Spec.Filter.Attributes[k] = v
}
}
if source.Spec.Filter.DeprecatedSourceAndType != nil {
sink.Spec.Filter.Attributes["source"] = source.Spec.Filter.DeprecatedSourceAndType.Source
sink.Spec.Filter.Attributes["type"] = source.Spec.Filter.DeprecatedSourceAndType.Type
}
}
sink.Status.Status = source.Status.Status
sink.Status.SubscriberURI = source.Status.SubscriberURI
return nil
default:
return fmt.Errorf("Unknown conversion, got: %T", sink)

}
}

// ConvertDown implements apis.Convertible.
// Converts obj from v1beta1.Trigger into v1alpha1.Trigger
func (sink *Trigger) ConvertDown(ctx context.Context, obj apis.Convertible) error {
switch source := obj.(type) {
case *v1beta1.Trigger:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be v1alpha1.Trigger right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait what the...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok the older version takes the conversion. I see.

sink.ObjectMeta = source.ObjectMeta
sink.Spec.Broker = source.Spec.Broker
sink.Spec.Subscriber = source.Spec.Subscriber
if source.Spec.Filter != nil {
attributes := TriggerFilterAttributes{}
for k, v := range source.Spec.Filter.Attributes {
attributes[k] = v
}
sink.Spec.Filter = &TriggerFilter{
Attributes: &attributes,
}
}

sink.Status.Status = source.Status.Status
sink.Status.SubscriberURI = source.Status.SubscriberURI
return nil
default:
return fmt.Errorf("Unknown conversion, got: %T", source)
}
}
190 changes: 190 additions & 0 deletions pkg/apis/eventing/v1alpha1/trigger_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/*
Copyright 2020 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 v1alpha1

import (
"context"
"errors"
"testing"

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

// TODO: Replace dummy some other Eventing object once they
// implement apis.Convertible
type dummy struct{}

func (*dummy) ConvertUp(ctx context.Context, obj apis.Convertible) error {
return errors.New("Won't go")
}

func (*dummy) ConvertDown(ctx context.Context, obj apis.Convertible) error {
return errors.New("Won't go")
}

func TestTriggerConversionBadType(t *testing.T) {
good, bad := &Trigger{}, &dummy{}

if err := good.ConvertUp(context.Background(), bad); err == nil {
t.Errorf("ConvertUp() = %#v, wanted error", bad)
}

if err := good.ConvertDown(context.Background(), bad); err == nil {
t.Errorf("ConvertDown() = %#v, wanted error", good)
}
}

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

tests := []struct {
name string
in *Trigger
}{{name: "simple configuration",
in: &Trigger{
ObjectMeta: metav1.ObjectMeta{
Name: "trigger-name",
Namespace: "trigger-ns",
Generation: 17,
},
Spec: TriggerSpec{
Broker: "default",
},
Status: TriggerStatus{
Status: duckv1.Status{
ObservedGeneration: 1,
Conditions: duckv1.Conditions{{
Type: "Ready",
Status: "True",
}},
},
},
},
}, {name: "filter rules, deprecated",
in: &Trigger{
ObjectMeta: metav1.ObjectMeta{
Name: "trigger-name",
Namespace: "trigger-ns",
Generation: 17,
},
Spec: TriggerSpec{
Broker: "default",
Filter: &TriggerFilter{
DeprecatedSourceAndType: &TriggerFilterSourceAndType{
Source: "mysource",
Type: "mytype",
},
},
},
Status: TriggerStatus{
Status: duckv1.Status{
ObservedGeneration: 1,
Conditions: duckv1.Conditions{{
Type: "Ready",
Status: "True",
}},
},
},
},
}, {name: "filter rules",
in: &Trigger{
ObjectMeta: metav1.ObjectMeta{
Name: "trigger-name",
Namespace: "trigger-ns",
Generation: 17,
},
Spec: TriggerSpec{
Broker: "default",
Filter: &TriggerFilter{
Attributes: &TriggerFilterAttributes{"source": "mysource", "type": "mytype"},
},
},
Status: TriggerStatus{
Status: duckv1.Status{
ObservedGeneration: 1,
Conditions: duckv1.Conditions{{
Type: "Ready",
Status: "True",
}},
},
},
},
}, {name: "filter rules, many",
in: &Trigger{
ObjectMeta: metav1.ObjectMeta{
Name: "trigger-name",
Namespace: "trigger-ns",
Generation: 17,
},
Spec: TriggerSpec{
Broker: "default",
Filter: &TriggerFilter{
Attributes: &TriggerFilterAttributes{"source": "mysource", "type": "mytype", "customkey": "customvalue"},
},
},
Status: TriggerStatus{
Status: duckv1.Status{
ObservedGeneration: 1,
Conditions: duckv1.Conditions{{
Type: "Ready",
Status: "True",
}},
},
},
},
}}
for _, test := range tests {
for _, version := range versions {
t.Run(test.name, func(t *testing.T) {
ver := version
if err := test.in.ConvertUp(context.Background(), ver); err != nil {
t.Errorf("ConvertUp() = %v", err)
}
got := &Trigger{}
if err := got.ConvertDown(context.Background(), ver); err != nil {
t.Errorf("ConvertDown() = %v", err)
}
// Since on the way down, we lose the DeprecatedSourceAndType,
// convert the in to equivalent out.
fixed := fixDeprecated(test.in)
if diff := cmp.Diff(fixed, got); diff != "" {
t.Errorf("roundtrip (-want, +got) = %v", diff)
}
})
}
}
}

// Since DeprecatedSourceAndType is lossy but semanctically equivalent
// if source,type are present and equivalent in the attributes map,
// fix that so diff works.
func fixDeprecated(in *Trigger) *Trigger {
if in.Spec.Filter != nil && in.Spec.Filter.DeprecatedSourceAndType != nil {
// attributes must be nil, can't have both Deprecated / Attributes
attributes := TriggerFilterAttributes{}
attributes["source"] = in.Spec.Filter.DeprecatedSourceAndType.Source
attributes["type"] = in.Spec.Filter.DeprecatedSourceAndType.Type
in.Spec.Filter.DeprecatedSourceAndType = nil
in.Spec.Filter.Attributes = &attributes
}
return in
}
3 changes: 3 additions & 0 deletions pkg/apis/eventing/v1alpha1/trigger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ var (
// Check that Trigger can return its spec untyped.
_ apis.HasSpec = (*Trigger)(nil)

// Check that Service can be converted to higher versions.
_ apis.Convertible = (*Trigger)(nil)

_ runtime.Object = (*Trigger)(nil)

// Check that we can create OwnerReferences to a Trigger.
Expand Down
44 changes: 44 additions & 0 deletions pkg/apis/eventing/v1beta1/broker_lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2020 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 v1beta1

import (
"knative.dev/pkg/apis"
)

var brokerCondSet = apis.NewLivingConditionSet(BrokerConditionAddressable)

const (
BrokerConditionReady = apis.ConditionReady
BrokerConditionAddressable apis.ConditionType = "Addressable"
)

// GetTopLevelCondition returns the top level Condition.
func (bs *BrokerStatus) GetTopLevelCondition() *apis.Condition {
return brokerCondSet.Manage(bs).GetTopLevelCondition()
}

// SetAddress makes this Broker addressable by setting the URI. It also
// sets the BrokerConditionAddressable to true.
func (bs *BrokerStatus) SetAddress(url *apis.URL) {
bs.Address.URL = url
if url != nil {
brokerCondSet.Manage(bs).MarkTrue(BrokerConditionAddressable)
} else {
brokerCondSet.Manage(bs).MarkFalse(BrokerConditionAddressable, "nil URL", "URL is nil")
}
}
22 changes: 7 additions & 15 deletions pkg/apis/eventing/v1beta1/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
corev1 "k8s.io/api/core/v1"

duckv1alpha1 "knative.dev/eventing/pkg/apis/duck/v1alpha1"
eventingv1alpha1 "knative.dev/eventing/pkg/apis/eventing/v1alpha1"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
pkgduckv1alpha1 "knative.dev/pkg/apis/duck/v1alpha1"
Expand Down Expand Up @@ -65,12 +64,9 @@ func (testHelper) ReadyChannelStatus() *duckv1alpha1.ChannelableStatus {
return cs
}

func (t testHelper) ReadyBrokerStatus() *eventingv1alpha1.BrokerStatus {
bs := &eventingv1alpha1.BrokerStatus{}
bs.PropagateIngressDeploymentAvailability(t.AvailableDeployment())
bs.PropagateTriggerChannelReadiness(t.ReadyChannelStatus())
bs.PropagateFilterDeploymentAvailability(t.AvailableDeployment())
bs.SetAddress(&apis.URL{Scheme: "http", Host: "foo"})
func (t testHelper) ReadyBrokerStatus() *BrokerStatus {
bs := &BrokerStatus{}
bs.SetAddress(apis.HTTP("example.com"))
return bs
}

Expand All @@ -86,17 +82,13 @@ func (t testHelper) AvailableDeployment() *v1.Deployment {
return d
}

func (t testHelper) UnknownBrokerStatus() *eventingv1alpha1.BrokerStatus {
bs := &eventingv1alpha1.BrokerStatus{}
bs.InitializeConditions()
func (t testHelper) UnknownBrokerStatus() *BrokerStatus {
bs := &BrokerStatus{}
return bs
}

func (t testHelper) FalseBrokerStatus() *eventingv1alpha1.BrokerStatus {
bs := &eventingv1alpha1.BrokerStatus{}
bs.MarkIngressFailed("DeploymentUnavailable", "The Deployment is unavailable.")
bs.MarkTriggerChannelFailed("ChannelNotReady", "trigger Channel is not ready: not addressalbe")
bs.MarkFilterFailed("DeploymentUnavailable", "The Deployment is unavailable.")
func (t testHelper) FalseBrokerStatus() *BrokerStatus {
bs := &BrokerStatus{}
bs.SetAddress(nil)
return bs
}
Loading