Debian Codegraph @

pkg/server/storage/resource_config.go

raw
1 /*
2 Copyright 2016 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package storage
18
19 import (
20 "k8s.io/apimachinery/pkg/runtime/schema"
21 )
22
23 // APIResourceConfigSource is the interface to determine which groups and versions are enabled
24 type APIResourceConfigSource interface {
25 ResourceEnabled(resource schema.GroupVersionResource) bool
26 AnyResourceForGroupEnabled(group string) bool
27 ResourceExplicitlyEnabled(resource schema.GroupVersionResource) bool
28 VersionExplicitlyEnabled(version schema.GroupVersion) bool
29 }
30
31 var _ APIResourceConfigSource = &ResourceConfig{}
32
33 type ResourceConfig struct {
34 GroupVersionConfigs map[schema.GroupVersion]bool
35 ResourceConfigs map[schema.GroupVersionResource]bool
36 ExplicitGroupVersionConfigs map[schema.GroupVersion]bool
37 ExplicitResourceConfigs map[schema.GroupVersionResource]bool
38 }
39
40 func NewResourceConfig() *ResourceConfig {
41 return &ResourceConfig{
42 GroupVersionConfigs: map[schema.GroupVersion]bool{},
43 ResourceConfigs: map[schema.GroupVersionResource]bool{},
44 ExplicitGroupVersionConfigs: map[schema.GroupVersion]bool{},
45 ExplicitResourceConfigs: map[schema.GroupVersionResource]bool{},
46 }
47 }
48
49 // DisableMatchingVersions disables all group/versions for which the matcher function returns true.
50 // This will remove any preferences previously set on individual resources.
51 func (o *ResourceConfig) DisableMatchingVersions(matcher func(gv schema.GroupVersion) bool) {
52 for version := range o.GroupVersionConfigs {
53 if matcher(version) {
54 o.GroupVersionConfigs[version] = false
55 o.removeMatchingResourcePreferences(resourceMatcherForVersion(version))
56 }
57 }
58 }
59
60 // EnableMatchingVersions enables all group/versions for which the matcher function returns true.
61 // This will remove any preferences previously set on individual resources.
62 func (o *ResourceConfig) EnableMatchingVersions(matcher func(gv schema.GroupVersion) bool) {
63 for version := range o.GroupVersionConfigs {
64 if matcher(version) {
65 o.GroupVersionConfigs[version] = true
66 o.removeMatchingResourcePreferences(resourceMatcherForVersion(version))
67 }
68 }
69 }
70
71 // resourceMatcherForVersion matches resources in the specified version
72 func resourceMatcherForVersion(gv schema.GroupVersion) func(gvr schema.GroupVersionResource) bool {
73 return func(gvr schema.GroupVersionResource) bool {
74 return gv == gvr.GroupVersion()
75 }
76 }
77
78 // removeMatchingResourcePreferences removes individual resource preferences that match. This is useful when an override of a version or level enablement should
79 // override the previously individual preferences.
80 func (o *ResourceConfig) removeMatchingResourcePreferences(matcher func(gvr schema.GroupVersionResource) bool) {
81 keysToRemove := []schema.GroupVersionResource{}
82 for k := range o.ResourceConfigs {
83 if matcher(k) {
84 keysToRemove = append(keysToRemove, k)
85 }
86 }
87 for _, k := range keysToRemove {
88 delete(o.ResourceConfigs, k)
89 delete(o.ExplicitResourceConfigs, k)
90 }
91 }
92
93 // DisableVersions disables the versions entirely.
94 // This will remove any preferences previously set on individual resources.
95 func (o *ResourceConfig) DisableVersions(versions ...schema.GroupVersion) {
96 for _, version := range versions {
97 o.GroupVersionConfigs[version] = false
98
99 // a preference about a version takes priority over the previously set resources
100 o.removeMatchingResourcePreferences(resourceMatcherForVersion(version))
101 }
102 }
103
104 func (o *ResourceConfig) ExplicitlyDisableVersions(versions ...schema.GroupVersion) {
105 for _, version := range versions {
106 o.ExplicitGroupVersionConfigs[version] = false
107 }
108 o.DisableVersions(versions...)
109 }
110
111 // EnableVersions enables all resources in a given groupVersion.
112 // This will remove any preferences previously set on individual resources.
113 func (o *ResourceConfig) EnableVersions(versions ...schema.GroupVersion) {
114 for _, version := range versions {
115 o.GroupVersionConfigs[version] = true
116
117 // a preference about a version takes priority over the previously set resources
118 o.removeMatchingResourcePreferences(resourceMatcherForVersion(version))
119 }
120
121 }
122
123 func (o *ResourceConfig) ExplicitlyEnableVersions(versions ...schema.GroupVersion) {
124 for _, version := range versions {
125 o.ExplicitGroupVersionConfigs[version] = true
126 }
127 o.EnableVersions(versions...)
128 }
129
130 // TODO this must be removed and we enable/disable individual resources.
131 func (o *ResourceConfig) versionEnabled(version schema.GroupVersion) bool {
132 return o.GroupVersionConfigs[version]
133 }
134
135 func (o *ResourceConfig) DisableResources(resources ...schema.GroupVersionResource) {
136 for _, resource := range resources {
137 o.ResourceConfigs[resource] = false
138 }
139 }
140
141 func (o *ResourceConfig) ExplicitlyDisableResources(resources ...schema.GroupVersionResource) {
142 for _, resource := range resources {
143 o.ExplicitResourceConfigs[resource] = false
144 }
145 o.DisableResources(resources...)
146 }
147
148 func (o *ResourceConfig) EnableResources(resources ...schema.GroupVersionResource) {
149 for _, resource := range resources {
150 o.ResourceConfigs[resource] = true
151 }
152 }
153
154 func (o *ResourceConfig) ExplicitlyEnableResources(resources ...schema.GroupVersionResource) {
155 for _, resource := range resources {
156 o.ExplicitResourceConfigs[resource] = true
157 }
158 o.EnableResources(resources...)
159 }
160
161 func (o *ResourceConfig) ResourceEnabled(resource schema.GroupVersionResource) bool {
162 // if a resource is explicitly set, that takes priority over the preference of the version.
163 resourceEnabled, explicitlySet := o.ResourceConfigs[resource]
164 if explicitlySet {
165 return resourceEnabled
166 }
167
168 if !o.versionEnabled(resource.GroupVersion()) {
169 return false
170 }
171 // they are enabled by default.
172 return true
173 }
174
175 func (o *ResourceConfig) AnyResourceForGroupEnabled(group string) bool {
176 for version := range o.GroupVersionConfigs {
177 if version.Group == group {
178 if o.versionEnabled(version) {
179 return true
180 }
181 }
182 }
183 for resource := range o.ResourceConfigs {
184 if resource.Group == group && o.ResourceEnabled(resource) {
185 return true
186 }
187 }
188
189 return false
190 }
191
192 func (o *ResourceConfig) ResourceExplicitlyEnabled(resource schema.GroupVersionResource) bool {
193 resourceEnabled, explicitlySet := o.ExplicitResourceConfigs[resource]
194 if explicitlySet {
195 return resourceEnabled
196 }
197 return false
198 }
199
200 func (o *ResourceConfig) VersionExplicitlyEnabled(version schema.GroupVersion) bool {
201 versionEnabled, explicitlySet := o.ExplicitGroupVersionConfigs[version]
202 if explicitlySet {
203 return versionEnabled
204 }
205 return false
206 }