forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
admission.go
148 lines (125 loc) · 4.72 KB
/
admission.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
package lifecycle
import (
"fmt"
"io"
"math/rand"
"time"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/admission"
kapi "k8s.io/kubernetes/pkg/api"
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
kadmission "k8s.io/kubernetes/pkg/kubeapiserver/admission"
"github.com/openshift/origin/pkg/api/latest"
authorizationapi "github.com/openshift/origin/pkg/authorization/apis/authorization"
oadmission "github.com/openshift/origin/pkg/cmd/server/admission"
"github.com/openshift/origin/pkg/project/cache"
projectutil "github.com/openshift/origin/pkg/project/util"
)
// TODO: modify the upstream plug-in so this can be collapsed
// need ability to specify a RESTMapper on upstream version
func Register(plugins *admission.Plugins) {
plugins.Register("OriginNamespaceLifecycle",
func(config io.Reader) (admission.Interface, error) {
return NewLifecycle(recommendedCreatableResources)
})
}
type lifecycle struct {
client kclientset.Interface
cache *cache.ProjectCache
// creatableResources is a set of resources that can be created even if the namespace is terminating
creatableResources map[schema.GroupResource]bool
}
var recommendedCreatableResources = map[schema.GroupResource]bool{
authorizationapi.Resource("resourceaccessreviews"): true,
authorizationapi.Resource("localresourceaccessreviews"): true,
authorizationapi.Resource("subjectaccessreviews"): true,
authorizationapi.Resource("localsubjectaccessreviews"): true,
authorizationapi.Resource("selfsubjectrulesreviews"): true,
authorizationapi.Resource("subjectrulesreviews"): true,
authorizationapi.LegacyResource("resourceaccessreviews"): true,
authorizationapi.LegacyResource("localresourceaccessreviews"): true,
authorizationapi.LegacyResource("subjectaccessreviews"): true,
authorizationapi.LegacyResource("localsubjectaccessreviews"): true,
authorizationapi.LegacyResource("selfsubjectrulesreviews"): true,
authorizationapi.LegacyResource("subjectrulesreviews"): true,
}
var _ = oadmission.WantsProjectCache(&lifecycle{})
var _ = kadmission.WantsInternalKubeClientSet(&lifecycle{})
// Admit enforces that a namespace must have the openshift finalizer associated with it in order to create origin API objects within it
func (e *lifecycle) Admit(a admission.Attributes) (err error) {
if len(a.GetNamespace()) == 0 {
return nil
}
// only pay attention to origin resources
if !latest.OriginKind(a.GetKind()) {
return nil
}
// always allow creatable resources through. These requests should always be allowed.
if e.creatableResources[a.GetResource().GroupResource()] {
return nil
}
groupMeta, err := kapi.Registry.Group(a.GetKind().Group)
if err != nil {
return err
}
mapping, err := groupMeta.RESTMapper.RESTMapping(a.GetKind().GroupKind())
if err != nil {
glog.V(4).Infof("Ignoring life-cycle enforcement for resource %v; no associated default version and kind could be found.", a.GetResource())
return nil
}
if mapping.Scope.Name() != meta.RESTScopeNameNamespace {
return nil
}
if !e.cache.Running() {
return admission.NewForbidden(a, err)
}
namespace, err := e.cache.GetNamespace(a.GetNamespace())
if err != nil {
return admission.NewForbidden(a, err)
}
// in case of concurrency issues, we will retry this logic
numRetries := 10
interval := time.Duration(rand.Int63n(90)+int64(10)) * time.Millisecond
for retry := 1; retry <= numRetries; retry++ {
// associate this namespace with openshift
_, err = projectutil.Associate(e.client, namespace)
if err == nil {
break
}
// we have exhausted all reasonable efforts to retry so give up now
if retry == numRetries {
return admission.NewForbidden(a, err)
}
// get the latest namespace for the next pass in case of resource version updates
time.Sleep(interval)
// it's possible the namespace actually was deleted, so just forbid if this occurs
namespace, err = e.client.Core().Namespaces().Get(a.GetNamespace(), metav1.GetOptions{})
if err != nil {
return admission.NewForbidden(a, err)
}
}
return nil
}
func (e *lifecycle) Handles(operation admission.Operation) bool {
return operation == admission.Create
}
func (e *lifecycle) SetProjectCache(c *cache.ProjectCache) {
e.cache = c
}
func (q *lifecycle) SetInternalKubeClientSet(c kclientset.Interface) {
q.client = c
}
func (e *lifecycle) Validate() error {
if e.cache == nil {
return fmt.Errorf("project lifecycle plugin needs a project cache")
}
return nil
}
func NewLifecycle(creatableResources map[schema.GroupResource]bool) (admission.Interface, error) {
return &lifecycle{
creatableResources: creatableResources,
}, nil
}