From b0475bbee2e5312bcbb1919bc6f8e0ce813a383c Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Thu, 7 May 2020 09:10:06 -0700 Subject: [PATCH 1/5] implement krshaped for autoscaling --- .../autoscaling/v1alpha1/metric_lifecycle.go | 5 +++ .../v1alpha1/metric_lifecycle_test.go | 8 ++++ pkg/apis/autoscaling/v1alpha1/metric_types.go | 13 ++++++ .../autoscaling/v1alpha1/metric_types_test.go | 42 +++++++++++++++++++ .../autoscaling/v1alpha1/metric_validation.go | 4 +- pkg/apis/autoscaling/v1alpha1/pa_lifecycle.go | 5 +++ .../autoscaling/v1alpha1/pa_lifecycle_test.go | 8 ++++ pkg/apis/autoscaling/v1alpha1/pa_types.go | 15 ++++++- .../autoscaling/v1alpha1/pa_types_test.go | 42 +++++++++++++++++++ .../autoscaling/v1alpha1/pa_validation.go | 4 +- 10 files changed, 141 insertions(+), 5 deletions(-) create mode 100644 pkg/apis/autoscaling/v1alpha1/metric_types_test.go create mode 100644 pkg/apis/autoscaling/v1alpha1/pa_types_test.go diff --git a/pkg/apis/autoscaling/v1alpha1/metric_lifecycle.go b/pkg/apis/autoscaling/v1alpha1/metric_lifecycle.go index d7e9e8581f4f..f7fa065bbafc 100644 --- a/pkg/apis/autoscaling/v1alpha1/metric_lifecycle.go +++ b/pkg/apis/autoscaling/v1alpha1/metric_lifecycle.go @@ -31,6 +31,11 @@ var condSet = apis.NewLivingConditionSet( MetricConditionReady, ) +// GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface. +func (*Metric) GetConditionSet() apis.ConditionSet { + return condSet +} + // GetGroupVersionKind implements OwnerRefable. func (m *Metric) GetGroupVersionKind() schema.GroupVersionKind { return SchemeGroupVersion.WithKind("Metric") diff --git a/pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go b/pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go index 87d1b66493ce..25cdfe1b4566 100644 --- a/pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go +++ b/pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go @@ -48,6 +48,14 @@ func TestMetricDuckTypes(t *testing.T) { } } +func TestMetricGetConditionSet(t *testing.T) { + r := &Metric{} + want := apis.ConditionReady + if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + func TestMetricIsReady(t *testing.T) { cases := []struct { name string diff --git a/pkg/apis/autoscaling/v1alpha1/metric_types.go b/pkg/apis/autoscaling/v1alpha1/metric_types.go index 14edaeca74a4..8cb8a125a656 100644 --- a/pkg/apis/autoscaling/v1alpha1/metric_types.go +++ b/pkg/apis/autoscaling/v1alpha1/metric_types.go @@ -52,6 +52,9 @@ var ( // Check that we can create OwnerReferences to a Metric. _ kmeta.OwnerRefable = (*Metric)(nil) + + // Check that the type conforms to the duck Knative Resource shape. + _ duckv1.KRShaped = (*Metric)(nil) ) // MetricSpec contains all values a metric collector needs to operate. @@ -78,3 +81,13 @@ type MetricList struct { Items []Metric `json:"items"` } + +// GetTypeMeta retrieves the ObjectMeta of the Metric. Implements the KRShaped interface. +func (t *Metric) GetTypeMeta() *metav1.TypeMeta { + return &t.TypeMeta +} + +// GetStatus retrieves the status of the Metric. Implements the KRShaped interface. +func (t *Metric) GetStatus() *duckv1.Status { + return &t.Status.Status +} diff --git a/pkg/apis/autoscaling/v1alpha1/metric_types_test.go b/pkg/apis/autoscaling/v1alpha1/metric_types_test.go new file mode 100644 index 000000000000..99778584fd42 --- /dev/null +++ b/pkg/apis/autoscaling/v1alpha1/metric_types_test.go @@ -0,0 +1,42 @@ +/* +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 v1alpha1 + +import ( + "testing" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestMetricGetStatus(t *testing.T) { + r := &Metric{ + Status: MetricStatus{}, + } + want := &r.Status.Status + if got := r.GetStatus(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + +func TestMetricGetObjectMeta(t *testing.T) { + r := &Metric{ + TypeMeta: metav1.TypeMeta{}, + } + want := &r.TypeMeta + if got := r.GetTypeMeta(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} diff --git a/pkg/apis/autoscaling/v1alpha1/metric_validation.go b/pkg/apis/autoscaling/v1alpha1/metric_validation.go index fd1adb04911a..e0c8d182d958 100644 --- a/pkg/apis/autoscaling/v1alpha1/metric_validation.go +++ b/pkg/apis/autoscaling/v1alpha1/metric_validation.go @@ -26,8 +26,8 @@ import ( // Validate validates the entire Metric. func (m *Metric) Validate(ctx context.Context) *apis.FieldError { - return serving.ValidateObjectMetadata(ctx, m.GetObjectMeta()).ViaField("metadata"). - Also(m.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) + errs := serving.ValidateObjectMetadata(m.GetObjectMeta()).ViaField("metadata") + return errs.Also(m.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) } // Validate validates Metric's Spec. diff --git a/pkg/apis/autoscaling/v1alpha1/pa_lifecycle.go b/pkg/apis/autoscaling/v1alpha1/pa_lifecycle.go index 26cbdd62637d..db4c044714cb 100644 --- a/pkg/apis/autoscaling/v1alpha1/pa_lifecycle.go +++ b/pkg/apis/autoscaling/v1alpha1/pa_lifecycle.go @@ -32,6 +32,11 @@ var podCondSet = apis.NewLivingConditionSet( PodAutoscalerConditionActive, ) +// GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface. +func (*PodAutoscaler) GetConditionSet() apis.ConditionSet { + return podCondSet +} + func (pa *PodAutoscaler) GetGroupVersionKind() schema.GroupVersionKind { return SchemeGroupVersion.WithKind("PodAutoscaler") } diff --git a/pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go b/pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go index dd82f0db7fc9..1e6cead78e68 100644 --- a/pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go +++ b/pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go @@ -50,6 +50,14 @@ func TestPodAutoscalerDuckTypes(t *testing.T) { } } +func TestPodAutoscalerGetConditionSet(t *testing.T) { + r := &PodAutoscaler{} + want := apis.ConditionReady + if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + func TestGeneration(t *testing.T) { r := PodAutoscaler{} if a := r.GetGeneration(); a != 0 { diff --git a/pkg/apis/autoscaling/v1alpha1/pa_types.go b/pkg/apis/autoscaling/v1alpha1/pa_types.go index 085a2df0627b..f4f66bea9b4f 100644 --- a/pkg/apis/autoscaling/v1alpha1/pa_types.go +++ b/pkg/apis/autoscaling/v1alpha1/pa_types.go @@ -55,6 +55,9 @@ var ( // Check that we can create OwnerReferences to a PodAutoscaler. _ kmeta.OwnerRefable = (*PodAutoscaler)(nil) + + // Check that the type conforms to the duck Knative Resource shape. + _ duckv1.KRShaped = (*PodAutoscaler)(nil) ) // ReachabilityType is the enumeration type for the different states of reachability @@ -69,7 +72,7 @@ const ( // ReachabilityReachable means the `ScaleTarget` is reachable, ie. it has an active route. ReachabilityReachable ReachabilityType = "Reachable" - // ReachabilityReachable means the `ScaleTarget` is not reachable, ie. it does not have an active route. + // ReachabilityUnreachable means the `ScaleTarget` is not reachable, ie. it does not have an active route. ReachabilityUnreachable ReachabilityType = "Unreachable" ) @@ -141,3 +144,13 @@ type PodAutoscalerList struct { Items []PodAutoscaler `json:"items"` } + +// GetTypeMeta retrieves the ObjectMeta of the PodAutoscaler. Implements the KRShaped interface. +func (t *PodAutoscaler) GetTypeMeta() *metav1.TypeMeta { + return &t.TypeMeta +} + +// GetStatus retrieves the status of the PodAutoscaler. Implements the KRShaped interface. +func (t *PodAutoscaler) GetStatus() *duckv1.Status { + return &t.Status.Status +} diff --git a/pkg/apis/autoscaling/v1alpha1/pa_types_test.go b/pkg/apis/autoscaling/v1alpha1/pa_types_test.go new file mode 100644 index 000000000000..045e8ade985e --- /dev/null +++ b/pkg/apis/autoscaling/v1alpha1/pa_types_test.go @@ -0,0 +1,42 @@ +/* +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 v1alpha1 + +import ( + "testing" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestPodAutoscalerGetStatus(t *testing.T) { + r := &PodAutoscaler{ + Status: PodAutoscalerStatus{}, + } + want := &r.Status.Status + if got := r.GetStatus(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + +func TestPodAutoscalerGetObjectMeta(t *testing.T) { + r := &PodAutoscaler{ + TypeMeta: metav1.TypeMeta{}, + } + want := &r.TypeMeta + if got := r.GetTypeMeta(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} diff --git a/pkg/apis/autoscaling/v1alpha1/pa_validation.go b/pkg/apis/autoscaling/v1alpha1/pa_validation.go index 4e123aa5099a..31ff3b18a410 100644 --- a/pkg/apis/autoscaling/v1alpha1/pa_validation.go +++ b/pkg/apis/autoscaling/v1alpha1/pa_validation.go @@ -25,8 +25,8 @@ import ( ) func (pa *PodAutoscaler) Validate(ctx context.Context) *apis.FieldError { - return serving.ValidateObjectMetadata(ctx, pa.GetObjectMeta()).ViaField("metadata"). - Also(pa.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) + errs := serving.ValidateObjectMetadata(pa.GetObjectMeta()).ViaField("metadata") + return errs.Also(pa.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) } // Validate validates PodAutoscaler Spec. From eca9806a6726d2522d7c974d74f5500fd3ee9a82 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Thu, 7 May 2020 09:22:29 -0700 Subject: [PATCH 2/5] merge validation changes from master --- pkg/apis/autoscaling/v1alpha1/metric_validation.go | 4 ++-- pkg/apis/autoscaling/v1alpha1/pa_validation.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/apis/autoscaling/v1alpha1/metric_validation.go b/pkg/apis/autoscaling/v1alpha1/metric_validation.go index e0c8d182d958..fd1adb04911a 100644 --- a/pkg/apis/autoscaling/v1alpha1/metric_validation.go +++ b/pkg/apis/autoscaling/v1alpha1/metric_validation.go @@ -26,8 +26,8 @@ import ( // Validate validates the entire Metric. func (m *Metric) Validate(ctx context.Context) *apis.FieldError { - errs := serving.ValidateObjectMetadata(m.GetObjectMeta()).ViaField("metadata") - return errs.Also(m.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) + return serving.ValidateObjectMetadata(ctx, m.GetObjectMeta()).ViaField("metadata"). + Also(m.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) } // Validate validates Metric's Spec. diff --git a/pkg/apis/autoscaling/v1alpha1/pa_validation.go b/pkg/apis/autoscaling/v1alpha1/pa_validation.go index 31ff3b18a410..4e123aa5099a 100644 --- a/pkg/apis/autoscaling/v1alpha1/pa_validation.go +++ b/pkg/apis/autoscaling/v1alpha1/pa_validation.go @@ -25,8 +25,8 @@ import ( ) func (pa *PodAutoscaler) Validate(ctx context.Context) *apis.FieldError { - errs := serving.ValidateObjectMetadata(pa.GetObjectMeta()).ViaField("metadata") - return errs.Also(pa.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) + return serving.ValidateObjectMetadata(ctx, pa.GetObjectMeta()).ViaField("metadata"). + Also(pa.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) } // Validate validates PodAutoscaler Spec. From 36e44139995a0bc02ed1b9adb8fe181642817de2 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Thu, 7 May 2020 09:26:11 -0700 Subject: [PATCH 3/5] update test got/want --- pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go | 2 +- pkg/apis/autoscaling/v1alpha1/metric_types_test.go | 4 ++-- pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go | 2 +- pkg/apis/autoscaling/v1alpha1/pa_types_test.go | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go b/pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go index 25cdfe1b4566..16d5993b26be 100644 --- a/pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go +++ b/pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go @@ -52,7 +52,7 @@ func TestMetricGetConditionSet(t *testing.T) { r := &Metric{} want := apis.ConditionReady if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { - t.Errorf("got: %v, want: %v", got, want) + t.Errorf("GetTopLevelConditionType=%v, want=%v", got, want) } } diff --git a/pkg/apis/autoscaling/v1alpha1/metric_types_test.go b/pkg/apis/autoscaling/v1alpha1/metric_types_test.go index 99778584fd42..4bdb295bf893 100644 --- a/pkg/apis/autoscaling/v1alpha1/metric_types_test.go +++ b/pkg/apis/autoscaling/v1alpha1/metric_types_test.go @@ -27,7 +27,7 @@ func TestMetricGetStatus(t *testing.T) { } want := &r.Status.Status if got := r.GetStatus(); got != want { - t.Errorf("got: %v, want: %v", got, want) + t.Errorf("GotStatus=%v, want=%v", got, want) } } @@ -37,6 +37,6 @@ func TestMetricGetObjectMeta(t *testing.T) { } want := &r.TypeMeta if got := r.GetTypeMeta(); got != want { - t.Errorf("got: %v, want: %v", got, want) + t.Errorf("GetTypeMeta=%v, want=%v", got, want) } } diff --git a/pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go b/pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go index 1e6cead78e68..77e167b7c415 100644 --- a/pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go +++ b/pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go @@ -54,7 +54,7 @@ func TestPodAutoscalerGetConditionSet(t *testing.T) { r := &PodAutoscaler{} want := apis.ConditionReady if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { - t.Errorf("got: %v, want: %v", got, want) + t.Errorf("GetTopLevelConditionType=%v, want=%v", got, want) } } diff --git a/pkg/apis/autoscaling/v1alpha1/pa_types_test.go b/pkg/apis/autoscaling/v1alpha1/pa_types_test.go index 045e8ade985e..977ec120ff89 100644 --- a/pkg/apis/autoscaling/v1alpha1/pa_types_test.go +++ b/pkg/apis/autoscaling/v1alpha1/pa_types_test.go @@ -27,7 +27,7 @@ func TestPodAutoscalerGetStatus(t *testing.T) { } want := &r.Status.Status if got := r.GetStatus(); got != want { - t.Errorf("got: %v, want: %v", got, want) + t.Errorf("GotStatus=%v, want=%v", got, want) } } @@ -37,6 +37,6 @@ func TestPodAutoscalerGetObjectMeta(t *testing.T) { } want := &r.TypeMeta if got := r.GetTypeMeta(); got != want { - t.Errorf("got: %v, want: %v", got, want) + t.Errorf("GetTypeMeta=%v, want=%v", got, want) } } From 6aad8fa9268b61d60931d3bce17d812eb71f24b7 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Thu, 7 May 2020 09:28:04 -0700 Subject: [PATCH 4/5] combine got want lines --- pkg/apis/autoscaling/v1alpha1/metric_types_test.go | 8 ++++---- pkg/apis/autoscaling/v1alpha1/pa_types_test.go | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/apis/autoscaling/v1alpha1/metric_types_test.go b/pkg/apis/autoscaling/v1alpha1/metric_types_test.go index 4bdb295bf893..8c0363982021 100644 --- a/pkg/apis/autoscaling/v1alpha1/metric_types_test.go +++ b/pkg/apis/autoscaling/v1alpha1/metric_types_test.go @@ -25,8 +25,8 @@ func TestMetricGetStatus(t *testing.T) { r := &Metric{ Status: MetricStatus{}, } - want := &r.Status.Status - if got := r.GetStatus(); got != want { + + if got, want := r.GetStatus(), &r.Status.Status; got != want { t.Errorf("GotStatus=%v, want=%v", got, want) } } @@ -35,8 +35,8 @@ func TestMetricGetObjectMeta(t *testing.T) { r := &Metric{ TypeMeta: metav1.TypeMeta{}, } - want := &r.TypeMeta - if got := r.GetTypeMeta(); got != want { + + if got, want := r.GetTypeMeta(), &r.TypeMeta; got != want { t.Errorf("GetTypeMeta=%v, want=%v", got, want) } } diff --git a/pkg/apis/autoscaling/v1alpha1/pa_types_test.go b/pkg/apis/autoscaling/v1alpha1/pa_types_test.go index 977ec120ff89..865fb297bb9d 100644 --- a/pkg/apis/autoscaling/v1alpha1/pa_types_test.go +++ b/pkg/apis/autoscaling/v1alpha1/pa_types_test.go @@ -25,8 +25,8 @@ func TestPodAutoscalerGetStatus(t *testing.T) { r := &PodAutoscaler{ Status: PodAutoscalerStatus{}, } - want := &r.Status.Status - if got := r.GetStatus(); got != want { + + if got, want := r.GetStatus(), &r.Status.Status; got != want { t.Errorf("GotStatus=%v, want=%v", got, want) } } @@ -35,8 +35,8 @@ func TestPodAutoscalerGetObjectMeta(t *testing.T) { r := &PodAutoscaler{ TypeMeta: metav1.TypeMeta{}, } - want := &r.TypeMeta - if got := r.GetTypeMeta(); got != want { + + if got, want := r.GetTypeMeta(), &r.TypeMeta; got != want { t.Errorf("GetTypeMeta=%v, want=%v", got, want) } } From b191de3d9f86f8bebe72e53553dfd8686f352a85 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Thu, 7 May 2020 09:34:57 -0700 Subject: [PATCH 5/5] one more --- pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go | 4 ++-- pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go b/pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go index 16d5993b26be..38bbe6f7cb42 100644 --- a/pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go +++ b/pkg/apis/autoscaling/v1alpha1/metric_lifecycle_test.go @@ -50,8 +50,8 @@ func TestMetricDuckTypes(t *testing.T) { func TestMetricGetConditionSet(t *testing.T) { r := &Metric{} - want := apis.ConditionReady - if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { + + if got, want := r.GetConditionSet().GetTopLevelConditionType(), apis.ConditionReady; got != want { t.Errorf("GetTopLevelConditionType=%v, want=%v", got, want) } } diff --git a/pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go b/pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go index 77e167b7c415..c459ea77f11b 100644 --- a/pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go +++ b/pkg/apis/autoscaling/v1alpha1/pa_lifecycle_test.go @@ -52,8 +52,8 @@ func TestPodAutoscalerDuckTypes(t *testing.T) { func TestPodAutoscalerGetConditionSet(t *testing.T) { r := &PodAutoscaler{} - want := apis.ConditionReady - if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { + + if got, want := r.GetConditionSet().GetTopLevelConditionType(), apis.ConditionReady; got != want { t.Errorf("GetTopLevelConditionType=%v, want=%v", got, want) } }