Merge pull request #84304 from liggitt/all-beta

Add support for --runtime-config=api/beta=false, --feature-gates=AllBeta=false

Kubernetes-commit: f1e912c38abcecfb64e36eb161128b15e69a135b
This commit is contained in:
Kubernetes Publisher 2019-11-14 16:38:01 -08:00
commit 6b2c200efa
7 changed files with 86 additions and 21 deletions

2
Godeps/Godeps.json generated
View File

@ -588,7 +588,7 @@
},
{
"ImportPath": "k8s.io/component-base",
"Rev": "90a8a7f61617"
"Rev": "50dd3c3ffc6e"
},
{
"ImportPath": "k8s.io/gengo",

4
go.mod
View File

@ -46,7 +46,7 @@ require (
k8s.io/api v0.0.0-20191114215726-6cd33094d465
k8s.io/apimachinery v0.0.0-20191114215425-67a48e0c9266
k8s.io/client-go v0.0.0-20191114220110-6f03b71b98e6
k8s.io/component-base v0.0.0-20191114221108-90a8a7f61617
k8s.io/component-base v0.0.0-20191115015918-50dd3c3ffc6e
k8s.io/klog v1.0.0
k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a
k8s.io/utils v0.0.0-20191030222137-2b95a09bc58d
@ -60,5 +60,5 @@ replace (
k8s.io/api => k8s.io/api v0.0.0-20191114215726-6cd33094d465
k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20191114215425-67a48e0c9266
k8s.io/client-go => k8s.io/client-go v0.0.0-20191114220110-6f03b71b98e6
k8s.io/component-base => k8s.io/component-base v0.0.0-20191114221108-90a8a7f61617
k8s.io/component-base => k8s.io/component-base v0.0.0-20191115015918-50dd3c3ffc6e
)

2
go.sum
View File

@ -349,7 +349,7 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
k8s.io/api v0.0.0-20191114215726-6cd33094d465/go.mod h1:TnwOuwwOmtUq5BythlhmbJs+lwQGEsSJ6N4S4ODl2m8=
k8s.io/apimachinery v0.0.0-20191114215425-67a48e0c9266/go.mod h1:dXFS2zaQR8fyzuvRdJDHw2Aerij/yVGJSre0bZQSVJA=
k8s.io/client-go v0.0.0-20191114220110-6f03b71b98e6/go.mod h1:Hykx8A+UlxwDre3qJcqZPgCAZGjl9vNp6yc7SmfMQ1k=
k8s.io/component-base v0.0.0-20191114221108-90a8a7f61617/go.mod h1:mGvZw6rGJi0lRK5Qn3KrpKMsz2SmgmRw9oQoA3xkE7U=
k8s.io/component-base v0.0.0-20191115015918-50dd3c3ffc6e/go.mod h1:mGvZw6rGJi0lRK5Qn3KrpKMsz2SmgmRw9oQoA3xkE7U=
k8s.io/gengo v0.0.0-20190128074634-0689ccc1d7d6/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
k8s.io/klog v0.0.0-20181102134211-b9b56d5dfc92/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=
k8s.io/klog v0.3.0/go.mod h1:Gq+BEi5rUBO/HRz0bTSXDUcqjScdoY3a9IHpCEIOOfk=

View File

@ -43,11 +43,14 @@ func NewAPIEnablementOptions() *APIEnablementOptions {
// AddFlags adds flags for a specific APIServer to the specified FlagSet
func (s *APIEnablementOptions) AddFlags(fs *pflag.FlagSet) {
fs.Var(&s.RuntimeConfig, "runtime-config", ""+
"A set of key=value pairs that describe runtime configuration that may be passed "+
"to apiserver. <group>/<version> (or <version> for the core group) key can be used to "+
"turn on/off specific api versions. api/all is special key to control all api versions, "+
"be careful setting it false, unless you know what you do. api/legacy is deprecated, "+
"we will remove it in the future, so stop using it.")
"A set of key=value pairs that enable or disable built-in APIs. Supported options are:\n"+
"v1=true|false for the core API group\n"+
"<group>/<version>=true|false for a specific API group and version (e.g. apps/v1=true)\n"+
"api/all=true|false controls all API versions\n"+
"api/ga=true|false controls all API versions of the form v[0-9]+\n"+
"api/beta=true|false controls all API versions of the form v[0-9]+beta[0-9]+\n"+
"api/alpha=true|false controls all API versions of the form v[0-9]+alpha[0-9]+\n"+
"api/legacy is deprecated, and will be removed in a future version")
}
// Validate validates RuntimeConfig with a list of registries.
@ -61,9 +64,9 @@ func (s *APIEnablementOptions) Validate(registries ...GroupRegisty) []error {
}
errors := []error{}
if s.RuntimeConfig["api/all"] == "false" && len(s.RuntimeConfig) == 1 {
if s.RuntimeConfig[resourceconfig.APIAll] == "false" && len(s.RuntimeConfig) == 1 {
// Do not allow only set api/all=false, in such case apiserver startup has no meaning.
return append(errors, fmt.Errorf("invalid key with only api/all=false"))
return append(errors, fmt.Errorf("invalid key with only %v=false", resourceconfig.APIAll))
}
groups, err := resourceconfig.ParseGroups(s.RuntimeConfig)

View File

@ -18,6 +18,7 @@ package resourceconfig
import (
"fmt"
"regexp"
"strconv"
"strings"
@ -51,6 +52,33 @@ func MergeResourceEncodingConfigs(
return resourceEncodingConfig
}
// Recognized values for the --runtime-config parameter to enable/disable groups of APIs
const (
APIAll = "api/all"
APIGA = "api/ga"
APIBeta = "api/beta"
APIAlpha = "api/alpha"
)
var (
gaPattern = regexp.MustCompile(`^v\d+$`)
betaPattern = regexp.MustCompile(`^v\d+beta\d+$`)
alphaPattern = regexp.MustCompile(`^v\d+alpha\d+$`)
matchers = map[string]func(gv schema.GroupVersion) bool{
// allows users to address all api versions
APIAll: func(gv schema.GroupVersion) bool { return true },
// allows users to address all api versions in the form v[0-9]+
APIGA: func(gv schema.GroupVersion) bool { return gaPattern.MatchString(gv.Version) },
// allows users to address all beta api versions
APIBeta: func(gv schema.GroupVersion) bool { return betaPattern.MatchString(gv.Version) },
// allows users to address all alpha api versions
APIAlpha: func(gv schema.GroupVersion) bool { return alphaPattern.MatchString(gv.Version) },
}
matcherOrder = []string{APIAll, APIGA, APIBeta, APIAlpha}
)
// MergeAPIResourceConfigs merges the given defaultAPIResourceConfig with the given resourceConfigOverrides.
// Exclude the groups not registered in registry, and check if version is
// not registered in group, then it will fail.
@ -62,14 +90,15 @@ func MergeAPIResourceConfigs(
resourceConfig := defaultAPIResourceConfig
overrides := resourceConfigOverrides
// "api/all=false" allows users to selectively enable specific api versions.
allAPIFlagValue, ok := overrides["api/all"]
if ok {
if allAPIFlagValue == "false" {
// Disable all group versions.
resourceConfig.DisableAll()
} else if allAPIFlagValue == "true" {
resourceConfig.EnableAll()
for _, flag := range matcherOrder {
if value, ok := overrides[flag]; ok {
if value == "false" {
resourceConfig.DisableMatchingVersions(matchers[flag])
} else if value == "true" {
resourceConfig.EnableMatchingVersions(matchers[flag])
} else {
return nil, fmt.Errorf("invalid value %v=%v", flag, value)
}
}
}
@ -78,7 +107,7 @@ func MergeAPIResourceConfigs(
// Iterate through all group/version overrides specified in runtimeConfig.
for key := range overrides {
// Have already handled them above. Can skip them here.
if key == "api/all" {
if _, ok := matchers[key]; ok {
continue
}
@ -153,7 +182,7 @@ func getRuntimeConfigValue(overrides cliflag.ConfigurationMap, apiKey string, de
func ParseGroups(resourceConfig cliflag.ConfigurationMap) ([]string, error) {
groups := []string{}
for key := range resourceConfig {
if key == "api/all" {
if _, ok := matchers[key]; ok {
continue
}
tokens := strings.Split(key, "/")

View File

@ -195,6 +195,21 @@ func TestParseRuntimeConfig(t *testing.T) {
},
err: false, // no error for backwards compatibility
},
{
// disable all beta resources
runtimeConfig: map[string]string{
"api/beta": "false",
},
defaultResourceConfig: func() *serverstore.ResourceConfig {
return newFakeAPIResourceConfigSource()
},
expectedAPIConfig: func() *serverstore.ResourceConfig {
config := newFakeAPIResourceConfigSource()
config.DisableVersions(extensionsapiv1beta1.SchemeGroupVersion)
return config
},
err: false, // no error for backwards compatibility
},
}
for index, test := range testCases {
t.Log(scheme.PrioritizedVersionsAllGroups())

View File

@ -52,6 +52,24 @@ func (o *ResourceConfig) EnableAll() {
}
}
// DisableMatchingVersions disables all group/versions for which the matcher function returns true. It does not modify individual resource enablement/disablement.
func (o *ResourceConfig) DisableMatchingVersions(matcher func(gv schema.GroupVersion) bool) {
for k := range o.GroupVersionConfigs {
if matcher(k) {
o.GroupVersionConfigs[k] = false
}
}
}
// EnableMatchingVersions enables all group/versions for which the matcher function returns true. It does not modify individual resource enablement/disablement.
func (o *ResourceConfig) EnableMatchingVersions(matcher func(gv schema.GroupVersion) bool) {
for k := range o.GroupVersionConfigs {
if matcher(k) {
o.GroupVersionConfigs[k] = true
}
}
}
// DisableVersions disables the versions entirely.
func (o *ResourceConfig) DisableVersions(versions ...schema.GroupVersion) {
for _, version := range versions {