From 9f15d5560b641a8c048bfb377a0510a3bb4d1f05 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Thu, 7 May 2020 09:01:22 -0700 Subject: [PATCH 01/10] serving v1 implements krshaped --- .../serving/v1/configuration_lifecycle.go | 5 ++ .../v1/configuration_lifecycle_test.go | 9 +++ pkg/apis/serving/v1/configuration_types.go | 13 +++++ .../serving/v1/configuration_types_test.go | 55 ++++++++++++++++++ .../serving/v1/configuration_validation.go | 2 +- pkg/apis/serving/v1/revision_lifecycle.go | 5 ++ .../serving/v1/revision_lifecycle_test.go | 8 +++ pkg/apis/serving/v1/revision_types.go | 13 +++++ pkg/apis/serving/v1/revision_types_test.go | 55 ++++++++++++++++++ pkg/apis/serving/v1/revision_validation.go | 6 +- .../serving/v1/revision_validation_test.go | 56 +------------------ pkg/apis/serving/v1/route_lifecycle.go | 5 ++ pkg/apis/serving/v1/route_lifecycle_test.go | 9 +++ pkg/apis/serving/v1/route_types.go | 13 +++++ pkg/apis/serving/v1/route_types_test.go | 55 ++++++++++++++++++ pkg/apis/serving/v1/route_validation.go | 2 +- pkg/apis/serving/v1/service_lifecycle.go | 5 ++ pkg/apis/serving/v1/service_lifecycle_test.go | 8 +++ pkg/apis/serving/v1/service_types.go | 13 +++++ pkg/apis/serving/v1/service_types_test.go | 55 ++++++++++++++++++ pkg/apis/serving/v1/service_validation.go | 2 +- 21 files changed, 332 insertions(+), 62 deletions(-) create mode 100644 pkg/apis/serving/v1/configuration_types_test.go create mode 100644 pkg/apis/serving/v1/revision_types_test.go create mode 100644 pkg/apis/serving/v1/route_types_test.go create mode 100644 pkg/apis/serving/v1/service_types_test.go diff --git a/pkg/apis/serving/v1/configuration_lifecycle.go b/pkg/apis/serving/v1/configuration_lifecycle.go index 74d70a31e514..ff410e952c32 100644 --- a/pkg/apis/serving/v1/configuration_lifecycle.go +++ b/pkg/apis/serving/v1/configuration_lifecycle.go @@ -24,6 +24,11 @@ import ( var configCondSet = apis.NewLivingConditionSet() +// GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface. +func (*Configuration) GetConditionSet() apis.ConditionSet { + return configCondSet +} + // GetGroupVersionKind returns the GroupVersionKind. func (r *Configuration) GetGroupVersionKind() schema.GroupVersionKind { return SchemeGroupVersion.WithKind("Configuration") diff --git a/pkg/apis/serving/v1/configuration_lifecycle_test.go b/pkg/apis/serving/v1/configuration_lifecycle_test.go index 5644fda44b9b..6fd43e2f07da 100644 --- a/pkg/apis/serving/v1/configuration_lifecycle_test.go +++ b/pkg/apis/serving/v1/configuration_lifecycle_test.go @@ -21,6 +21,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime/schema" + "knative.dev/pkg/apis" "knative.dev/pkg/apis/duck" duckv1 "knative.dev/pkg/apis/duck/v1" apistest "knative.dev/pkg/apis/testing" @@ -45,6 +46,14 @@ func TestConfigurationDuckTypes(t *testing.T) { } } +func TestConfigurationGetConditionSet(t *testing.T) { + r := &Configuration{} + want := apis.ConditionReady + if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + func TestConfigurationGetGroupVersionKind(t *testing.T) { r := &Configuration{} want := schema.GroupVersionKind{ diff --git a/pkg/apis/serving/v1/configuration_types.go b/pkg/apis/serving/v1/configuration_types.go index 065e55ad58b4..49aacd99e4b3 100644 --- a/pkg/apis/serving/v1/configuration_types.go +++ b/pkg/apis/serving/v1/configuration_types.go @@ -55,6 +55,9 @@ var ( // Check that we can create OwnerReferences to a Configuration. _ kmeta.OwnerRefable = (*Configuration)(nil) + + // Check that the type conforms to the duck Knative Resource shape. + _ duckv1.KRShaped = (*Configuration)(nil) ) // ConfigurationSpec holds the desired state of the Configuration (from the client). @@ -105,3 +108,13 @@ type ConfigurationList struct { Items []Configuration `json:"items"` } + +// GetTypeMeta retrieves the ObjectMeta of the Configuration. Implements the KRShaped interface. +func (t *Configuration) GetTypeMeta() *metav1.TypeMeta { + return &t.TypeMeta +} + +// GetStatus retrieves the status of the Configuration. Implements the KRShaped interface. +func (t *Configuration) GetStatus() *duckv1.Status { + return &t.Status.Status +} diff --git a/pkg/apis/serving/v1/configuration_types_test.go b/pkg/apis/serving/v1/configuration_types_test.go new file mode 100644 index 000000000000..38fe3d1b136f --- /dev/null +++ b/pkg/apis/serving/v1/configuration_types_test.go @@ -0,0 +1,55 @@ +/* +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 ( + "testing" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "knative.dev/pkg/apis" +) + +func TestIsConfigurationCondition(t *testing.T) { + cType := apis.ConditionType("DefinitelyNotConfigurationType") + + if IsConfigurationCondition(cType) { + t.Error("Not expected to be a configuration type") + } + + if !IsConfigurationCondition(ConfigurationConditionReady) { + t.Error("Expected to be a configuration type") + } +} + +func TestConfigurationGetStatus(t *testing.T) { + r := &Configuration{ + Status: ConfigurationStatus{}, + } + want := &r.Status.Status + if got := r.GetStatus(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + +func TestConfigurationGetObjectMeta(t *testing.T) { + r := &Configuration{ + 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/serving/v1/configuration_validation.go b/pkg/apis/serving/v1/configuration_validation.go index c273d2ae5492..d690570d3d0c 100644 --- a/pkg/apis/serving/v1/configuration_validation.go +++ b/pkg/apis/serving/v1/configuration_validation.go @@ -32,7 +32,7 @@ func (c *Configuration) Validate(ctx context.Context) (errs *apis.FieldError) { // have changed (i.e. due to config-defaults changes), we elide the metadata and // spec validation. if !apis.IsInStatusUpdate(ctx) { - errs = errs.Also(serving.ValidateObjectMetadata(ctx, c.GetObjectMeta()).Also( + errs = errs.Also(serving.ValidateObjectMetadata(c.GetObjectMeta()).Also( c.validateLabels().ViaField("labels")).ViaField("metadata")) ctx = apis.WithinParent(ctx, c.ObjectMeta) errs = errs.Also(c.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) diff --git a/pkg/apis/serving/v1/revision_lifecycle.go b/pkg/apis/serving/v1/revision_lifecycle.go index 7be076a3f2ea..ca1b69da5510 100644 --- a/pkg/apis/serving/v1/revision_lifecycle.go +++ b/pkg/apis/serving/v1/revision_lifecycle.go @@ -52,6 +52,11 @@ var revisionCondSet = apis.NewLivingConditionSet( RevisionConditionContainerHealthy, ) +// GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface. +func (*Revision) GetConditionSet() apis.ConditionSet { + return revisionCondSet +} + // GetGroupVersionKind returns the GroupVersionKind. func (r *Revision) GetGroupVersionKind() schema.GroupVersionKind { return SchemeGroupVersion.WithKind("Revision") diff --git a/pkg/apis/serving/v1/revision_lifecycle_test.go b/pkg/apis/serving/v1/revision_lifecycle_test.go index 52a43cd74671..0568fa59a014 100644 --- a/pkg/apis/serving/v1/revision_lifecycle_test.go +++ b/pkg/apis/serving/v1/revision_lifecycle_test.go @@ -58,6 +58,14 @@ func TestRevisionDuckTypes(t *testing.T) { } } +func TestRevisionGetConditionSet(t *testing.T) { + r := &Revision{} + want := apis.ConditionReady + if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + func TestRevisionGetGroupVersionKind(t *testing.T) { r := &Revision{} want := schema.GroupVersionKind{ diff --git a/pkg/apis/serving/v1/revision_types.go b/pkg/apis/serving/v1/revision_types.go index 61efe107a381..7c933f6abb30 100644 --- a/pkg/apis/serving/v1/revision_types.go +++ b/pkg/apis/serving/v1/revision_types.go @@ -56,6 +56,9 @@ var ( // Check that we can create OwnerReferences to a Revision. _ kmeta.OwnerRefable = (*Revision)(nil) + + // Check that the type conforms to the duck Knative Resource shape. + _ duckv1.KRShaped = (*Revision)(nil) ) // RevisionTemplateSpec describes the data a revision should have when created from a template. @@ -167,3 +170,13 @@ type RevisionList struct { Items []Revision `json:"items"` } + +// GetTypeMeta retrieves the ObjectMeta of the Revision. Implements the KRShaped interface. +func (t *Revision) GetTypeMeta() *metav1.TypeMeta { + return &t.TypeMeta +} + +// GetStatus retrieves the status of the Revision. Implements the KRShaped interface. +func (t *Revision) GetStatus() *duckv1.Status { + return &t.Status.Status +} diff --git a/pkg/apis/serving/v1/revision_types_test.go b/pkg/apis/serving/v1/revision_types_test.go new file mode 100644 index 000000000000..bd20441f28ff --- /dev/null +++ b/pkg/apis/serving/v1/revision_types_test.go @@ -0,0 +1,55 @@ +/* +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 ( + "testing" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "knative.dev/pkg/apis" +) + +func TestIsRevisionCondition(t *testing.T) { + cType := apis.ConditionType("DefinitelyNotRevisionType") + + if IsRevisionCondition(cType) { + t.Error("Not expected to be a revision type") + } + + if !IsRevisionCondition(RevisionConditionReady) { + t.Error("Expected to be a revision type") + } +} + +func TestRevisionGetStatus(t *testing.T) { + r := &Revision{ + Status: RevisionStatus{}, + } + want := &r.Status.Status + if got := r.GetStatus(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + +func TestRevisionGetObjectMeta(t *testing.T) { + r := &Revision{ + 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/serving/v1/revision_validation.go b/pkg/apis/serving/v1/revision_validation.go index 5634802dca16..fd426b7ea2b7 100644 --- a/pkg/apis/serving/v1/revision_validation.go +++ b/pkg/apis/serving/v1/revision_validation.go @@ -23,13 +23,12 @@ import ( "knative.dev/pkg/apis" "knative.dev/pkg/kmp" "knative.dev/serving/pkg/apis/autoscaling" - apisconfig "knative.dev/serving/pkg/apis/config" "knative.dev/serving/pkg/apis/serving" ) // Validate ensures Revision is properly configured. func (r *Revision) Validate(ctx context.Context) *apis.FieldError { - errs := serving.ValidateObjectMetadata(ctx, r.GetObjectMeta()).Also( + errs := serving.ValidateObjectMetadata(r.GetObjectMeta()).Also( r.ValidateLabels().ViaField("labels")).ViaField("metadata") errs = errs.Also(r.Status.Validate(apis.WithinStatus(ctx)).ViaField("status")) @@ -58,8 +57,7 @@ func (r *Revision) Validate(ctx context.Context) *apis.FieldError { // Validate implements apis.Validatable func (rts *RevisionTemplateSpec) Validate(ctx context.Context) *apis.FieldError { errs := rts.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec") - allowZeroInitialScale := apisconfig.FromContextOrDefaults(ctx).Autoscaler.AllowZeroInitialScale - errs = errs.Also(autoscaling.ValidateAnnotations(allowZeroInitialScale, rts.GetAnnotations()).ViaField("metadata.annotations")) + errs = errs.Also(autoscaling.ValidateAnnotations(rts.GetAnnotations()).ViaField("metadata.annotations")) // If the RevisionTemplateSpec has a name specified, then check that // it follows the requirements on the name. diff --git a/pkg/apis/serving/v1/revision_validation_test.go b/pkg/apis/serving/v1/revision_validation_test.go index b5f3130667fc..1cbce588b6ef 100644 --- a/pkg/apis/serving/v1/revision_validation_test.go +++ b/pkg/apis/serving/v1/revision_validation_test.go @@ -19,7 +19,6 @@ package v1 import ( "context" "fmt" - "strconv" "testing" "github.com/google/go-cmp/cmp" @@ -733,7 +732,6 @@ func TestImmutableFields(t *testing.T) { func TestRevisionTemplateSpecValidation(t *testing.T) { tests := []struct { name string - ctx context.Context rts *RevisionTemplateSpec want *apis.FieldError }{{ @@ -893,54 +891,11 @@ func TestRevisionTemplateSpecValidation(t *testing.T) { Message: "invalid value: 50mx", Paths: []string{fmt.Sprintf("[%s]", serving.QueueSideCarResourcePercentageAnnotation)}, }).ViaField("metadata.annotations"), - }, { - name: "Invalid initial scale when cluster doesn't allow zero", - ctx: autoscalerConfigCtx(false, 1), - rts: &RevisionTemplateSpec{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: map[string]string{ - autoscaling.InitialScaleAnnotationKey: "0", - }, - }, - Spec: RevisionSpec{ - PodSpec: corev1.PodSpec{ - Containers: []corev1.Container{{ - Image: "helloworld", - }}, - }, - }, - }, - want: (&apis.FieldError{ - Message: "invalid value: 0", - Paths: []string{autoscaling.InitialScaleAnnotationKey}, - }).ViaField("metadata.annotations"), - }, { - name: "Valid initial scale when cluster allows zero", - ctx: autoscalerConfigCtx(true, 1), - rts: &RevisionTemplateSpec{ - ObjectMeta: metav1.ObjectMeta{ - Annotations: map[string]string{ - autoscaling.InitialScaleAnnotationKey: "0", - }, - }, - Spec: RevisionSpec{ - PodSpec: corev1.PodSpec{ - Containers: []corev1.Container{{ - Image: "helloworld", - }}, - }, - }, - }, - want: nil, }} for _, test := range tests { t.Run(test.name, func(t *testing.T) { - ctx := context.Background() - if test.ctx != nil { - ctx = test.ctx - } - ctx = apis.WithinParent(ctx, metav1.ObjectMeta{ + ctx := apis.WithinParent(context.Background(), metav1.ObjectMeta{ Name: "parent", }) @@ -951,12 +906,3 @@ func TestRevisionTemplateSpecValidation(t *testing.T) { }) } } - -func autoscalerConfigCtx(allowInitialScaleZero bool, initialScale int) context.Context { - testConfigs := &config.Config{} - testConfigs.Autoscaler, _ = autoscalerconfig.NewConfigFromMap(map[string]string{ - "allow-zero-initial-scale": strconv.FormatBool(allowInitialScaleZero), - "initial-scale": strconv.Itoa(initialScale), - }) - return config.ToContext(context.Background(), testConfigs) -} diff --git a/pkg/apis/serving/v1/route_lifecycle.go b/pkg/apis/serving/v1/route_lifecycle.go index 4b67a2b9efcf..1942731f53fb 100644 --- a/pkg/apis/serving/v1/route_lifecycle.go +++ b/pkg/apis/serving/v1/route_lifecycle.go @@ -31,6 +31,11 @@ var routeCondSet = apis.NewLivingConditionSet( RouteConditionIngressReady, ) +// GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface. +func (*Route) GetConditionSet() apis.ConditionSet { + return routeCondSet +} + // GetGroupVersionKind returns the GroupVersionKind. func (r *Route) GetGroupVersionKind() schema.GroupVersionKind { return SchemeGroupVersion.WithKind("Route") diff --git a/pkg/apis/serving/v1/route_lifecycle_test.go b/pkg/apis/serving/v1/route_lifecycle_test.go index 3b16f5559a00..af68f377907d 100644 --- a/pkg/apis/serving/v1/route_lifecycle_test.go +++ b/pkg/apis/serving/v1/route_lifecycle_test.go @@ -20,6 +20,7 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime/schema" + "knative.dev/pkg/apis" "knative.dev/pkg/apis/duck" duckv1 "knative.dev/pkg/apis/duck/v1" apistest "knative.dev/pkg/apis/testing" @@ -45,6 +46,14 @@ func TestRouteDuckTypes(t *testing.T) { } } +func TestRouteGetConditionSet(t *testing.T) { + r := &Route{} + want := apis.ConditionReady + if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + func TestRouteGetGroupVersionKind(t *testing.T) { r := &Route{} want := schema.GroupVersionKind{ diff --git a/pkg/apis/serving/v1/route_types.go b/pkg/apis/serving/v1/route_types.go index 2345d545ca87..6a8fc0a9e804 100644 --- a/pkg/apis/serving/v1/route_types.go +++ b/pkg/apis/serving/v1/route_types.go @@ -59,6 +59,9 @@ var ( // Check that we can create OwnerReferences to a Route. _ kmeta.OwnerRefable = (*Route)(nil) + + // Check that the type conforms to the duck Knative Resource shape. + _ duckv1.KRShaped = (*Route)(nil) ) // TrafficTarget holds a single entry of the routing table for a Route. @@ -184,3 +187,13 @@ type RouteList struct { Items []Route `json:"items"` } + +// GetTypeMeta retrieves the ObjectMeta of the Route. Implements the KRShaped interface. +func (t *Route) GetTypeMeta() *metav1.TypeMeta { + return &t.TypeMeta +} + +// GetStatus retrieves the status of the Route. Implements the KRShaped interface. +func (t *Route) GetStatus() *duckv1.Status { + return &t.Status.Status +} diff --git a/pkg/apis/serving/v1/route_types_test.go b/pkg/apis/serving/v1/route_types_test.go new file mode 100644 index 000000000000..62945b4f91a7 --- /dev/null +++ b/pkg/apis/serving/v1/route_types_test.go @@ -0,0 +1,55 @@ +/* +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 ( + "testing" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "knative.dev/pkg/apis" +) + +func TestIsRouteCondition(t *testing.T) { + cType := apis.ConditionType("DefinitelyNotRouteType") + + if IsRouteCondition(cType) { + t.Error("Not expected to be a route type") + } + + if !IsRouteCondition(RouteConditionReady) { + t.Error("Expected to be a route type") + } +} + +func TestRouteGetStatus(t *testing.T) { + r := &Route{ + Status: RouteStatus{}, + } + want := &r.Status.Status + if got := r.GetStatus(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + +func TestRouteGetObjectMeta(t *testing.T) { + r := &Route{ + 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/serving/v1/route_validation.go b/pkg/apis/serving/v1/route_validation.go index a6b35dccb438..311a119164bc 100644 --- a/pkg/apis/serving/v1/route_validation.go +++ b/pkg/apis/serving/v1/route_validation.go @@ -28,7 +28,7 @@ import ( // Validate makes sure that Route is properly configured. func (r *Route) Validate(ctx context.Context) *apis.FieldError { - errs := serving.ValidateObjectMetadata(ctx, r.GetObjectMeta()).Also( + errs := serving.ValidateObjectMetadata(r.GetObjectMeta()).Also( r.validateLabels().ViaField("labels")).ViaField("metadata") errs = errs.Also(r.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) errs = errs.Also(r.Status.Validate(apis.WithinStatus(ctx)).ViaField("status")) diff --git a/pkg/apis/serving/v1/service_lifecycle.go b/pkg/apis/serving/v1/service_lifecycle.go index 1198263a4213..ea03097fdd5e 100644 --- a/pkg/apis/serving/v1/service_lifecycle.go +++ b/pkg/apis/serving/v1/service_lifecycle.go @@ -35,6 +35,11 @@ var serviceCondSet = apis.NewLivingConditionSet( ServiceConditionRoutesReady, ) +// GetConditionSet retrieves the condition set for this resource. Implements the KRShaped interface. +func (*Service) GetConditionSet() apis.ConditionSet { + return serviceCondSet +} + // GetGroupVersionKind returns the GroupVersionKind. func (s *Service) GetGroupVersionKind() schema.GroupVersionKind { return SchemeGroupVersion.WithKind("Service") diff --git a/pkg/apis/serving/v1/service_lifecycle_test.go b/pkg/apis/serving/v1/service_lifecycle_test.go index 86cdf7f865d5..c0214ecdf0af 100644 --- a/pkg/apis/serving/v1/service_lifecycle_test.go +++ b/pkg/apis/serving/v1/service_lifecycle_test.go @@ -47,6 +47,14 @@ func TestServiceDuckTypes(t *testing.T) { } } +func TestServiceGetConditionSet(t *testing.T) { + r := &Service{} + want := apis.ConditionReady + if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + func TestServiceGetGroupVersionKind(t *testing.T) { r := &Service{} want := schema.GroupVersionKind{ diff --git a/pkg/apis/serving/v1/service_types.go b/pkg/apis/serving/v1/service_types.go index f25cd91549c5..899212865c9e 100644 --- a/pkg/apis/serving/v1/service_types.go +++ b/pkg/apis/serving/v1/service_types.go @@ -63,6 +63,9 @@ var ( // Check that we can create OwnerReferences to a Service. _ kmeta.OwnerRefable = (*Service)(nil) + + // Check that the type conforms to the duck Knative Resource shape. + _ duckv1.KRShaped = (*Service)(nil) ) // ServiceSpec represents the configuration for the Service object. @@ -132,3 +135,13 @@ type ServiceList struct { Items []Service `json:"items"` } + +// GetTypeMeta retrieves the ObjectMeta of the Service. Implements the KRShaped interface. +func (t *Service) GetTypeMeta() *metav1.TypeMeta { + return &t.TypeMeta +} + +// GetStatus retrieves the status of the Service. Implements the KRShaped interface. +func (t *Service) GetStatus() *duckv1.Status { + return &t.Status.Status +} diff --git a/pkg/apis/serving/v1/service_types_test.go b/pkg/apis/serving/v1/service_types_test.go new file mode 100644 index 000000000000..87cc9b5d267a --- /dev/null +++ b/pkg/apis/serving/v1/service_types_test.go @@ -0,0 +1,55 @@ +/* +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 ( + "testing" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "knative.dev/pkg/apis" +) + +func TestIsServiceCondition(t *testing.T) { + cType := apis.ConditionType("DefinitelyNotServiceType") + + if IsServiceCondition(cType) { + t.Error("Not expected to be a service type") + } + + if !IsServiceCondition(ServiceConditionReady) { + t.Error("Expected to be a service type") + } +} + +func TestServiceGetStatus(t *testing.T) { + r := &Service{ + Status: ServiceStatus{}, + } + want := &r.Status.Status + if got := r.GetStatus(); got != want { + t.Errorf("got: %v, want: %v", got, want) + } +} + +func TestServiceGetObjectMeta(t *testing.T) { + r := &Service{ + 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/serving/v1/service_validation.go b/pkg/apis/serving/v1/service_validation.go index f6c4ddc8e51d..67099f60d65a 100644 --- a/pkg/apis/serving/v1/service_validation.go +++ b/pkg/apis/serving/v1/service_validation.go @@ -31,7 +31,7 @@ func (s *Service) Validate(ctx context.Context) (errs *apis.FieldError) { // have changed (i.e. due to config-defaults changes), we elide the metadata and // spec validation. if !apis.IsInStatusUpdate(ctx) { - errs = errs.Also(serving.ValidateObjectMetadata(ctx, s.GetObjectMeta()).Also( + errs = errs.Also(serving.ValidateObjectMetadata(s.GetObjectMeta()).Also( s.validateLabels().ViaField("labels")).ViaField("metadata")) ctx = apis.WithinParent(ctx, s.ObjectMeta) errs = errs.Also(s.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) From f04b0eed315e466cb9136859ec9f9f966b607f82 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Thu, 7 May 2020 09:17:39 -0700 Subject: [PATCH 02/10] merge validation changes --- pkg/apis/serving/v1/configuration_validation.go | 2 +- pkg/apis/serving/v1/revision_validation.go | 5 +++-- pkg/apis/serving/v1/route_validation.go | 2 +- pkg/apis/serving/v1/service_validation.go | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pkg/apis/serving/v1/configuration_validation.go b/pkg/apis/serving/v1/configuration_validation.go index d690570d3d0c..c273d2ae5492 100644 --- a/pkg/apis/serving/v1/configuration_validation.go +++ b/pkg/apis/serving/v1/configuration_validation.go @@ -32,7 +32,7 @@ func (c *Configuration) Validate(ctx context.Context) (errs *apis.FieldError) { // have changed (i.e. due to config-defaults changes), we elide the metadata and // spec validation. if !apis.IsInStatusUpdate(ctx) { - errs = errs.Also(serving.ValidateObjectMetadata(c.GetObjectMeta()).Also( + errs = errs.Also(serving.ValidateObjectMetadata(ctx, c.GetObjectMeta()).Also( c.validateLabels().ViaField("labels")).ViaField("metadata")) ctx = apis.WithinParent(ctx, c.ObjectMeta) errs = errs.Also(c.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) diff --git a/pkg/apis/serving/v1/revision_validation.go b/pkg/apis/serving/v1/revision_validation.go index fd426b7ea2b7..d8cbe0b0067f 100644 --- a/pkg/apis/serving/v1/revision_validation.go +++ b/pkg/apis/serving/v1/revision_validation.go @@ -28,7 +28,7 @@ import ( // Validate ensures Revision is properly configured. func (r *Revision) Validate(ctx context.Context) *apis.FieldError { - errs := serving.ValidateObjectMetadata(r.GetObjectMeta()).Also( + errs := serving.ValidateObjectMetadata(ctx, r.GetObjectMeta()).Also( r.ValidateLabels().ViaField("labels")).ViaField("metadata") errs = errs.Also(r.Status.Validate(apis.WithinStatus(ctx)).ViaField("status")) @@ -57,7 +57,8 @@ func (r *Revision) Validate(ctx context.Context) *apis.FieldError { // Validate implements apis.Validatable func (rts *RevisionTemplateSpec) Validate(ctx context.Context) *apis.FieldError { errs := rts.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec") - errs = errs.Also(autoscaling.ValidateAnnotations(rts.GetAnnotations()).ViaField("metadata.annotations")) + allowZeroInitialScale := apisconfig.FromContextOrDefaults(ctx).Autoscaler.AllowZeroInitialScale + errs = errs.Also(autoscaling.ValidateAnnotations(allowZeroInitialScale, rts.GetAnnotations()).ViaField("metadata.annotations")) // If the RevisionTemplateSpec has a name specified, then check that // it follows the requirements on the name. diff --git a/pkg/apis/serving/v1/route_validation.go b/pkg/apis/serving/v1/route_validation.go index 311a119164bc..a6b35dccb438 100644 --- a/pkg/apis/serving/v1/route_validation.go +++ b/pkg/apis/serving/v1/route_validation.go @@ -28,7 +28,7 @@ import ( // Validate makes sure that Route is properly configured. func (r *Route) Validate(ctx context.Context) *apis.FieldError { - errs := serving.ValidateObjectMetadata(r.GetObjectMeta()).Also( + errs := serving.ValidateObjectMetadata(ctx, r.GetObjectMeta()).Also( r.validateLabels().ViaField("labels")).ViaField("metadata") errs = errs.Also(r.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) errs = errs.Also(r.Status.Validate(apis.WithinStatus(ctx)).ViaField("status")) diff --git a/pkg/apis/serving/v1/service_validation.go b/pkg/apis/serving/v1/service_validation.go index 67099f60d65a..f6c4ddc8e51d 100644 --- a/pkg/apis/serving/v1/service_validation.go +++ b/pkg/apis/serving/v1/service_validation.go @@ -31,7 +31,7 @@ func (s *Service) Validate(ctx context.Context) (errs *apis.FieldError) { // have changed (i.e. due to config-defaults changes), we elide the metadata and // spec validation. if !apis.IsInStatusUpdate(ctx) { - errs = errs.Also(serving.ValidateObjectMetadata(s.GetObjectMeta()).Also( + errs = errs.Also(serving.ValidateObjectMetadata(ctx, s.GetObjectMeta()).Also( s.validateLabels().ViaField("labels")).ViaField("metadata")) ctx = apis.WithinParent(ctx, s.ObjectMeta) errs = errs.Also(s.Spec.Validate(apis.WithinSpec(ctx)).ViaField("spec")) From 4675baac0d34ca2de8ec25f68338dbe4954afa31 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Thu, 7 May 2020 09:19:09 -0700 Subject: [PATCH 03/10] merge from master --- pkg/apis/serving/v1/revision_validation.go | 1 + .../serving/v1/revision_validation_test.go | 56 ++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/pkg/apis/serving/v1/revision_validation.go b/pkg/apis/serving/v1/revision_validation.go index d8cbe0b0067f..5634802dca16 100644 --- a/pkg/apis/serving/v1/revision_validation.go +++ b/pkg/apis/serving/v1/revision_validation.go @@ -23,6 +23,7 @@ import ( "knative.dev/pkg/apis" "knative.dev/pkg/kmp" "knative.dev/serving/pkg/apis/autoscaling" + apisconfig "knative.dev/serving/pkg/apis/config" "knative.dev/serving/pkg/apis/serving" ) diff --git a/pkg/apis/serving/v1/revision_validation_test.go b/pkg/apis/serving/v1/revision_validation_test.go index 1cbce588b6ef..b5f3130667fc 100644 --- a/pkg/apis/serving/v1/revision_validation_test.go +++ b/pkg/apis/serving/v1/revision_validation_test.go @@ -19,6 +19,7 @@ package v1 import ( "context" "fmt" + "strconv" "testing" "github.com/google/go-cmp/cmp" @@ -732,6 +733,7 @@ func TestImmutableFields(t *testing.T) { func TestRevisionTemplateSpecValidation(t *testing.T) { tests := []struct { name string + ctx context.Context rts *RevisionTemplateSpec want *apis.FieldError }{{ @@ -891,11 +893,54 @@ func TestRevisionTemplateSpecValidation(t *testing.T) { Message: "invalid value: 50mx", Paths: []string{fmt.Sprintf("[%s]", serving.QueueSideCarResourcePercentageAnnotation)}, }).ViaField("metadata.annotations"), + }, { + name: "Invalid initial scale when cluster doesn't allow zero", + ctx: autoscalerConfigCtx(false, 1), + rts: &RevisionTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + autoscaling.InitialScaleAnnotationKey: "0", + }, + }, + Spec: RevisionSpec{ + PodSpec: corev1.PodSpec{ + Containers: []corev1.Container{{ + Image: "helloworld", + }}, + }, + }, + }, + want: (&apis.FieldError{ + Message: "invalid value: 0", + Paths: []string{autoscaling.InitialScaleAnnotationKey}, + }).ViaField("metadata.annotations"), + }, { + name: "Valid initial scale when cluster allows zero", + ctx: autoscalerConfigCtx(true, 1), + rts: &RevisionTemplateSpec{ + ObjectMeta: metav1.ObjectMeta{ + Annotations: map[string]string{ + autoscaling.InitialScaleAnnotationKey: "0", + }, + }, + Spec: RevisionSpec{ + PodSpec: corev1.PodSpec{ + Containers: []corev1.Container{{ + Image: "helloworld", + }}, + }, + }, + }, + want: nil, }} for _, test := range tests { t.Run(test.name, func(t *testing.T) { - ctx := apis.WithinParent(context.Background(), metav1.ObjectMeta{ + ctx := context.Background() + if test.ctx != nil { + ctx = test.ctx + } + ctx = apis.WithinParent(ctx, metav1.ObjectMeta{ Name: "parent", }) @@ -906,3 +951,12 @@ func TestRevisionTemplateSpecValidation(t *testing.T) { }) } } + +func autoscalerConfigCtx(allowInitialScaleZero bool, initialScale int) context.Context { + testConfigs := &config.Config{} + testConfigs.Autoscaler, _ = autoscalerconfig.NewConfigFromMap(map[string]string{ + "allow-zero-initial-scale": strconv.FormatBool(allowInitialScaleZero), + "initial-scale": strconv.Itoa(initialScale), + }) + return config.ToContext(context.Background(), testConfigs) +} From d5b9794e1ba3153e37aca89b6f8628242020eea3 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Thu, 7 May 2020 09:33:59 -0700 Subject: [PATCH 04/10] Update tests with review feedback --- pkg/apis/serving/v1/configuration_lifecycle_test.go | 6 +++--- pkg/apis/serving/v1/configuration_types_test.go | 12 ++++++------ pkg/apis/serving/v1/revision_lifecycle_test.go | 6 +++--- pkg/apis/serving/v1/revision_types_test.go | 12 ++++++------ pkg/apis/serving/v1/route_lifecycle_test.go | 6 +++--- pkg/apis/serving/v1/route_types_test.go | 12 ++++++------ pkg/apis/serving/v1/service_lifecycle_test.go | 6 +++--- pkg/apis/serving/v1/service_types_test.go | 12 ++++++------ 8 files changed, 36 insertions(+), 36 deletions(-) diff --git a/pkg/apis/serving/v1/configuration_lifecycle_test.go b/pkg/apis/serving/v1/configuration_lifecycle_test.go index 6fd43e2f07da..5a3ba67086e6 100644 --- a/pkg/apis/serving/v1/configuration_lifecycle_test.go +++ b/pkg/apis/serving/v1/configuration_lifecycle_test.go @@ -48,9 +48,9 @@ func TestConfigurationDuckTypes(t *testing.T) { func TestConfigurationGetConditionSet(t *testing.T) { r := &Configuration{} - want := apis.ConditionReady - if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { - t.Errorf("got: %v, want: %v", got, want) + + if got, want := r.GetConditionSet().GetTopLevelConditionType(), apis.ConditionReady; got != want { + t.Errorf("GotTopLevelCondition=%v, want=%v", got, want) } } diff --git a/pkg/apis/serving/v1/configuration_types_test.go b/pkg/apis/serving/v1/configuration_types_test.go index 38fe3d1b136f..49691e9b07e3 100644 --- a/pkg/apis/serving/v1/configuration_types_test.go +++ b/pkg/apis/serving/v1/configuration_types_test.go @@ -38,9 +38,9 @@ func TestConfigurationGetStatus(t *testing.T) { r := &Configuration{ Status: ConfigurationStatus{}, } - want := &r.Status.Status - if got := r.GetStatus(); got != want { - t.Errorf("got: %v, want: %v", got, want) + + if got, want := r.GetStatus(), &r.Status.Status; got != want { + t.Errorf("GotStatus=%v, want=%v", got, want) } } @@ -48,8 +48,8 @@ func TestConfigurationGetObjectMeta(t *testing.T) { r := &Configuration{ TypeMeta: metav1.TypeMeta{}, } - want := &r.TypeMeta - if got := r.GetTypeMeta(); got != want { - t.Errorf("got: %v, want: %v", got, want) + + if got, want := r.GetTypeMeta(), &r.TypeMeta; got != want { + t.Errorf("GotTypeMeta=%v, want=%v", got, want) } } diff --git a/pkg/apis/serving/v1/revision_lifecycle_test.go b/pkg/apis/serving/v1/revision_lifecycle_test.go index 0568fa59a014..60124dfa205a 100644 --- a/pkg/apis/serving/v1/revision_lifecycle_test.go +++ b/pkg/apis/serving/v1/revision_lifecycle_test.go @@ -60,9 +60,9 @@ func TestRevisionDuckTypes(t *testing.T) { func TestRevisionGetConditionSet(t *testing.T) { r := &Revision{} - want := apis.ConditionReady - if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { - t.Errorf("got: %v, want: %v", got, want) + + if got, want := r.GetConditionSet().GetTopLevelConditionType(), apis.ConditionReady; got != want { + t.Errorf("GotTopLevelCondition=%v, want=%v", got, want) } } diff --git a/pkg/apis/serving/v1/revision_types_test.go b/pkg/apis/serving/v1/revision_types_test.go index bd20441f28ff..7951a7c1237b 100644 --- a/pkg/apis/serving/v1/revision_types_test.go +++ b/pkg/apis/serving/v1/revision_types_test.go @@ -38,9 +38,9 @@ func TestRevisionGetStatus(t *testing.T) { r := &Revision{ Status: RevisionStatus{}, } - want := &r.Status.Status - if got := r.GetStatus(); got != want { - t.Errorf("got: %v, want: %v", got, want) + + if got, want := r.GetStatus(), &r.Status.Status; got != want { + t.Errorf("GotStatus=%v, want=%v", got, want) } } @@ -48,8 +48,8 @@ func TestRevisionGetObjectMeta(t *testing.T) { r := &Revision{ TypeMeta: metav1.TypeMeta{}, } - want := &r.TypeMeta - if got := r.GetTypeMeta(); got != want { - t.Errorf("got: %v, want: %v", got, want) + + if got, want := r.GetTypeMeta(), &r.TypeMeta; got != want { + t.Errorf("GotTypeMeta=%v, want=%v", got, want) } } diff --git a/pkg/apis/serving/v1/route_lifecycle_test.go b/pkg/apis/serving/v1/route_lifecycle_test.go index af68f377907d..ea185c852b16 100644 --- a/pkg/apis/serving/v1/route_lifecycle_test.go +++ b/pkg/apis/serving/v1/route_lifecycle_test.go @@ -48,9 +48,9 @@ func TestRouteDuckTypes(t *testing.T) { func TestRouteGetConditionSet(t *testing.T) { r := &Route{} - want := apis.ConditionReady - if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { - t.Errorf("got: %v, want: %v", got, want) + + if got, want := r.GetConditionSet().GetTopLevelConditionType(), apis.ConditionReady; got != want { + t.Errorf("GotTopLevelCondition=%v, want=%v", got, want) } } diff --git a/pkg/apis/serving/v1/route_types_test.go b/pkg/apis/serving/v1/route_types_test.go index 62945b4f91a7..bb5e48214436 100644 --- a/pkg/apis/serving/v1/route_types_test.go +++ b/pkg/apis/serving/v1/route_types_test.go @@ -38,9 +38,9 @@ func TestRouteGetStatus(t *testing.T) { r := &Route{ Status: RouteStatus{}, } - want := &r.Status.Status - if got := r.GetStatus(); got != want { - t.Errorf("got: %v, want: %v", got, want) + + if got, want := r.GetStatus(), &r.Status.Status; got != want { + t.Errorf("GotStatus=%v, want=%v", got, want) } } @@ -48,8 +48,8 @@ func TestRouteGetObjectMeta(t *testing.T) { r := &Route{ TypeMeta: metav1.TypeMeta{}, } - want := &r.TypeMeta - if got := r.GetTypeMeta(); got != want { - t.Errorf("got: %v, want: %v", got, want) + + if got, want := r.GetTypeMeta(), &r.TypeMeta; got != want { + t.Errorf("GotTypeMeta=%v, want=%v", got, want) } } diff --git a/pkg/apis/serving/v1/service_lifecycle_test.go b/pkg/apis/serving/v1/service_lifecycle_test.go index c0214ecdf0af..b3f1b2fbb4d5 100644 --- a/pkg/apis/serving/v1/service_lifecycle_test.go +++ b/pkg/apis/serving/v1/service_lifecycle_test.go @@ -49,9 +49,9 @@ func TestServiceDuckTypes(t *testing.T) { func TestServiceGetConditionSet(t *testing.T) { r := &Service{} - want := apis.ConditionReady - if got := r.GetConditionSet().GetTopLevelConditionType(); got != want { - t.Errorf("got: %v, want: %v", got, want) + + if got, want := r.GetConditionSet().GetTopLevelConditionType(), apis.ConditionReady; got != want { + t.Errorf("GotTopLevelCondition=%v, want=%v", got, want) } } diff --git a/pkg/apis/serving/v1/service_types_test.go b/pkg/apis/serving/v1/service_types_test.go index 87cc9b5d267a..0a0c8f83a262 100644 --- a/pkg/apis/serving/v1/service_types_test.go +++ b/pkg/apis/serving/v1/service_types_test.go @@ -38,9 +38,9 @@ func TestServiceGetStatus(t *testing.T) { r := &Service{ Status: ServiceStatus{}, } - want := &r.Status.Status - if got := r.GetStatus(); got != want { - t.Errorf("got: %v, want: %v", got, want) + + if got, want := r.GetStatus(), &r.Status.Status; got != want { + t.Errorf("GotStatus=%v, want=%v", got, want) } } @@ -48,8 +48,8 @@ func TestServiceGetObjectMeta(t *testing.T) { r := &Service{ TypeMeta: metav1.TypeMeta{}, } - want := &r.TypeMeta - if got := r.GetTypeMeta(); got != want { - t.Errorf("got: %v, want: %v", got, want) + + if got, want := r.GetTypeMeta(), &r.TypeMeta; got != want { + t.Errorf("GotTypeMeta=%v, want=%v", got, want) } } From c5093eaffe6f1bc7a6666a1df66f5c4dd55efc5a Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Tue, 12 May 2020 07:48:09 -0700 Subject: [PATCH 05/10] fix typos --- pkg/apis/serving/v1/configuration_lifecycle_test.go | 2 +- pkg/apis/serving/v1/configuration_types.go | 2 +- pkg/apis/serving/v1/revision_lifecycle_test.go | 2 +- pkg/apis/serving/v1/revision_types.go | 2 +- pkg/apis/serving/v1/route_lifecycle_test.go | 2 +- pkg/apis/serving/v1/route_types.go | 2 +- pkg/apis/serving/v1/service_lifecycle_test.go | 2 +- pkg/apis/serving/v1/service_types.go | 2 +- 8 files changed, 8 insertions(+), 8 deletions(-) diff --git a/pkg/apis/serving/v1/configuration_lifecycle_test.go b/pkg/apis/serving/v1/configuration_lifecycle_test.go index 5a3ba67086e6..1006314529bb 100644 --- a/pkg/apis/serving/v1/configuration_lifecycle_test.go +++ b/pkg/apis/serving/v1/configuration_lifecycle_test.go @@ -50,7 +50,7 @@ func TestConfigurationGetConditionSet(t *testing.T) { r := &Configuration{} if got, want := r.GetConditionSet().GetTopLevelConditionType(), apis.ConditionReady; got != want { - t.Errorf("GotTopLevelCondition=%v, want=%v", got, want) + t.Errorf("GetTopLevelCondition=%v, want=%v", got, want) } } diff --git a/pkg/apis/serving/v1/configuration_types.go b/pkg/apis/serving/v1/configuration_types.go index 49aacd99e4b3..01a87a6702c1 100644 --- a/pkg/apis/serving/v1/configuration_types.go +++ b/pkg/apis/serving/v1/configuration_types.go @@ -109,7 +109,7 @@ type ConfigurationList struct { Items []Configuration `json:"items"` } -// GetTypeMeta retrieves the ObjectMeta of the Configuration. Implements the KRShaped interface. +// GetTypeMeta retrieves the TypeMeta of the Configuration. Implements the KRShaped interface. func (t *Configuration) GetTypeMeta() *metav1.TypeMeta { return &t.TypeMeta } diff --git a/pkg/apis/serving/v1/revision_lifecycle_test.go b/pkg/apis/serving/v1/revision_lifecycle_test.go index 60124dfa205a..06ba0c17a9b7 100644 --- a/pkg/apis/serving/v1/revision_lifecycle_test.go +++ b/pkg/apis/serving/v1/revision_lifecycle_test.go @@ -62,7 +62,7 @@ func TestRevisionGetConditionSet(t *testing.T) { r := &Revision{} if got, want := r.GetConditionSet().GetTopLevelConditionType(), apis.ConditionReady; got != want { - t.Errorf("GotTopLevelCondition=%v, want=%v", got, want) + t.Errorf("GetTopLevelCondition=%v, want=%v", got, want) } } diff --git a/pkg/apis/serving/v1/revision_types.go b/pkg/apis/serving/v1/revision_types.go index 7c933f6abb30..70d46135f143 100644 --- a/pkg/apis/serving/v1/revision_types.go +++ b/pkg/apis/serving/v1/revision_types.go @@ -171,7 +171,7 @@ type RevisionList struct { Items []Revision `json:"items"` } -// GetTypeMeta retrieves the ObjectMeta of the Revision. Implements the KRShaped interface. +// GetTypeMeta retrieves the TypeMeta of the Revision. Implements the KRShaped interface. func (t *Revision) GetTypeMeta() *metav1.TypeMeta { return &t.TypeMeta } diff --git a/pkg/apis/serving/v1/route_lifecycle_test.go b/pkg/apis/serving/v1/route_lifecycle_test.go index ea185c852b16..aea593dfadf8 100644 --- a/pkg/apis/serving/v1/route_lifecycle_test.go +++ b/pkg/apis/serving/v1/route_lifecycle_test.go @@ -50,7 +50,7 @@ func TestRouteGetConditionSet(t *testing.T) { r := &Route{} if got, want := r.GetConditionSet().GetTopLevelConditionType(), apis.ConditionReady; got != want { - t.Errorf("GotTopLevelCondition=%v, want=%v", got, want) + t.Errorf("GetTopLevelCondition=%v, want=%v", got, want) } } diff --git a/pkg/apis/serving/v1/route_types.go b/pkg/apis/serving/v1/route_types.go index 6a8fc0a9e804..50918edbe572 100644 --- a/pkg/apis/serving/v1/route_types.go +++ b/pkg/apis/serving/v1/route_types.go @@ -188,7 +188,7 @@ type RouteList struct { Items []Route `json:"items"` } -// GetTypeMeta retrieves the ObjectMeta of the Route. Implements the KRShaped interface. +// GetTypeMeta retrieves the TypeMeta of the Route. Implements the KRShaped interface. func (t *Route) GetTypeMeta() *metav1.TypeMeta { return &t.TypeMeta } diff --git a/pkg/apis/serving/v1/service_lifecycle_test.go b/pkg/apis/serving/v1/service_lifecycle_test.go index b3f1b2fbb4d5..0103570db794 100644 --- a/pkg/apis/serving/v1/service_lifecycle_test.go +++ b/pkg/apis/serving/v1/service_lifecycle_test.go @@ -51,7 +51,7 @@ func TestServiceGetConditionSet(t *testing.T) { r := &Service{} if got, want := r.GetConditionSet().GetTopLevelConditionType(), apis.ConditionReady; got != want { - t.Errorf("GotTopLevelCondition=%v, want=%v", got, want) + t.Errorf("GetTopLevelCondition=%v, want=%v", got, want) } } diff --git a/pkg/apis/serving/v1/service_types.go b/pkg/apis/serving/v1/service_types.go index 899212865c9e..189fc294acd5 100644 --- a/pkg/apis/serving/v1/service_types.go +++ b/pkg/apis/serving/v1/service_types.go @@ -136,7 +136,7 @@ type ServiceList struct { Items []Service `json:"items"` } -// GetTypeMeta retrieves the ObjectMeta of the Service. Implements the KRShaped interface. +// GetTypeMeta retrieves the TypeMeta of the Service. Implements the KRShaped interface. func (t *Service) GetTypeMeta() *metav1.TypeMeta { return &t.TypeMeta } From 83d91abf0e27bc37a1c5ada5e7571ce461d89161 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Wed, 13 May 2020 08:28:06 -0700 Subject: [PATCH 06/10] Remove gettypemeta function --- pkg/apis/serving/v1/configuration_types.go | 5 ----- pkg/apis/serving/v1/configuration_types_test.go | 10 ---------- pkg/apis/serving/v1/revision_types.go | 5 ----- pkg/apis/serving/v1/revision_types_test.go | 10 ---------- pkg/apis/serving/v1/route_types.go | 5 ----- pkg/apis/serving/v1/route_types_test.go | 10 ---------- pkg/apis/serving/v1/service_types.go | 5 ----- pkg/apis/serving/v1/service_types_test.go | 10 ---------- vendor/knative.dev/pkg/apis/duck/v1/kresource_type.go | 9 ++------- 9 files changed, 2 insertions(+), 67 deletions(-) diff --git a/pkg/apis/serving/v1/configuration_types.go b/pkg/apis/serving/v1/configuration_types.go index 01a87a6702c1..fbf1ae155c00 100644 --- a/pkg/apis/serving/v1/configuration_types.go +++ b/pkg/apis/serving/v1/configuration_types.go @@ -109,11 +109,6 @@ type ConfigurationList struct { Items []Configuration `json:"items"` } -// GetTypeMeta retrieves the TypeMeta of the Configuration. Implements the KRShaped interface. -func (t *Configuration) GetTypeMeta() *metav1.TypeMeta { - return &t.TypeMeta -} - // GetStatus retrieves the status of the Configuration. Implements the KRShaped interface. func (t *Configuration) GetStatus() *duckv1.Status { return &t.Status.Status diff --git a/pkg/apis/serving/v1/configuration_types_test.go b/pkg/apis/serving/v1/configuration_types_test.go index 49691e9b07e3..4aa53875f08b 100644 --- a/pkg/apis/serving/v1/configuration_types_test.go +++ b/pkg/apis/serving/v1/configuration_types_test.go @@ -34,16 +34,6 @@ func TestIsConfigurationCondition(t *testing.T) { } } -func TestConfigurationGetStatus(t *testing.T) { - r := &Configuration{ - Status: ConfigurationStatus{}, - } - - if got, want := r.GetStatus(), &r.Status.Status; got != want { - t.Errorf("GotStatus=%v, want=%v", got, want) - } -} - func TestConfigurationGetObjectMeta(t *testing.T) { r := &Configuration{ TypeMeta: metav1.TypeMeta{}, diff --git a/pkg/apis/serving/v1/revision_types.go b/pkg/apis/serving/v1/revision_types.go index 70d46135f143..dbad280b13e0 100644 --- a/pkg/apis/serving/v1/revision_types.go +++ b/pkg/apis/serving/v1/revision_types.go @@ -171,11 +171,6 @@ type RevisionList struct { Items []Revision `json:"items"` } -// GetTypeMeta retrieves the TypeMeta of the Revision. Implements the KRShaped interface. -func (t *Revision) GetTypeMeta() *metav1.TypeMeta { - return &t.TypeMeta -} - // GetStatus retrieves the status of the Revision. Implements the KRShaped interface. func (t *Revision) GetStatus() *duckv1.Status { return &t.Status.Status diff --git a/pkg/apis/serving/v1/revision_types_test.go b/pkg/apis/serving/v1/revision_types_test.go index 7951a7c1237b..72d0551eece1 100644 --- a/pkg/apis/serving/v1/revision_types_test.go +++ b/pkg/apis/serving/v1/revision_types_test.go @@ -34,16 +34,6 @@ func TestIsRevisionCondition(t *testing.T) { } } -func TestRevisionGetStatus(t *testing.T) { - r := &Revision{ - Status: RevisionStatus{}, - } - - if got, want := r.GetStatus(), &r.Status.Status; got != want { - t.Errorf("GotStatus=%v, want=%v", got, want) - } -} - func TestRevisionGetObjectMeta(t *testing.T) { r := &Revision{ TypeMeta: metav1.TypeMeta{}, diff --git a/pkg/apis/serving/v1/route_types.go b/pkg/apis/serving/v1/route_types.go index 50918edbe572..2d810ebf5013 100644 --- a/pkg/apis/serving/v1/route_types.go +++ b/pkg/apis/serving/v1/route_types.go @@ -188,11 +188,6 @@ type RouteList struct { Items []Route `json:"items"` } -// GetTypeMeta retrieves the TypeMeta of the Route. Implements the KRShaped interface. -func (t *Route) GetTypeMeta() *metav1.TypeMeta { - return &t.TypeMeta -} - // GetStatus retrieves the status of the Route. Implements the KRShaped interface. func (t *Route) GetStatus() *duckv1.Status { return &t.Status.Status diff --git a/pkg/apis/serving/v1/route_types_test.go b/pkg/apis/serving/v1/route_types_test.go index bb5e48214436..dd6e3aa6106b 100644 --- a/pkg/apis/serving/v1/route_types_test.go +++ b/pkg/apis/serving/v1/route_types_test.go @@ -34,16 +34,6 @@ func TestIsRouteCondition(t *testing.T) { } } -func TestRouteGetStatus(t *testing.T) { - r := &Route{ - Status: RouteStatus{}, - } - - if got, want := r.GetStatus(), &r.Status.Status; got != want { - t.Errorf("GotStatus=%v, want=%v", got, want) - } -} - func TestRouteGetObjectMeta(t *testing.T) { r := &Route{ TypeMeta: metav1.TypeMeta{}, diff --git a/pkg/apis/serving/v1/service_types.go b/pkg/apis/serving/v1/service_types.go index 189fc294acd5..ce5174319d30 100644 --- a/pkg/apis/serving/v1/service_types.go +++ b/pkg/apis/serving/v1/service_types.go @@ -136,11 +136,6 @@ type ServiceList struct { Items []Service `json:"items"` } -// GetTypeMeta retrieves the TypeMeta of the Service. Implements the KRShaped interface. -func (t *Service) GetTypeMeta() *metav1.TypeMeta { - return &t.TypeMeta -} - // GetStatus retrieves the status of the Service. Implements the KRShaped interface. func (t *Service) GetStatus() *duckv1.Status { return &t.Status.Status diff --git a/pkg/apis/serving/v1/service_types_test.go b/pkg/apis/serving/v1/service_types_test.go index 0a0c8f83a262..94b75a1c4da5 100644 --- a/pkg/apis/serving/v1/service_types_test.go +++ b/pkg/apis/serving/v1/service_types_test.go @@ -34,16 +34,6 @@ func TestIsServiceCondition(t *testing.T) { } } -func TestServiceGetStatus(t *testing.T) { - r := &Service{ - Status: ServiceStatus{}, - } - - if got, want := r.GetStatus(), &r.Status.Status; got != want { - t.Errorf("GotStatus=%v, want=%v", got, want) - } -} - func TestServiceGetObjectMeta(t *testing.T) { r := &Service{ TypeMeta: metav1.TypeMeta{}, diff --git a/vendor/knative.dev/pkg/apis/duck/v1/kresource_type.go b/vendor/knative.dev/pkg/apis/duck/v1/kresource_type.go index 80558428c009..20bf0200f534 100644 --- a/vendor/knative.dev/pkg/apis/duck/v1/kresource_type.go +++ b/vendor/knative.dev/pkg/apis/duck/v1/kresource_type.go @@ -22,6 +22,7 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" "knative.dev/pkg/apis" ) @@ -29,8 +30,7 @@ import ( // KRShaped is an interface for retrieving the duck elements of an arbitrary resource. type KRShaped interface { metav1.ObjectMetaAccessor - - GetTypeMeta() *metav1.TypeMeta + schema.ObjectKind GetStatus() *Status @@ -81,11 +81,6 @@ type KResourceList struct { Items []KResource `json:"items"` } -// GetTypeMeta retrieves the ObjectMeta of the KResource. Implements the KRShaped interface. -func (t *KResource) GetTypeMeta() *metav1.TypeMeta { - return &t.TypeMeta -} - // GetStatus retrieves the status of the KResource. Implements the KRShaped interface. func (t *KResource) GetStatus() *Status { return &t.Status From 3f3fda0f8f3670e98d8ce0c3eb59ca2ca5b015cb Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Wed, 13 May 2020 08:57:28 -0700 Subject: [PATCH 07/10] remove unessecary unit tests --- pkg/apis/serving/v1/configuration_types_test.go | 11 ----------- pkg/apis/serving/v1/revision_types_test.go | 11 ----------- pkg/apis/serving/v1/route_types_test.go | 11 ----------- pkg/apis/serving/v1/service_types_test.go | 11 ----------- 4 files changed, 44 deletions(-) diff --git a/pkg/apis/serving/v1/configuration_types_test.go b/pkg/apis/serving/v1/configuration_types_test.go index 4aa53875f08b..e4b84e515d78 100644 --- a/pkg/apis/serving/v1/configuration_types_test.go +++ b/pkg/apis/serving/v1/configuration_types_test.go @@ -18,7 +18,6 @@ package v1 import ( "testing" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" ) @@ -33,13 +32,3 @@ func TestIsConfigurationCondition(t *testing.T) { t.Error("Expected to be a configuration type") } } - -func TestConfigurationGetObjectMeta(t *testing.T) { - r := &Configuration{ - TypeMeta: metav1.TypeMeta{}, - } - - if got, want := r.GetTypeMeta(), &r.TypeMeta; got != want { - t.Errorf("GotTypeMeta=%v, want=%v", got, want) - } -} diff --git a/pkg/apis/serving/v1/revision_types_test.go b/pkg/apis/serving/v1/revision_types_test.go index 72d0551eece1..16f6f7e35c90 100644 --- a/pkg/apis/serving/v1/revision_types_test.go +++ b/pkg/apis/serving/v1/revision_types_test.go @@ -18,7 +18,6 @@ package v1 import ( "testing" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" ) @@ -33,13 +32,3 @@ func TestIsRevisionCondition(t *testing.T) { t.Error("Expected to be a revision type") } } - -func TestRevisionGetObjectMeta(t *testing.T) { - r := &Revision{ - TypeMeta: metav1.TypeMeta{}, - } - - if got, want := r.GetTypeMeta(), &r.TypeMeta; got != want { - t.Errorf("GotTypeMeta=%v, want=%v", got, want) - } -} diff --git a/pkg/apis/serving/v1/route_types_test.go b/pkg/apis/serving/v1/route_types_test.go index dd6e3aa6106b..941048e0688e 100644 --- a/pkg/apis/serving/v1/route_types_test.go +++ b/pkg/apis/serving/v1/route_types_test.go @@ -18,7 +18,6 @@ package v1 import ( "testing" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" ) @@ -33,13 +32,3 @@ func TestIsRouteCondition(t *testing.T) { t.Error("Expected to be a route type") } } - -func TestRouteGetObjectMeta(t *testing.T) { - r := &Route{ - TypeMeta: metav1.TypeMeta{}, - } - - if got, want := r.GetTypeMeta(), &r.TypeMeta; got != want { - t.Errorf("GotTypeMeta=%v, want=%v", got, want) - } -} diff --git a/pkg/apis/serving/v1/service_types_test.go b/pkg/apis/serving/v1/service_types_test.go index 94b75a1c4da5..196da500b8da 100644 --- a/pkg/apis/serving/v1/service_types_test.go +++ b/pkg/apis/serving/v1/service_types_test.go @@ -18,7 +18,6 @@ package v1 import ( "testing" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "knative.dev/pkg/apis" ) @@ -33,13 +32,3 @@ func TestIsServiceCondition(t *testing.T) { t.Error("Expected to be a service type") } } - -func TestServiceGetObjectMeta(t *testing.T) { - r := &Service{ - TypeMeta: metav1.TypeMeta{}, - } - - if got, want := r.GetTypeMeta(), &r.TypeMeta; got != want { - t.Errorf("GotTypeMeta=%v, want=%v", got, want) - } -} From 19065dd322e79314cfeb93e20246b392bf2ccdfc Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Fri, 15 May 2020 07:14:05 -0700 Subject: [PATCH 08/10] update deps --- go.mod | 6 +- go.sum | 12 +- .../pkg/apis/duck/v1/kresource_type.go | 2 +- vendor/knative.dev/pkg/configmap/parse.go | 112 ++++++++++++++++++ vendor/knative.dev/pkg/metrics/config.go | 16 +-- .../pkg/reconciler/reconcile_common.go | 4 +- vendor/knative.dev/pkg/test/kube_checks.go | 52 ++++++++ .../knative.dev/test-infra/scripts/library.sh | 6 +- .../scripts/shellcheck-presubmit.sh | 14 +++ vendor/modules.txt | 6 +- 10 files changed, 206 insertions(+), 24 deletions(-) create mode 100644 vendor/knative.dev/pkg/configmap/parse.go diff --git a/go.mod b/go.mod index 4b14649c1fa6..a4bcc1d44dec 100644 --- a/go.mod +++ b/go.mod @@ -43,9 +43,9 @@ require ( k8s.io/code-generator v0.18.0 k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a k8s.io/metrics v0.17.2 - knative.dev/caching v0.0.0-20200513171358-85661149a26b - knative.dev/pkg v0.0.0-20200514052058-c75d324f8b8b - knative.dev/test-infra v0.0.0-20200513224158-2b7ecf0da961 + knative.dev/caching v0.0.0-20200514160600-0bfd3042f06e + knative.dev/pkg v0.0.0-20200515002500-16d7b963416f + knative.dev/test-infra v0.0.0-20200514223200-ef4fd3ad398f ) replace ( diff --git a/go.sum b/go.sum index 8841883eb492..277bf704073f 100644 --- a/go.sum +++ b/go.sum @@ -1430,26 +1430,26 @@ k8s.io/utils v0.0.0-20191114184206-e782cd3c129f/go.mod h1:sZAwmy6armz5eXlNoLmJcl k8s.io/utils v0.0.0-20200124190032-861946025e34 h1:HjlUD6M0K3P8nRXmr2B9o4F9dUy9TCj/aEpReeyi6+k= k8s.io/utils v0.0.0-20200124190032-861946025e34/go.mod h1:sZAwmy6armz5eXlNoLmJcl4F1QuKu7sr+mFQ0byX7Ew= knative.dev/caching v0.0.0-20190719140829-2032732871ff/go.mod h1:dHXFU6CGlLlbzaWc32g80cR92iuBSpsslDNBWI8C7eg= -knative.dev/caching v0.0.0-20200513171358-85661149a26b h1:bo//a1fb5a6Hk5JiTMVwXNOBEcKwxjX9thRsHAQfrHo= -knative.dev/caching v0.0.0-20200513171358-85661149a26b/go.mod h1:stT6dupvTjnWLN5FMsMwWEH7nYgyk0TM9YLtZ3y9ngA= +knative.dev/caching v0.0.0-20200514160600-0bfd3042f06e h1:lxv79/KmbOX58sPa3QNETXXYA/v+VjRI0rO0svceuI8= +knative.dev/caching v0.0.0-20200514160600-0bfd3042f06e/go.mod h1:G0HrGdj/gvxo2ymHRTFKyTDTJ+ODkgetTYKOcpEkvto= knative.dev/eventing-contrib v0.6.1-0.20190723221543-5ce18048c08b/go.mod h1:SnXZgSGgMSMLNFTwTnpaOH7hXDzTFtw0J8OmHflNx3g= knative.dev/pkg v0.0.0-20191101194912-56c2594e4f11/go.mod h1:pgODObA1dTyhNoFxPZTTjNWfx6F0aKsKzn+vaT9XO/Q= knative.dev/pkg v0.0.0-20191111150521-6d806b998379/go.mod h1:pgODObA1dTyhNoFxPZTTjNWfx6F0aKsKzn+vaT9XO/Q= knative.dev/pkg v0.0.0-20200428194351-90fc61bae7f7/go.mod h1:o+e8OVEJKIuvXPsGVPIautjXgs05xbos7G+QMRjuUps= knative.dev/pkg v0.0.0-20200505191044-3da93ebb24c2 h1:Qu2NlOHb9p3g+CSL/ok9+FySowN60URFEKRSXfWtDv4= knative.dev/pkg v0.0.0-20200505191044-3da93ebb24c2/go.mod h1:Q6sL35DdGs8hIQZKdaCXJGgY8f90BmNBKSb8z6d/BTM= -knative.dev/pkg v0.0.0-20200512191130-b10849aacf82 h1:fNi8SJwQ5ADR0vj5EEpAcgdqlMKG1Ukym6BROixBLcc= -knative.dev/pkg v0.0.0-20200512191130-b10849aacf82/go.mod h1:gv+6TBl/yLc/x2dpVfmjF+MWLbxWRm9ie5dJRASsPmw= knative.dev/pkg v0.0.0-20200514052058-c75d324f8b8b h1:lM0uwBgNcS9S4cWLqIS6Fvjt3kR/6oAPXgbXfEzBJwU= knative.dev/pkg v0.0.0-20200514052058-c75d324f8b8b/go.mod h1:tMOHGbxtRz8zYFGEGpV/bpoTEM1o89MwYFC4YJXl3GY= +knative.dev/pkg v0.0.0-20200515002500-16d7b963416f h1:kcpAMvYUqftHMA69wZ7g83zEW4y8cdnqfdJsSPOlrJQ= +knative.dev/pkg v0.0.0-20200515002500-16d7b963416f/go.mod h1:tMOHGbxtRz8zYFGEGpV/bpoTEM1o89MwYFC4YJXl3GY= knative.dev/test-infra v0.0.0-20200407185800-1b88cb3b45a5/go.mod h1:xcdUkMJrLlBswIZqL5zCuBFOC22WIPMQoVX1L35i0vQ= knative.dev/test-infra v0.0.0-20200505052144-5ea2f705bb55/go.mod h1:WqF1Azka+FxPZ20keR2zCNtiQA1MP9ZB4BH4HuI+SIU= -knative.dev/test-infra v0.0.0-20200509000045-c7114387eed5 h1:4lQU2AMChE3Hb2WgHDFfhRbYlITCMUgQ2Y6eeIIEgxM= -knative.dev/test-infra v0.0.0-20200509000045-c7114387eed5/go.mod h1:aMif0KXL4g19YCYwsy4Ocjjz5xgPlseYV+B95Oo4JGE= knative.dev/test-infra v0.0.0-20200513011557-d03429a76034 h1:JxqONCZVS7or+Fv3ebVQoipuIBH7Ig3Qbx170hgIF+A= knative.dev/test-infra v0.0.0-20200513011557-d03429a76034/go.mod h1:aMif0KXL4g19YCYwsy4Ocjjz5xgPlseYV+B95Oo4JGE= knative.dev/test-infra v0.0.0-20200513224158-2b7ecf0da961 h1:idvJyBTh6J2owRgJgwuC0Y8BBEiCJf02IfK6+Vk0U7c= knative.dev/test-infra v0.0.0-20200513224158-2b7ecf0da961/go.mod h1:aMif0KXL4g19YCYwsy4Ocjjz5xgPlseYV+B95Oo4JGE= +knative.dev/test-infra v0.0.0-20200514223200-ef4fd3ad398f h1:2j9xK15xMRz5h+yXIHt19iBAhWoEzRlZjSNurXzp4Nc= +knative.dev/test-infra v0.0.0-20200514223200-ef4fd3ad398f/go.mod h1:+uml4upluwrnWHRSezi4WVcKJ0OJHynF16Ot7fu0ky4= modernc.org/cc v1.0.0/go.mod h1:1Sk4//wdnYJiUIxnW8ddKpaOJCF37yAdqYnkxUpaYxw= modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk= modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k= diff --git a/vendor/knative.dev/pkg/apis/duck/v1/kresource_type.go b/vendor/knative.dev/pkg/apis/duck/v1/kresource_type.go index 20bf0200f534..78d8ece66a99 100644 --- a/vendor/knative.dev/pkg/apis/duck/v1/kresource_type.go +++ b/vendor/knative.dev/pkg/apis/duck/v1/kresource_type.go @@ -29,7 +29,7 @@ import ( // KRShaped is an interface for retrieving the duck elements of an arbitrary resource. type KRShaped interface { - metav1.ObjectMetaAccessor + metav1.Object schema.ObjectKind GetStatus() *Status diff --git a/vendor/knative.dev/pkg/configmap/parse.go b/vendor/knative.dev/pkg/configmap/parse.go new file mode 100644 index 000000000000..097b38f90b3f --- /dev/null +++ b/vendor/knative.dev/pkg/configmap/parse.go @@ -0,0 +1,112 @@ +/* +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 configmap + +import ( + "strconv" + "strings" + "time" +) + +// ParseFunc is a function taking ConfigMap data and applying a parse operation to it. +type ParseFunc func(map[string]string) error + +// AsString passes the value at key through into the target, if it exists. +func AsString(key string, target *string) ParseFunc { + return func(data map[string]string) error { + if raw, ok := data[key]; ok { + *target = raw + } + return nil + } +} + +// AsBool parses the value at key as a boolean into the target, if it exists. +func AsBool(key string, target *bool) ParseFunc { + return func(data map[string]string) error { + if raw, ok := data[key]; ok { + *target = strings.EqualFold(raw, "true") + } + return nil + } +} + +// AsInt32 parses the value at key as an int32 into the target, if it exists. +func AsInt32(key string, target *int32) ParseFunc { + return func(data map[string]string) error { + if raw, ok := data[key]; ok { + val, err := strconv.ParseInt(raw, 10, 32) + if err != nil { + return err + } + *target = int32(val) + } + return nil + } +} + +// AsInt64 parses the value at key as an int64 into the target, if it exists. +func AsInt64(key string, target *int64) ParseFunc { + return func(data map[string]string) error { + if raw, ok := data[key]; ok { + val, err := strconv.ParseInt(raw, 10, 64) + if err != nil { + return err + } + *target = val + } + return nil + } +} + +// AsFloat64 parses the value at key as a float64 into the target, if it exists. +func AsFloat64(key string, target *float64) ParseFunc { + return func(data map[string]string) error { + if raw, ok := data[key]; ok { + val, err := strconv.ParseFloat(raw, 64) + if err != nil { + return err + } + *target = val + } + return nil + } +} + +// AsDuration parses the value at key as a time.Duration into the target, if it exists. +func AsDuration(key string, target *time.Duration) ParseFunc { + return func(data map[string]string) error { + if raw, ok := data[key]; ok { + val, err := time.ParseDuration(raw) + if err != nil { + return err + } + *target = val + } + return nil + } +} + +// Parse parses the given map using the parser functions passed in. +func Parse(data map[string]string, parsers ...ParseFunc) error { + for _, parse := range parsers { + if err := parse(data); err != nil { + return err + } + } + return nil +} diff --git a/vendor/knative.dev/pkg/metrics/config.go b/vendor/knative.dev/pkg/metrics/config.go index a752e8d375a1..c7426e342861 100644 --- a/vendor/knative.dev/pkg/metrics/config.go +++ b/vendor/knative.dev/pkg/metrics/config.go @@ -26,6 +26,7 @@ import ( "path" "strconv" "strings" + "sync" "time" "go.opencensus.io/stats" @@ -34,10 +35,6 @@ import ( "knative.dev/pkg/metrics/metricskey" ) -const ( - DomainEnv = "METRICS_DOMAIN" -) - // metricsBackend specifies the backend to use for metrics type metricsBackend string @@ -55,6 +52,8 @@ const ( StackdriverClusterNameKey = "metrics.stackdriver-cluster-name" StackdriverUseSecretKey = "metrics.stackdriver-use-secret" + DomainEnv = "METRICS_DOMAIN" + // Stackdriver is used for Stackdriver backend Stackdriver metricsBackend = "stackdriver" // Prometheus is used for Prometheus backend @@ -120,6 +119,8 @@ type metricsConfig struct { stackdriverClientConfig StackdriverClientConfig } +var logOnce sync.Once + // StackdriverClientConfig encapsulates the metadata required to configure a Stackdriver client. type StackdriverClientConfig struct { // ProjectID is the stackdriver project ID to which data is uploaded. @@ -155,14 +156,15 @@ func NewStackdriverClientConfigFromMap(config map[string]string) *StackdriverCli // measurements in the metricsConfig's designated backend. func (mc *metricsConfig) record(ctx context.Context, mss []stats.Measurement, ros ...stats.Options) error { if mc == nil { - log.Println(`The metricsConfig has not been initialized yet. - + logOnce.Do(func() { + log.Println(`The metricsConfig has not been initialized yet. If this is a Go unit test consuming metric.Record(...) or metric.RecordBatch(...) then it should add the following import: - import ( _ "knative.dev/pkg/metrics/testing" )`) + }) + // Don't record data points if the metric config is not initialized yet. // At this point, it's unclear whether should record or not. return nil diff --git a/vendor/knative.dev/pkg/reconciler/reconcile_common.go b/vendor/knative.dev/pkg/reconciler/reconcile_common.go index 38e3bbe38334..8af08f61afff 100644 --- a/vendor/knative.dev/pkg/reconciler/reconcile_common.go +++ b/vendor/knative.dev/pkg/reconciler/reconcile_common.go @@ -29,7 +29,7 @@ const failedGenerationBump = "NewObservedGenFailure" func PreProcessReconcile(ctx context.Context, resource duckv1.KRShaped) { newStatus := resource.GetStatus() - if newStatus.ObservedGeneration != resource.GetObjectMeta().GetGeneration() { + if newStatus.ObservedGeneration != resource.GetGeneration() { condSet := resource.GetConditionSet() manager := condSet.Manage(newStatus) @@ -46,7 +46,7 @@ func PostProcessReconcile(ctx context.Context, resource duckv1.KRShaped) { // Bump observed generation to denote that we have processed this // generation regardless of success or failure. - newStatus.ObservedGeneration = resource.GetObjectMeta().GetGeneration() + newStatus.ObservedGeneration = resource.GetGeneration() rc := mgr.GetTopLevelCondition() if rc.Reason == failedGenerationBump { diff --git a/vendor/knative.dev/pkg/test/kube_checks.go b/vendor/knative.dev/pkg/test/kube_checks.go index 35bcbd201bb6..61a259ac96e2 100644 --- a/vendor/knative.dev/pkg/test/kube_checks.go +++ b/vendor/knative.dev/pkg/test/kube_checks.go @@ -25,8 +25,10 @@ import ( "strings" "time" + "github.com/google/go-cmp/cmp" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" + apierrs "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/util/wait" k8styped "k8s.io/client-go/kubernetes/typed/core/v1" @@ -93,6 +95,19 @@ func WaitForPodState(client *KubeClient, inState func(p *corev1.Pod) (bool, erro }) } +// WaitForPodDeleted waits for the given pod to disappear from the given namespace. +func WaitForPodDeleted(client *KubeClient, name, namespace string) error { + if err := WaitForPodState(client, func(p *corev1.Pod) (bool, error) { + // Always return false. We're oly interested in the error which indicates pod deletion or timeout. + return false, nil + }, name, namespace); err != nil { + if !apierrs.IsNotFound(err) { + return err + } + } + return nil +} + // WaitForServiceHasAtLeastOneEndpoint polls the status of the specified Service // from client every interval until number of service endpoints = numOfEndpoints func WaitForServiceEndpoints(client *KubeClient, svcName string, svcNamespace string, numOfEndpoints int) error { @@ -121,6 +136,29 @@ func countEndpointsNum(e *corev1.Endpoints) int { return num } +// GetEndpointAddresses returns addresses of endpoints for the given service. +func GetEndpointAddresses(client *KubeClient, svcName, svcNamespace string) ([]string, error) { + endpoints, err := client.Kube.CoreV1().Endpoints(svcNamespace).Get(svcName, metav1.GetOptions{}) + if err != nil || countEndpointsNum(endpoints) == 0 { + return nil, fmt.Errorf("no endpoints or error: %w", err) + } + var hosts []string + for _, sub := range endpoints.Subsets { + for _, addr := range sub.Addresses { + hosts = append(hosts, addr.IP) + } + } + return hosts, nil +} + +// WaitForChangedEndpoints waits until the endpoints for the given service differ from origEndpoints. +func WaitForChangedEndpoints(client *KubeClient, svcName, svcNamespace string, origEndpoints []string) error { + return wait.PollImmediate(1*time.Second, 2*time.Minute, func() (bool, error) { + newEndpoints, err := GetEndpointAddresses(client, svcName, svcNamespace) + return !cmp.Equal(origEndpoints, newEndpoints), err + }) +} + // GetConfigMap gets the configmaps for a given namespace func GetConfigMap(client *KubeClient, namespace string) k8styped.ConfigMapInterface { return client.Kube.CoreV1().ConfigMaps(namespace) @@ -176,3 +214,17 @@ func PodsRunning(podList *corev1.PodList) (bool, error) { func PodRunning(pod *corev1.Pod) bool { return pod.Status.Phase == corev1.PodRunning || pod.Status.Phase == corev1.PodSucceeded } + +// WaitForDeploymentScale waits until the given deployment has the expected scale. +func WaitForDeploymentScale(client *KubeClient, name, namespace string, scale int) error { + return WaitForDeploymentState( + client, + name, + func(d *appsv1.Deployment) (bool, error) { + return d.Status.ReadyReplicas == int32(scale), nil + }, + "DeploymentIsScaled", + namespace, + time.Minute, + ) +} diff --git a/vendor/knative.dev/test-infra/scripts/library.sh b/vendor/knative.dev/test-infra/scripts/library.sh index 0b3927419449..092f39811989 100644 --- a/vendor/knative.dev/test-infra/scripts/library.sh +++ b/vendor/knative.dev/test-infra/scripts/library.sh @@ -544,7 +544,8 @@ function update_licenses() { local dst=$1 local dir=$2 shift - run_go_tool github.com/google/go-licenses go-licenses save "${dir}" --save_path="${dst}" --force || return 1 + run_go_tool github.com/google/go-licenses go-licenses save "${dir}" --save_path="${dst}" --force || \ + { echo "--- FAIL: go-licenses failed to update licenses"; return 1; } # Hack to make sure directories retain write permissions after save. This # can happen if the directory being copied is a Go module. # See https://github.com/google/go-licenses/issues/11 @@ -554,7 +555,8 @@ function update_licenses() { # Run go-licenses to check for forbidden licenses. function check_licenses() { # Check that we don't have any forbidden licenses. - run_go_tool github.com/google/go-licenses go-licenses check "${REPO_ROOT_DIR}/..." || return 1 + run_go_tool github.com/google/go-licenses go-licenses check "${REPO_ROOT_DIR}/..." || \ + { echo "--- FAIL: go-licenses failed the license check"; return 1; } } # Run the given linter on the given files, checking it exists first. diff --git a/vendor/knative.dev/test-infra/scripts/shellcheck-presubmit.sh b/vendor/knative.dev/test-infra/scripts/shellcheck-presubmit.sh index ce1515bd3543..d9c48129af39 100644 --- a/vendor/knative.dev/test-infra/scripts/shellcheck-presubmit.sh +++ b/vendor/knative.dev/test-infra/scripts/shellcheck-presubmit.sh @@ -1,5 +1,19 @@ #!/usr/bin/env bash +# 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. + set -e source "$(dirname "${BASH_SOURCE[0]}")/library.sh" diff --git a/vendor/modules.txt b/vendor/modules.txt index 91587308b03f..d8d39c1cf194 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1148,7 +1148,7 @@ k8s.io/utils/integer k8s.io/utils/path k8s.io/utils/pointer k8s.io/utils/trace -# knative.dev/caching v0.0.0-20200513171358-85661149a26b +# knative.dev/caching v0.0.0-20200514160600-0bfd3042f06e ## explicit knative.dev/caching/config knative.dev/caching/pkg/apis/caching @@ -1169,7 +1169,7 @@ knative.dev/caching/pkg/client/injection/informers/caching/v1alpha1/image/fake knative.dev/caching/pkg/client/injection/informers/factory knative.dev/caching/pkg/client/injection/informers/factory/fake knative.dev/caching/pkg/client/listers/caching/v1alpha1 -# knative.dev/pkg v0.0.0-20200514052058-c75d324f8b8b +# knative.dev/pkg v0.0.0-20200515002500-16d7b963416f ## explicit knative.dev/pkg/apiextensions/storageversion knative.dev/pkg/apiextensions/storageversion/cmd/migrate @@ -1284,7 +1284,7 @@ knative.dev/pkg/webhook/resourcesemantics/conversion knative.dev/pkg/webhook/resourcesemantics/defaulting knative.dev/pkg/webhook/resourcesemantics/validation knative.dev/pkg/websocket -# knative.dev/test-infra v0.0.0-20200513224158-2b7ecf0da961 +# knative.dev/test-infra v0.0.0-20200514223200-ef4fd3ad398f ## explicit knative.dev/test-infra/scripts # sigs.k8s.io/structured-merge-diff v1.0.1 From 2b7c2e52a3cff7d2c27108df8e5e249d21f5d01c Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Mon, 18 May 2020 10:21:36 -0700 Subject: [PATCH 09/10] unmerge --- vendor/knative.dev/pkg/metrics/config.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/vendor/knative.dev/pkg/metrics/config.go b/vendor/knative.dev/pkg/metrics/config.go index 25e91d398623..b9c5aa29a76f 100644 --- a/vendor/knative.dev/pkg/metrics/config.go +++ b/vendor/knative.dev/pkg/metrics/config.go @@ -25,7 +25,6 @@ import ( "path" "strconv" "strings" - "sync" "time" "go.opencensus.io/stats" @@ -118,8 +117,6 @@ type metricsConfig struct { stackdriverClientConfig StackdriverClientConfig } -var logOnce sync.Once - // StackdriverClientConfig encapsulates the metadata required to configure a Stackdriver client. type StackdriverClientConfig struct { // ProjectID is the stackdriver project ID to which data is uploaded. From ef4bff1db0e3153f47b2dcc6d11048566ed7fdd4 Mon Sep 17 00:00:00 2001 From: Weston Haught Date: Mon, 18 May 2020 11:04:59 -0700 Subject: [PATCH 10/10] include getStatus test --- pkg/apis/serving/v1/configuration_types_test.go | 15 +++++++++++++++ pkg/apis/serving/v1/revision_types_test.go | 15 +++++++++++++++ pkg/apis/serving/v1/route_types_test.go | 15 +++++++++++++++ pkg/apis/serving/v1/service_types_test.go | 15 +++++++++++++++ 4 files changed, 60 insertions(+) diff --git a/pkg/apis/serving/v1/configuration_types_test.go b/pkg/apis/serving/v1/configuration_types_test.go index e4b84e515d78..8cc5ea3507f1 100644 --- a/pkg/apis/serving/v1/configuration_types_test.go +++ b/pkg/apis/serving/v1/configuration_types_test.go @@ -18,7 +18,9 @@ package v1 import ( "testing" + "github.com/google/go-cmp/cmp" "knative.dev/pkg/apis" + duckv1 "knative.dev/pkg/apis/duck/v1" ) func TestIsConfigurationCondition(t *testing.T) { @@ -32,3 +34,16 @@ func TestIsConfigurationCondition(t *testing.T) { t.Error("Expected to be a configuration type") } } + +func TestConfigurationGetStatus(t *testing.T) { + status := &duckv1.Status{} + config := Configuration{ + Status: ConfigurationStatus{ + Status: *status, + }, + } + + if !cmp.Equal(config.GetStatus(), status) { + t.Errorf("GetStatus did not retrieve status. Got=%v Want=%v", config.GetStatus(), status) + } +} diff --git a/pkg/apis/serving/v1/revision_types_test.go b/pkg/apis/serving/v1/revision_types_test.go index 16f6f7e35c90..d0b6d48b32d8 100644 --- a/pkg/apis/serving/v1/revision_types_test.go +++ b/pkg/apis/serving/v1/revision_types_test.go @@ -18,7 +18,9 @@ package v1 import ( "testing" + "github.com/google/go-cmp/cmp" "knative.dev/pkg/apis" + duckv1 "knative.dev/pkg/apis/duck/v1" ) func TestIsRevisionCondition(t *testing.T) { @@ -32,3 +34,16 @@ func TestIsRevisionCondition(t *testing.T) { t.Error("Expected to be a revision type") } } + +func TestRevisionGetStatus(t *testing.T) { + status := &duckv1.Status{} + config := Revision{ + Status: RevisionStatus{ + Status: *status, + }, + } + + if !cmp.Equal(config.GetStatus(), status) { + t.Errorf("GetStatus did not retrieve status. Got=%v Want=%v", config.GetStatus(), status) + } +} diff --git a/pkg/apis/serving/v1/route_types_test.go b/pkg/apis/serving/v1/route_types_test.go index 941048e0688e..11ae39b2f47d 100644 --- a/pkg/apis/serving/v1/route_types_test.go +++ b/pkg/apis/serving/v1/route_types_test.go @@ -18,7 +18,9 @@ package v1 import ( "testing" + "github.com/google/go-cmp/cmp" "knative.dev/pkg/apis" + duckv1 "knative.dev/pkg/apis/duck/v1" ) func TestIsRouteCondition(t *testing.T) { @@ -32,3 +34,16 @@ func TestIsRouteCondition(t *testing.T) { t.Error("Expected to be a route type") } } + +func TestRouteGetStatus(t *testing.T) { + status := &duckv1.Status{} + config := Route{ + Status: RouteStatus{ + Status: *status, + }, + } + + if !cmp.Equal(config.GetStatus(), status) { + t.Errorf("GetStatus did not retrieve status. Got=%v Want=%v", config.GetStatus(), status) + } +} diff --git a/pkg/apis/serving/v1/service_types_test.go b/pkg/apis/serving/v1/service_types_test.go index 196da500b8da..786f5c952af0 100644 --- a/pkg/apis/serving/v1/service_types_test.go +++ b/pkg/apis/serving/v1/service_types_test.go @@ -18,7 +18,9 @@ package v1 import ( "testing" + "github.com/google/go-cmp/cmp" "knative.dev/pkg/apis" + duckv1 "knative.dev/pkg/apis/duck/v1" ) func TestIsServiceCondition(t *testing.T) { @@ -32,3 +34,16 @@ func TestIsServiceCondition(t *testing.T) { t.Error("Expected to be a service type") } } + +func TestServiceGetStatus(t *testing.T) { + status := &duckv1.Status{} + config := Service{ + Status: ServiceStatus{ + Status: *status, + }, + } + + if !cmp.Equal(config.GetStatus(), status) { + t.Errorf("GetStatus did not retrieve status. Got=%v Want=%v", config.GetStatus(), status) + } +}