-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathdeployment.go
480 lines (428 loc) · 14.9 KB
/
deployment.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//
// DISCLAIMER
//
// Copyright 2018 ArangoDB GmbH, Cologne, Germany
//
// 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.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Ewout Prangsma
//
package deployment
import (
"fmt"
"reflect"
"sync"
"sync/atomic"
"time"
"github.com/arangodb/arangosync/client"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/record"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha"
"github.com/arangodb/kube-arangodb/pkg/deployment/chaos"
"github.com/arangodb/kube-arangodb/pkg/deployment/reconcile"
"github.com/arangodb/kube-arangodb/pkg/deployment/resilience"
"github.com/arangodb/kube-arangodb/pkg/deployment/resources"
"github.com/arangodb/kube-arangodb/pkg/generated/clientset/versioned"
"github.com/arangodb/kube-arangodb/pkg/util"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
"github.com/arangodb/kube-arangodb/pkg/util/retry"
"github.com/arangodb/kube-arangodb/pkg/util/trigger"
)
// Config holds configuration settings for a Deployment
type Config struct {
ServiceAccount string
AllowChaos bool
LifecycleImage string
AlpineImage string
}
// Dependencies holds dependent services for a Deployment
type Dependencies struct {
Log zerolog.Logger
KubeCli kubernetes.Interface
DatabaseCRCli versioned.Interface
EventRecorder record.EventRecorder
}
// deploymentEventType strongly typed type of event
type deploymentEventType string
const (
eventArangoDeploymentUpdated deploymentEventType = "ArangoDeploymentUpdated"
)
// deploymentEvent holds an event passed from the controller to the deployment.
type deploymentEvent struct {
Type deploymentEventType
Deployment *api.ArangoDeployment
}
const (
deploymentEventQueueSize = 256
minInspectionInterval = util.Interval(time.Second) // Ensure we inspect the generated resources no less than with this interval
maxInspectionInterval = util.Interval(time.Minute) // Ensure we inspect the generated resources no less than with this interval
)
// Deployment is the in process state of an ArangoDeployment.
type Deployment struct {
apiObject *api.ArangoDeployment // API object
status struct {
mutex sync.Mutex
version int32
last api.DeploymentStatus // Internal status copy of the CR
}
config Config
deps Dependencies
eventCh chan *deploymentEvent
stopCh chan struct{}
stopped int32
inspectTrigger trigger.Trigger
updateDeploymentTrigger trigger.Trigger
clientCache *clientCache
recentInspectionErrors int
clusterScalingIntegration *clusterScalingIntegration
reconciler *reconcile.Reconciler
resilience *resilience.Resilience
resources *resources.Resources
chaosMonkey *chaos.Monkey
syncClientCache client.ClientCache
}
// New creates a new Deployment from the given API object.
func New(config Config, deps Dependencies, apiObject *api.ArangoDeployment) (*Deployment, error) {
if err := apiObject.Spec.Validate(); err != nil {
return nil, maskAny(err)
}
d := &Deployment{
apiObject: apiObject,
config: config,
deps: deps,
eventCh: make(chan *deploymentEvent, deploymentEventQueueSize),
stopCh: make(chan struct{}),
clientCache: newClientCache(deps.KubeCli, apiObject),
}
d.status.last = *(apiObject.Status.DeepCopy())
d.reconciler = reconcile.NewReconciler(deps.Log, d)
d.resilience = resilience.NewResilience(deps.Log, d)
d.resources = resources.NewResources(deps.Log, d)
if d.status.last.AcceptedSpec == nil {
// We've validated the spec, so let's use it from now.
d.status.last.AcceptedSpec = apiObject.Spec.DeepCopy()
}
go d.run()
go d.listenForPodEvents(d.stopCh)
go d.listenForPVCEvents(d.stopCh)
go d.listenForSecretEvents(d.stopCh)
go d.listenForServiceEvents(d.stopCh)
if apiObject.Spec.GetMode() == api.DeploymentModeCluster {
ci := newClusterScalingIntegration(d)
d.clusterScalingIntegration = ci
go ci.ListenForClusterEvents(d.stopCh)
go d.resources.RunDeploymentHealthLoop(d.stopCh)
}
if config.AllowChaos {
d.chaosMonkey = chaos.NewMonkey(deps.Log, d)
go d.chaosMonkey.Run(d.stopCh)
}
return d, nil
}
// Update the deployment.
// This sends an update event in the deployment event queue.
func (d *Deployment) Update(apiObject *api.ArangoDeployment) {
d.send(&deploymentEvent{
Type: eventArangoDeploymentUpdated,
Deployment: apiObject,
})
}
// Delete the deployment.
// Called when the deployment was deleted by the user.
func (d *Deployment) Delete() {
d.deps.Log.Info().Msg("deployment is deleted by user")
if atomic.CompareAndSwapInt32(&d.stopped, 0, 1) {
close(d.stopCh)
}
}
// send given event into the deployment event queue.
func (d *Deployment) send(ev *deploymentEvent) {
select {
case d.eventCh <- ev:
l, ecap := len(d.eventCh), cap(d.eventCh)
if l > int(float64(ecap)*0.8) {
d.deps.Log.Warn().
Int("used", l).
Int("capacity", ecap).
Msg("event queue buffer is almost full")
}
case <-d.stopCh:
}
}
// run is the core the core worker.
// It processes the event queue and polls the state of generated
// resource on a regular basis.
func (d *Deployment) run() {
log := d.deps.Log
if d.GetPhase() == api.DeploymentPhaseNone {
// Create secrets
if err := d.resources.EnsureSecrets(); err != nil {
d.CreateEvent(k8sutil.NewErrorEvent("Failed to create secrets", err, d.GetAPIObject()))
}
// Create services
if err := d.resources.EnsureServices(); err != nil {
d.CreateEvent(k8sutil.NewErrorEvent("Failed to create services", err, d.GetAPIObject()))
}
// Create members
if err := d.createInitialMembers(d.apiObject); err != nil {
d.CreateEvent(k8sutil.NewErrorEvent("Failed to create initial members", err, d.GetAPIObject()))
}
// Create PVCs
if err := d.resources.EnsurePVCs(); err != nil {
d.CreateEvent(k8sutil.NewErrorEvent("Failed to create persistent volume claims", err, d.GetAPIObject()))
}
// Create pods
if err := d.resources.EnsurePods(); err != nil {
d.CreateEvent(k8sutil.NewErrorEvent("Failed to create pods", err, d.GetAPIObject()))
}
status, lastVersion := d.GetStatus()
status.Phase = api.DeploymentPhaseRunning
if err := d.UpdateStatus(status, lastVersion); err != nil {
log.Warn().Err(err).Msg("update initial CR status failed")
}
log.Info().Msg("start running...")
}
inspectionInterval := maxInspectionInterval
for {
select {
case <-d.stopCh:
// Remove finalizers from created resources
log.Info().Msg("Deployment removed, removing finalizers to prevent orphaned resources")
if err := d.removePodFinalizers(); err != nil {
log.Warn().Err(err).Msg("Failed to remove Pod finalizers")
}
if err := d.removePVCFinalizers(); err != nil {
log.Warn().Err(err).Msg("Failed to remove PVC finalizers")
}
// We're being stopped.
return
case event := <-d.eventCh:
// Got event from event queue
switch event.Type {
case eventArangoDeploymentUpdated:
d.updateDeploymentTrigger.Trigger()
default:
panic("unknown event type" + event.Type)
}
case <-d.inspectTrigger.Done():
log.Debug().Msg("Inspect deployment...")
inspectionInterval = d.inspectDeployment(inspectionInterval)
log.Debug().Str("interval", inspectionInterval.String()).Msg("...inspected deployment")
case <-d.updateDeploymentTrigger.Done():
inspectionInterval = minInspectionInterval
if err := d.handleArangoDeploymentUpdatedEvent(); err != nil {
d.CreateEvent(k8sutil.NewErrorEvent("Failed to handle deployment update", err, d.GetAPIObject()))
}
case <-inspectionInterval.After():
// Trigger inspection
d.inspectTrigger.Trigger()
// Backoff with next interval
inspectionInterval = inspectionInterval.Backoff(1.5, maxInspectionInterval)
}
}
}
// handleArangoDeploymentUpdatedEvent is called when the deployment is updated by the user.
func (d *Deployment) handleArangoDeploymentUpdatedEvent() error {
log := d.deps.Log.With().Str("deployment", d.apiObject.GetName()).Logger()
// Get the most recent version of the deployment from the API server
current, err := d.deps.DatabaseCRCli.DatabaseV1alpha().ArangoDeployments(d.apiObject.GetNamespace()).Get(d.apiObject.GetName(), metav1.GetOptions{})
if err != nil {
log.Debug().Err(err).Msg("Failed to get current version of deployment from API server")
if k8sutil.IsNotFound(err) {
return nil
}
return maskAny(err)
}
specBefore := d.apiObject.Spec
status := d.status.last
if d.status.last.AcceptedSpec != nil {
specBefore = *status.AcceptedSpec.DeepCopy()
}
newAPIObject := current.DeepCopy()
newAPIObject.Spec.SetDefaultsFrom(specBefore)
newAPIObject.Spec.SetDefaults(d.apiObject.GetName())
newAPIObject.Status = status
resetFields := specBefore.ResetImmutableFields(&newAPIObject.Spec)
if len(resetFields) > 0 {
log.Debug().Strs("fields", resetFields).Msg("Found modified immutable fields")
newAPIObject.Spec.SetDefaults(d.apiObject.GetName())
}
if err := newAPIObject.Spec.Validate(); err != nil {
d.CreateEvent(k8sutil.NewErrorEvent("Validation failed", err, d.apiObject))
// Try to reset object
if err := d.updateCRSpec(d.apiObject.Spec); err != nil {
log.Error().Err(err).Msg("Restore original spec failed")
d.CreateEvent(k8sutil.NewErrorEvent("Restore original failed", err, d.apiObject))
}
return nil
}
if len(resetFields) > 0 {
for _, fieldName := range resetFields {
log.Debug().Str("field", fieldName).Msg("Reset immutable field")
d.CreateEvent(k8sutil.NewImmutableFieldEvent(fieldName, d.apiObject))
}
}
// Save updated spec
if err := d.updateCRSpec(newAPIObject.Spec); err != nil {
return maskAny(fmt.Errorf("failed to update ArangoDeployment spec: %v", err))
}
// Save updated accepted spec
{
status, lastVersion := d.GetStatus()
status.AcceptedSpec = newAPIObject.Spec.DeepCopy()
if err := d.UpdateStatus(status, lastVersion); err != nil {
return maskAny(fmt.Errorf("failed to update ArangoDeployment status: %v", err))
}
}
// Notify cluster of desired server count
if ci := d.clusterScalingIntegration; ci != nil {
ci.SendUpdateToCluster(d.apiObject.Spec)
}
// Trigger inspect
d.inspectTrigger.Trigger()
return nil
}
// CreateEvent creates a given event.
// On error, the error is logged.
func (d *Deployment) CreateEvent(evt *k8sutil.Event) {
d.deps.EventRecorder.Event(evt.InvolvedObject, evt.Type, evt.Reason, evt.Message)
}
// Update the status of the API object from the internal status
func (d *Deployment) updateCRStatus() error {
if d.apiObject.Status.Equal(d.status.last) {
// Nothing has changed
return nil
}
// Send update to API server
ns := d.apiObject.GetNamespace()
depls := d.deps.DatabaseCRCli.DatabaseV1alpha().ArangoDeployments(ns)
update := d.apiObject.DeepCopy()
attempt := 0
for {
attempt++
update.Status = d.status.last
if update.GetDeletionTimestamp() == nil {
ensureFinalizers(update)
}
newAPIObject, err := depls.Update(update)
if err == nil {
// Update internal object
d.apiObject = newAPIObject
return nil
}
if attempt < 10 && k8sutil.IsConflict(err) {
// API object may have been changed already,
// Reload api object and try again
var current *api.ArangoDeployment
current, err = depls.Get(update.GetName(), metav1.GetOptions{})
if err == nil {
update = current.DeepCopy()
continue
}
}
if err != nil {
d.deps.Log.Debug().Err(err).Msg("failed to patch ArangoDeployment status")
return maskAny(fmt.Errorf("failed to patch ArangoDeployment status: %v", err))
}
}
}
// Update the spec part of the API object (d.apiObject)
// to the given object, while preserving the status.
// On success, d.apiObject is updated.
func (d *Deployment) updateCRSpec(newSpec api.DeploymentSpec) error {
if reflect.DeepEqual(d.apiObject.Spec, newSpec) {
// Nothing to update
return nil
}
// Send update to API server
update := d.apiObject.DeepCopy()
attempt := 0
for {
attempt++
update.Spec = newSpec
update.Status = d.status.last
ns := d.apiObject.GetNamespace()
newAPIObject, err := d.deps.DatabaseCRCli.DatabaseV1alpha().ArangoDeployments(ns).Update(update)
if err == nil {
// Update internal object
d.apiObject = newAPIObject
return nil
}
if attempt < 10 && k8sutil.IsConflict(err) {
// API object may have been changed already,
// Reload api object and try again
var current *api.ArangoDeployment
current, err = d.deps.DatabaseCRCli.DatabaseV1alpha().ArangoDeployments(ns).Get(update.GetName(), metav1.GetOptions{})
if err == nil {
update = current.DeepCopy()
continue
}
}
if err != nil {
d.deps.Log.Debug().Err(err).Msg("failed to patch ArangoDeployment spec")
return maskAny(fmt.Errorf("failed to patch ArangoDeployment spec: %v", err))
}
}
}
// failOnError reports the given error and sets the deployment status to failed.
// Since there is no recovery from a failed deployment, use with care!
func (d *Deployment) failOnError(err error, msg string) {
log.Error().Err(err).Msg(msg)
d.status.last.Reason = err.Error()
d.reportFailedStatus()
}
// reportFailedStatus sets the status of the deployment to Failed and keeps trying to forward
// that to the API server.
func (d *Deployment) reportFailedStatus() {
log := d.deps.Log
log.Info().Msg("deployment failed. Reporting failed reason...")
op := func() error {
d.status.last.Phase = api.DeploymentPhaseFailed
err := d.updateCRStatus()
if err == nil || k8sutil.IsNotFound(err) {
// Status has been updated
return nil
}
if !k8sutil.IsConflict(err) {
log.Warn().Err(err).Msg("retry report status: fail to update")
return maskAny(err)
}
depl, err := d.deps.DatabaseCRCli.DatabaseV1alpha().ArangoDeployments(d.apiObject.Namespace).Get(d.apiObject.Name, metav1.GetOptions{})
if err != nil {
// Update (PUT) will return conflict even if object is deleted since we have UID set in object.
// Because it will check UID first and return something like:
// "Precondition failed: UID in precondition: 0xc42712c0f0, UID in object meta: ".
if k8sutil.IsNotFound(err) {
return nil
}
log.Warn().Err(err).Msg("retry report status: fail to get latest version")
return maskAny(err)
}
d.apiObject = depl
return maskAny(fmt.Errorf("retry needed"))
}
retry.Retry(op, time.Hour*24*365)
}
// isOwnerOf returns true if the given object belong to this deployment.
func (d *Deployment) isOwnerOf(obj metav1.Object) bool {
ownerRefs := obj.GetOwnerReferences()
if len(ownerRefs) < 1 {
return false
}
return ownerRefs[0].UID == d.apiObject.UID
}