reduce API surface area of whether a resource is enabled
Kubernetes-commit: a59b92e8c039fb3646dec18f9e64ee2b5462db42
This commit is contained in:
parent
091db3f2f6
commit
57ccdb5af8
|
@ -76,25 +76,8 @@ var (
|
|||
}
|
||||
|
||||
groupVersionMatchersOrder = []string{APIAll, APIGA, APIBeta, APIAlpha}
|
||||
|
||||
groupVersionResourceMatchers = map[string]func(gvr schema.GroupVersionResource) bool{
|
||||
// allows users to address all api versions
|
||||
APIAll: func(gvr schema.GroupVersionResource) bool { return true },
|
||||
// allows users to address all api versions in the form v[0-9]+
|
||||
APIGA: func(gvr schema.GroupVersionResource) bool { return gaPattern.MatchString(gvr.Version) },
|
||||
// allows users to address all beta api versions
|
||||
APIBeta: func(gvr schema.GroupVersionResource) bool { return betaPattern.MatchString(gvr.Version) },
|
||||
// allows users to address all alpha api versions
|
||||
APIAlpha: func(gvr schema.GroupVersionResource) bool { return alphaPattern.MatchString(gvr.Version) },
|
||||
}
|
||||
)
|
||||
|
||||
func resourceMatcherForVersion(gv schema.GroupVersion) func(gvr schema.GroupVersionResource) bool {
|
||||
return func(gvr schema.GroupVersionResource) bool {
|
||||
return gv == gvr.GroupVersion()
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
@ -115,8 +98,6 @@ func MergeAPIResourceConfigs(
|
|||
} else {
|
||||
return nil, fmt.Errorf("invalid value %v=%v", flag, value)
|
||||
}
|
||||
// remove individual resource preferences that were hardcoded into the default. The override trumps those settings.
|
||||
resourceConfig.RemoveMatchingResourcePreferences(groupVersionResourceMatchers[flag])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -187,8 +168,6 @@ func MergeAPIResourceConfigs(
|
|||
|
||||
// apply version preferences first, so that we can remove the hardcoded resource preferences that are being overridden
|
||||
for _, versionPreference := range versionPreferences {
|
||||
// if a user has expressed a preference about a version, that preference takes priority over the hardcoded resources
|
||||
resourceConfig.RemoveMatchingResourcePreferences(resourceMatcherForVersion(versionPreference.groupVersion))
|
||||
if versionPreference.enabled {
|
||||
// enable the groupVersion for "group/version=true"
|
||||
resourceConfig.EnableVersions(versionPreference.groupVersion)
|
||||
|
|
|
@ -80,8 +80,15 @@ func TestParseRuntimeConfig(t *testing.T) {
|
|||
config.DisableVersions(extensionsapiv1beta1.SchemeGroupVersion)
|
||||
return config
|
||||
},
|
||||
expectedEnabledAPIs: defaultFakeEnabledResources(),
|
||||
err: false,
|
||||
expectedEnabledAPIs: map[schema.GroupVersionResource]bool{
|
||||
extensionsapiv1beta1.SchemeGroupVersion.WithResource("ingresses"): false, // this becomes false because the DisableVersions set in the defaultConfig is now order dependent.
|
||||
extensionsapiv1beta1.SchemeGroupVersion.WithResource("deployments"): false,
|
||||
extensionsapiv1beta1.SchemeGroupVersion.WithResource("replicasets"): false,
|
||||
extensionsapiv1beta1.SchemeGroupVersion.WithResource("daemonsets"): false,
|
||||
appsv1.SchemeGroupVersion.WithResource("deployments"): true,
|
||||
apiv1.SchemeGroupVersion.WithResource("pods"): true,
|
||||
},
|
||||
err: false,
|
||||
},
|
||||
{
|
||||
name: "version-enabled-by-runtimeConfig-override",
|
||||
|
@ -147,8 +154,6 @@ func TestParseRuntimeConfig(t *testing.T) {
|
|||
expectedAPIConfig: func() *serverstore.ResourceConfig {
|
||||
config := newFakeAPIResourceConfigSource()
|
||||
config.EnableVersions(scheme.PrioritizedVersionsAllGroups()...)
|
||||
// disabling groups of APIs removes the individual resource preferences from the default
|
||||
config.RemoveMatchingResourcePreferences(matchAllExplicitResourcesForFake)
|
||||
return config
|
||||
},
|
||||
expectedEnabledAPIs: map[schema.GroupVersionResource]bool{
|
||||
|
@ -174,8 +179,6 @@ func TestParseRuntimeConfig(t *testing.T) {
|
|||
config := newFakeAPIResourceConfigSource()
|
||||
config.DisableVersions(appsv1.SchemeGroupVersion)
|
||||
config.DisableVersions(extensionsapiv1beta1.SchemeGroupVersion)
|
||||
// disabling groups of APIs removes the individual resource preferences from the default
|
||||
config.RemoveMatchingResourcePreferences(matchAllExplicitResourcesForFake)
|
||||
return config
|
||||
},
|
||||
expectedEnabledAPIs: map[schema.GroupVersionResource]bool{
|
||||
|
@ -243,8 +246,6 @@ func TestParseRuntimeConfig(t *testing.T) {
|
|||
expectedAPIConfig: func() *serverstore.ResourceConfig {
|
||||
config := newFakeAPIResourceConfigSource()
|
||||
config.DisableVersions(extensionsapiv1beta1.SchemeGroupVersion)
|
||||
// disabling groups of APIs removes the individual resource preferences from the default
|
||||
config.RemoveMatchingResourcePreferences(matchAllExplicitResourcesForFake)
|
||||
return config
|
||||
},
|
||||
expectedEnabledAPIs: map[schema.GroupVersionResource]bool{
|
||||
|
@ -290,8 +291,6 @@ func TestParseRuntimeConfig(t *testing.T) {
|
|||
expectedAPIConfig: func() *serverstore.ResourceConfig {
|
||||
config := newFakeAPIResourceConfigSource()
|
||||
config.DisableVersions(extensionsapiv1beta1.SchemeGroupVersion)
|
||||
// disabling groups of APIs removes the individual resource preferences from the default
|
||||
config.RemoveMatchingResourcePreferences(matchAllExplicitResourcesForFake)
|
||||
return config
|
||||
},
|
||||
expectedEnabledAPIs: map[schema.GroupVersionResource]bool{
|
||||
|
@ -568,17 +567,6 @@ func newFakeAPIResourceConfigSource() *serverstore.ResourceConfig {
|
|||
return ret
|
||||
}
|
||||
|
||||
func matchAllExplicitResourcesForFake(gvr schema.GroupVersionResource) bool {
|
||||
switch gvr {
|
||||
case extensionsapiv1beta1.SchemeGroupVersion.WithResource("ingresses"),
|
||||
extensionsapiv1beta1.SchemeGroupVersion.WithResource("deployments"),
|
||||
extensionsapiv1beta1.SchemeGroupVersion.WithResource("replicasets"),
|
||||
extensionsapiv1beta1.SchemeGroupVersion.WithResource("daemonsets"):
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// apiResourcesToCheck are the apis we use in this set of unit tests. They will be check for enable/disable status
|
||||
func apiResourcesToCheck() []schema.GroupVersionResource {
|
||||
return []schema.GroupVersionResource{
|
||||
|
|
|
@ -37,27 +37,38 @@ func NewResourceConfig() *ResourceConfig {
|
|||
return &ResourceConfig{GroupVersionConfigs: map[schema.GroupVersion]bool{}, ResourceConfigs: map[schema.GroupVersionResource]bool{}}
|
||||
}
|
||||
|
||||
// DisableMatchingVersions disables all group/versions for which the matcher function returns true. It does not modify individual resource enablement/disablement.
|
||||
// DisableMatchingVersions disables all group/versions for which the matcher function returns true.
|
||||
// This will remove any preferences previously set on individual resources.
|
||||
func (o *ResourceConfig) DisableMatchingVersions(matcher func(gv schema.GroupVersion) bool) {
|
||||
for k := range o.GroupVersionConfigs {
|
||||
if matcher(k) {
|
||||
o.GroupVersionConfigs[k] = false
|
||||
for version := range o.GroupVersionConfigs {
|
||||
if matcher(version) {
|
||||
o.GroupVersionConfigs[version] = false
|
||||
o.removeMatchingResourcePreferences(resourceMatcherForVersion(version))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// EnableMatchingVersions enables all group/versions for which the matcher function returns true. It does not modify individual resource enablement/disablement.
|
||||
// EnableMatchingVersions enables all group/versions for which the matcher function returns true.
|
||||
// This will remove any preferences previously set on individual resources.
|
||||
func (o *ResourceConfig) EnableMatchingVersions(matcher func(gv schema.GroupVersion) bool) {
|
||||
for k := range o.GroupVersionConfigs {
|
||||
if matcher(k) {
|
||||
o.GroupVersionConfigs[k] = true
|
||||
for version := range o.GroupVersionConfigs {
|
||||
if matcher(version) {
|
||||
o.GroupVersionConfigs[version] = true
|
||||
o.removeMatchingResourcePreferences(resourceMatcherForVersion(version))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// RemoveMatchingResourcePreferences removes individual resource preferences that match. This is useful when an override of a version or level enablement should
|
||||
// resourceMatcherForVersion matches resources in the specified version
|
||||
func resourceMatcherForVersion(gv schema.GroupVersion) func(gvr schema.GroupVersionResource) bool {
|
||||
return func(gvr schema.GroupVersionResource) bool {
|
||||
return gv == gvr.GroupVersion()
|
||||
}
|
||||
}
|
||||
|
||||
// removeMatchingResourcePreferences removes individual resource preferences that match. This is useful when an override of a version or level enablement should
|
||||
// override the previously individual preferences.
|
||||
func (o *ResourceConfig) RemoveMatchingResourcePreferences(matcher func(gvr schema.GroupVersionResource) bool) {
|
||||
func (o *ResourceConfig) removeMatchingResourcePreferences(matcher func(gvr schema.GroupVersionResource) bool) {
|
||||
keysToRemove := []schema.GroupVersionResource{}
|
||||
for k := range o.ResourceConfigs {
|
||||
if matcher(k) {
|
||||
|
@ -70,20 +81,30 @@ func (o *ResourceConfig) RemoveMatchingResourcePreferences(matcher func(gvr sche
|
|||
}
|
||||
|
||||
// DisableVersions disables the versions entirely.
|
||||
// This will remove any preferences previously set on individual resources.
|
||||
func (o *ResourceConfig) DisableVersions(versions ...schema.GroupVersion) {
|
||||
for _, version := range versions {
|
||||
o.GroupVersionConfigs[version] = false
|
||||
|
||||
// a preference about a version takes priority over the previously set resources
|
||||
o.removeMatchingResourcePreferences(resourceMatcherForVersion(version))
|
||||
}
|
||||
}
|
||||
|
||||
// EnableVersions enables all resources in a given groupVersion.
|
||||
// This will remove any preferences previously set on individual resources.
|
||||
func (o *ResourceConfig) EnableVersions(versions ...schema.GroupVersion) {
|
||||
for _, version := range versions {
|
||||
o.GroupVersionConfigs[version] = true
|
||||
|
||||
// a preference about a version takes priority over the previously set resources
|
||||
o.removeMatchingResourcePreferences(resourceMatcherForVersion(version))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// TODO this must be removed and we enable/disable individual resources.
|
||||
func (o *ResourceConfig) VersionEnabled(version schema.GroupVersion) bool {
|
||||
func (o *ResourceConfig) versionEnabled(version schema.GroupVersion) bool {
|
||||
enabled, _ := o.GroupVersionConfigs[version]
|
||||
return enabled
|
||||
}
|
||||
|
@ -107,7 +128,7 @@ func (o *ResourceConfig) ResourceEnabled(resource schema.GroupVersionResource) b
|
|||
return resourceEnabled
|
||||
}
|
||||
|
||||
if !o.VersionEnabled(resource.GroupVersion()) {
|
||||
if !o.versionEnabled(resource.GroupVersion()) {
|
||||
return false
|
||||
}
|
||||
// they are enabled by default.
|
||||
|
@ -117,7 +138,7 @@ func (o *ResourceConfig) ResourceEnabled(resource schema.GroupVersionResource) b
|
|||
func (o *ResourceConfig) AnyResourceForGroupEnabled(group string) bool {
|
||||
for version := range o.GroupVersionConfigs {
|
||||
if version.Group == group {
|
||||
if o.VersionEnabled(version) {
|
||||
if o.versionEnabled(version) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,13 +32,13 @@ func TestDisabledVersion(t *testing.T) {
|
|||
config.DisableVersions(g1v1)
|
||||
config.EnableVersions(g1v2, g2v1)
|
||||
|
||||
if config.VersionEnabled(g1v1) {
|
||||
if config.versionEnabled(g1v1) {
|
||||
t.Errorf("expected disabled for %v, from %v", g1v1, config)
|
||||
}
|
||||
if !config.VersionEnabled(g1v2) {
|
||||
if !config.versionEnabled(g1v2) {
|
||||
t.Errorf("expected enabled for %v, from %v", g1v1, config)
|
||||
}
|
||||
if !config.VersionEnabled(g2v1) {
|
||||
if !config.versionEnabled(g2v1) {
|
||||
t.Errorf("expected enabled for %v, from %v", g1v1, config)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -240,6 +240,7 @@ func getAllResourcesAlias(resource schema.GroupResource) schema.GroupResource {
|
|||
|
||||
func (s *DefaultStorageFactory) getStorageGroupResource(groupResource schema.GroupResource) schema.GroupResource {
|
||||
for _, potentialStorageResource := range s.Overrides[groupResource].cohabitatingResources {
|
||||
// TODO determine if have ever stored any of our cohabitating resources in a different location on new clusters
|
||||
if s.APIResourceConfigSource.AnyResourceForGroupEnabled(potentialStorageResource.Group) {
|
||||
return potentialStorageResource
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue