diff --git a/hack/update-codegen.sh b/hack/update-codegen.sh index 624bcbd6c44e..b5b30c01f33c 100755 --- a/hack/update-codegen.sh +++ b/hack/update-codegen.sh @@ -30,5 +30,13 @@ ${CODEGEN_PKG}/generate-groups.sh "deepcopy,client,informer,lister" \ "serving:v1alpha1 istio:v1alpha2" \ --go-header-file ${SERVING_ROOT}/hack/boilerplate/boilerplate.go.txt +# Generate defaulters manually because generate-groups.sh doesn't, contrary to +# the usage docs. This can be removed when/if generate-groups.sh allows adding +# "defaulter" to the list of generators. +${GOPATH}/bin/defaulter-gen \ + --input-dirs github.com/knative/serving/pkg/apis/serving/v1alpha1 \ + -O zz_generated.defaulters \ + --go-header-file ${SERVING_ROOT}/hack/boilerplate/boilerplate.go.txt + # Make sure our dependencies are up-to-date ${SERVING_ROOT}/hack/update-deps.sh diff --git a/pkg/apis/serving/v1alpha1/defaults.go b/pkg/apis/serving/v1alpha1/defaults.go new file mode 100644 index 000000000000..2231c7839ad7 --- /dev/null +++ b/pkg/apis/serving/v1alpha1/defaults.go @@ -0,0 +1,48 @@ +/* +Copyright 2018 Google LLC. + +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 ( + "k8s.io/apimachinery/pkg/runtime" +) + +// Use these defaulting functions by calling the Default() method on the Scheme +// var in the generated clientset scheme package. +// +// Example: +// +// import ( +// "thisrepo/pkg/client/clientset/versioned/scheme" +// "thisrepo/pkg/apis/whatever/v1" +// ) +// func main() { +// obj := &v1.SomeObject{} +// scheme.Scheme.Default(obj) +// } + +func addDefaultingFuncs(scheme *runtime.Scheme) error { + return RegisterDefaults(scheme) +} + +func SetDefaults_Revision(rev *Revision) { + if rev.Spec.ServingState == "" { + rev.Spec.ServingState = RevisionServingStateActive + } + if rev.Spec.ConcurrencyModel == "" { + rev.Spec.ConcurrencyModel = RevisionRequestConcurrencyModelMulti + } +} diff --git a/pkg/apis/serving/v1alpha1/defaults_test.go b/pkg/apis/serving/v1alpha1/defaults_test.go new file mode 100644 index 000000000000..a56edded43c0 --- /dev/null +++ b/pkg/apis/serving/v1alpha1/defaults_test.go @@ -0,0 +1,59 @@ +/* +Copyright 2018 Google LLC. All rights reserved. +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" + + "github.com/google/go-cmp/cmp" + "k8s.io/apimachinery/pkg/runtime" +) + +var testCases = []struct { + name string + before runtime.Object + after runtime.Object +}{ + + { + name: "Revision", + before: &Revision{}, + after: &Revision{ + Spec: RevisionSpec{ + ServingState: RevisionServingStateActive, + ConcurrencyModel: RevisionRequestConcurrencyModelMulti, + }, + }, + }, +} + +func TestDefaults(t *testing.T) { + var testScheme = getTestScheme() + + for _, tc := range testCases { + testScheme.Default(tc.before) + if diff := cmp.Diff(tc.after, tc.before); diff != "" { + t.Errorf("%s: Unexpected default (-want +got): %v", tc.name, diff) + } + } +} + +// We can't import the generated scheme because it depends on this package, +// creating an import cycle. +func getTestScheme() *runtime.Scheme { + scheme := runtime.NewScheme() + if err := AddToScheme(scheme); err != nil { + panic(err) + } + return scheme +} diff --git a/pkg/apis/serving/v1alpha1/doc.go b/pkg/apis/serving/v1alpha1/doc.go index 89ac34ba6f0d..755689881f43 100644 --- a/pkg/apis/serving/v1alpha1/doc.go +++ b/pkg/apis/serving/v1alpha1/doc.go @@ -20,4 +20,5 @@ limitations under the License. // +k8s:deepcopy-gen=package // +groupName=serving.knative.dev +// +k8s:defaulter-gen=TypeMeta package v1alpha1 diff --git a/pkg/apis/serving/v1alpha1/register.go b/pkg/apis/serving/v1alpha1/register.go index b7b8e51fefa1..d4d9e9c915ec 100644 --- a/pkg/apis/serving/v1alpha1/register.go +++ b/pkg/apis/serving/v1alpha1/register.go @@ -38,7 +38,7 @@ func Resource(resource string) schema.GroupResource { } var ( - SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes) + SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes, addDefaultingFuncs) AddToScheme = SchemeBuilder.AddToScheme ) diff --git a/pkg/apis/serving/v1alpha1/zz_generated.defaulters.go b/pkg/apis/serving/v1alpha1/zz_generated.defaulters.go new file mode 100644 index 000000000000..4cf285435295 --- /dev/null +++ b/pkg/apis/serving/v1alpha1/zz_generated.defaulters.go @@ -0,0 +1,45 @@ +// +build !ignore_autogenerated + +/* +Copyright 2018 Google LLC + +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. +*/ + +// This file was autogenerated by defaulter-gen. Do not edit it manually! + +package v1alpha1 + +import ( + runtime "k8s.io/apimachinery/pkg/runtime" +) + +// RegisterDefaults adds defaulters functions to the given scheme. +// Public to allow building arbitrary schemes. +// All generated defaulters are covering - they call all nested defaulters. +func RegisterDefaults(scheme *runtime.Scheme) error { + scheme.AddTypeDefaultingFunc(&Revision{}, func(obj interface{}) { SetObjectDefaults_Revision(obj.(*Revision)) }) + scheme.AddTypeDefaultingFunc(&RevisionList{}, func(obj interface{}) { SetObjectDefaults_RevisionList(obj.(*RevisionList)) }) + return nil +} + +func SetObjectDefaults_Revision(in *Revision) { + SetDefaults_Revision(in) +} + +func SetObjectDefaults_RevisionList(in *RevisionList) { + for i := range in.Items { + a := &in.Items[i] + SetObjectDefaults_Revision(a) + } +}