From 665012885601188d00e54f4da8fe539cd28765be Mon Sep 17 00:00:00 2001 From: Matt Moore Date: Fri, 21 Sep 2018 15:51:19 -0700 Subject: [PATCH] Pick up the KindToResource helper. (#2075) --- Gopkg.lock | 4 +- Gopkg.toml | 6 +-- .../v1alpha1/autoscaling/kpa_scaler.go | 8 +--- .../apis/duck/v1alpha1/channelable_types.go | 10 ++++ .../apis/duck/v1alpha1/conditions_types.go | 9 ++++ .../apis/duck/v1alpha1/generational_types.go | 10 ++++ .../duck/v1alpha1/legacy_targetable_types.go | 10 ++++ .../pkg/apis/duck/v1alpha1/register.go | 16 ++++++- .../pkg/apis/duck/v1alpha1/sinkable_types.go | 10 ++++ .../apis/duck/v1alpha1/subscribable_types.go | 10 ++++ .../apis/duck/v1alpha1/targetable_types.go | 10 ++++ .../github.com/knative/pkg/apis/interfaces.go | 12 +++++ .../knative/pkg/apis/kind2resource.go | 47 +++++++++++++++++++ 13 files changed, 149 insertions(+), 13 deletions(-) create mode 100644 vendor/github.com/knative/pkg/apis/kind2resource.go diff --git a/Gopkg.lock b/Gopkg.lock index 404e0ba9e62f..71bb2ad0414b 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -338,7 +338,7 @@ revision = "f0d7bb60956f88d4743f5fea66b5607b96d262c9" [[projects]] - digest = "1:a327f01fbc97243a15bbf228f37be9e14527aec5b90010be9d3b75cfda098664" + digest = "1:95c2ff130db0410dc9735905cecff862352a3981c6ed048d83fd959ed19fc16a" name = "github.com/knative/pkg" packages = [ "apis", @@ -377,7 +377,7 @@ "webhook", ] pruneopts = "NUT" - revision = "51f6214feeea1485dc8691a661d953fea1409402" + revision = "f81d0f8f0fac9943f3892c5e86007dc00382a909" [[projects]] branch = "master" diff --git a/Gopkg.toml b/Gopkg.toml index 184136fd3deb..2ea8a64d2475 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -31,10 +31,10 @@ required = [ # HEAD as of 2018-09-20 revision = "2891231d4e47f80c9b72637da071468d7072cb62" -[[constraint]] +[[override]] name = "github.com/knative/pkg" - # HEAD as of 2018-09-20 - revision = "51f6214feeea1485dc8691a661d953fea1409402" + # HEAD as of 2018-09-21 + revision = "f81d0f8f0fac9943f3892c5e86007dc00382a909" [[constraint]] name = "github.com/knative/caching" diff --git a/pkg/reconciler/v1alpha1/autoscaling/kpa_scaler.go b/pkg/reconciler/v1alpha1/autoscaling/kpa_scaler.go index 40e9b9f450a0..751e30a67efc 100644 --- a/pkg/reconciler/v1alpha1/autoscaling/kpa_scaler.go +++ b/pkg/reconciler/v1alpha1/autoscaling/kpa_scaler.go @@ -18,7 +18,6 @@ package autoscaling import ( "context" - "strings" "sync" "go.uber.org/zap" @@ -27,6 +26,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/client-go/scale" + "github.com/knative/pkg/apis" "github.com/knative/pkg/configmap" "github.com/knative/pkg/logging" kpa "github.com/knative/serving/pkg/apis/autoscaling/v1alpha1" @@ -116,11 +116,7 @@ func (rs *kpaScaler) Scale(ctx context.Context, kpa *kpa.PodAutoscaler, desiredS logger.Error("Unable to parse APIVersion.", zap.Error(err)) return err } - resource := schema.GroupResource{ - Group: gv.Group, - // TODO(mattmoor): Do something better than this. - Resource: strings.ToLower(kpa.Spec.ScaleTargetRef.Kind) + "s", - } + resource := apis.KindToResource(gv.WithKind(kpa.Spec.ScaleTargetRef.Kind)).GroupResource() resourceName := kpa.Spec.ScaleTargetRef.Name // Identify the current scale. diff --git a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/channelable_types.go b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/channelable_types.go index 3e450b316041..2fcb12295369 100644 --- a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/channelable_types.go +++ b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/channelable_types.go @@ -18,7 +18,9 @@ package v1alpha1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "github.com/knative/pkg/apis" "github.com/knative/pkg/apis/duck" ) @@ -72,6 +74,9 @@ type ChannelSpec struct { // In order for Channelable to be Implementable, Channel must be Populatable. var _ duck.Populatable = (*Channel)(nil) +// Ensure Channel satisfies apis.Listable +var _ apis.Listable = (*Channel)(nil) + // GetFullType implements duck.Implementable func (_ *Channelable) GetFullType() duck.Populatable { return &Channel{} @@ -85,6 +90,11 @@ func (t *Channel) Populate() { } } +// GetListType implements apis.Listable +func (r *Channel) GetListType() runtime.Object { + return &ChannelList{} +} + // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // ChannelList is a list of Channel resources diff --git a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/conditions_types.go b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/conditions_types.go index bde5adf71729..7c36ffc82bb7 100644 --- a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/conditions_types.go +++ b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/conditions_types.go @@ -21,6 +21,7 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" "github.com/knative/pkg/apis" "github.com/knative/pkg/apis/duck" @@ -121,6 +122,9 @@ type KResourceStatus struct { // 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 { return &KResource{} @@ -138,6 +142,11 @@ func (t *KResource) Populate() { }} } +// GetListType implements apis.Listable +func (r *KResource) GetListType() runtime.Object { + return &KResourceList{} +} + // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // KResourceList is a list of KResource resources diff --git a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/generational_types.go b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/generational_types.go index f091de47a2ab..a3198bdde4af 100644 --- a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/generational_types.go +++ b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/generational_types.go @@ -18,7 +18,9 @@ package v1alpha1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "github.com/knative/pkg/apis" "github.com/knative/pkg/apis/duck" ) @@ -55,6 +57,9 @@ type GenerationalSpec struct { // In order for Generation to be Implementable, Generational must be Populatable. var _ duck.Populatable = (*Generational)(nil) +// Ensure Generational satisfies apis.Listable +var _ apis.Listable = (*Generational)(nil) + // GetFullType implements duck.Implementable func (_ *Generation) GetFullType() duck.Populatable { return &Generational{} @@ -65,6 +70,11 @@ func (t *Generational) Populate() { t.Spec.Generation = 1234 } +// GetListType implements apis.Listable +func (r *Generational) GetListType() runtime.Object { + return &GenerationalList{} +} + // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // GenerationalList is a list of Generational resources diff --git a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/legacy_targetable_types.go b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/legacy_targetable_types.go index f36b74e997be..d7c391f2a1df 100644 --- a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/legacy_targetable_types.go +++ b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/legacy_targetable_types.go @@ -18,7 +18,9 @@ package v1alpha1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "github.com/knative/pkg/apis" "github.com/knative/pkg/apis/duck" ) @@ -64,6 +66,9 @@ type LegacyTarget struct { // In order for LegacyTargetable to be Implementable, LegacyTarget must be Populatable. var _ duck.Populatable = (*LegacyTarget)(nil) +// Ensure LegacyTarget satisfies apis.Listable +var _ apis.Listable = (*LegacyTarget)(nil) + // GetFullType implements duck.Implementable func (_ *LegacyTargetable) GetFullType() duck.Populatable { return &LegacyTarget{} @@ -77,6 +82,11 @@ func (t *LegacyTarget) Populate() { } } +// GetListType implements apis.Listable +func (r *LegacyTarget) GetListType() runtime.Object { + return &LegacyTargetList{} +} + // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // LegacyTargetList is a list of LegacyTarget resources diff --git a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/register.go b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/register.go index f197cb560e45..f3bfc5ae1d1e 100644 --- a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/register.go +++ b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/register.go @@ -45,8 +45,20 @@ var ( func addKnownTypes(scheme *runtime.Scheme) error { scheme.AddKnownTypes( SchemeGroupVersion, - // &VirtualService{}, - // &VirtualServiceList{}, + &Channel{}, + (&Channel{}).GetListType(), + &KResource{}, + (&KResource{}).GetListType(), + &Generational{}, + (&Generational{}).GetListType(), + &Target{}, + (&Target{}).GetListType(), + &LegacyTarget{}, + (&LegacyTarget{}).GetListType(), + &Sink{}, + (&Sink{}).GetListType(), + &Subscription{}, + (&Subscription{}).GetListType(), ) metav1.AddToGroupVersion(scheme, SchemeGroupVersion) return nil diff --git a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/sinkable_types.go b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/sinkable_types.go index d7027d8bcf8e..62434b2de751 100644 --- a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/sinkable_types.go +++ b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/sinkable_types.go @@ -18,7 +18,9 @@ package v1alpha1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "github.com/knative/pkg/apis" "github.com/knative/pkg/apis/duck" ) @@ -60,6 +62,9 @@ type SinkStatus struct { // In order for Sinkable to be Implementable, Sink must be Populatable. var _ duck.Populatable = (*Sink)(nil) +// Ensure Sink satisfies apis.Listable +var _ apis.Listable = (*Sink)(nil) + // GetFullType implements duck.Implementable func (_ *Sinkable) GetFullType() duck.Populatable { return &Sink{} @@ -75,6 +80,11 @@ func (t *Sink) Populate() { } } +// GetListType implements apis.Listable +func (r *Sink) GetListType() runtime.Object { + return &SinkList{} +} + // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // SinkList is a list of Sink resources diff --git a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/subscribable_types.go b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/subscribable_types.go index f037780f27bc..d73a75dc1239 100644 --- a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/subscribable_types.go +++ b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/subscribable_types.go @@ -19,7 +19,9 @@ package v1alpha1 import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "github.com/knative/pkg/apis" "github.com/knative/pkg/apis/duck" ) @@ -69,6 +71,9 @@ type SubscriptionStatus struct { // In order for Subscribable to be Implementable, Subscribable must be Populatable. var _ duck.Populatable = (*Subscription)(nil) +// Ensure Subscription satisfies apis.Listable +var _ apis.Listable = (*Subscription)(nil) + // GetFullType implements duck.Implementable func (_ *Subscribable) GetFullType() duck.Populatable { return &Subscription{} @@ -86,6 +91,11 @@ func (t *Subscription) Populate() { } } +// GetListType implements apis.Listable +func (r *Subscription) GetListType() runtime.Object { + return &SubscriptionList{} +} + // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // SubscribableList is a list of Subscribable resources diff --git a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/targetable_types.go b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/targetable_types.go index 717107ceef54..863e39f7dd93 100644 --- a/vendor/github.com/knative/pkg/apis/duck/v1alpha1/targetable_types.go +++ b/vendor/github.com/knative/pkg/apis/duck/v1alpha1/targetable_types.go @@ -18,7 +18,9 @@ package v1alpha1 import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "github.com/knative/pkg/apis" "github.com/knative/pkg/apis/duck" ) @@ -60,6 +62,9 @@ type TargetStatus struct { // In order for Targetable to be Implementable, Target must be Populatable. var _ duck.Populatable = (*Target)(nil) +// Ensure Target satisfies apis.Listable +var _ apis.Listable = (*Target)(nil) + // GetFullType implements duck.Implementable func (_ *Targetable) GetFullType() duck.Populatable { return &Target{} @@ -75,6 +80,11 @@ func (t *Target) Populate() { } } +// GetListType implements apis.Listable +func (r *Target) GetListType() runtime.Object { + return &TargetList{} +} + // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object // TargetList is a list of Target resources diff --git a/vendor/github.com/knative/pkg/apis/interfaces.go b/vendor/github.com/knative/pkg/apis/interfaces.go index f8c40f52beee..d21ab41b3157 100644 --- a/vendor/github.com/knative/pkg/apis/interfaces.go +++ b/vendor/github.com/knative/pkg/apis/interfaces.go @@ -16,6 +16,10 @@ limitations under the License. package apis +import ( + "k8s.io/apimachinery/pkg/runtime" +) + // Defaultable defines an interface for setting the defaults for the // uninitialized fields of this instance. type Defaultable interface { @@ -35,3 +39,11 @@ type Immutable interface { // fields haven't changed from the provided original. CheckImmutableFields(original Immutable) *FieldError } + +// Listable indicates that a particular type can be returned via the returned +// list type by the API server. +type Listable interface { + runtime.Object + + GetListType() runtime.Object +} diff --git a/vendor/github.com/knative/pkg/apis/kind2resource.go b/vendor/github.com/knative/pkg/apis/kind2resource.go new file mode 100644 index 000000000000..37ffe080345a --- /dev/null +++ b/vendor/github.com/knative/pkg/apis/kind2resource.go @@ -0,0 +1,47 @@ +/* +Copyright 2018 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 apis + +import ( + "fmt" + "strings" + + "k8s.io/apimachinery/pkg/runtime/schema" +) + +// KindToResource converts a GroupVersionKind to a GroupVersionResource +// through the world's simplest (worst) pluralizer. +func KindToResource(gvk schema.GroupVersionKind) schema.GroupVersionResource { + return schema.GroupVersionResource{ + Group: gvk.Group, + Version: gvk.Version, + Resource: pluralizeKind(gvk.Kind), + } +} + +// Takes a kind and pluralizes it. This is super terrible, but I am +// not aware of a generic way to do this. +// I am not alone in thinking this and I haven't found a better solution: +// This seems relevant: +// https://github.com/kubernetes/kubernetes/issues/18622 +func pluralizeKind(kind string) string { + ret := strings.ToLower(kind) + if strings.HasSuffix(ret, "s") { + return fmt.Sprintf("%ses", ret) + } + return fmt.Sprintf("%ss", ret) +}