From 5aa9856655a9bcb2073f386b5e98f36011c9b084 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Fri, 1 May 2020 15:21:56 -0700 Subject: [PATCH 1/4] Split KResource into a file. Include an interface. --- apis/duck/v1/kresource_type.go | 79 ++++++++++++++++++++++++++++++++++ apis/duck/v1/status_types.go | 46 -------------------- 2 files changed, 79 insertions(+), 46 deletions(-) create mode 100644 apis/duck/v1/kresource_type.go diff --git a/apis/duck/v1/kresource_type.go b/apis/duck/v1/kresource_type.go new file mode 100644 index 0000000000..9dfdb60502 --- /dev/null +++ b/apis/duck/v1/kresource_type.go @@ -0,0 +1,79 @@ +package v1 + +import ( + "time" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + + "knative.dev/pkg/apis" +) + +// DuckShaped is an interface for retrieving the duck elements of an arbitraty resource. +type DuckShaped interface { + GetTypeMeta() *metav1.TypeMeta + + GetObjectMeta() *metav1.ObjectMeta + + GetStatus() *Status +} + +// Asserts KResource conformance with the shape of duck resource. +var _ DuckShaped = (*KResource)(nil) + +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// KResource is a skeleton type wrapping Conditions in the manner we expect +// resource writers defining compatible resources to embed it. We will +// typically use this type to deserialize Conditions ObjectReferences and +// access the Conditions data. This is not a real resource. +type KResource struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Status Status `json:"status"` +} + +// Populate implements duck.Populatable +func (t *KResource) Populate() { + t.Status.ObservedGeneration = 42 + t.Status.Conditions = Conditions{{ + // Populate ALL fields + Type: "Birthday", + Status: corev1.ConditionTrue, + LastTransitionTime: apis.VolatileTime{Inner: metav1.NewTime(time.Date(1984, 02, 28, 18, 52, 00, 00, time.UTC))}, + Reason: "Celebrate", + Message: "n3wScott, find your party hat :tada:", + }} +} + +// GetListType implements apis.Listable +func (*KResource) GetListType() runtime.Object { + return &KResourceList{} +} + +// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object + +// KResourceList is a list of KResource resources +type KResourceList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata"` + + Items []KResource `json:"items"` +} + +// GetTypeMeta retrieves the ObjectMeta of the KResource. Implements the Shaped interface. +func (t *KResource) GetTypeMeta() *metav1.TypeMeta { + return &t.TypeMeta +} + +// GetObjectMeta retrieves the ObjectMeta of the KResource. Implements the Shaped interface. +func (t *KResource) GetObjectMeta() *metav1.ObjectMeta { + return &t.ObjectMeta +} + +// GetStatus retrieves the status of the KResource. Implements the Shaped interface. +func (t *KResource) GetStatus() *Status { + return &k.Status +} diff --git a/apis/duck/v1/status_types.go b/apis/duck/v1/status_types.go index 2165e78385..9186e961f4 100644 --- a/apis/duck/v1/status_types.go +++ b/apis/duck/v1/status_types.go @@ -18,11 +18,6 @@ package v1 import ( "context" - "time" - - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" "knative.dev/pkg/apis" "knative.dev/pkg/apis/duck" @@ -36,19 +31,6 @@ type Conditions apis.Conditions // Conditions is an Implementable "duck type". var _ duck.Implementable = (*Conditions)(nil) -// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object - -// KResource is a skeleton type wrapping Conditions in the manner we expect -// resource writers defining compatible resources to embed it. We will -// typically use this type to deserialize Conditions ObjectReferences and -// access the Conditions data. This is not a real resource. -type KResource struct { - metav1.TypeMeta `json:",inline"` - metav1.ObjectMeta `json:"metadata,omitempty"` - - Status Status `json:"status"` -} - // 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. @@ -126,31 +108,3 @@ func (source *Status) ConvertTo(ctx context.Context, sink *Status, predicates .. sink.SetConditions(conditions) } - -// Populate implements duck.Populatable -func (t *KResource) Populate() { - t.Status.ObservedGeneration = 42 - t.Status.Conditions = Conditions{{ - // Populate ALL fields - Type: "Birthday", - Status: corev1.ConditionTrue, - LastTransitionTime: apis.VolatileTime{Inner: metav1.NewTime(time.Date(1984, 02, 28, 18, 52, 00, 00, time.UTC))}, - Reason: "Celebrate", - Message: "n3wScott, find your party hat :tada:", - }} -} - -// GetListType implements apis.Listable -func (*KResource) GetListType() runtime.Object { - return &KResourceList{} -} - -// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object - -// KResourceList is a list of KResource resources -type KResourceList struct { - metav1.TypeMeta `json:",inline"` - metav1.ListMeta `json:"metadata"` - - Items []KResource `json:"items"` -} From 894810650c6f2063fac0588b6383fb0d6fb4070c Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Fri, 1 May 2020 15:26:10 -0700 Subject: [PATCH 2/4] Update apis/duck/v1/kresource_type.go Co-authored-by: Matt Moore --- apis/duck/v1/kresource_type.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/apis/duck/v1/kresource_type.go b/apis/duck/v1/kresource_type.go index 9dfdb60502..0171cbab49 100644 --- a/apis/duck/v1/kresource_type.go +++ b/apis/duck/v1/kresource_type.go @@ -1,3 +1,19 @@ +/* +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 ( From 484ba2f8578f1c30b37a236ae696e59c0e30dd43 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Fri, 1 May 2020 15:32:26 -0700 Subject: [PATCH 3/4] type --- apis/duck/v1/kresource_type.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apis/duck/v1/kresource_type.go b/apis/duck/v1/kresource_type.go index 9dfdb60502..f5624e07a1 100644 --- a/apis/duck/v1/kresource_type.go +++ b/apis/duck/v1/kresource_type.go @@ -75,5 +75,5 @@ func (t *KResource) GetObjectMeta() *metav1.ObjectMeta { // GetStatus retrieves the status of the KResource. Implements the Shaped interface. func (t *KResource) GetStatus() *Status { - return &k.Status + return &t.Status } From af872c15f69416581ff1f095e8b4ea2d36e84f89 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Fri, 1 May 2020 15:48:37 -0700 Subject: [PATCH 4/4] rename to KRShaped --- apis/duck/v1/kresource_type.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/apis/duck/v1/kresource_type.go b/apis/duck/v1/kresource_type.go index 1d1c390e07..22a3b33ecd 100644 --- a/apis/duck/v1/kresource_type.go +++ b/apis/duck/v1/kresource_type.go @@ -26,8 +26,8 @@ import ( "knative.dev/pkg/apis" ) -// DuckShaped is an interface for retrieving the duck elements of an arbitraty resource. -type DuckShaped interface { +// KRShaped is an interface for retrieving the duck elements of an arbitraty resource. +type KRShaped interface { GetTypeMeta() *metav1.TypeMeta GetObjectMeta() *metav1.ObjectMeta @@ -35,8 +35,8 @@ type DuckShaped interface { GetStatus() *Status } -// Asserts KResource conformance with the shape of duck resource. -var _ DuckShaped = (*KResource)(nil) +// Asserts KResource conformance with KRShaped +var _ KRShaped = (*KResource)(nil) // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object @@ -79,17 +79,17 @@ type KResourceList struct { Items []KResource `json:"items"` } -// GetTypeMeta retrieves the ObjectMeta of the KResource. Implements the Shaped interface. +// GetTypeMeta retrieves the ObjectMeta of the KResource. Implements the KRShaped interface. func (t *KResource) GetTypeMeta() *metav1.TypeMeta { return &t.TypeMeta } -// GetObjectMeta retrieves the ObjectMeta of the KResource. Implements the Shaped interface. +// GetObjectMeta retrieves the ObjectMeta of the KResource. Implements the KRShaped interface. func (t *KResource) GetObjectMeta() *metav1.ObjectMeta { return &t.ObjectMeta } -// GetStatus retrieves the status of the KResource. Implements the Shaped interface. +// GetStatus retrieves the status of the KResource. Implements the KRShaped interface. func (t *KResource) GetStatus() *Status { return &t.Status }