Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated defaulters for Revision #1236

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
48 changes: 48 additions & 0 deletions pkg/apis/serving/v1alpha1/defaults.go
Original file line number Diff line number Diff line change
@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this function discovered?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow defaulter-gen knows that this function exists and generates code that sets up the proper hooks for it. See RegisterDefaults in https://github.com/knative/serving/pull/1236/files#diff-dee345d24fd055f2eeae84b3a53d1ea5R31.

RegisterDefaults is in turn called by addDefaultingFuncs which is given to SchemeBuilder as a scheme building function. I'm not sure why RegisterDefaults can't be given to SchemeBuilder directly, but all the examples I found (The rbac types, among others) do the addDefaultingFuncs thing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if rev.Spec.ServingState == "" {
rev.Spec.ServingState = RevisionServingStateActive
}
if rev.Spec.ConcurrencyModel == "" {
rev.Spec.ConcurrencyModel = RevisionRequestConcurrencyModelMulti
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you want to move the call to rev.Status.InitializeConditions to here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking about it. Maybe in a separate PR.

59 changes: 59 additions & 0 deletions pkg/apis/serving/v1alpha1/defaults_test.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions pkg/apis/serving/v1alpha1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ limitations under the License.

// +k8s:deepcopy-gen=package
// +groupName=serving.knative.dev
// +k8s:defaulter-gen=TypeMeta
package v1alpha1
2 changes: 1 addition & 1 deletion pkg/apis/serving/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Resource(resource string) schema.GroupResource {
}

var (
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes, addDefaultingFuncs)
AddToScheme = SchemeBuilder.AddToScheme
)

Expand Down
45 changes: 45 additions & 0 deletions pkg/apis/serving/v1alpha1/zz_generated.defaulters.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.