forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decorator.go
60 lines (51 loc) · 2.1 KB
/
decorator.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
package namespaceconditions
import (
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apiserver/pkg/admission"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
corev1lister "k8s.io/client-go/listers/core/v1"
)
// this is a list of namespaces with special meaning. The kube ones are here in particular because
// we don't control their creation or labeling on their creation
var runLevelZeroNamespaces = sets.NewString("kube-system", "kube-public")
var runLevelOneNamespaces = sets.NewString("openshift-node", "openshift-infra", "openshift")
func init() {
runLevelOneNamespaces.Insert(runLevelZeroNamespaces.List()...)
}
// NamespaceLabelConditions provides a decorator that can delegate and conditionally add label conditions
type NamespaceLabelConditions struct {
NamespaceClient corev1client.NamespacesGetter
NamespaceLister corev1lister.NamespaceLister
SkipLevelZeroNames sets.String
SkipLevelOneNames sets.String
}
func (d *NamespaceLabelConditions) WithNamespaceLabelConditions(admissionPlugin admission.Interface, name string) admission.Interface {
switch {
case d.SkipLevelOneNames.Has(name):
// return a decorated admission plugin that skips runlevel 0 and 1 namespaces based on name (for known values) and
// label.
return &pluginHandlerWithNamespaceNameConditions{
admissionPlugin: &pluginHandlerWithNamespaceLabelConditions{
admissionPlugin: admissionPlugin,
namespaceClient: d.NamespaceClient,
namespaceLister: d.NamespaceLister,
namespaceSelector: skipRunLevelOneSelector,
},
namespacesToExclude: runLevelOneNamespaces,
}
case d.SkipLevelZeroNames.Has(name):
// return a decorated admission plugin that skips runlevel 0 namespaces based on name (for known values) and
// label.
return &pluginHandlerWithNamespaceNameConditions{
admissionPlugin: &pluginHandlerWithNamespaceLabelConditions{
admissionPlugin: admissionPlugin,
namespaceClient: d.NamespaceClient,
namespaceLister: d.NamespaceLister,
namespaceSelector: skipRunLevelZeroSelector,
},
namespacesToExclude: runLevelZeroNamespaces,
}
default:
return admissionPlugin
}
}