Skip to content

Commit

Permalink
Avoid importing apis/duck from apis/duck/<<version>>
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaroaleman committed Jun 25, 2020
1 parent eb05e8d commit c30d649
Show file tree
Hide file tree
Showing 16 changed files with 211 additions and 172 deletions.
43 changes: 43 additions & 0 deletions apis/duck/ducktypes/ducktypes.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 ducktypes

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

// Implementable is implemented by the Fooable duck type that consumers
// are expected to embed as a `.status.fooable` field.
type Implementable interface {
// GetFullType returns an instance of a full resource wrapping
// an instance of this Implementable that can populate its fields
// to verify json roundtripping.
GetFullType() Populatable
}

// Populatable is implemented by a skeleton resource wrapping an Implementable
// duck type. It will generally have TypeMeta, ObjectMeta, and a Status field
// wrapping a Fooable field.
type Populatable interface {
apis.Listable

// Populate fills in all possible fields, so that we can verify that
// they roundtrip properly through JSON.
Populate()
}

const GroupName = "duck.knative.dev"
6 changes: 5 additions & 1 deletion apis/duck/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ limitations under the License.

package duck

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

const (
GroupName = "duck.knative.dev"
GroupName = ducktypes.GroupName

// AddressableDuckVersionLabel is the label we use to declare
// that a type conforms to the Addressable duck type.
Expand Down
10 changes: 3 additions & 7 deletions apis/duck/v1/addressable_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"

"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck"
"knative.dev/pkg/apis/duck/ducktypes"
)

// +genduck
Expand All @@ -40,8 +40,6 @@ type Addressable struct {
}

var (
// Addressable is an Implementable "duck type".
_ duck.Implementable = (*Addressable)(nil)
// Addressable is a Convertible type.
_ apis.Convertible = (*Addressable)(nil)
)
Expand All @@ -66,13 +64,11 @@ type AddressStatus struct {
}

var (
// Verify AddressableType resources meet duck contracts.
_ duck.Populatable = (*AddressableType)(nil)
_ apis.Listable = (*AddressableType)(nil)
_ apis.Listable = (*AddressableType)(nil)
)

// GetFullType implements duck.Implementable
func (*Addressable) GetFullType() duck.Populatable {
func (*Addressable) GetFullType() ducktypes.Populatable {
return &AddressableType{}
}

Expand Down
57 changes: 0 additions & 57 deletions apis/duck/v1/implements_test.go

This file was deleted.

8 changes: 3 additions & 5 deletions apis/duck/v1/podspec_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"

"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck"
"knative.dev/pkg/apis/duck/ducktypes"
)

// +genduck
Expand Down Expand Up @@ -50,13 +50,11 @@ type WithPodSpec struct {
// Assert that we implement the interfaces necessary to
// use duck.VerifyType.
var (
_ duck.Populatable = (*WithPod)(nil)
_ duck.Implementable = (*PodSpecable)(nil)
_ apis.Listable = (*WithPod)(nil)
_ apis.Listable = (*WithPod)(nil)
)

// GetFullType implements duck.Implementable
func (*PodSpecable) GetFullType() duck.Populatable {
func (*PodSpecable) GetFullType() ducktypes.Populatable {
return &WithPod{}
}

Expand Down
5 changes: 3 additions & 2 deletions apis/duck/v1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"knative.dev/pkg/apis/duck"

"knative.dev/pkg/apis/duck/ducktypes"
)

// SchemeGroupVersion is group version used to register these objects
var SchemeGroupVersion = schema.GroupVersion{Group: duck.GroupName, Version: "v1"}
var SchemeGroupVersion = schema.GroupVersion{Group: ducktypes.GroupName, Version: "v1"}

// Kind takes an unqualified kind and returns back a Group qualified GroupKind
func Kind(kind string) schema.GroupKind {
Expand Down
11 changes: 3 additions & 8 deletions apis/duck/v1/source_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ import (
"k8s.io/apimachinery/pkg/runtime"

"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck"
"knative.dev/pkg/apis/duck/ducktypes"
)

// Source is an Implementable "duck type".
var _ duck.Implementable = (*Source)(nil)

// +genduck
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

Expand Down Expand Up @@ -113,9 +110,7 @@ func (ss *SourceStatus) IsReady() bool {
}

var (
// Verify Source resources meet duck contracts.
_ duck.Populatable = (*Source)(nil)
_ apis.Listable = (*Source)(nil)
_ apis.Listable = (*Source)(nil)
)

const (
Expand All @@ -125,7 +120,7 @@ const (
)

// GetFullType implements duck.Implementable
func (*Source) GetFullType() duck.Populatable {
func (*Source) GetFullType() ducktypes.Populatable {
return &Source{}
}

Expand Down
10 changes: 2 additions & 8 deletions apis/duck/v1/status_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"context"

"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck"
"knative.dev/pkg/apis/duck/ducktypes"
"knative.dev/pkg/kmeta"
)

Expand All @@ -29,9 +29,6 @@ import (
// Conditions is a simple wrapper around apis.Conditions to implement duck.Implementable.
type Conditions apis.Conditions

// Conditions is an Implementable "duck type".
var _ duck.Implementable = (*Conditions)(nil)

// Status shows how we expect folks to embed Conditions in
// their Status field.
// WARNING: Adding fields to this struct will add them to all Knative resources.
Expand Down Expand Up @@ -66,14 +63,11 @@ func (s *Status) SetConditions(c apis.Conditions) {
s.Conditions = Conditions(c)
}

// In order for Conditions to be Implementable, KResource must be Populatable.
var _ duck.Populatable = (*KResource)(nil)

// Ensure KResource satisfies apis.Listable
var _ apis.Listable = (*KResource)(nil)

// GetFullType implements duck.Implementable
func (*Conditions) GetFullType() duck.Populatable {
func (*Conditions) GetFullType() ducktypes.Populatable {
return &KResource{}
}

Expand Down
78 changes: 78 additions & 0 deletions apis/duck/v1_tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
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 duck

import (
"testing"

appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"

"knative.dev/pkg/apis/duck/v1"
)

// Conditions is an Implementable "duck type".
var _ Implementable = (*v1.Conditions)(nil)

// In order for Conditions to be Implementable, KResource must be Populatable.
var _ Populatable = (*v1.KResource)(nil)

// Source is an Implementable "duck type".
var _ Implementable = (*v1.Source)(nil)

// Verify Source resources meet duck contracts.
var _ Populatable = (*v1.Source)(nil)

var _ Populatable = (*v1.WithPod)(nil)
var _ Implementable = (*v1.PodSpecable)(nil)

func TestV1TypesImplements(t *testing.T) {
testCases := []struct {
instance interface{}
iface Implementable
}{
{instance: &v1.AddressableType{}, iface: &v1.Addressable{}},
{instance: &v1.KResource{}, iface: &v1.Conditions{}},
}
for _, tc := range testCases {
if err := VerifyType(tc.instance, tc.iface); err != nil {
t.Error(err)
}
}
}

func TestV1ImplementsPodSpecable(t *testing.T) {
instances := []interface{}{
&v1.WithPod{},
&appsv1.ReplicaSet{},
&appsv1.Deployment{},
&appsv1.StatefulSet{},
&appsv1.DaemonSet{},
&batchv1.Job{},
}
for _, instance := range instances {
if err := VerifyType(instance, &v1.PodSpecable{}); err != nil {
t.Error(err)
}
}
}

// Addressable is an Implementable "duck type".
var _ Implementable = (*v1.Addressable)(nil)

// Verify AddressableType resources meet duck contracts.
var _ Populatable = (*v1.AddressableType)(nil)
12 changes: 4 additions & 8 deletions apis/duck/v1beta1/addressable_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"k8s.io/apimachinery/pkg/runtime"

"knative.dev/pkg/apis"
"knative.dev/pkg/apis/duck"
v1 "knative.dev/pkg/apis/duck/v1"
"knative.dev/pkg/apis/duck/ducktypes"
"knative.dev/pkg/apis/duck/v1"
)

// +genduck
Expand All @@ -41,8 +41,6 @@ type Addressable struct {
}

var (
// Addressable is an Implementable "duck type".
_ duck.Implementable = (*Addressable)(nil)
// Addressable is a Convertible type.
_ apis.Convertible = (*Addressable)(nil)
)
Expand All @@ -67,13 +65,11 @@ type AddressStatus struct {
}

var (
// Verify AddressableType resources meet duck contracts.
_ duck.Populatable = (*AddressableType)(nil)
_ apis.Listable = (*AddressableType)(nil)
_ apis.Listable = (*AddressableType)(nil)
)

// GetFullType implements duck.Implementable
func (*Addressable) GetFullType() duck.Populatable {
func (*Addressable) GetFullType() ducktypes.Populatable {
return &AddressableType{}
}

Expand Down
Loading

0 comments on commit c30d649

Please sign in to comment.