Skip to content

Commit a949875

Browse files
authored
Add missed deployment name changes (#578)
1 parent 0daa0ad commit a949875

File tree

1 file changed

+66
-66
lines changed

1 file changed

+66
-66
lines changed

pkg/controller/atlasdeployment/atlasdeployment_controller.go

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (r *AtlasDeploymentReconciler) Reconcile(context context.Context, req ctrl.
131131
}
132132
ctx.Client = atlasClient
133133

134-
// Allow users to specify M0/M2/M5 clusters without providing TENANT for Normal and Serverless clusters
134+
// Allow users to specify M0/M2/M5 deployments without providing TENANT for Normal and Serverless deployments
135135
r.verifyNonTenantCase(deployment)
136136

137137
handleDeployment := r.selectDeploymentHandler(deployment)
@@ -150,64 +150,64 @@ func (r *AtlasDeploymentReconciler) Reconcile(context context.Context, req ctrl.
150150
return workflow.OK().ReconcileResult(), nil
151151
}
152152

153-
func (r *AtlasDeploymentReconciler) verifyNonTenantCase(cluster *mdbv1.AtlasDeployment) {
153+
func (r *AtlasDeploymentReconciler) verifyNonTenantCase(deployment *mdbv1.AtlasDeployment) {
154154
var pSettings *mdbv1.ProviderSettingsSpec
155-
var clusterType string
156-
if cluster.Spec.DeploymentSpec != nil {
157-
if cluster.Spec.DeploymentSpec.ProviderSettings == nil {
155+
var deploymentType string
156+
if deployment.Spec.DeploymentSpec != nil {
157+
if deployment.Spec.DeploymentSpec.ProviderSettings == nil {
158158
return
159159
}
160-
pSettings = cluster.Spec.DeploymentSpec.ProviderSettings
161-
clusterType = "TENANT"
160+
pSettings = deployment.Spec.DeploymentSpec.ProviderSettings
161+
deploymentType = "TENANT"
162162
}
163163

164-
if cluster.Spec.ServerlessSpec != nil {
165-
if cluster.Spec.ServerlessSpec.ProviderSettings == nil {
164+
if deployment.Spec.ServerlessSpec != nil {
165+
if deployment.Spec.ServerlessSpec.ProviderSettings == nil {
166166
return
167167
}
168-
pSettings = cluster.Spec.ServerlessSpec.ProviderSettings
169-
clusterType = "SERVERLESS"
168+
pSettings = deployment.Spec.ServerlessSpec.ProviderSettings
169+
deploymentType = "SERVERLESS"
170170
}
171171

172-
modifyProviderSettings(pSettings, clusterType)
172+
modifyProviderSettings(pSettings, deploymentType)
173173
}
174174

175-
func modifyProviderSettings(pSettings *mdbv1.ProviderSettingsSpec, clusterType string) {
176-
if pSettings == nil || string(pSettings.ProviderName) == clusterType {
175+
func modifyProviderSettings(pSettings *mdbv1.ProviderSettingsSpec, deploymentType string) {
176+
if pSettings == nil || string(pSettings.ProviderName) == deploymentType {
177177
return
178178
}
179179

180-
switch strings.ToUpper(clusterType) {
180+
switch strings.ToUpper(deploymentType) {
181181
case "TENANT":
182182
switch pSettings.InstanceSizeName {
183183
case "M0", "M2", "M5":
184184
pSettings.BackingProviderName = string(pSettings.ProviderName)
185-
pSettings.ProviderName = provider.ProviderName(clusterType)
185+
pSettings.ProviderName = provider.ProviderName(deploymentType)
186186
}
187187
case "SERVERLESS":
188188
pSettings.BackingProviderName = string(pSettings.ProviderName)
189-
pSettings.ProviderName = provider.ProviderName(clusterType)
189+
pSettings.ProviderName = provider.ProviderName(deploymentType)
190190
}
191191
}
192192

193-
func (r *AtlasDeploymentReconciler) selectDeploymentHandler(cluster *mdbv1.AtlasDeployment) clusterHandlerFunc {
194-
if cluster.IsAdvancedDeployment() {
193+
func (r *AtlasDeploymentReconciler) selectDeploymentHandler(deployment *mdbv1.AtlasDeployment) deploymentHandlerFunc {
194+
if deployment.IsAdvancedDeployment() {
195195
return r.handleAdvancedDeployment
196196
}
197-
if cluster.IsServerless() {
197+
if deployment.IsServerless() {
198198
return r.handleServerlessInstance
199199
}
200200
return r.handleRegularDeployment
201201
}
202202

203203
func (r *AtlasDeploymentReconciler) handleDeploymentBackupSchedule(ctx *workflow.Context, deployment *mdbv1.AtlasDeployment, projectID, cName string, backupEnabled bool, req ctrl.Request) error {
204204
if deployment.Spec.BackupScheduleRef.Name == "" && deployment.Spec.BackupScheduleRef.Namespace == "" {
205-
r.Log.Debug("no backup schedule configured for the cluster")
205+
r.Log.Debug("no backup schedule configured for the deployment")
206206
return nil
207207
}
208208

209209
if !backupEnabled {
210-
return fmt.Errorf("can not proceed with backup schedule. Backups are not enabled for cluster %v", deployment.ClusterName)
210+
return fmt.Errorf("can not proceed with backup schedule. Backups are not enabled for deployment %v", deployment.ClusterName)
211211
}
212212

213213
resourcesToWatch := []watch.WatchedObject{}
@@ -231,7 +231,7 @@ func (r *AtlasDeploymentReconciler) handleDeploymentBackupSchedule(ctx *workflow
231231
resourcesToWatch = append(resourcesToWatch, watch.WatchedObject{ResourceKind: bPolicy.Kind, Resource: pKey})
232232

233233
// Create new backup schedule
234-
r.Log.Infof("updating backupschedule for the atlas cluster: %v", cName)
234+
r.Log.Infof("updating backupschedule for the atlas deployment: %v", cName)
235235
apiScheduleRes := &mongodbatlas.CloudProviderSnapshotBackupPolicy{
236236
ClusterName: cName,
237237
ReferenceHourOfDay: &bSchedule.Spec.ReferenceHourOfDay,
@@ -273,13 +273,13 @@ func (r *AtlasDeploymentReconciler) handleDeploymentBackupSchedule(ctx *workflow
273273
if _, _, err := ctx.Client.CloudProviderSnapshotBackupPolicies.Update(context.Background(), projectID, cName, apiScheduleRes); err != nil {
274274
return fmt.Errorf("unable to create backupschedule %v. e: %w", bKey, err)
275275
}
276-
r.Log.Infof("successfully updated backupschedule for cluster %v", cName)
276+
r.Log.Infof("successfully updated backupschedule for deployment %v", cName)
277277
return nil
278278
}
279279

280-
// handleAdvancedDeployment ensures the state of the cluster using the Advanced Deployment API
281-
func (r *AtlasDeploymentReconciler) handleAdvancedDeployment(ctx *workflow.Context, project *mdbv1.AtlasProject, cluster *mdbv1.AtlasDeployment, req reconcile.Request) (workflow.Result, error) {
282-
c, result := r.ensureAdvancedDeploymentState(ctx, project, cluster)
280+
// handleAdvancedDeployment ensures the state of the deployment using the Advanced Deployment API
281+
func (r *AtlasDeploymentReconciler) handleAdvancedDeployment(ctx *workflow.Context, project *mdbv1.AtlasProject, deployment *mdbv1.AtlasDeployment, req reconcile.Request) (workflow.Result, error) {
282+
c, result := r.ensureAdvancedDeploymentState(ctx, project, deployment)
283283
if c != nil && c.StateName != "" {
284284
ctx.EnsureStatusOption(status.AtlasDeploymentStateNameOption(c.StateName))
285285
}
@@ -288,13 +288,13 @@ func (r *AtlasDeploymentReconciler) handleAdvancedDeployment(ctx *workflow.Conte
288288
return result, nil
289289
}
290290

291-
if err := r.handleDeploymentBackupSchedule(ctx, cluster, project.ID(), c.Name, *c.BackupEnabled, req); err != nil {
291+
if err := r.handleDeploymentBackupSchedule(ctx, deployment, project.ID(), c.Name, *c.BackupEnabled, req); err != nil {
292292
result := workflow.Terminate(workflow.Internal, err.Error())
293293
ctx.SetConditionFromResult(status.DeploymentReadyType, result)
294294
return result, nil
295295
}
296296

297-
if csResult := r.ensureConnectionSecrets(ctx, project, c.Name, c.ConnectionStrings, cluster); !csResult.IsOk() {
297+
if csResult := r.ensureConnectionSecrets(ctx, project, c.Name, c.ConnectionStrings, deployment); !csResult.IsOk() {
298298
return csResult, nil
299299
}
300300

@@ -308,50 +308,50 @@ func (r *AtlasDeploymentReconciler) handleAdvancedDeployment(ctx *workflow.Conte
308308
}
309309

310310
// handleServerlessInstance ensures the state of the serverless instance using the serverless API
311-
func (r *AtlasDeploymentReconciler) handleServerlessInstance(ctx *workflow.Context, project *mdbv1.AtlasProject, cluster *mdbv1.AtlasDeployment, req reconcile.Request) (workflow.Result, error) {
312-
c, result := ensureServerlessInstanceState(ctx, project, cluster.Spec.ServerlessSpec)
313-
return r.ensureConnectionSecretsAndSetStatusOptions(ctx, project, cluster, result, c)
311+
func (r *AtlasDeploymentReconciler) handleServerlessInstance(ctx *workflow.Context, project *mdbv1.AtlasProject, deployment *mdbv1.AtlasDeployment, req reconcile.Request) (workflow.Result, error) {
312+
c, result := ensureServerlessInstanceState(ctx, project, deployment.Spec.ServerlessSpec)
313+
return r.ensureConnectionSecretsAndSetStatusOptions(ctx, project, deployment, result, c)
314314
}
315315

316-
// handleRegularDeployment ensures the state of the cluster using the Regular Deployment API
317-
func (r *AtlasDeploymentReconciler) handleRegularDeployment(ctx *workflow.Context, project *mdbv1.AtlasProject, cluster *mdbv1.AtlasDeployment, req reconcile.Request) (workflow.Result, error) {
318-
c, result := ensureDeploymentState(ctx, project, cluster)
319-
if c != nil && c.StateName != "" {
320-
ctx.EnsureStatusOption(status.AtlasDeploymentStateNameOption(c.StateName))
316+
// handleRegularDeployment ensures the state of the deployment using the Regular Deployment API
317+
func (r *AtlasDeploymentReconciler) handleRegularDeployment(ctx *workflow.Context, project *mdbv1.AtlasProject, deployment *mdbv1.AtlasDeployment, req reconcile.Request) (workflow.Result, error) {
318+
atlasDeployment, result := ensureDeploymentState(ctx, project, deployment)
319+
if atlasDeployment != nil && atlasDeployment.StateName != "" {
320+
ctx.EnsureStatusOption(status.AtlasDeploymentStateNameOption(atlasDeployment.StateName))
321321
}
322322

323323
if !result.IsOk() {
324324
return result, nil
325325
}
326326

327-
if err := r.handleDeploymentBackupSchedule(ctx, cluster, project.ID(), c.Name, *c.ProviderBackupEnabled || *c.BackupEnabled, req); err != nil {
327+
if err := r.handleDeploymentBackupSchedule(ctx, deployment, project.ID(), atlasDeployment.Name, *atlasDeployment.ProviderBackupEnabled || *atlasDeployment.BackupEnabled, req); err != nil {
328328
result := workflow.Terminate(workflow.Internal, err.Error())
329329
ctx.SetConditionFromResult(status.DeploymentReadyType, result)
330330
return result, nil
331331
}
332-
return r.ensureConnectionSecretsAndSetStatusOptions(ctx, project, cluster, result, c)
332+
return r.ensureConnectionSecretsAndSetStatusOptions(ctx, project, deployment, result, atlasDeployment)
333333
}
334334

335335
// ensureConnectionSecretsAndSetStatusOptions creates the relevant connection secrets and sets
336-
// status options to the given context. This function can be used for regular clusters and serverless instances
337-
func (r *AtlasDeploymentReconciler) ensureConnectionSecretsAndSetStatusOptions(ctx *workflow.Context, project *mdbv1.AtlasProject, cluster *mdbv1.AtlasDeployment, result workflow.Result, deployment *mongodbatlas.Cluster) (workflow.Result, error) {
338-
if deployment != nil && deployment.StateName != "" {
339-
ctx.EnsureStatusOption(status.AtlasDeploymentStateNameOption(deployment.StateName))
336+
// status options to the given context. This function can be used for regular deployments and serverless instances
337+
func (r *AtlasDeploymentReconciler) ensureConnectionSecretsAndSetStatusOptions(ctx *workflow.Context, project *mdbv1.AtlasProject, deployment *mdbv1.AtlasDeployment, result workflow.Result, d *mongodbatlas.Cluster) (workflow.Result, error) {
338+
if d != nil && d.StateName != "" {
339+
ctx.EnsureStatusOption(status.AtlasDeploymentStateNameOption(d.StateName))
340340
}
341341

342342
if !result.IsOk() {
343343
return result, nil
344344
}
345345

346-
if csResult := r.ensureConnectionSecrets(ctx, project, deployment.Name, deployment.ConnectionStrings, cluster); !csResult.IsOk() {
346+
if csResult := r.ensureConnectionSecrets(ctx, project, d.Name, d.ConnectionStrings, deployment); !csResult.IsOk() {
347347
return csResult, nil
348348
}
349349

350350
ctx.
351351
SetConditionTrue(status.DeploymentReadyType).
352-
EnsureStatusOption(status.AtlasDeploymentMongoDBVersionOption(deployment.MongoDBVersion)).
353-
EnsureStatusOption(status.AtlasDeploymentConnectionStringsOption(deployment.ConnectionStrings)).
354-
EnsureStatusOption(status.AtlasDeploymentMongoURIUpdatedOption(deployment.MongoURIUpdated))
352+
EnsureStatusOption(status.AtlasDeploymentMongoDBVersionOption(d.MongoDBVersion)).
353+
EnsureStatusOption(status.AtlasDeploymentConnectionStringsOption(d.ConnectionStrings)).
354+
EnsureStatusOption(status.AtlasDeploymentMongoURIUpdatedOption(d.MongoURIUpdated))
355355

356356
ctx.SetConditionTrue(status.ReadyType)
357357
return result, nil
@@ -382,8 +382,8 @@ func (r *AtlasDeploymentReconciler) handleAdvancedOptions(ctx *workflow.Context,
382382
return workflow.OK()
383383
}
384384

385-
func (r *AtlasDeploymentReconciler) readProjectResource(cluster *mdbv1.AtlasDeployment, project *mdbv1.AtlasProject) workflow.Result {
386-
if err := r.Client.Get(context.Background(), cluster.AtlasProjectObjectKey(), project); err != nil {
385+
func (r *AtlasDeploymentReconciler) readProjectResource(deployment *mdbv1.AtlasDeployment, project *mdbv1.AtlasProject) workflow.Result {
386+
if err := r.Client.Get(context.Background(), deployment.AtlasProjectObjectKey(), project); err != nil {
387387
return workflow.Terminate(workflow.Internal, err.Error())
388388
}
389389
return workflow.OK()
@@ -419,31 +419,31 @@ func (r *AtlasDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error {
419419
// Delete implements a handler for the Delete event.
420420
func (r *AtlasDeploymentReconciler) Delete(e event.DeleteEvent) error {
421421
// TODO: Add deletion for AtlasBackupSchedule and AtlasBackupPolicy
422-
cluster, ok := e.Object.(*mdbv1.AtlasDeployment)
422+
deployment, ok := e.Object.(*mdbv1.AtlasDeployment)
423423
if !ok {
424424
r.Log.Errorf("Ignoring malformed Delete() call (expected type %T, got %T)", &mdbv1.AtlasDeployment{}, e.Object)
425425
return nil
426426
}
427427

428-
log := r.Log.With("atlasdeployment", kube.ObjectKeyFromObject(cluster))
428+
log := r.Log.With("atlasdeployment", kube.ObjectKeyFromObject(deployment))
429429

430-
log.Infow("-> Starting AtlasDeployment deletion", "spec", cluster.Spec)
430+
log.Infow("-> Starting AtlasDeployment deletion", "spec", deployment.Spec)
431431

432432
project := &mdbv1.AtlasProject{}
433-
if result := r.readProjectResource(cluster, project); !result.IsOk() {
433+
if result := r.readProjectResource(deployment, project); !result.IsOk() {
434434
return errors.New("cannot read project resource")
435435
}
436436

437-
log = log.With("projectID", project.Status.ID, "clusterName", cluster.GetDeploymentName())
437+
log = log.With("projectID", project.Status.ID, "deploymentName", deployment.GetDeploymentName())
438438

439-
if customresource.ResourceShouldBeLeftInAtlas(cluster) {
439+
if customresource.ResourceShouldBeLeftInAtlas(deployment) {
440440
log.Infof("Not removing Atlas Deployment from Atlas as the '%s' annotation is set", customresource.ResourcePolicyAnnotation)
441-
} else if err := r.deleteDeploymentFromAtlas(cluster, project, log); err != nil {
442-
log.Error("Failed to remove cluster from Atlas: %s", err)
441+
} else if err := r.deleteDeploymentFromAtlas(deployment, project, log); err != nil {
442+
log.Error("Failed to remove deployment from Atlas: %s", err)
443443
}
444444

445-
// We always remove the connection secrets even if the cluster is not removed from Atlas
446-
secrets, err := connectionsecret.ListByDeploymentName(r.Client, cluster.Namespace, project.ID(), cluster.GetDeploymentName())
445+
// We always remove the connection secrets even if the deployment is not removed from Atlas
446+
secrets, err := connectionsecret.ListByDeploymentName(r.Client, deployment.Namespace, project.ID(), deployment.GetDeploymentName())
447447
if err != nil {
448448
return fmt.Errorf("failed to find connection secrets for the user: %w", err)
449449
}
@@ -460,7 +460,7 @@ func (r *AtlasDeploymentReconciler) Delete(e event.DeleteEvent) error {
460460
return nil
461461
}
462462

463-
func (r *AtlasDeploymentReconciler) deleteDeploymentFromAtlas(cluster *mdbv1.AtlasDeployment, project *mdbv1.AtlasProject, log *zap.SugaredLogger) error {
463+
func (r *AtlasDeploymentReconciler) deleteDeploymentFromAtlas(deployment *mdbv1.AtlasDeployment, project *mdbv1.AtlasProject, log *zap.SugaredLogger) error {
464464
connection, err := atlas.ReadConnection(log, r.Client, r.GlobalAPISecret, project.ConnectionSecretObjectKey())
465465
if err != nil {
466466
return err
@@ -476,14 +476,14 @@ func (r *AtlasDeploymentReconciler) deleteDeploymentFromAtlas(cluster *mdbv1.Atl
476476

477477
for time.Now().Before(timeout) {
478478
deleteDeploymentFunc := atlasClient.Clusters.Delete
479-
if cluster.Spec.AdvancedDeploymentSpec != nil {
479+
if deployment.Spec.AdvancedDeploymentSpec != nil {
480480
deleteDeploymentFunc = atlasClient.AdvancedClusters.Delete
481481
}
482-
if cluster.IsServerless() {
482+
if deployment.IsServerless() {
483483
deleteDeploymentFunc = atlasClient.ServerlessInstances.Delete
484484
}
485485

486-
_, err = deleteDeploymentFunc(context.Background(), project.Status.ID, cluster.GetDeploymentName())
486+
_, err = deleteDeploymentFunc(context.Background(), project.Status.ID, deployment.GetDeploymentName())
487487

488488
var apiError *mongodbatlas.ErrorResponse
489489
if errors.As(err, &apiError) && apiError.ErrorCode == atlas.ClusterNotFound {
@@ -497,13 +497,13 @@ func (r *AtlasDeploymentReconciler) deleteDeploymentFromAtlas(cluster *mdbv1.Atl
497497
continue
498498
}
499499

500-
log.Info("Started Atlas cluster deletion process")
500+
log.Info("Started Atlas deployment deletion process")
501501
return
502502
}
503503

504-
log.Error("Failed to delete Atlas cluster in time")
504+
log.Error("Failed to delete Atlas deployment in time")
505505
}()
506506
return nil
507507
}
508508

509-
type clusterHandlerFunc func(ctx *workflow.Context, project *mdbv1.AtlasProject, cluster *mdbv1.AtlasDeployment, req reconcile.Request) (workflow.Result, error)
509+
type deploymentHandlerFunc func(ctx *workflow.Context, project *mdbv1.AtlasProject, deployment *mdbv1.AtlasDeployment, req reconcile.Request) (workflow.Result, error)

0 commit comments

Comments
 (0)