forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chain_builder.go
377 lines (344 loc) · 12.4 KB
/
chain_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package admission
import (
"bytes"
"io"
"io/ioutil"
"net"
"os"
"reflect"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apiserver/pkg/admission"
"k8s.io/apiserver/pkg/admission/plugin/namespace/lifecycle"
noderestriction "k8s.io/kubernetes/plugin/pkg/admission/noderestriction"
expandpvcadmission "k8s.io/kubernetes/plugin/pkg/admission/persistentvolume/resize"
saadmit "k8s.io/kubernetes/plugin/pkg/admission/serviceaccount"
storageclassdefaultadmission "k8s.io/kubernetes/plugin/pkg/admission/storage/storageclass/setdefault"
oadmission "github.com/openshift/origin/pkg/cmd/server/admission"
configapi "github.com/openshift/origin/pkg/cmd/server/apis/config"
configapilatest "github.com/openshift/origin/pkg/cmd/server/apis/config/latest"
imageadmission "github.com/openshift/origin/pkg/image/admission"
imagepolicy "github.com/openshift/origin/pkg/image/admission/apis/imagepolicy"
ingressadmission "github.com/openshift/origin/pkg/ingress/admission"
overrideapi "github.com/openshift/origin/pkg/quota/admission/apis/clusterresourceoverride"
sccadmission "github.com/openshift/origin/pkg/security/admission"
serviceadmit "github.com/openshift/origin/pkg/service/admission"
)
var (
// these are admission plugins that cannot be applied until after the kubeapiserver starts.
// TODO if nothing comes to mind in 3.10, kill this
SkipRunLevelZeroPlugins = sets.NewString()
// these are admission plugins that cannot be applied until after the openshiftapiserver apiserver starts.
SkipRunLevelOnePlugins = sets.NewString(
"ProjectRequestLimit",
"openshift.io/RestrictSubjectBindings",
"openshift.io/ClusterResourceQuota",
imagepolicy.PluginName,
overrideapi.PluginName,
"OriginPodNodeEnvironment",
"RunOnceDuration",
sccadmission.PluginName,
"SCCExecRestrictions",
)
// openshiftAdmissionControlPlugins gives the in-order default admission chain for openshift resources.
openshiftAdmissionControlPlugins = []string{
"ProjectRequestLimit",
"OriginNamespaceLifecycle",
"openshift.io/RestrictSubjectBindings",
"openshift.io/JenkinsBootstrapper",
"openshift.io/BuildConfigSecretInjector",
"BuildByStrategy",
imageadmission.PluginName,
"PodNodeConstraints",
"OwnerReferencesPermissionEnforcement",
"Initializers",
"MutatingAdmissionWebhook",
"ValidatingAdmissionWebhook",
"ResourceQuota",
}
// kubeAdmissionPlugins gives the in-order default admission chain for kube resources.
kubeAdmissionPlugins = []string{
"AlwaysAdmit",
"NamespaceAutoProvision",
"NamespaceExists",
lifecycle.PluginName,
"EventRateLimit",
"RunOnceDuration",
"PodNodeConstraints",
"OriginPodNodeEnvironment",
"PodNodeSelector",
overrideapi.PluginName,
serviceadmit.ExternalIPPluginName,
serviceadmit.RestrictedEndpointsPluginName,
imagepolicy.PluginName,
"ImagePolicyWebhook",
"PodPreset",
"InitialResources",
"LimitRanger",
"ServiceAccount",
noderestriction.PluginName,
"SecurityContextDeny",
sccadmission.PluginName,
"PodSecurityPolicy",
"DenyEscalatingExec",
"DenyExecOnPrivileged",
storageclassdefaultadmission.PluginName,
expandpvcadmission.PluginName,
"AlwaysPullImages",
"LimitPodHardAntiAffinityTopology",
"SCCExecRestrictions",
"PersistentVolumeLabel",
"OwnerReferencesPermissionEnforcement",
ingressadmission.IngressAdmission,
"Priority",
"ExtendedResourceToleration",
"DefaultTolerationSeconds",
"StorageObjectInUseProtection",
"Initializers",
"MutatingAdmissionWebhook",
"ValidatingAdmissionWebhook",
"PodTolerationRestriction",
"AlwaysDeny",
// NOTE: ResourceQuota and ClusterResourceQuota must be the last 2 plugins.
// DO NOT ADD ANY PLUGINS AFTER THIS LINE!
"ResourceQuota",
"openshift.io/ClusterResourceQuota",
}
// combinedAdmissionControlPlugins gives the in-order default admission chain for all resources resources.
// When possible, this list is used. The set of openshift+kube chains must exactly match this set. In addition,
// the order specified in the openshift and kube chains must match the order here.
combinedAdmissionControlPlugins = []string{
"AlwaysAdmit",
"NamespaceAutoProvision",
"NamespaceExists",
lifecycle.PluginName,
"EventRateLimit",
"ProjectRequestLimit",
"OriginNamespaceLifecycle",
"openshift.io/RestrictSubjectBindings",
"openshift.io/JenkinsBootstrapper",
"openshift.io/BuildConfigSecretInjector",
"BuildByStrategy",
imageadmission.PluginName,
"RunOnceDuration",
"PodNodeConstraints",
"OriginPodNodeEnvironment",
"PodNodeSelector",
overrideapi.PluginName,
serviceadmit.ExternalIPPluginName,
serviceadmit.RestrictedEndpointsPluginName,
imagepolicy.PluginName,
"ImagePolicyWebhook",
"PodPreset",
"InitialResources",
"LimitRanger",
"ServiceAccount",
noderestriction.PluginName,
"SecurityContextDeny",
sccadmission.PluginName,
"PodSecurityPolicy",
"DenyEscalatingExec",
"DenyExecOnPrivileged",
storageclassdefaultadmission.PluginName,
expandpvcadmission.PluginName,
"AlwaysPullImages",
"LimitPodHardAntiAffinityTopology",
"SCCExecRestrictions",
"PersistentVolumeLabel",
"OwnerReferencesPermissionEnforcement",
ingressadmission.IngressAdmission,
"Priority",
"ExtendedResourceToleration",
"DefaultTolerationSeconds",
"StorageObjectInUseProtection",
"Initializers",
"MutatingAdmissionWebhook",
"ValidatingAdmissionWebhook",
"PodTolerationRestriction",
"AlwaysDeny",
// NOTE: ResourceQuota and ClusterResourceQuota must be the last 2 plugins.
// DO NOT ADD ANY PLUGINS AFTER THIS LINE!
"ResourceQuota",
"openshift.io/ClusterResourceQuota",
}
)
// fixupAdmissionPlugins fixes the input plugins to handle deprecation and duplicates.
func fixupAdmissionPlugins(plugins []string) []string {
result := replace(plugins, "openshift.io/OriginResourceQuota", "ResourceQuota")
result = dedupe(result)
return result
}
func NewAdmissionChains(
options configapi.MasterConfig,
admissionInitializer admission.PluginInitializer,
admissionDecorator admission.Decorator,
) (admission.Interface, error) {
admissionPluginConfigFilename := ""
if len(options.KubernetesMasterConfig.APIServerArguments["admission-control-config-file"]) > 0 {
admissionPluginConfigFilename = options.KubernetesMasterConfig.APIServerArguments["admission-control-config-file"][0]
} else {
pluginConfig := map[string]configapi.AdmissionPluginConfig{}
for pluginName, config := range options.AdmissionConfig.PluginConfig {
pluginConfig[pluginName] = *config
}
upstreamAdmissionConfig, err := configapilatest.ConvertOpenshiftAdmissionConfigToKubeAdmissionConfig(pluginConfig)
if err != nil {
return nil, err
}
configBytes, err := configapilatest.WriteYAML(upstreamAdmissionConfig)
if err != nil {
return nil, err
}
tempFile, err := ioutil.TempFile("", "master-config.yaml")
if err != nil {
return nil, err
}
defer os.Remove(tempFile.Name())
if _, err := tempFile.Write(configBytes); err != nil {
return nil, err
}
tempFile.Close()
admissionPluginConfigFilename = tempFile.Name()
}
admissionPluginNames := combinedAdmissionControlPlugins
if len(options.AdmissionConfig.PluginOrderOverride) > 0 {
admissionPluginNames = options.AdmissionConfig.PluginOrderOverride
}
admissionPluginNames = fixupAdmissionPlugins(admissionPluginNames)
admissionChain, err := newAdmissionChainFunc(admissionPluginNames, admissionPluginConfigFilename, options, admissionInitializer, admissionDecorator)
if err != nil {
return nil, err
}
return admissionChain, err
}
// newAdmissionChainFunc is for unit testing only. You should NEVER OVERRIDE THIS outside of a unit test.
var newAdmissionChainFunc = newAdmissionChain
func newAdmissionChain(pluginNames []string, admissionConfigFilename string, options configapi.MasterConfig, admissionInitializer admission.PluginInitializer, admissionDecorator admission.Decorator) (admission.Interface, error) {
plugins := []admission.Interface{}
for _, pluginName := range pluginNames {
var (
plugin admission.Interface
)
switch pluginName {
case serviceadmit.ExternalIPPluginName:
// this needs to be moved upstream to be part of core config
reject, admit, err := serviceadmit.ParseRejectAdmitCIDRRules(options.NetworkConfig.ExternalIPNetworkCIDRs)
if err != nil {
// should have been caught with validation
return nil, err
}
allowIngressIP := false
if _, ipNet, err := net.ParseCIDR(options.NetworkConfig.IngressIPNetworkCIDR); err == nil && !ipNet.IP.IsUnspecified() {
allowIngressIP = true
}
plugin = serviceadmit.NewExternalIPRanger(reject, admit, allowIngressIP)
admissionInitializer.Initialize(plugin)
case serviceadmit.RestrictedEndpointsPluginName:
// we need to set some customer parameters, so create by hand
var restricted []string
restricted = append(restricted, options.NetworkConfig.ServiceNetworkCIDR)
for _, cidr := range options.NetworkConfig.ClusterNetworks {
restricted = append(restricted, cidr.CIDR)
}
restrictedNetworks, err := serviceadmit.ParseSimpleCIDRRules(restricted)
if err != nil {
// should have been caught with validation
return nil, err
}
plugin = serviceadmit.NewRestrictedEndpointsAdmission(restrictedNetworks)
admissionInitializer.Initialize(plugin)
case saadmit.PluginName:
// we need to set some custom parameters on the service account admission controller, so create that one by hand
saAdmitter := saadmit.NewServiceAccount()
saAdmitter.LimitSecretReferences = options.ServiceAccountConfig.LimitSecretReferences
plugin = saAdmitter
admissionInitializer.Initialize(plugin)
default:
// TODO this needs to be refactored to use the admission scheme we created upstream. I think this holds us for the rebase.
pluginsConfigProvider, err := admission.ReadAdmissionConfiguration([]string{pluginName}, admissionConfigFilename, configapi.Scheme)
if err != nil {
return nil, err
}
plugin, err = OriginAdmissionPlugins.NewFromPlugins([]string{pluginName}, pluginsConfigProvider, admissionInitializer, admissionDecorator)
if err != nil {
// should have been caught with validation
return nil, err
}
if plugin == nil {
continue
}
}
plugins = append(plugins, plugin)
}
// ensure that plugins have been properly initialized
if err := oadmission.Validate(plugins); err != nil {
return nil, err
}
return admission.NewChainHandler(plugins...), nil
}
// replace returns a slice where each instance of the input that is x is replaced with y
func replace(input []string, x, y string) []string {
result := []string{}
for i := range input {
if input[i] == x {
result = append(result, y)
} else {
result = append(result, input[i])
}
}
return result
}
// dedupe removes duplicate items from the input list.
// the last instance of a duplicate is kept in the input list.
func dedupe(input []string) []string {
items := sets.NewString()
result := []string{}
for i := len(input) - 1; i >= 0; i-- {
if items.Has(input[i]) {
continue
}
items.Insert(input[i])
result = append([]string{input[i]}, result...)
}
return result
}
func init() {
// add a filter that will remove DefaultAdmissionConfig
admission.FactoryFilterFn = filterEnableAdmissionConfigs
}
func filterEnableAdmissionConfigs(delegate admission.Factory) admission.Factory {
return func(config io.Reader) (admission.Interface, error) {
config1, config2, err := splitStream(config)
if err != nil {
return nil, err
}
// if the config isn't a DefaultAdmissionConfig, then assume we're enabled (we were called after all)
// if the config *is* a DefaultAdmissionConfig and it explicitly said
obj, err := configapilatest.ReadYAML(config1)
// if we can't read it, let the plugin deal with it
if err != nil {
return delegate(config2)
}
// if nothing was there, let the plugin deal with it
if obj == nil {
return delegate(config2)
}
// if it wasn't a DefaultAdmissionConfig object, let the plugin deal with it
if _, ok := obj.(*configapi.DefaultAdmissionConfig); !ok {
return delegate(config2)
}
// if it was a DefaultAdmissionConfig, then it must have said "enabled" and it wasn't really meant for the
// admission plugin
return delegate(nil)
}
}
// splitStream reads the stream bytes and constructs two copies of it.
func splitStream(config io.Reader) (io.Reader, io.Reader, error) {
if config == nil || reflect.ValueOf(config).IsNil() {
return nil, nil, nil
}
configBytes, err := ioutil.ReadAll(config)
if err != nil {
return nil, nil, err
}
return bytes.NewBuffer(configBytes), bytes.NewBuffer(configBytes), nil
}