forked from knative/eventing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Types for v1 messaging (knative#3403)
* adding v1 API for messaging types * fixes * conversions stashing work * channel conversion * removing changes to conversion to make this PR smaller * address review comments
- Loading branch information
Showing
83 changed files
with
8,606 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
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 v1 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"knative.dev/pkg/apis" | ||
) | ||
|
||
// ConvertTo implements apis.Convertible | ||
func (source *Channel) ConvertTo(ctx context.Context, sink apis.Convertible) error { | ||
return fmt.Errorf("v1 is the highest known version, got: %T", sink) | ||
} | ||
|
||
// ConvertFrom implements apis.Convertible | ||
func (sink *Channel) ConvertFrom(ctx context.Context, source apis.Convertible) error { | ||
return fmt.Errorf("v1 is the highest known version, got: %T", source) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
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 v1 | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestChannelConversionBadType(t *testing.T) { | ||
good, bad := &Channel{}, &Channel{} | ||
|
||
if err := good.ConvertTo(context.Background(), bad); err == nil { | ||
t.Errorf("ConvertTo() = %#v, wanted error", bad) | ||
} | ||
|
||
if err := good.ConvertFrom(context.Background(), bad); err == nil { | ||
t.Errorf("ConvertFrom() = %#v, wanted error", good) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
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 v1 | ||
|
||
import ( | ||
"context" | ||
|
||
"knative.dev/eventing/pkg/apis/messaging" | ||
"knative.dev/eventing/pkg/apis/messaging/config" | ||
"knative.dev/pkg/apis" | ||
) | ||
|
||
func (c *Channel) SetDefaults(ctx context.Context) { | ||
if c.Annotations == nil { | ||
c.Annotations = make(map[string]string) | ||
} | ||
if _, ok := c.Annotations[messaging.SubscribableDuckVersionAnnotation]; !ok { | ||
c.Annotations[messaging.SubscribableDuckVersionAnnotation] = "v1" | ||
} | ||
|
||
c.Spec.SetDefaults(apis.WithinParent(ctx, c.ObjectMeta)) | ||
} | ||
|
||
func (cs *ChannelSpec) SetDefaults(ctx context.Context) { | ||
if cs.ChannelTemplate != nil { | ||
return | ||
} | ||
|
||
cfg := config.FromContextOrDefaults(ctx) | ||
c, err := cfg.ChannelDefaults.GetChannelConfig(apis.ParentMeta(ctx).Namespace) | ||
if err == nil { | ||
cs.ChannelTemplate = &ChannelTemplateSpec{ | ||
c.TypeMeta, | ||
c.Spec, | ||
} | ||
} | ||
} | ||
|
||
// ChannelDefaulter sets the default Channel CRD and Arguments on Channels that do not | ||
// specify any implementation. | ||
type ChannelDefaulter interface { | ||
// GetDefault determines the default Channel CRD for the given namespace. | ||
GetDefault(namespace string) *ChannelTemplateSpec | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
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 v1 | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"knative.dev/eventing/pkg/apis/messaging/config" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
var ( | ||
defaultChannelTemplate = &config.ChannelTemplateSpec{ | ||
TypeMeta: v1.TypeMeta{ | ||
APIVersion: SchemeGroupVersion.String(), | ||
Kind: "InMemoryChannel", | ||
}, | ||
} | ||
ourDefaultChannelTemplate = &ChannelTemplateSpec{ | ||
TypeMeta: v1.TypeMeta{ | ||
APIVersion: SchemeGroupVersion.String(), | ||
Kind: "InMemoryChannel", | ||
}, | ||
} | ||
) | ||
|
||
func TestChannelSetDefaults(t *testing.T) { | ||
testCases := map[string]struct { | ||
nilChannelDefaulter bool | ||
channelTemplate *config.ChannelTemplateSpec | ||
initial Channel | ||
expected Channel | ||
}{ | ||
"nil ChannelDefaulter": { | ||
nilChannelDefaulter: true, | ||
expected: Channel{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{"messaging.knative.dev/subscribable": "v1"}}}, | ||
}, | ||
"unset ChannelDefaulter": { | ||
expected: Channel{ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{"messaging.knative.dev/subscribable": "v1"}}}, | ||
}, | ||
"set ChannelDefaulter": { | ||
channelTemplate: defaultChannelTemplate, | ||
expected: Channel{ | ||
ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{"messaging.knative.dev/subscribable": "v1"}}, | ||
Spec: ChannelSpec{ | ||
ChannelTemplate: ourDefaultChannelTemplate, | ||
}, | ||
}, | ||
}, | ||
"template already specified": { | ||
channelTemplate: defaultChannelTemplate, | ||
initial: Channel{ | ||
Spec: ChannelSpec{ | ||
ChannelTemplate: &ChannelTemplateSpec{ | ||
TypeMeta: v1.TypeMeta{ | ||
APIVersion: SchemeGroupVersion.String(), | ||
Kind: "OtherChannel", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: Channel{ | ||
ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{"messaging.knative.dev/subscribable": "v1"}}, | ||
Spec: ChannelSpec{ | ||
ChannelTemplate: &ChannelTemplateSpec{ | ||
TypeMeta: v1.TypeMeta{ | ||
APIVersion: SchemeGroupVersion.String(), | ||
Kind: "OtherChannel", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for n, tc := range testCases { | ||
t.Run(n, func(t *testing.T) { | ||
ctx := context.Background() | ||
if !tc.nilChannelDefaulter { | ||
ctx = config.ToContext(ctx, &config.Config{ | ||
ChannelDefaults: &config.ChannelDefaults{ | ||
ClusterDefault: tc.channelTemplate, | ||
}, | ||
}) | ||
} | ||
tc.initial.SetDefaults(ctx) | ||
if diff := cmp.Diff(tc.expected, tc.initial); diff != "" { | ||
t.Fatalf("Unexpected defaults (-want, +got): %s", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* 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 v1 | ||
|
||
import ( | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
eventingduck "knative.dev/eventing/pkg/apis/duck/v1" | ||
"knative.dev/pkg/apis" | ||
duckv1 "knative.dev/pkg/apis/duck/v1" | ||
) | ||
|
||
var chCondSet = apis.NewLivingConditionSet(ChannelConditionBackingChannelReady, ChannelConditionAddressable) | ||
|
||
const ( | ||
// ChannelConditionReady has status True when all subconditions below have been set to True. | ||
ChannelConditionReady = apis.ConditionReady | ||
|
||
// ChannelConditionBackingChannelReady has status True when the backing Channel CRD is ready. | ||
ChannelConditionBackingChannelReady apis.ConditionType = "BackingChannelReady" | ||
|
||
// ChannelConditionAddressable has status true when this Channel meets | ||
// the Addressable contract and has a non-empty hostname. | ||
ChannelConditionAddressable apis.ConditionType = "Addressable" | ||
) | ||
|
||
// GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface. | ||
func (*Channel) GetConditionSet() apis.ConditionSet { | ||
return chCondSet | ||
} | ||
|
||
// GetGroupVersionKind returns GroupVersionKind for Channels. | ||
func (*Channel) GetGroupVersionKind() schema.GroupVersionKind { | ||
return SchemeGroupVersion.WithKind("Channel") | ||
} | ||
|
||
// GetUntypedSpec returns the spec of the Channel. | ||
func (c *Channel) GetUntypedSpec() interface{} { | ||
return c.Spec | ||
} | ||
|
||
// GetCondition returns the condition currently associated with the given type, or nil. | ||
func (cs *ChannelStatus) GetCondition(t apis.ConditionType) *apis.Condition { | ||
return chCondSet.Manage(cs).GetCondition(t) | ||
} | ||
|
||
// GetTopLevelCondition returns the top level Condition. | ||
func (cs *ChannelStatus) GetTopLevelCondition() *apis.Condition { | ||
return chCondSet.Manage(cs).GetTopLevelCondition() | ||
} | ||
|
||
// IsReady returns true if the resource is ready overall. | ||
func (cs *ChannelStatus) IsReady() bool { | ||
return chCondSet.Manage(cs).IsHappy() | ||
} | ||
|
||
// InitializeConditions sets relevant unset conditions to Unknown state. | ||
func (cs *ChannelStatus) InitializeConditions() { | ||
chCondSet.Manage(cs).InitializeConditions() | ||
} | ||
|
||
func (cs *ChannelStatus) SetAddress(address *duckv1.Addressable) { | ||
cs.Address = address | ||
if address == nil || address.URL.IsEmpty() { | ||
chCondSet.Manage(cs).MarkFalse(ChannelConditionAddressable, "EmptyHostname", "hostname is the empty string") | ||
} else { | ||
chCondSet.Manage(cs).MarkTrue(ChannelConditionAddressable) | ||
|
||
} | ||
} | ||
|
||
func (cs *ChannelStatus) MarkBackingChannelFailed(reason, messageFormat string, messageA ...interface{}) { | ||
chCondSet.Manage(cs).MarkFalse(ChannelConditionBackingChannelReady, reason, messageFormat, messageA...) | ||
} | ||
|
||
func (cs *ChannelStatus) MarkBackingChannelUnknown(reason, messageFormat string, messageA ...interface{}) { | ||
chCondSet.Manage(cs).MarkUnknown(ChannelConditionBackingChannelReady, reason, messageFormat, messageA...) | ||
} | ||
|
||
func (cs *ChannelStatus) MarkBackingChannelNotConfigured() { | ||
chCondSet.Manage(cs).MarkUnknown(ChannelConditionBackingChannelReady, | ||
"BackingChannelNotConfigured", "BackingChannel has not yet been reconciled.") | ||
} | ||
|
||
func (cs *ChannelStatus) MarkBackingChannelReady() { | ||
chCondSet.Manage(cs).MarkTrue(ChannelConditionBackingChannelReady) | ||
} | ||
|
||
func (cs *ChannelStatus) PropagateStatuses(chs *eventingduck.ChannelableStatus) { | ||
// TODO: Once you can get a Ready status from Channelable in a generic way, use it here. | ||
readyCondition := chs.Status.GetCondition(apis.ConditionReady) | ||
if readyCondition == nil { | ||
cs.MarkBackingChannelNotConfigured() | ||
} else { | ||
switch { | ||
case readyCondition.Status == corev1.ConditionUnknown: | ||
cs.MarkBackingChannelUnknown(readyCondition.Reason, readyCondition.Message) | ||
case readyCondition.Status == corev1.ConditionTrue: | ||
cs.MarkBackingChannelReady() | ||
case readyCondition.Status == corev1.ConditionFalse: | ||
cs.MarkBackingChannelFailed(readyCondition.Reason, readyCondition.Message) | ||
default: | ||
cs.MarkBackingChannelUnknown("BackingChannelUnknown", "The status of BackingChannel is invalid: %v", readyCondition.Status) | ||
} | ||
} | ||
// Set the address and update the Addressable conditions. | ||
cs.SetAddress(chs.AddressStatus.Address) | ||
// Set the subscribable status. | ||
cs.SubscribableStatus = chs.SubscribableStatus | ||
} |
Oops, something went wrong.