Skip to content

Commit

Permalink
moved sinkbiding to v1, including types, default, conversion and webh… (
Browse files Browse the repository at this point in the history
#4235)

* moved sinkbiding to v1, including types, default, conversion and webhook:

* fixed boilerplate

* fixed nits

* fixed nits
  • Loading branch information
capri-xiyue authored Oct 8, 2020
1 parent 42f7a15 commit b2387db
Show file tree
Hide file tree
Showing 37 changed files with 2,617 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cmd/webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ var ourTypes = map[schema.GroupVersionKind]resourcesemantics.GenericCRD{
sourcesv1beta1.SchemeGroupVersion.WithKind("ContainerSource"): &sourcesv1beta1.ContainerSource{},
// v1
sourcesv1.SchemeGroupVersion.WithKind("ApiServerSource"): &sourcesv1.ApiServerSource{},
sourcesv1.SchemeGroupVersion.WithKind("SinkBinding"): &sourcesv1.SinkBinding{},

// For group flows.knative.dev
// v1beta1
Expand Down Expand Up @@ -333,13 +334,14 @@ func NewConversionController(ctx context.Context, cmw configmap.Watcher) *contro
sourcesv1beta1_: &sourcesv1beta1.PingSource{},
},
},
sourcesv1beta1.Kind("SinkBinding"): {
sourcesv1.Kind("SinkBinding"): {
DefinitionName: sources.SinkBindingResource.String(),
HubVersion: sourcesv1alpha1_,
Zygotes: map[string]conversion.ConvertibleObject{
sourcesv1alpha1_: &sourcesv1alpha1.SinkBinding{},
sourcesv1alpha2_: &sourcesv1alpha2.SinkBinding{},
sourcesv1beta1_: &sourcesv1beta1.SinkBinding{},
sourcesv1_: &sourcesv1.SinkBinding{},
},
},
sourcesv1beta1.Kind("ContainerSource"): {
Expand Down
4 changes: 4 additions & 0 deletions config/core/resources/sinkbindings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ spec:
name: v1beta1
served: true
storage: true
- <<: *version
name: v1
served: true
storage: false
schema:
openAPIV3Schema:
type: object
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/sources/v1/implements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func TestTypesImplements(t *testing.T) {
// ApiServerSource
{instance: &ApiServerSource{}, iface: &duckv1.Conditions{}},
{instance: &ApiServerSource{}, iface: &duckv1.Source{}},
// SinkBinding
{instance: &SinkBinding{}, iface: &duckv1.Conditions{}},
{instance: &SinkBinding{}, iface: &duckv1.Source{}},
{instance: &SinkBinding{}, iface: &duckv1.Binding{}},
}
for _, tc := range testCases {
if err := duck.VerifyType(tc.instance, tc.iface); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/sources/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&ApiServerSource{},
&ApiServerSourceList{},
&SinkBinding{},
&SinkBindingList{},
)
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/sources/v1/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func TestKnownTypes(t *testing.T) {
for _, name := range []string{
"ApiServerSource",
"ApiServerSourceList",
"SinkBinding",
"SinkBindingList",
} {
if _, ok := types[name]; !ok {
t.Errorf("Did not find %q as registered type", name)
Expand Down
43 changes: 43 additions & 0 deletions pkg/apis/sources/v1/sinkbinding_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
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/pkg/apis"
)

// sinkURIKey is used as the key for associating information
// with a context.Context.
type sinkURIKey struct{}

// WithSinkURI notes on the context for binding that the resolved SinkURI
// is the provided apis.URL.
func WithSinkURI(ctx context.Context, uri *apis.URL) context.Context {
return context.WithValue(ctx, sinkURIKey{}, uri)
}

// GetSinkURI accesses the apis.URL for the Sink URI that has been associated
// with this context.
func GetSinkURI(ctx context.Context) *apis.URL {
value := ctx.Value(sinkURIKey{})
if value == nil {
return nil
}
return value.(*apis.URL)
}
43 changes: 43 additions & 0 deletions pkg/apis/sources/v1/sinkbinding_context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
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"

"knative.dev/pkg/apis"
)

func TestGetSinkURI(t *testing.T) {
ctx := context.Background()

if uri := GetSinkURI(ctx); uri != nil {
t.Errorf("GetSinkURI() = %v, wanted nil", uri)
}

want := &apis.URL{
Scheme: "https",
Host: "knobots.io",
Path: "/omg/a/path",
}
ctx = WithSinkURI(ctx, want)

if got := GetSinkURI(ctx); got != want {
t.Errorf("GetSinkURI() = %v, wanted %v", got, want)
}
}
34 changes: 34 additions & 0 deletions pkg/apis/sources/v1/sinkbinding_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 *SinkBinding) 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 *SinkBinding) 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/sources/v1/sinkbinding_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 TestSinkBindingConversionBadType(t *testing.T) {
good, bad := &SinkBinding{}, &dummyObject{}

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)
}
}
34 changes: 34 additions & 0 deletions pkg/apis/sources/v1/sinkbinding_defaults.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"

"knative.dev/pkg/apis"
)

// SetDefaults implements apis.Defaultable
func (fb *SinkBinding) SetDefaults(ctx context.Context) {
if fb.Spec.Subject.Namespace == "" {
// Default the subject's namespace to our namespace.
fb.Spec.Subject.Namespace = fb.Namespace
}

withNS := apis.WithinParent(ctx, fb.ObjectMeta)
fb.Spec.Sink.SetDefaults(withNS)
}
Loading

0 comments on commit b2387db

Please sign in to comment.