Skip to content

Commit

Permalink
Types for v1 messaging (#3403)
Browse files Browse the repository at this point in the history
* 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
Nicolas Lopez authored Jun 25, 2020
1 parent 9537c9c commit aafe4de
Show file tree
Hide file tree
Showing 83 changed files with 8,606 additions and 8 deletions.
4 changes: 2 additions & 2 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ chmod +x ${CODEGEN_PKG}/generate-groups.sh
# instead of the $GOPATH directly. For normal projects this can be dropped.
${CODEGEN_PKG}/generate-groups.sh "deepcopy,client,informer,lister" \
knative.dev/eventing/pkg/client knative.dev/eventing/pkg/apis \
"eventing:v1beta1 eventing:v1 messaging:v1beta1 flows:v1beta1 sources:v1alpha1 sources:v1alpha2 configs:v1alpha1" \
"eventing:v1beta1 eventing:v1 messaging:v1beta1 messaging:v1 flows:v1beta1 sources:v1alpha1 sources:v1alpha2 configs:v1alpha1" \
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt

# Deep copy config
Expand All @@ -65,7 +65,7 @@ ${CODEGEN_PKG}/generate-groups.sh "deepcopy" \
chmod +x ${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh
${KNATIVE_CODEGEN_PKG}/hack/generate-knative.sh "injection" \
knative.dev/eventing/pkg/client knative.dev/eventing/pkg/apis \
"eventing:v1beta1 eventing:v1 messaging:v1beta1 flows:v1beta1 sources:v1alpha1 sources:v1alpha2 duck:v1alpha1 duck:v1beta1 duck:v1 configs:v1alpha1" \
"eventing:v1beta1 eventing:v1 messaging:v1beta1 messaging:v1 flows:v1beta1 sources:v1alpha1 sources:v1alpha2 duck:v1alpha1 duck:v1beta1 duck:v1 configs:v1alpha1" \
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt

# Make sure our dependencies are up-to-date
Expand Down
34 changes: 34 additions & 0 deletions pkg/apis/messaging/v1/channel_conversion.go
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)
}
34 changes: 34 additions & 0 deletions pkg/apis/messaging/v1/channel_conversion_test.go
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)
}
}
58 changes: 58 additions & 0 deletions pkg/apis/messaging/v1/channel_defaults.go
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
}
109 changes: 109 additions & 0 deletions pkg/apis/messaging/v1/channel_defaults_test.go
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)
}
})
}
}
124 changes: 124 additions & 0 deletions pkg/apis/messaging/v1/channel_lifecycle.go
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
}
Loading

0 comments on commit aafe4de

Please sign in to comment.