forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
namespace_scc_allocation_controller.go
339 lines (299 loc) · 10.2 KB
/
namespace_scc_allocation_controller.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
package controller
import (
"fmt"
"math/big"
"reflect"
"time"
"github.com/golang/glog"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/util/wait"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
corev1informers "k8s.io/client-go/informers/core/v1"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
corev1listers "k8s.io/client-go/listers/core/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/workqueue"
coreapi "k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/controller"
securityv1 "github.com/openshift/api/security/v1"
securityv1client "github.com/openshift/client-go/security/clientset/versioned/typed/security/v1"
"github.com/openshift/origin/pkg/security"
"github.com/openshift/origin/pkg/security/mcs"
"github.com/openshift/origin/pkg/security/uid"
"github.com/openshift/origin/pkg/security/uidallocator"
)
const (
controllerName = "namespace-security-allocation-controller"
rangeName = "scc-uid"
)
// NamespaceSCCAllocationController allocates uids/labels for namespaces
type NamespaceSCCAllocationController struct {
requiredUIDRange *uid.Range
mcsAllocator MCSAllocationFunc
nsLister corev1listers.NamespaceLister
nsListerSynced cache.InformerSynced
currentUIDRangeAllocation *securityv1.RangeAllocation
namespaceClient corev1client.NamespaceInterface
rangeAllocationClient securityv1client.RangeAllocationsGetter
queue workqueue.RateLimitingInterface
}
func NewNamespaceSCCAllocationController(
namespaceInformer corev1informers.NamespaceInformer,
client corev1client.NamespaceInterface,
rangeAllocationClient securityv1client.RangeAllocationsGetter,
requiredUIDRange *uid.Range,
mcs MCSAllocationFunc,
) *NamespaceSCCAllocationController {
c := &NamespaceSCCAllocationController{
requiredUIDRange: requiredUIDRange,
mcsAllocator: mcs,
namespaceClient: client,
rangeAllocationClient: rangeAllocationClient,
nsLister: namespaceInformer.Lister(),
nsListerSynced: namespaceInformer.Informer().HasSynced,
queue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), controllerName),
}
namespaceInformer.Informer().AddEventHandlerWithResyncPeriod(
cache.ResourceEventHandlerFuncs{
AddFunc: c.enqueueNamespace,
UpdateFunc: func(oldObj, newObj interface{}) {
c.enqueueNamespace(newObj)
},
},
10*time.Minute,
)
return c
}
// Run starts the workers for this controller.
func (c *NamespaceSCCAllocationController) Run(stopCh <-chan struct{}) {
defer utilruntime.HandleCrash()
defer c.queue.ShutDown()
defer glog.V(1).Infof("Shutting down")
// Wait for the stores to fill
if !controller.WaitForCacheSync(controllerName, stopCh, c.nsListerSynced) {
return
}
glog.V(1).Infof("Repairing SCC UID Allocations")
if err := c.WaitForRepair(stopCh); err != nil {
// this is consistent with previous behavior
glog.Fatal(err)
}
glog.V(1).Infof("Repair complete")
go c.worker()
<-stopCh
}
// syncNamespace will sync the namespace with the given key.
// This function is not meant to be invoked concurrently with the same key.
func (c *NamespaceSCCAllocationController) syncNamespace(key string) error {
ns, err := c.nsLister.Get(key)
if apierrors.IsNotFound(err) {
return nil
}
if err != nil {
return err
}
if _, ok := ns.Annotations[security.UIDRangeAnnotation]; ok {
return nil
}
return c.allocate(ns)
}
func (c *NamespaceSCCAllocationController) allocate(ns *corev1.Namespace) error {
// unless we affirmatively succeed, clear the local state to try again
success := false
defer func() {
if success {
return
}
c.currentUIDRangeAllocation = nil
}()
// if we don't have the current state, go get it
if c.currentUIDRangeAllocation == nil {
newRange, err := c.rangeAllocationClient.RangeAllocations().Get(rangeName, metav1.GetOptions{})
if err != nil {
return err
}
c.currentUIDRangeAllocation = newRange
}
// do uid allocation. We reserve the UID we want first, lock it in etcd, then update the namespace.
// We allocate by reading in a giant bit int bitmap (one bit per offset location), finding the next step,
// then calculating the offset location
uidRange, err := uid.ParseRange(c.currentUIDRangeAllocation.Range)
if err != nil {
return err
}
if !reflect.DeepEqual(*uidRange, *c.requiredUIDRange) {
return fmt.Errorf("conflicting UID range; expected %#v, got %#v", *c.requiredUIDRange, *uidRange)
}
allocatedBitMapInt := big.NewInt(0).SetBytes(c.currentUIDRangeAllocation.Data)
bitIndex, found := allocateNextContiguousBit(allocatedBitMapInt, int(uidRange.Size()))
if !found {
return fmt.Errorf("uid range exceeded")
}
allocatedBitMapInt = allocatedBitMapInt.SetBit(allocatedBitMapInt, bitIndex, 1)
newRangeAllocation := c.currentUIDRangeAllocation.DeepCopy()
newRangeAllocation.Data = allocatedBitMapInt.Bytes()
actualRangeAllocation, err := c.rangeAllocationClient.RangeAllocations().Update(newRangeAllocation)
if err != nil {
return err
}
c.currentUIDRangeAllocation = actualRangeAllocation
block, ok := uidRange.BlockAt(uint32(bitIndex))
if !ok {
return fmt.Errorf("%d not in range", bitIndex)
}
// Now modify the namespace
nsCopy := ns.DeepCopy()
if nsCopy.Annotations == nil {
nsCopy.Annotations = make(map[string]string)
}
nsCopy.Annotations[security.UIDRangeAnnotation] = block.String()
nsCopy.Annotations[security.SupplementalGroupsAnnotation] = block.String()
if _, ok := nsCopy.Annotations[security.MCSAnnotation]; !ok {
if label := c.mcsAllocator(block); label != nil {
nsCopy.Annotations[security.MCSAnnotation] = label.String()
}
}
_, err = c.namespaceClient.Update(nsCopy)
if apierrors.IsNotFound(err) {
return nil
}
if err != nil {
return err
}
success = true
return nil
}
// allocateNextContiguousBit finds a free bit in the int and returns which one it is and whether it succeeded
func allocateNextContiguousBit(allocated *big.Int, max int) (int, bool) {
for i := 0; i < max; i++ {
if allocated.Bit(i) == 0 {
return i, true
}
}
return 0, false
}
func (c *NamespaceSCCAllocationController) WaitForRepair(stopCh <-chan struct{}) error {
return wait.PollImmediate(10*time.Second, 5*time.Minute, func() (bool, error) {
select {
case <-stopCh:
return true, nil
default:
}
err := c.Repair()
if err == nil {
return true, nil
}
utilruntime.HandleError(err)
return false, nil
})
}
func (c *NamespaceSCCAllocationController) Repair() error {
// TODO: (per smarterclayton) if Get() or List() is a weak consistency read,
// or if they are executed against different leaders,
// the ordering guarantee required to ensure no item is allocated twice is violated.
// List must return a ResourceVersion higher than the etcd index Get,
// and the release code must not release items that have allocated but not yet been created
// See #8295
// get the curr so we have a resourceVersion to pin to
uidRange, err := c.rangeAllocationClient.RangeAllocations().Get(rangeName, metav1.GetOptions{})
needCreate := apierrors.IsNotFound(err)
if err != nil && !needCreate {
return err
}
if needCreate {
uidRange = &securityv1.RangeAllocation{ObjectMeta: metav1.ObjectMeta{Name: rangeName}}
}
uids := uidallocator.NewInMemory(c.requiredUIDRange)
nsList, err := c.nsLister.List(labels.Everything())
if err != nil {
return err
}
for _, ns := range nsList {
value, ok := ns.Annotations[security.UIDRangeAnnotation]
if !ok {
continue
}
block, err := uid.ParseBlock(value)
if err != nil {
continue
}
switch err := uids.Allocate(block); err {
case nil:
case uidallocator.ErrNotInRange, uidallocator.ErrAllocated:
continue
case uidallocator.ErrFull:
// TODO: send event
return fmt.Errorf("the UID range %s is full; you must widen the range in order to allocate more UIDs", c.requiredUIDRange)
default:
return fmt.Errorf("unable to allocate UID block %s for namespace %s due to an unknown error, exiting: %v", block, ns.Name, err)
}
}
newRangeAllocation := &coreapi.RangeAllocation{}
if err := uids.Snapshot(newRangeAllocation); err != nil {
return err
}
uidRange.Range = newRangeAllocation.Range
uidRange.Data = newRangeAllocation.Data
if needCreate {
if _, err := c.rangeAllocationClient.RangeAllocations().Create(uidRange); err != nil {
return err
}
return nil
}
if _, err := c.rangeAllocationClient.RangeAllocations().Update(uidRange); err != nil {
return err
}
return nil
}
type MCSAllocationFunc func(uid.Block) *mcs.Label
// DefaultMCSAllocation returns a label from the MCS range that matches the offset
// within the overall range. blockSize must be a positive integer representing the
// number of labels to jump past in the category space (if 1, range == label, if 2
// each range will have two labels).
func DefaultMCSAllocation(from *uid.Range, to *mcs.Range, blockSize int) MCSAllocationFunc {
return func(block uid.Block) *mcs.Label {
ok, offset := from.Offset(block)
if !ok {
return nil
}
if blockSize > 0 {
offset = offset * uint32(blockSize)
}
label, _ := to.LabelAt(uint64(offset))
return label
}
}
func (c *NamespaceSCCAllocationController) enqueueNamespace(obj interface{}) {
ns, ok := obj.(*corev1.Namespace)
if !ok {
return
}
c.queue.Add(ns.Name)
}
// worker runs a worker thread that just dequeues items, processes them, and marks them done.
// It enforces that the syncHandler is never invoked concurrently with the same key.
func (c *NamespaceSCCAllocationController) worker() {
for c.work() {
}
}
// work returns true if the worker thread should continue
func (c *NamespaceSCCAllocationController) work() bool {
key, quit := c.queue.Get()
if quit {
return false
}
defer c.queue.Done(key)
if err := c.syncNamespace(key.(string)); err == nil {
// this means the request was successfully handled. We should "forget" the item so that any retry
// later on is reset
c.queue.Forget(key)
} else {
// if we had an error it means that we didn't handle it, which means that we want to requeue the work
utilruntime.HandleError(fmt.Errorf("error syncing namespace, it will be retried: %v", err))
c.queue.AddRateLimited(key)
}
return true
}