Merge pull request #67207 from luxas/remove_shared_config_defaulting

Automatic merge from submit-queue (batch tested with PRs 66602, 67178, 67207, 67125, 66332). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Remove defaulting from shared ComponentConfig types

**What this PR does / why we need it**:

As @deads2k commented in kubernetes/community#2354, we should not register defaults for the shared componentconfig types as it gets very hard for consumer to opt-out of the default defaulting funcs. Instead, the package provides a  `DefaultFoo` function the consuming API group can call if it wants to as an opt-in in `SetDefaults_Bar` (where `Bar` wraps `Foo` as a field)

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
ref: kubernetes/community#2354

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
/assign @sttts @liggitt @deads2k
This commit is contained in:
Kubernetes Submit Queue 2018-08-10 14:13:12 -07:00 committed by GitHub
commit 94a754c794
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 29 additions and 102 deletions

View File

@ -80,6 +80,7 @@
- k8s.io/apiserver - k8s.io/apiserver
- k8s.io/client-go - k8s.io/client-go
- k8s.io/kube-openapi - k8s.io/kube-openapi
- k8s.io/utils
- baseImportPath: "./vendor/k8s.io/metrics/" - baseImportPath: "./vendor/k8s.io/metrics/"
allowedImports: allowedImports:

View File

@ -10,7 +10,6 @@ go_library(
"types.go", "types.go",
"zz_generated.conversion.go", "zz_generated.conversion.go",
"zz_generated.deepcopy.go", "zz_generated.deepcopy.go",
"zz_generated.defaults.go",
], ],
importmap = "k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/apis/config/v1alpha1", importmap = "k8s.io/kubernetes/vendor/k8s.io/apimachinery/pkg/apis/config/v1alpha1",
importpath = "k8s.io/apimachinery/pkg/apis/config/v1alpha1", importpath = "k8s.io/apimachinery/pkg/apis/config/v1alpha1",

View File

@ -16,15 +16,16 @@ limitations under the License.
package v1alpha1 package v1alpha1
import ( // RecommendedDefaultClientConnectionConfiguration defaults a pointer to a
kruntime "k8s.io/apimachinery/pkg/runtime" // ClientConnectionConfiguration struct. This will set the recommended default
) // values, but they may be subject to change between API versions. This function
// is intentionally not registered in the scheme as a "normal" `SetDefaults_Foo`
func addDefaultingFuncs(scheme *kruntime.Scheme) error { // function to allow consumers of this type to set whatever defaults for their
return RegisterDefaults(scheme) // embedded configs. Forcing consumers to use these defaults would be problematic
} // as defaulting in the scheme is done as part of the conversion, and there would
// be no easy way to opt-out. Instead, if you want to use this defaulting method
func SetDefaults_ClientConnectionConfiguration(obj *ClientConnectionConfiguration) { // run it in your wrapper struct of this type in its `SetDefaults_` method.
func RecommendedDefaultClientConnectionConfiguration(obj *ClientConnectionConfiguration) {
if len(obj.ContentType) == 0 { if len(obj.ContentType) == 0 {
obj.ContentType = "application/vnd.kubernetes.protobuf" obj.ContentType = "application/vnd.kubernetes.protobuf"
} }

View File

@ -16,6 +16,5 @@ limitations under the License.
// +k8s:deepcopy-gen=package // +k8s:deepcopy-gen=package
// +k8s:conversion-gen=k8s.io/apimachinery/pkg/apis/config // +k8s:conversion-gen=k8s.io/apimachinery/pkg/apis/config
// +k8s:defaulter-gen=TypeMeta
package v1alpha1 // import "k8s.io/apimachinery/pkg/apis/config/v1alpha1" package v1alpha1 // import "k8s.io/apimachinery/pkg/apis/config/v1alpha1"

View File

@ -21,15 +21,7 @@ import (
) )
var ( var (
// localSchemeBuilder and AddToScheme will stay in k8s.io/kubernetes.
SchemeBuilder runtime.SchemeBuilder SchemeBuilder runtime.SchemeBuilder
localSchemeBuilder = &SchemeBuilder localSchemeBuilder = &SchemeBuilder
AddToScheme = localSchemeBuilder.AddToScheme AddToScheme = localSchemeBuilder.AddToScheme
) )
func init() {
// We only register manually written functions here. The registration of the
// generated functions takes place in the generated files. The separation
// makes the code compile even when the generated files are missing.
localSchemeBuilder.Register(addDefaultingFuncs)
}

View File

@ -1,32 +0,0 @@
// +build !ignore_autogenerated
/*
Copyright The Kubernetes 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.
*/
// Code generated by defaulter-gen. DO NOT EDIT.
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 {
return nil
}

View File

@ -1905,6 +1905,10 @@
{ {
"ImportPath": "k8s.io/client-go/util/flowcontrol", "ImportPath": "k8s.io/client-go/util/flowcontrol",
"Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" "Rev": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
{
"ImportPath": "k8s.io/utils/pointer",
"Rev": "66066c83e385e385ccc3c964b44fd7dcd413d0ed"
} }
] ]
} }

View File

@ -10,7 +10,6 @@ go_library(
"types.go", "types.go",
"zz_generated.conversion.go", "zz_generated.conversion.go",
"zz_generated.deepcopy.go", "zz_generated.deepcopy.go",
"zz_generated.defaults.go",
], ],
importmap = "k8s.io/kubernetes/vendor/k8s.io/apiserver/pkg/apis/config/v1alpha1", importmap = "k8s.io/kubernetes/vendor/k8s.io/apiserver/pkg/apis/config/v1alpha1",
importpath = "k8s.io/apiserver/pkg/apis/config/v1alpha1", importpath = "k8s.io/apiserver/pkg/apis/config/v1alpha1",
@ -20,6 +19,7 @@ go_library(
"//staging/src/k8s.io/apimachinery/pkg/conversion:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/conversion:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/apis/config:go_default_library", "//staging/src/k8s.io/apiserver/pkg/apis/config:go_default_library",
"//vendor/k8s.io/utils/pointer:go_default_library",
], ],
) )

View File

@ -20,15 +20,19 @@ import (
"time" "time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kruntime "k8s.io/apimachinery/pkg/runtime" utilpointer "k8s.io/utils/pointer"
) )
func addDefaultingFuncs(scheme *kruntime.Scheme) error { // RecommendedDefaultLeaderElectionConfiguration defaults a pointer to a
return RegisterDefaults(scheme) // LeaderElectionConfiguration struct. This will set the recommended default
} // values, but they may be subject to change between API versions. This function
// is intentionally not registered in the scheme as a "normal" `SetDefaults_Foo`
func SetDefaults_LeaderElectionConfiguration(obj *LeaderElectionConfiguration) { // function to allow consumers of this type to set whatever defaults for their
booltrue := true // embedded configs. Forcing consumers to use these defaults would be problematic
// as defaulting in the scheme is done as part of the conversion, and there would
// be no easy way to opt-out. Instead, if you want to use this defaulting method
// run it in your wrapper struct of this type in its `SetDefaults_` method.
func RecommendedDefaultLeaderElectionConfiguration(obj *LeaderElectionConfiguration) {
zero := metav1.Duration{} zero := metav1.Duration{}
if obj.LeaseDuration == zero { if obj.LeaseDuration == zero {
obj.LeaseDuration = metav1.Duration{Duration: 15 * time.Second} obj.LeaseDuration = metav1.Duration{Duration: 15 * time.Second}
@ -43,6 +47,6 @@ func SetDefaults_LeaderElectionConfiguration(obj *LeaderElectionConfiguration) {
obj.ResourceLock = EndpointsResourceLock obj.ResourceLock = EndpointsResourceLock
} }
if obj.LeaderElect == nil { if obj.LeaderElect == nil {
obj.LeaderElect = &booltrue obj.LeaderElect = utilpointer.BoolPtr(true)
} }
} }

View File

@ -16,6 +16,5 @@ limitations under the License.
// +k8s:deepcopy-gen=package // +k8s:deepcopy-gen=package
// +k8s:conversion-gen=k8s.io/apiserver/pkg/apis/config // +k8s:conversion-gen=k8s.io/apiserver/pkg/apis/config
// +k8s:defaulter-gen=TypeMeta
package v1alpha1 package v1alpha1 // import "k8s.io/apiserver/pkg/apis/config/v1alpha1"

View File

@ -21,15 +21,7 @@ import (
) )
var ( var (
// localSchemeBuilder and AddToScheme will stay in k8s.io/kubernetes.
SchemeBuilder runtime.SchemeBuilder SchemeBuilder runtime.SchemeBuilder
localSchemeBuilder = &SchemeBuilder localSchemeBuilder = &SchemeBuilder
AddToScheme = localSchemeBuilder.AddToScheme AddToScheme = localSchemeBuilder.AddToScheme
) )
func init() {
// We only register manually written functions here. The registration of the
// generated functions takes place in the generated files. The separation
// makes the code compile even when the generated files are missing.
localSchemeBuilder.Register(addDefaultingFuncs)
}

View File

@ -1,32 +0,0 @@
// +build !ignore_autogenerated
/*
Copyright The Kubernetes 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.
*/
// Code generated by defaulter-gen. DO NOT EDIT.
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 {
return nil
}