-
Notifications
You must be signed in to change notification settings - Fork 82
/
configmaps.go
348 lines (307 loc) · 9.55 KB
/
configmaps.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
340
341
342
343
344
345
346
347
348
// Package configmaps implements tester for ConfigMap.
package configmaps
import (
"context"
"errors"
"fmt"
"math/rand"
"os"
"regexp"
"strings"
"time"
"github.com/aws/aws-k8s-tester/eksconfig"
k8sclient "github.com/aws/aws-k8s-tester/pkg/k8s-client"
"github.com/dustin/go-humanize"
"go.uber.org/zap"
"golang.org/x/time/rate"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
)
// Config defines "ConfigMap" configuration.
type Config struct {
Logger *zap.Logger
Stopc chan struct{}
Sig chan os.Signal
EKSConfig *eksconfig.Config
K8SClient k8sClientSetGetter
}
type k8sClientSetGetter interface {
KubernetesClientSet() *clientset.Clientset
}
// Tester defines ConfigMap tester.
type Tester interface {
// Create creates "ConfigMap" objects to test ConfigMap.
Create() error
// Delete deletes "ConfigMap" and Pods.
Delete() error
}
// New creates a new ConfigMap tester.
func New(cfg Config) (Tester, error) {
return &tester{cfg: cfg, cancel: make(chan struct{})}, nil
}
type tester struct {
cfg Config
deploymentCreated time.Time
cancel chan struct{}
}
func (ts *tester) Create() error {
if ts.cfg.EKSConfig.AddOnConfigMaps.Created {
ts.cfg.Logger.Info("skipping create AddOnConfigMaps")
return nil
}
ts.cfg.EKSConfig.AddOnConfigMaps.Created = true
ts.cfg.EKSConfig.Sync()
createStart := time.Now()
defer func() {
ts.cfg.EKSConfig.AddOnConfigMaps.CreateTook = time.Since(createStart)
ts.cfg.EKSConfig.AddOnConfigMaps.CreateTookString = ts.cfg.EKSConfig.AddOnConfigMaps.CreateTook.String()
ts.cfg.EKSConfig.Sync()
}()
if err := k8sclient.CreateNamespace(ts.cfg.Logger, ts.cfg.K8SClient.KubernetesClientSet(), ts.cfg.EKSConfig.AddOnConfigMaps.Namespace); err != nil {
return err
}
if err := ts.createConfigMaps(); err != nil {
return err
}
return ts.cfg.EKSConfig.Sync()
}
func (ts *tester) Delete() error {
if !ts.cfg.EKSConfig.AddOnConfigMaps.Created {
ts.cfg.Logger.Info("skipping delete AddOnConfigMaps")
return nil
}
deleteStart := time.Now()
defer func() {
ts.cfg.EKSConfig.AddOnConfigMaps.DeleteTook = time.Since(deleteStart)
ts.cfg.EKSConfig.AddOnConfigMaps.DeleteTookString = ts.cfg.EKSConfig.AddOnConfigMaps.DeleteTook.String()
ts.cfg.EKSConfig.Sync()
}()
if err := k8sclient.DeleteNamespaceAndWait(ts.cfg.Logger,
ts.cfg.K8SClient.KubernetesClientSet(),
ts.cfg.EKSConfig.AddOnConfigMaps.Namespace,
k8sclient.DefaultNamespaceDeletionInterval,
k8sclient.DefaultNamespaceDeletionTimeout); err != nil {
return fmt.Errorf("failed to delete ConfigMaps namespace (%v)", err)
}
ts.cfg.EKSConfig.AddOnConfigMaps.Created = false
return ts.cfg.EKSConfig.Sync()
}
// only letters and numbers for ConfigMap key names
var regex = regexp.MustCompile("[^a-zA-Z0-9]+")
const writesFailThreshold = 10
func (ts *tester) createConfigMaps() (err error) {
size := humanize.Bytes(uint64(ts.cfg.EKSConfig.AddOnConfigMaps.Size))
ts.cfg.Logger.Info("creating ConfigMaps",
zap.Int("objects", ts.cfg.EKSConfig.AddOnConfigMaps.Objects),
zap.String("each-size", size),
)
// valid config key must consist of alphanumeric characters
pfx := strings.ToLower(regex.ReplaceAllString(ts.cfg.EKSConfig.Name, ""))
val := randString(ts.cfg.EKSConfig.AddOnConfigMaps.Size)
// overwrite if any
ts.cfg.EKSConfig.AddOnConfigMaps.CreatedNames = make([]string, 0, ts.cfg.EKSConfig.AddOnConfigMaps.Objects)
ts.cfg.EKSConfig.Sync()
if ts.cfg.EKSConfig.AddOnConfigMaps.QPS <= 1 {
err = ts.createConfigMapsSequential(pfx, val, writesFailThreshold)
} else {
err = ts.createConfigMapsParallel(pfx, val, writesFailThreshold)
}
ts.cfg.EKSConfig.Sync()
return err
}
func (ts *tester) createConfigMapsSequential(pfx, val string, failThreshold int) error {
qps := float64(ts.cfg.EKSConfig.AddOnConfigMaps.QPS)
burst := int(ts.cfg.EKSConfig.AddOnConfigMaps.Burst)
rateLimiter := rate.NewLimiter(rate.Limit(qps), burst)
ts.cfg.Logger.Info("creating ConfigMaps sequential",
zap.Float64("qps", qps),
zap.Int("burst", burst),
)
fails := 0
for i := 0; i < ts.cfg.EKSConfig.AddOnConfigMaps.Objects; i++ {
if !rateLimiter.Allow() {
ts.cfg.Logger.Debug("waiting for rate limiter creating ConfigMap", zap.Int("index", i))
werr := rateLimiter.Wait(context.Background())
ts.cfg.Logger.Debug("waited for rate limiter", zap.Int("index", i), zap.Error(werr))
}
key := fmt.Sprintf("%s%06d", pfx, i)
configMap := &v1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: key,
Namespace: ts.cfg.EKSConfig.AddOnConfigMaps.Namespace,
Labels: map[string]string{
"name": key,
},
},
Data: map[string]string{key: val},
}
t1 := time.Now()
_, err := ts.cfg.K8SClient.KubernetesClientSet().
CoreV1().
ConfigMaps(ts.cfg.EKSConfig.AddOnConfigMaps.Namespace).
Create(configMap)
t2 := time.Now()
if err != nil {
select {
case <-ts.cancel:
return errors.New("ConfigMap creation aborted")
case <-ts.cfg.Stopc:
return errors.New("ConfigMap creation aborted")
default:
fails++
ts.cfg.Logger.Warn("create ConfigMap failed",
zap.Int("fails", fails),
zap.Int("threshold", failThreshold),
zap.Error(err),
)
if fails >= failThreshold {
close(ts.cancel)
return fmt.Errorf("exceeded ConfigMap writes fail threshold %d (%v)", failThreshold, err)
}
}
continue
}
fails = 0
configMapName := configMap.GetObjectMeta().GetName()
ts.cfg.EKSConfig.AddOnConfigMaps.CreatedNames = append(ts.cfg.EKSConfig.AddOnConfigMaps.CreatedNames, configMapName)
ts.cfg.EKSConfig.Sync()
if ts.cfg.EKSConfig.LogLevel == "debug" || i%200 == 0 {
ts.cfg.Logger.Info("created ConfigMap", zap.String("key", configMapName), zap.Duration("took", t2.Sub(t1)))
}
}
ts.cfg.Logger.Info("created ConfigMaps sequential",
zap.Int("objects", ts.cfg.EKSConfig.AddOnConfigMaps.Objects),
zap.Int("success", len(ts.cfg.EKSConfig.AddOnConfigMaps.CreatedNames)),
)
return ts.cfg.EKSConfig.Sync()
}
func (ts *tester) createConfigMapsParallel(pfx, val string, failThreshold int) error {
qps := float64(ts.cfg.EKSConfig.AddOnConfigMaps.QPS)
burst := int(ts.cfg.EKSConfig.AddOnConfigMaps.Burst)
rateLimiter := rate.NewLimiter(rate.Limit(qps), burst)
ts.cfg.Logger.Info("creating ConfigMaps parallel",
zap.Float64("qps", qps),
zap.Int("burst", burst),
)
rch := make(chan result, int(qps))
for i := 0; i < ts.cfg.EKSConfig.AddOnConfigMaps.Objects; i++ {
go func(i int) {
if !rateLimiter.Allow() {
ts.cfg.Logger.Debug("waiting for rate limiter creating ConfigMap", zap.Int("index", i))
werr := rateLimiter.Wait(context.Background())
ts.cfg.Logger.Debug("waited for rate limiter", zap.Int("index", i), zap.Error(werr))
}
select {
case <-ts.cancel:
return
case <-ts.cfg.Stopc:
return
default:
}
key := fmt.Sprintf("%s%06d", pfx, i)
configMap := &v1.ConfigMap{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "ConfigMap",
},
ObjectMeta: metav1.ObjectMeta{
Name: key,
Namespace: ts.cfg.EKSConfig.AddOnConfigMaps.Namespace,
Labels: map[string]string{
"name": key,
},
},
Data: map[string]string{key: val},
}
t1 := time.Now()
_, err := ts.cfg.K8SClient.KubernetesClientSet().
CoreV1().
ConfigMaps(ts.cfg.EKSConfig.AddOnConfigMaps.Namespace).
Create(configMap)
t2 := time.Now()
if err != nil {
select {
case <-ts.cancel:
ts.cfg.Logger.Warn("exiting")
return
case <-ts.cfg.Stopc:
ts.cfg.Logger.Warn("exiting")
return
case rch <- result{configMap: configMap, err: err, took: t2.Sub(t1), start: t1, end: t2}:
}
return
}
select {
case <-ts.cancel:
ts.cfg.Logger.Warn("exiting")
return
case <-ts.cfg.Stopc:
ts.cfg.Logger.Warn("exiting")
return
case rch <- result{configMap: configMap, err: nil, took: t2.Sub(t1), start: t1, end: t2}:
}
if ts.cfg.EKSConfig.LogLevel == "debug" || i%200 == 0 {
ts.cfg.Logger.Info("created ConfigMap",
zap.String("key", configMap.GetObjectMeta().GetName()),
zap.Duration("took", t2.Sub(t1)),
)
}
}(i)
}
fails := 0
for i := 0; i < ts.cfg.EKSConfig.AddOnConfigMaps.Objects; i++ {
var rv result
select {
case rv = <-rch:
case <-ts.cancel:
ts.cfg.Logger.Warn("exiting")
return errors.New("aborted")
case <-ts.cfg.Stopc:
ts.cfg.Logger.Warn("exiting")
return errors.New("aborted")
}
if rv.err != nil {
fails++
ts.cfg.Logger.Warn("create ConfigMap failed",
zap.Int("fails", fails),
zap.Int("threshold", failThreshold),
zap.Error(rv.err),
)
if fails >= failThreshold {
close(ts.cancel)
return fmt.Errorf("exceeded ConfigMap writes fail threshold %d (%v)", failThreshold, rv.err)
}
continue
}
fails = 0
configMapName := rv.configMap.GetObjectMeta().GetName()
ts.cfg.EKSConfig.AddOnConfigMaps.CreatedNames = append(ts.cfg.EKSConfig.AddOnConfigMaps.CreatedNames, configMapName)
ts.cfg.EKSConfig.Sync()
}
ts.cfg.Logger.Info("created ConfigMaps parallel",
zap.Int("objects", ts.cfg.EKSConfig.AddOnConfigMaps.Objects),
zap.Int("success", len(ts.cfg.EKSConfig.AddOnConfigMaps.CreatedNames)),
)
return ts.cfg.EKSConfig.Sync()
}
type result struct {
configMap *v1.ConfigMap
err error
took time.Duration
start time.Time
end time.Time
}
const ll = "0123456789abcdefghijklmnopqrstuvwxyz"
func randString(n int) string {
b := make([]byte, n)
for i := range b {
rand.Seed(time.Now().UnixNano())
b[i] = ll[rand.Intn(len(ll))]
}
return string(b)
}