-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathoperator_deployment.go
207 lines (185 loc) · 6.77 KB
/
operator_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
//
// 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 operator
import (
"fmt"
"github.com/pkg/errors"
kwatch "k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/tools/cache"
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha"
"github.com/arangodb/kube-arangodb/pkg/deployment"
"github.com/arangodb/kube-arangodb/pkg/metrics"
"github.com/arangodb/kube-arangodb/pkg/util/k8sutil"
)
var (
deploymentsCreated = metrics.MustRegisterCounter("controller", "deployments_created", "Number of deployments that have been created")
deploymentsDeleted = metrics.MustRegisterCounter("controller", "deployments_deleted", "Number of deployments that have been deleted")
deploymentsFailed = metrics.MustRegisterCounter("controller", "deployments_failed", "Number of deployments that have failed")
deploymentsModified = metrics.MustRegisterCounter("controller", "deployments_modified", "Number of deployment modifications")
deploymentsCurrent = metrics.MustRegisterGauge("controller", "deployments", "Number of deployments currently being managed")
)
// run the deployments part of the operator.
// This registers a listener and waits until the process stops.
func (o *Operator) runDeployments(stop <-chan struct{}) {
rw := k8sutil.NewResourceWatcher(
o.log,
o.Dependencies.CRCli.DatabaseV1alpha().RESTClient(),
api.ArangoDeploymentResourcePlural,
o.Config.Namespace,
&api.ArangoDeployment{},
cache.ResourceEventHandlerFuncs{
AddFunc: o.onAddArangoDeployment,
UpdateFunc: o.onUpdateArangoDeployment,
DeleteFunc: o.onDeleteArangoDeployment,
})
o.Dependencies.DeploymentProbe.SetReady()
rw.Run(stop)
}
// onAddArangoDeployment deployment addition callback
func (o *Operator) onAddArangoDeployment(obj interface{}) {
apiObject := obj.(*api.ArangoDeployment)
o.log.Debug().
Str("name", apiObject.GetObjectMeta().GetName()).
Msg("ArangoDeployment added")
o.syncArangoDeployment(apiObject)
}
// onUpdateArangoDeployment deployment update callback
func (o *Operator) onUpdateArangoDeployment(oldObj, newObj interface{}) {
apiObject := newObj.(*api.ArangoDeployment)
o.log.Debug().
Str("name", apiObject.GetObjectMeta().GetName()).
Msg("ArangoDeployment updated")
o.syncArangoDeployment(apiObject)
}
// onDeleteArangoDeployment deployment delete callback
func (o *Operator) onDeleteArangoDeployment(obj interface{}) {
log := o.log
apiObject, ok := obj.(*api.ArangoDeployment)
if !ok {
tombstone, ok := obj.(cache.DeletedFinalStateUnknown)
if !ok {
log.Error().Interface("event-object", obj).Msg("unknown object from ArangoDeployment delete event")
return
}
apiObject, ok = tombstone.Obj.(*api.ArangoDeployment)
if !ok {
log.Error().Interface("event-object", obj).Msg("Tombstone contained object that is not an ArangoDeployment")
return
}
}
log.Debug().
Str("name", apiObject.GetObjectMeta().GetName()).
Msg("ArangoDeployment deleted")
ev := &Event{
Type: kwatch.Deleted,
Deployment: apiObject,
}
// pt.start()
err := o.handleDeploymentEvent(ev)
if err != nil {
log.Warn().Err(err).Msg("Failed to handle event")
}
//pt.stop()
}
// syncArangoDeployment synchronized the given deployment.
func (o *Operator) syncArangoDeployment(apiObject *api.ArangoDeployment) {
ev := &Event{
Type: kwatch.Added,
Deployment: apiObject,
}
// re-watch or restart could give ADD event.
// If for an ADD event the cluster spec is invalid then it is not added to the local cache
// so modifying that deployment will result in another ADD event
if _, ok := o.deployments[apiObject.Name]; ok {
ev.Type = kwatch.Modified
}
//pt.start()
err := o.handleDeploymentEvent(ev)
if err != nil {
o.log.Warn().Err(err).Msg("Failed to handle event")
}
//pt.stop()
}
// handleDeploymentEvent processed the given event.
func (o *Operator) handleDeploymentEvent(event *Event) error {
apiObject := event.Deployment
if apiObject.Status.Phase.IsFailed() {
deploymentsFailed.Inc()
if event.Type == kwatch.Deleted {
delete(o.deployments, apiObject.Name)
return nil
}
return maskAny(fmt.Errorf("ignore failed deployment (%s). Please delete its CR", apiObject.Name))
}
switch event.Type {
case kwatch.Added:
if _, ok := o.deployments[apiObject.Name]; ok {
return maskAny(fmt.Errorf("unsafe state. deployment (%s) was created before but we received event (%s)", apiObject.Name, event.Type))
}
// Fill in defaults
apiObject.Spec.SetDefaults(apiObject.GetName())
// Validate deployment spec
if err := apiObject.Spec.Validate(); err != nil {
return maskAny(errors.Wrapf(err, "invalid deployment spec. please fix the following problem with the deployment spec: %v", err))
}
cfg, deps := o.makeDeploymentConfigAndDeps(apiObject)
nc, err := deployment.New(cfg, deps, apiObject)
if err != nil {
return maskAny(fmt.Errorf("failed to create deployment: %s", err))
}
o.deployments[apiObject.Name] = nc
deploymentsCreated.Inc()
deploymentsCurrent.Set(float64(len(o.deployments)))
case kwatch.Modified:
depl, ok := o.deployments[apiObject.Name]
if !ok {
return maskAny(fmt.Errorf("unsafe state. deployment (%s) was never created but we received event (%s)", apiObject.Name, event.Type))
}
depl.Update(apiObject)
deploymentsModified.Inc()
case kwatch.Deleted:
depl, ok := o.deployments[apiObject.Name]
if !ok {
return maskAny(fmt.Errorf("unsafe state. deployment (%s) was never created but we received event (%s)", apiObject.Name, event.Type))
}
depl.Delete()
delete(o.deployments, apiObject.Name)
deploymentsDeleted.Inc()
deploymentsCurrent.Set(float64(len(o.deployments)))
}
return nil
}
// makeDeploymentConfigAndDeps creates a Config & Dependencies object for a new Deployment.
func (o *Operator) makeDeploymentConfigAndDeps(apiObject *api.ArangoDeployment) (deployment.Config, deployment.Dependencies) {
cfg := deployment.Config{
ServiceAccount: o.Config.ServiceAccount,
AllowChaos: o.Config.AllowChaos,
}
deps := deployment.Dependencies{
Log: o.Dependencies.LogService.MustGetLogger("deployment").With().
Str("deployment", apiObject.GetName()).
Logger(),
KubeCli: o.Dependencies.KubeCli,
DatabaseCRCli: o.Dependencies.CRCli,
}
return cfg, deps
}