-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from all commits
94c5ddd
ead63cf
cd58ee1
03a555a
8143387
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
if rev.Spec.ServingState == "" { | ||
rev.Spec.ServingState = RevisionServingStateActive | ||
} | ||
if rev.Spec.ConcurrencyModel == "" { | ||
rev.Spec.ConcurrencyModel = RevisionRequestConcurrencyModelMulti | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you want to move the call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm thinking about it. Maybe in a separate PR. |
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 byaddDefaultingFuncs
which is given to SchemeBuilder as a scheme building function. I'm not sure whyRegisterDefaults
can't be given to SchemeBuilder directly, but all the examples I found (The rbac types, among others) do theaddDefaultingFuncs
thing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://www.youtube.com/watch?v=b6zEaA-nAlU