1/* 2Copyright 2016 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package storage 18 19import ( 20 "k8s.io/apimachinery/pkg/runtime/schema" 21) 22 23// APIResourceConfigSource is the interface to determine which groups and versions are enabled 24type APIResourceConfigSource interface { 25 VersionEnabled(version schema.GroupVersion) bool 26 ResourceEnabled(resource schema.GroupVersionResource) bool 27 AnyVersionForGroupEnabled(group string) bool 28} 29 30var _ APIResourceConfigSource = &ResourceConfig{} 31 32type ResourceConfig struct { 33 GroupVersionConfigs map[schema.GroupVersion]bool 34 ResourceConfigs map[schema.GroupVersionResource]bool 35} 36 37func NewResourceConfig() *ResourceConfig { 38 return &ResourceConfig{GroupVersionConfigs: map[schema.GroupVersion]bool{}, ResourceConfigs: map[schema.GroupVersionResource]bool{}} 39} 40 41// DisableAll disables all group/versions. It does not modify individual resource enablement/disablement. 42func (o *ResourceConfig) DisableAll() { 43 for k := range o.GroupVersionConfigs { 44 o.GroupVersionConfigs[k] = false 45 } 46} 47 48// EnableAll enables all group/versions. It does not modify individual resource enablement/disablement. 49func (o *ResourceConfig) EnableAll() { 50 for k := range o.GroupVersionConfigs { 51 o.GroupVersionConfigs[k] = true 52 } 53} 54 55// DisableMatchingVersions disables all group/versions for which the matcher function returns true. It does not modify individual resource enablement/disablement. 56func (o *ResourceConfig) DisableMatchingVersions(matcher func(gv schema.GroupVersion) bool) { 57 for k := range o.GroupVersionConfigs { 58 if matcher(k) { 59 o.GroupVersionConfigs[k] = false 60 } 61 } 62} 63 64// EnableMatchingVersions enables all group/versions for which the matcher function returns true. It does not modify individual resource enablement/disablement. 65func (o *ResourceConfig) EnableMatchingVersions(matcher func(gv schema.GroupVersion) bool) { 66 for k := range o.GroupVersionConfigs { 67 if matcher(k) { 68 o.GroupVersionConfigs[k] = true 69 } 70 } 71} 72 73// DisableVersions disables the versions entirely. 74func (o *ResourceConfig) DisableVersions(versions ...schema.GroupVersion) { 75 for _, version := range versions { 76 o.GroupVersionConfigs[version] = false 77 } 78} 79 80func (o *ResourceConfig) EnableVersions(versions ...schema.GroupVersion) { 81 for _, version := range versions { 82 o.GroupVersionConfigs[version] = true 83 } 84} 85 86func (o *ResourceConfig) VersionEnabled(version schema.GroupVersion) bool { 87 enabled, _ := o.GroupVersionConfigs[version] 88 return enabled 89} 90 91func (o *ResourceConfig) DisableResources(resources ...schema.GroupVersionResource) { 92 for _, resource := range resources { 93 o.ResourceConfigs[resource] = false 94 } 95} 96 97func (o *ResourceConfig) EnableResources(resources ...schema.GroupVersionResource) { 98 for _, resource := range resources { 99 o.ResourceConfigs[resource] = true 100 } 101} 102 103func (o *ResourceConfig) ResourceEnabled(resource schema.GroupVersionResource) bool { 104 if !o.VersionEnabled(resource.GroupVersion()) { 105 return false 106 } 107 resourceEnabled, explicitlySet := o.ResourceConfigs[resource] 108 if !explicitlySet { 109 return true 110 } 111 return resourceEnabled 112} 113 114func (o *ResourceConfig) AnyVersionForGroupEnabled(group string) bool { 115 for version := range o.GroupVersionConfigs { 116 if version.Group == group { 117 if o.VersionEnabled(version) { 118 return true 119 } 120 } 121 } 122 123 return false 124} 125