-
Notifications
You must be signed in to change notification settings - Fork 66
/
deployment.go
292 lines (243 loc) · 10.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
package atlasdeployment
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"go.mongodb.org/atlas/mongodbatlas"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1"
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/status"
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/connectionsecret"
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/workflow"
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/compat"
"github.com/mongodb/mongodb-atlas-kubernetes/pkg/util/stringutil"
)
func ensureDeploymentState(ctx *workflow.Context, project *mdbv1.AtlasProject, deployment *mdbv1.AtlasDeployment) (atlasDeployment *mongodbatlas.Cluster, _ workflow.Result) {
atlasDeployment, resp, err := ctx.Client.Clusters.Get(context.Background(), project.Status.ID, deployment.Spec.DeploymentSpec.Name)
if err != nil {
if resp == nil {
return atlasDeployment, workflow.Terminate(workflow.Internal, err.Error())
}
if resp.StatusCode != http.StatusNotFound {
return atlasDeployment, workflow.Terminate(workflow.DeploymentNotCreatedInAtlas, err.Error())
}
atlasDeployment, err = deployment.Spec.Deployment()
if err != nil {
return atlasDeployment, workflow.Terminate(workflow.Internal, err.Error())
}
ctx.Log.Infof("Deployment %s doesn't exist in Atlas - creating", deployment.Spec.DeploymentSpec.Name)
atlasDeployment, _, err = ctx.Client.Clusters.Create(context.Background(), project.Status.ID, atlasDeployment)
if err != nil {
return atlasDeployment, workflow.Terminate(workflow.DeploymentNotCreatedInAtlas, err.Error())
}
}
switch atlasDeployment.StateName {
case "IDLE":
return regularDeploymentIdle(ctx, project, deployment, atlasDeployment)
case "CREATING":
return atlasDeployment, workflow.InProgress(workflow.DeploymentCreating, "deployment is provisioning")
case "UPDATING", "REPAIRING":
return atlasDeployment, workflow.InProgress(workflow.DeploymentUpdating, "deployment is updating")
// TODO: add "DELETING", "DELETED", handle 404 on delete
default:
return atlasDeployment, workflow.Terminate(workflow.Internal, fmt.Sprintf("unknown deployment state %q", atlasDeployment.StateName))
}
}
func regularDeploymentIdle(ctx *workflow.Context, project *mdbv1.AtlasProject, deployment *mdbv1.AtlasDeployment, atlasDeployment *mongodbatlas.Cluster) (*mongodbatlas.Cluster, workflow.Result) {
resultingDeployment, err := MergedDeployment(*atlasDeployment, deployment.Spec)
if err != nil {
return atlasDeployment, workflow.Terminate(workflow.Internal, err.Error())
}
if done := DeploymentsEqual(ctx.Log, *atlasDeployment, resultingDeployment); done {
return atlasDeployment, workflow.OK()
}
if deployment.Spec.DeploymentSpec.Paused != nil {
if atlasDeployment.Paused == nil || *atlasDeployment.Paused != *deployment.Spec.DeploymentSpec.Paused {
// paused is different from Atlas
// we need to first send a special (un)pause request before reconciling everything else
resultingDeployment = mongodbatlas.Cluster{
Paused: deployment.Spec.DeploymentSpec.Paused,
}
} else {
// otherwise, don't send the paused field
resultingDeployment.Paused = nil
}
}
resultingDeployment = cleanupDeployment(resultingDeployment)
// Handle shared (M0,M2,M5) deployment to non-shared deployment upgrade
scheduled, err := handleSharedDeploymentUpgrade(ctx, atlasDeployment, &resultingDeployment)
if err != nil {
return atlasDeployment, workflow.Terminate(workflow.Internal, err.Error())
}
if scheduled {
return atlasDeployment, workflow.InProgress(workflow.DeploymentUpdating, "deployment is upgrading")
}
atlasDeployment, _, err = ctx.Client.Clusters.Update(context.Background(), project.Status.ID, deployment.Spec.DeploymentSpec.Name, &resultingDeployment)
if err != nil {
return atlasDeployment, workflow.Terminate(workflow.DeploymentNotUpdatedInAtlas, err.Error())
}
return atlasDeployment, workflow.InProgress(workflow.DeploymentUpdating, "deployment is updating")
}
// cleanupDeployment will unset some fields that cannot be changed via API or are deprecated.
func cleanupDeployment(deployment mongodbatlas.Cluster) mongodbatlas.Cluster {
deployment.ID = ""
deployment.MongoDBVersion = ""
deployment.MongoURI = ""
deployment.MongoURIUpdated = ""
deployment.MongoURIWithOptions = ""
deployment.SrvAddress = ""
deployment.StateName = ""
deployment.ReplicationFactor = nil
deployment.ReplicationSpec = nil
deployment.ConnectionStrings = nil
deployment = removeOutdatedFields(&deployment, nil)
return deployment
}
// removeOutdatedFields unsets fields which are should be empty based on flags
func removeOutdatedFields(removeFrom *mongodbatlas.Cluster, lookAt *mongodbatlas.Cluster) mongodbatlas.Cluster {
if lookAt == nil {
lookAt = removeFrom
}
result := *removeFrom
if lookAt.AutoScaling != nil && lookAt.AutoScaling.Compute != nil {
if *lookAt.AutoScaling.Compute.Enabled {
result.ProviderSettings.InstanceSizeName = ""
} else {
result.ProviderSettings.AutoScaling.Compute = &mongodbatlas.Compute{}
}
}
return result
}
// MergedDeployment will return the result of merging AtlasDeploymentSpec with Atlas Deployment
func MergedDeployment(atlasDeployment mongodbatlas.Cluster, spec mdbv1.AtlasDeploymentSpec) (result mongodbatlas.Cluster, err error) {
if err = compat.JSONCopy(&result, atlasDeployment); err != nil {
return
}
if err = compat.JSONCopy(&result, spec.DeploymentSpec); err != nil {
return
}
mergeRegionConfigs(result.ReplicationSpecs, spec.DeploymentSpec.ReplicationSpecs)
// According to the docs for 'providerSettings.regionName' (https://docs.atlas.mongodb.com/reference/api/clusters-create-one/):
// "Don't specify this parameter when creating a multi-region deployment using the replicationSpec object or a Global
// Deployment with the replicationSpecs array."
// The problem is that Atlas API accepts the create/update request but then returns the 'ProviderSettings.RegionName' empty in GET request
// So we need to consider this while comparing (to avoid perpetual updates)
if len(result.ReplicationSpecs) > 0 && atlasDeployment.ProviderSettings.RegionName == "" {
result.ProviderSettings.RegionName = ""
}
return
}
// mergeRegionConfigs removes replicationSpecs[i].RegionsConfigs[key] from Atlas Deployment that are absent in Operator.
// Dev idea: this could have been added into some more generic method like `JSONCopy` or something wrapping it to make
// sure any Atlas map get redundant keys removed. So far there's only one map in Deployment ('RegionsConfig') so we'll do this
// explicitly - but may make sense to refactor this later if more maps are added (and all follow the same logic).
func mergeRegionConfigs(atlasSpecs []mongodbatlas.ReplicationSpec, operatorSpecs []mdbv1.ReplicationSpec) {
for i, operatorSpec := range operatorSpecs {
if len(operatorSpec.RegionsConfig) == 0 {
// Edge case: if the operator doesn't specify regions configs - Atlas will put the default ones. We shouldn't
// remove it in this case.
continue
}
atlasSpec := atlasSpecs[i]
for key := range atlasSpec.RegionsConfig {
if _, ok := operatorSpec.RegionsConfig[key]; !ok {
delete(atlasSpec.RegionsConfig, key)
}
}
}
}
// DeploymentsEqual compares two Atlas Deployments
func DeploymentsEqual(log *zap.SugaredLogger, deploymentAtlas mongodbatlas.Cluster, deploymentOperator mongodbatlas.Cluster) bool {
deploymentAtlas = removeOutdatedFields(&deploymentAtlas, &deploymentOperator)
deploymentOperator = removeOutdatedFields(&deploymentOperator, nil)
d := cmp.Diff(deploymentAtlas, deploymentOperator, cmpopts.EquateEmpty())
if d != "" {
log.Debugf("Deployments are different: %s", d)
}
return d == ""
}
func (r *AtlasDeploymentReconciler) ensureConnectionSecrets(ctx *workflow.Context, project *mdbv1.AtlasProject, name string, connectionStrings *mongodbatlas.ConnectionStrings, deploymentResource *mdbv1.AtlasDeployment) workflow.Result {
databaseUsers := mdbv1.AtlasDatabaseUserList{}
err := r.Client.List(context.TODO(), &databaseUsers, client.InNamespace(project.Namespace))
if err != nil {
return workflow.Terminate(workflow.Internal, err.Error())
}
secrets := make([]string, 0)
for _, dbUser := range databaseUsers.Items {
found := false
for _, c := range dbUser.Status.Conditions {
if c.Type == status.ReadyType && c.Status == v1.ConditionTrue {
found = true
break
}
}
if !found {
ctx.Log.Debugw("AtlasDatabaseUser not ready - not creating connection secret", "user.name", dbUser.Name)
continue
}
scopes := dbUser.GetScopes(mdbv1.DeploymentScopeType)
if len(scopes) != 0 && !stringutil.Contains(scopes, name) {
continue
}
password, err := dbUser.ReadPassword(r.Client)
if err != nil {
return workflow.Terminate(workflow.DeploymentConnectionSecretsNotCreated, err.Error())
}
data := connectionsecret.ConnectionData{
DBUserName: dbUser.Spec.Username,
ConnURL: connectionStrings.Standard,
SrvConnURL: connectionStrings.StandardSrv,
Password: password,
}
secretName, err := connectionsecret.Ensure(r.Client, project.Namespace, project.Spec.Name, project.ID(), name, data)
if err != nil {
return workflow.Terminate(workflow.DeploymentConnectionSecretsNotCreated, err.Error())
}
secrets = append(secrets, secretName)
}
if len(secrets) > 0 {
r.EventRecorder.Eventf(deploymentResource, "Normal", "ConnectionSecretsEnsured", "Connection Secrets were created/updated: %s", strings.Join(secrets, ", "))
}
return workflow.OK()
}
func handleSharedDeploymentUpgrade(ctx *workflow.Context, current *mongodbatlas.Cluster, new *mongodbatlas.Cluster) (scheduled bool, _ error) {
baseErr := "can not perform deployment upgrade. ERR: %v"
if !deploymentShouldBeUpgraded(current, new) {
ctx.Log.Debug("deployment shouldn't be upgraded")
return false, nil
}
// Remove backingProviderName
new.ProviderSettings.BackingProviderName = ""
ctx.Log.Infof("performing deployment upgrade from %s, to %s",
current.ProviderSettings.InstanceSizeName, new.ProviderSettings.InstanceSizeName)
// TODO: Replace with the go-atlas-client when this method will be added to go-atlas-client
atlasClient := ctx.Client
urlStr := fmt.Sprintf("/api/atlas/v1.0/groups/%s/clusters/tenantUpgrade", current.GroupID)
req, err := atlasClient.NewRequest(context.Background(), http.MethodPost, urlStr, new)
if err != nil {
return false, fmt.Errorf(baseErr, err)
}
_, err = atlasClient.Do(context.Background(), req, &new)
if err != nil {
return false, fmt.Errorf(baseErr, err)
}
return true, nil
}
func deploymentShouldBeUpgraded(current *mongodbatlas.Cluster, new *mongodbatlas.Cluster) bool {
if isSharedDeployment(current.ProviderSettings.InstanceSizeName) && !isSharedDeployment(new.ProviderSettings.InstanceSizeName) {
return true
}
return false
}
func isSharedDeployment(instanceSizeName string) bool {
switch strings.ToUpper(instanceSizeName) {
case "M0", "M2", "M5":
return true
}
return false
}