-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgidallocator.go
282 lines (233 loc) · 7.24 KB
/
gidallocator.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
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gidallocator
import (
"fmt"
"math"
"strconv"
"strings"
"sync"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
glog "k8s.io/klog"
"sigs.k8s.io/sig-storage-lib-external-provisioner/allocator"
"sigs.k8s.io/sig-storage-lib-external-provisioner/controller"
"sigs.k8s.io/sig-storage-lib-external-provisioner/util"
)
const (
// VolumeGidAnnotationKey is the key of the annotation on the PersistentVolume
// object that specifies a supplemental GID.
VolumeGidAnnotationKey = "pv.beta.kubernetes.io/gid"
defaultGidMin = 2000
defaultGidMax = math.MaxInt32
// absoluteGidMin/Max are currently the same as the
// default values, but they play a different role and
// could take a different value. Only thing we need is:
// absGidMin <= defGidMin <= defGidMax <= absGidMax
absoluteGidMin = 2000
absoluteGidMax = math.MaxInt32
)
// Allocator allocates GIDs to PVs. It allocates from per-SC ranges and ensures
// that no two PVs of the same SC get the same GID.
type Allocator struct {
client kubernetes.Interface
gidTable map[string]*allocator.MinMaxAllocator
gidTableLock sync.Mutex
}
// New creates a new GID Allocator
func New(client kubernetes.Interface) Allocator {
return Allocator{
client: client,
gidTable: make(map[string]*allocator.MinMaxAllocator),
}
}
// AllocateNext allocates the next available GID for the given ProvisionOptions
// (claim's options for a volume it wants) from the appropriate GID table.
func (a *Allocator) AllocateNext(options controller.ProvisionOptions) (int, error) {
class := util.GetPersistentVolumeClaimClass(options.PVC)
gidMin, gidMax, err := parseClassParameters(options.StorageClass.Parameters)
if err != nil {
return 0, err
}
gidTable, err := a.getGidTable(class, gidMin, gidMax)
if err != nil {
return 0, fmt.Errorf("failed to get gidTable: %v", err)
}
gid, _, err := gidTable.AllocateNext()
if err != nil {
return 0, fmt.Errorf("failed to reserve gid from table: %v", err)
}
return gid, nil
}
// Release releases the given volume's allocated GID from the appropriate GID
// table.
func (a *Allocator) Release(volume *v1.PersistentVolume) error {
class, err := a.client.StorageV1().StorageClasses().Get(util.GetPersistentVolumeClass(volume), metav1.GetOptions{})
gidMin, gidMax, err := parseClassParameters(class.Parameters)
if err != nil {
return err
}
gid, exists, err := getGid(volume)
if err != nil {
glog.Error(err)
} else if exists {
gidTable, err := a.getGidTable(class.Name, gidMin, gidMax)
if err != nil {
return fmt.Errorf("failed to get gidTable: %v", err)
}
err = gidTable.Release(gid)
if err != nil {
return fmt.Errorf("failed to release gid %v: %v", gid, err)
}
}
return nil
}
//
// Return the gid table for a storage class.
// - If this is the first time, fill it with all the gids
// used in PVs of this storage class by traversing the PVs.
// - Adapt the range of the table to the current range of the SC.
//
func (a *Allocator) getGidTable(className string, min int, max int) (*allocator.MinMaxAllocator, error) {
var err error
a.gidTableLock.Lock()
gidTable, ok := a.gidTable[className]
a.gidTableLock.Unlock()
if ok {
err = gidTable.SetRange(min, max)
if err != nil {
return nil, err
}
return gidTable, nil
}
// create a new table and fill it
newGidTable, err := allocator.NewMinMaxAllocator(0, absoluteGidMax)
if err != nil {
return nil, err
}
// collect gids with the full range
err = a.collectGids(className, newGidTable)
if err != nil {
return nil, err
}
// and only reduce the range afterwards
err = newGidTable.SetRange(min, max)
if err != nil {
return nil, err
}
// if in the meantime a table appeared, use it
a.gidTableLock.Lock()
defer a.gidTableLock.Unlock()
gidTable, ok = a.gidTable[className]
if ok {
err = gidTable.SetRange(min, max)
if err != nil {
return nil, err
}
return gidTable, nil
}
a.gidTable[className] = newGidTable
return newGidTable, nil
}
// Traverse the PVs, fetching all the GIDs from those
// in a given storage class, and mark them in the table.
//
func (a *Allocator) collectGids(className string, gidTable *allocator.MinMaxAllocator) error {
pvList, err := a.client.CoreV1().PersistentVolumes().List(metav1.ListOptions{})
if err != nil {
glog.Errorf("failed to get existing persistent volumes")
return err
}
for _, pv := range pvList.Items {
if util.GetPersistentVolumeClass(&pv) != className {
continue
}
pvName := pv.ObjectMeta.Name
gidStr, ok := pv.Annotations[VolumeGidAnnotationKey]
if !ok {
glog.Warningf("no gid found in pv '%v'", pvName)
continue
}
gid, err := convertGid(gidStr)
if err != nil {
glog.Error(err)
continue
}
_, err = gidTable.Allocate(gid)
if err == allocator.ErrConflict {
glog.Warningf("gid %v found in pv %v was already allocated", gid, pvName)
} else if err != nil {
glog.Errorf("failed to store gid %v found in pv '%v': %v", gid, pvName, err)
return err
}
}
return nil
}
func parseClassParameters(params map[string]string) (int, int, error) {
gidMin := defaultGidMin
gidMax := defaultGidMax
for k, v := range params {
switch strings.ToLower(k) {
case "gidmin":
parseGidMin, err := convertGid(v)
if err != nil {
return 0, 0, fmt.Errorf("invalid value %s for parameter %s: %v", v, k, err)
}
if parseGidMin < absoluteGidMin {
return 0, 0, fmt.Errorf("gidMin must be >= %v", absoluteGidMin)
}
if parseGidMin > absoluteGidMax {
return 0, 0, fmt.Errorf("gidMin must be <= %v", absoluteGidMax)
}
gidMin = parseGidMin
case "gidmax":
parseGidMax, err := convertGid(v)
if err != nil {
return 0, 0, fmt.Errorf("invalid value %s for parameter %s: %v", v, k, err)
}
if parseGidMax < absoluteGidMin {
return 0, 0, fmt.Errorf("gidMax must be >= %v", absoluteGidMin)
}
if parseGidMax > absoluteGidMax {
return 0, 0, fmt.Errorf("gidMax must be <= %v", absoluteGidMax)
}
gidMax = parseGidMax
}
}
if gidMin > gidMax {
return 0, 0, fmt.Errorf("gidMax %v is not >= gidMin %v", gidMax, gidMin)
}
return gidMin, gidMax, nil
}
func getGid(volume *v1.PersistentVolume) (int, bool, error) {
gidStr, ok := volume.Annotations[VolumeGidAnnotationKey]
if !ok {
return 0, false, nil
}
gid, err := convertGid(gidStr)
return gid, true, err
}
func convertGid(gidString string) (int, error) {
gid64, err := strconv.ParseInt(gidString, 10, 32)
if err != nil {
return 0, fmt.Errorf("failed to parse gid %v ", gidString)
}
if gid64 < 0 {
return 0, fmt.Errorf("negative GIDs are not allowed: %v", gidString)
}
// ParseInt returns a int64, but since we parsed only
// for 32 bit, we can cast to int without loss:
gid := int(gid64)
return gid, nil
}