This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
allocator.go
147 lines (127 loc) · 3.88 KB
/
allocator.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
package mcsallocator
import (
"errors"
"fmt"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/registry/core/service/allocator"
"github.com/openshift/origin/pkg/security/mcs"
)
// Interface manages the allocation of ports out of a range. Interface
// should be threadsafe.
type Interface interface {
Allocate(*mcs.Label) error
AllocateNext() (*mcs.Label, error)
Release(*mcs.Label) error
}
var (
ErrFull = errors.New("range is full")
ErrNotInRange = errors.New("provided label is not in the valid range")
ErrAllocated = errors.New("provided label is already allocated")
ErrMismatchedRange = errors.New("the provided label does not match the current label range")
)
type Allocator struct {
r *mcs.Range
alloc allocator.Interface
}
// Allocator implements Interface and Snapshottable
var _ Interface = &Allocator{}
// New creates a Allocator over a UID range, calling factory to construct the backing store.
func New(r *mcs.Range, factory allocator.AllocatorFactory) *Allocator {
return &Allocator{
r: r,
alloc: factory(int(r.Size()), r.String()),
}
}
// NewInMemory creates an in-memory Allocator
func NewInMemory(r *mcs.Range) *Allocator {
factory := func(max int, rangeSpec string) allocator.Interface {
return allocator.NewContiguousAllocationMap(max, rangeSpec)
}
return New(r, factory)
}
// Free returns the count of port left in the range.
func (r *Allocator) Free() int {
return r.alloc.Free()
}
// Allocate attempts to reserve the provided label. ErrNotInRange or
// ErrAllocated will be returned if the label is not valid for this range
// or has already been reserved. ErrFull will be returned if there
// are no labels left.
func (r *Allocator) Allocate(label *mcs.Label) error {
ok, offset := r.contains(label)
if !ok {
return ErrNotInRange
}
allocated, err := r.alloc.Allocate(int(offset))
if err != nil {
return err
}
if !allocated {
return ErrAllocated
}
return nil
}
// AllocateNext reserves one of the labels from the pool. ErrFull may
// be returned if there are no labels left.
func (r *Allocator) AllocateNext() (*mcs.Label, error) {
offset, ok, err := r.alloc.AllocateNext()
if err != nil {
return nil, err
}
if !ok {
return nil, ErrFull
}
label, ok := r.r.LabelAt(uint64(offset))
if !ok {
return nil, ErrNotInRange
}
return label, nil
}
// Release releases the port back to the pool. Releasing an
// unallocated port or a port out of the range is a no-op and
// returns no error.
func (r *Allocator) Release(label *mcs.Label) error {
ok, offset := r.contains(label)
if !ok {
// TODO: log a warning
return nil
}
return r.alloc.Release(int(offset))
}
// Has returns true if the provided port is already allocated and a call
// to Allocate(label) would fail with ErrAllocated.
func (r *Allocator) Has(label *mcs.Label) bool {
ok, offset := r.contains(label)
if !ok {
return false
}
return r.alloc.Has(int(offset))
}
// Snapshot saves the current state of the pool.
func (r *Allocator) Snapshot(dst *api.RangeAllocation) error {
snapshottable, ok := r.alloc.(allocator.Snapshottable)
if !ok {
return fmt.Errorf("not a snapshottable allocator")
}
rangeString, data := snapshottable.Snapshot()
dst.Range = rangeString
dst.Data = data
return nil
}
// Restore restores the pool to the previously captured state. ErrMismatchedNetwork
// is returned if the provided port range doesn't exactly match the previous range.
func (r *Allocator) Restore(into *mcs.Range, data []byte) error {
if into.String() != r.r.String() {
return ErrMismatchedRange
}
snapshottable, ok := r.alloc.(allocator.Snapshottable)
if !ok {
return fmt.Errorf("not a snapshottable allocator")
}
return snapshottable.Restore(into.String(), data)
}
// contains returns true and the offset if the label is in the range (and aligned), and false
// and nil otherwise.
func (r *Allocator) contains(label *mcs.Label) (bool, uint64) {
return r.r.Offset(label)
}