Skip to content

Commit 445b06a

Browse files
authored
use errors for termination (#2072)
1 parent b8aa566 commit 445b06a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+199
-192
lines changed

internal/controller/atlasbackupcompliancepolicy/atlasbackupcompliancepolicy_controller.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package atlasbackupcompliancepolicy
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67

78
"go.uber.org/zap"
@@ -148,7 +149,7 @@ func (r *AtlasBackupCompliancePolicyReconciler) skip(ctx context.Context, log *z
148149
log.Infow(fmt.Sprintf("-> Skipping AtlasBackupCompliancePolicy reconciliation as annotation %s=%s", customresource.ReconciliationPolicyAnnotation, customresource.ReconciliationPolicySkip), "spec", bcp.Spec)
149150
if !bcp.GetDeletionTimestamp().IsZero() {
150151
if err := customresource.ManageFinalizer(ctx, r.Client, bcp, customresource.UnsetFinalizer); err != nil {
151-
result := workflow.Terminate(workflow.Internal, err.Error())
152+
result := workflow.Terminate(workflow.Internal, err)
152153
log.Errorw("Failed to remove finalizer", "terminate", err)
153154

154155
return result.ReconcileResult()
@@ -166,15 +167,15 @@ func (r *AtlasBackupCompliancePolicyReconciler) invalidate(invalid workflow.Resu
166167

167168
func (r *AtlasBackupCompliancePolicyReconciler) unsupport(ctx *workflow.Context) ctrl.Result {
168169
unsupported := workflow.Terminate(
169-
workflow.AtlasGovUnsupported, "the AtlasBackupCompliancePolicy is not supported by Atlas for government").
170+
workflow.AtlasGovUnsupported, errors.New("the AtlasBackupCompliancePolicy is not supported by Atlas for government")).
170171
WithoutRetry()
171172
ctx.SetConditionFromResult(api.ReadyType, unsupported)
172173
return unsupported.ReconcileResult()
173174
}
174175

175176
func (r *AtlasBackupCompliancePolicyReconciler) terminate(ctx *workflow.Context, errorCondition workflow.ConditionReason, err error) ctrl.Result {
176177
r.Log.Error(err)
177-
terminated := workflow.Terminate(errorCondition, err.Error())
178+
terminated := workflow.Terminate(errorCondition, err)
178179
ctx.SetConditionFromResult(api.ReadyType, terminated)
179180
return terminated.ReconcileResult()
180181
}

internal/controller/atlascustomrole/atlascustomrole_controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (r *AtlasCustomRoleReconciler) terminate(
152152
err error,
153153
) ctrl.Result {
154154
r.Log.Errorf("resource %T(%s/%s) failed on condition %s: %s", object, object.GetNamespace(), object.GetName(), condition, err)
155-
result := workflow.Terminate(reason, err.Error())
155+
result := workflow.Terminate(reason, err)
156156
ctx.SetConditionFromResult(condition, result)
157157

158158
if !retry {
@@ -170,7 +170,7 @@ func (r *AtlasCustomRoleReconciler) idle(ctx *workflow.Context) ctrl.Result {
170170
// fail terminates the reconciliation silently(no updates on conditions)
171171
func (r *AtlasCustomRoleReconciler) fail(req ctrl.Request, err error) ctrl.Result {
172172
r.Log.Errorf("Failed to query object %s: %s", req.NamespacedName, err)
173-
return workflow.TerminateSilently().ReconcileResult()
173+
return workflow.TerminateSilently(err).ReconcileResult()
174174
}
175175

176176
// skip prevents the reconciliation to start and successfully return
@@ -183,8 +183,9 @@ func (r *AtlasCustomRoleReconciler) skip() ctrl.Result {
183183

184184
// notFound terminates the reconciliation silently(no updates on conditions) and without retry
185185
func (r *AtlasCustomRoleReconciler) notFound(req ctrl.Request) ctrl.Result {
186-
r.Log.Infof("Object %s doesn't exist, was it deleted after reconcile request?", req.NamespacedName)
187-
return workflow.TerminateSilently().WithoutRetry().ReconcileResult()
186+
err := fmt.Errorf("object %s doesn't exist, was it deleted after reconcile request?", req.NamespacedName)
187+
r.Log.Infof(err.Error())
188+
return workflow.TerminateSilently(err).WithoutRetry().ReconcileResult()
188189
}
189190

190191
func (r *AtlasCustomRoleReconciler) SetupWithManager(mgr ctrl.Manager, skipNameValidation bool) error {

internal/controller/atlascustomrole/custom_role.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ import (
44
"fmt"
55
"reflect"
66

7+
"sigs.k8s.io/controller-runtime/pkg/client"
8+
79
"github.com/mongodb/mongodb-atlas-kubernetes/v2/api"
810
akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1"
911
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/customresource"
1012
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/workflow"
11-
12-
"sigs.k8s.io/controller-runtime/pkg/client"
13-
1413
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/translation/customroles"
1514
"github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/translation/project"
1615
)
@@ -45,12 +44,12 @@ func handleCustomRole(ctx *workflow.Context, k8sClient client.Client, project *p
4544
func (r *roleController) Reconcile() workflow.Result {
4645
if r.project.ID == "" {
4746
return workflow.Terminate(workflow.ProjectCustomRolesReady,
48-
fmt.Sprintf("the referenced AtlasProject resource '%s' doesn't have ID (status.ID is empty)", r.project.Name))
47+
fmt.Errorf("the referenced AtlasProject resource '%s' doesn't have ID (status.ID is empty)", r.project.Name))
4948
}
5049
roleFoundInAtlas := false
5150
roleInAtlas, err := r.service.Get(r.ctx.Context, r.project.ID, r.role.Spec.Role.Name)
5251
if err != nil {
53-
return workflow.Terminate(workflow.ProjectCustomRolesReady, err.Error())
52+
return workflow.Terminate(workflow.ProjectCustomRolesReady, err)
5453
}
5554
roleFoundInAtlas = roleInAtlas != customroles.CustomRole{}
5655

@@ -113,7 +112,7 @@ func (r *roleController) delete(roleInAtlas customroles.CustomRole) workflow.Res
113112

114113
func (r *roleController) terminate(reason workflow.ConditionReason, err error) workflow.Result {
115114
r.ctx.Log.Error(err)
116-
result := workflow.Terminate(reason, err.Error())
115+
result := workflow.Terminate(reason, err)
117116
r.ctx.SetConditionFromResult(api.ProjectCustomRolesReadyType, result)
118117
return result
119118
}

internal/controller/atlascustomrole/custom_role_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package atlascustomrole
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"net/http"
78
"reflect"
@@ -177,7 +178,7 @@ func Test_roleController_Reconcile(t *testing.T) {
177178
Status: status.AtlasCustomRoleStatus{},
178179
},
179180
},
180-
want: workflow.Terminate(workflow.AtlasCustomRoleNotCreated, "unable to create role"),
181+
want: workflow.Terminate(workflow.AtlasCustomRoleNotCreated, errors.New("unable to create role")),
181182
},
182183
{
183184
name: "Create custom role with error on Getting roles",
@@ -225,7 +226,7 @@ func Test_roleController_Reconcile(t *testing.T) {
225226
Status: status.AtlasCustomRoleStatus{},
226227
},
227228
},
228-
want: workflow.Terminate(workflow.ProjectCustomRolesReady, "unable to Get roles"),
229+
want: workflow.Terminate(workflow.ProjectCustomRolesReady, errors.New("unable to Get roles")),
229230
},
230231
{
231232
name: "Update custom role successfully",
@@ -374,7 +375,7 @@ func Test_roleController_Reconcile(t *testing.T) {
374375
Status: status.AtlasCustomRoleStatus{},
375376
},
376377
},
377-
want: workflow.Terminate(workflow.AtlasCustomRoleNotUpdated, "unable to update custom role"),
378+
want: workflow.Terminate(workflow.AtlasCustomRoleNotUpdated, errors.New("unable to update custom role")),
378379
},
379380
{
380381
name: "Update custom role successfully no update",
@@ -693,7 +694,7 @@ func Test_roleController_Reconcile(t *testing.T) {
693694
Status: status.AtlasCustomRoleStatus{},
694695
},
695696
},
696-
want: workflow.Terminate(workflow.AtlasCustomRoleNotDeleted, "unable to delete custom role"),
697+
want: workflow.Terminate(workflow.AtlasCustomRoleNotDeleted, errors.New("unable to delete custom role")),
697698
},
698699
}
699700
for _, tt := range tests {
@@ -929,7 +930,7 @@ func Test_handleCustomRole(t *testing.T) {
929930
},
930931
},
931932
},
932-
want: workflow.Terminate(workflow.ProjectCustomRolesReady, "the referenced AtlasProject resource 'testProject' doesn't have ID (status.ID is empty)"),
933+
want: workflow.Terminate(workflow.ProjectCustomRolesReady, errors.New("the referenced AtlasProject resource 'testProject' doesn't have ID (status.ID is empty)")),
933934
},
934935
{
935936
name: "DO NOT create custom role if external project reference doesn't exist",

internal/controller/atlasdatabaseuser/atlasdatabaseuser_controller.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,15 @@ func (r *AtlasDatabaseUserReconciler) Reconcile(ctx context.Context, req ctrl.Re
106106

107107
// notFound terminates the reconciliation silently(no updates on conditions) and without retry
108108
func (r *AtlasDatabaseUserReconciler) notFound(req ctrl.Request) ctrl.Result {
109-
r.Log.Infof("Object %s doesn't exist, was it deleted after reconcile request?", req.NamespacedName)
110-
return workflow.TerminateSilently().WithoutRetry().ReconcileResult()
109+
err := fmt.Errorf("object %s doesn't exist, was it deleted after reconcile request?", req.NamespacedName)
110+
r.Log.Infof(err.Error())
111+
return workflow.TerminateSilently(err).WithoutRetry().ReconcileResult()
111112
}
112113

113114
// fail terminates the reconciliation silently(no updates on conditions)
114115
func (r *AtlasDatabaseUserReconciler) fail(req ctrl.Request, err error) ctrl.Result {
115116
r.Log.Errorf("Failed to query object %s: %s", req.NamespacedName, err)
116-
return workflow.TerminateSilently().ReconcileResult()
117+
return workflow.TerminateSilently(err).ReconcileResult()
117118
}
118119

119120
// skip prevents the reconciliation to start and successfully return
@@ -132,7 +133,7 @@ func (r *AtlasDatabaseUserReconciler) terminate(
132133
err error,
133134
) ctrl.Result {
134135
r.Log.Errorf("resource %T(%s/%s) failed on condition %s: %s", object, object.GetNamespace(), object.GetName(), condition, err)
135-
result := workflow.Terminate(reason, err.Error())
136+
result := workflow.Terminate(reason, err)
136137
ctx.SetConditionFromResult(condition, result)
137138

138139
if !retry {

internal/controller/atlasdatabaseuser/atlasdatabaseuser_controller_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func TestNotFound(t *testing.T) {
174174
assert.Equal(t, ctrl.Result{}, c.notFound(ctrl.Request{NamespacedName: types.NamespacedName{Name: "object", Namespace: "test"}}))
175175
assert.Equal(t, 1, logs.Len())
176176
assert.Equal(t, zapcore.Level(0), logs.All()[0].Level)
177-
assert.Equal(t, "Object test/object doesn't exist, was it deleted after reconcile request?", logs.All()[0].Message)
177+
assert.Equal(t, "object test/object doesn't exist, was it deleted after reconcile request?", logs.All()[0].Message)
178178
})
179179
}
180180

@@ -311,7 +311,7 @@ func TestReady(t *testing.T) {
311311
return errors.New("failed to set finalizer")
312312
},
313313
},
314-
expectedResult: workflow.Terminate(workflow.AtlasFinalizerNotSet, "").ReconcileResult(),
314+
expectedResult: workflow.Terminate(workflow.AtlasFinalizerNotSet, errors.New("")).ReconcileResult(),
315315
expectedConditions: []api.Condition{
316316
api.FalseCondition(api.DatabaseUserReadyType).
317317
WithReason(string(workflow.AtlasFinalizerNotSet)).
@@ -348,7 +348,7 @@ func TestReady(t *testing.T) {
348348
return errors.New("failed to set last applied config")
349349
},
350350
},
351-
expectedResult: workflow.Terminate(workflow.Internal, "").ReconcileResult(),
351+
expectedResult: workflow.Terminate(workflow.Internal, errors.New("")).ReconcileResult(),
352352
expectedConditions: []api.Condition{
353353
api.FalseCondition(api.DatabaseUserReadyType).
354354
WithReason(string(workflow.Internal)).

internal/controller/atlasdatafederation/connectionsecrets.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ func (r *AtlasDataFederationReconciler) ensureConnectionSecrets(ctx *workflow.Co
1919
databaseUsers := akov2.AtlasDatabaseUserList{}
2020
err := r.Client.List(ctx.Context, &databaseUsers, &client.ListOptions{})
2121
if err != nil {
22-
return workflow.Terminate(workflow.Internal, err.Error())
22+
return workflow.Terminate(workflow.Internal, err)
2323
}
2424

2525
atlasDF, err := federationService.Get(ctx.Context, project.ID(), df.Spec.Name)
2626
if err != nil {
27-
return workflow.Terminate(workflow.Internal, err.Error())
27+
return workflow.Terminate(workflow.Internal, err)
2828
}
2929

3030
connectionHosts := atlasDF.Hostnames
@@ -57,7 +57,7 @@ func (r *AtlasDataFederationReconciler) ensureConnectionSecrets(ctx *workflow.Co
5757

5858
password, err := dbUser.ReadPassword(ctx.Context, r.Client)
5959
if err != nil {
60-
return workflow.Terminate(workflow.DeploymentConnectionSecretsNotCreated, err.Error())
60+
return workflow.Terminate(workflow.DeploymentConnectionSecretsNotCreated, err)
6161
}
6262

6363
var connURLs []string
@@ -75,7 +75,7 @@ func (r *AtlasDataFederationReconciler) ensureConnectionSecrets(ctx *workflow.Co
7575

7676
secretName, err := connectionsecret.Ensure(ctx.Context, r.Client, dbUser.Namespace, project.Spec.Name, project.ID(), df.Spec.Name, data)
7777
if err != nil {
78-
return workflow.Terminate(workflow.DeploymentConnectionSecretsNotCreated, err.Error())
78+
return workflow.Terminate(workflow.DeploymentConnectionSecretsNotCreated, err)
7979
}
8080
secrets = append(secrets, secretName)
8181
}

internal/controller/atlasdatafederation/datafederation.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ func (r *AtlasDataFederationReconciler) ensureDataFederation(ctx *workflow.Conte
1414

1515
akoDataFederation, err := datafederation.NewDataFederation(&dataFederation.Spec, projectID, nil)
1616
if err != nil {
17-
return workflow.Terminate(workflow.Internal, err.Error())
17+
return workflow.Terminate(workflow.Internal, err)
1818
}
1919

2020
atlasDataFederation, err := federationService.Get(ctx.Context, projectID, operatorSpec.Name)
2121
if err != nil {
2222
if !errors.Is(err, datafederation.ErrorNotFound) {
23-
return workflow.Terminate(workflow.Internal, err.Error())
23+
return workflow.Terminate(workflow.Internal, err)
2424
}
2525

2626
err = federationService.Create(ctx.Context, akoDataFederation)
2727
if err != nil {
28-
return workflow.Terminate(workflow.DataFederationNotCreatedInAtlas, err.Error())
28+
return workflow.Terminate(workflow.DataFederationNotCreatedInAtlas, err)
2929
}
3030

3131
return workflow.InProgress(workflow.DataFederationCreating, "Data Federation is being created")
@@ -37,7 +37,7 @@ func (r *AtlasDataFederationReconciler) ensureDataFederation(ctx *workflow.Conte
3737

3838
err = federationService.Update(ctx.Context, akoDataFederation)
3939
if err != nil {
40-
return workflow.Terminate(workflow.DataFederationNotUpdatedInAtlas, err.Error())
40+
return workflow.Terminate(workflow.DataFederationNotUpdatedInAtlas, err)
4141
}
4242

4343
return workflow.InProgress(workflow.DataFederationUpdating, "Data Federation is being updated")

internal/controller/atlasdatafederation/datafederation_controller.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (r *AtlasDataFederationReconciler) Reconcile(context context.Context, req c
6565
if !dataFederation.GetDeletionTimestamp().IsZero() {
6666
err := customresource.ManageFinalizer(context, r.Client, dataFederation, customresource.UnsetFinalizer)
6767
if err != nil {
68-
result = workflow.Terminate(workflow.Internal, err.Error())
68+
result = workflow.Terminate(workflow.Internal, err)
6969
log.Errorw("failed to remove finalizer", "error", err)
7070
return result.ReconcileResult(), nil
7171
}
@@ -85,7 +85,7 @@ func (r *AtlasDataFederationReconciler) Reconcile(context context.Context, req c
8585
}
8686

8787
if !r.AtlasProvider.IsResourceSupported(dataFederation) {
88-
result := workflow.Terminate(workflow.AtlasGovUnsupported, "the AtlasDataFederation is not supported by Atlas for government").
88+
result := workflow.Terminate(workflow.AtlasGovUnsupported, errors.New("the AtlasDataFederation is not supported by Atlas for government")).
8989
WithoutRetry()
9090
ctx.SetConditionFromResult(api.DataFederationReadyType, result)
9191
return result.ReconcileResult(), nil
@@ -99,14 +99,14 @@ func (r *AtlasDataFederationReconciler) Reconcile(context context.Context, req c
9999

100100
endpointService, err := datafederation.NewDatafederationPrivateEndpointService(ctx.Context, r.AtlasProvider, project.ConnectionSecretObjectKey(), log)
101101
if err != nil {
102-
result = workflow.Terminate(workflow.AtlasAPIAccessNotConfigured, err.Error())
102+
result = workflow.Terminate(workflow.AtlasAPIAccessNotConfigured, err)
103103
ctx.SetConditionFromResult(api.DatabaseUserReadyType, result)
104104
return result.ReconcileResult(), nil
105105
}
106106

107107
dataFederationService, err := datafederation.NewAtlasDataFederationService(ctx.Context, r.AtlasProvider, project.ConnectionSecretObjectKey(), log)
108108
if err != nil {
109-
result = workflow.Terminate(workflow.AtlasAPIAccessNotConfigured, err.Error())
109+
result = workflow.Terminate(workflow.AtlasAPIAccessNotConfigured, err)
110110
ctx.SetConditionFromResult(api.DatabaseUserReadyType, result)
111111
return result.ReconcileResult(), nil
112112
}
@@ -129,12 +129,12 @@ func (r *AtlasDataFederationReconciler) Reconcile(context context.Context, req c
129129
if !customresource.HaveFinalizer(dataFederation, customresource.FinalizerLabel) {
130130
err = r.Client.Get(context, kube.ObjectKeyFromObject(dataFederation), dataFederation)
131131
if err != nil {
132-
result = workflow.Terminate(workflow.Internal, err.Error())
132+
result = workflow.Terminate(workflow.Internal, err)
133133
return result.ReconcileResult(), nil
134134
}
135135
customresource.SetFinalizer(dataFederation, customresource.FinalizerLabel)
136136
if err = r.Client.Update(context, dataFederation); err != nil {
137-
result = workflow.Terminate(workflow.Internal, err.Error())
137+
result = workflow.Terminate(workflow.Internal, err)
138138
log.Errorw("failed to add finalizer", "error", err)
139139
return result.ReconcileResult(), nil
140140
}
@@ -147,7 +147,7 @@ func (r *AtlasDataFederationReconciler) Reconcile(context context.Context, req c
147147

148148
err = customresource.ApplyLastConfigApplied(context, dataFederation, r.Client)
149149
if err != nil {
150-
result = workflow.Terminate(workflow.Internal, err.Error())
150+
result = workflow.Terminate(workflow.Internal, err)
151151
ctx.SetConditionFromResult(api.DataFederationReadyType, result)
152152
log.Error(result.GetMessage())
153153

@@ -166,19 +166,19 @@ func (r *AtlasDataFederationReconciler) handleDelete(ctx *workflow.Context, log
166166
} else {
167167
if err := r.deleteConnectionSecrets(ctx.Context, dataFederation); err != nil {
168168
log.Errorf("failed to remove DataFederation connection secrets from Atlas: %s", err)
169-
result := workflow.Terminate(workflow.Internal, err.Error())
169+
result := workflow.Terminate(workflow.Internal, err)
170170
ctx.SetConditionFromResult(api.DataFederationReadyType, result)
171171
return result
172172
}
173173
if err := r.deleteDataFederationFromAtlas(ctx.Context, service, dataFederation, project, log); err != nil {
174174
log.Errorf("failed to remove DataFederation from Atlas: %s", err)
175-
result := workflow.Terminate(workflow.Internal, err.Error())
175+
result := workflow.Terminate(workflow.Internal, err)
176176
ctx.SetConditionFromResult(api.DataFederationReadyType, result)
177177
return result
178178
}
179179
}
180180
if err := customresource.ManageFinalizer(ctx.Context, r.Client, dataFederation, customresource.UnsetFinalizer); err != nil {
181-
result := workflow.Terminate(workflow.AtlasFinalizerNotRemoved, err.Error())
181+
result := workflow.Terminate(workflow.AtlasFinalizerNotRemoved, err)
182182
log.Errorw("failed to remove finalizer", "error", err)
183183
return result
184184
}
@@ -207,7 +207,7 @@ func (r *AtlasDataFederationReconciler) deleteDataFederationFromAtlas(ctx contex
207207

208208
func (r *AtlasDataFederationReconciler) readProjectResource(ctx context.Context, dataFederation *akov2.AtlasDataFederation, project *akov2.AtlasProject) workflow.Result {
209209
if err := r.Client.Get(ctx, dataFederation.AtlasProjectObjectKey(), project); err != nil {
210-
return workflow.Terminate(workflow.Internal, err.Error())
210+
return workflow.Terminate(workflow.Internal, err)
211211
}
212212
return workflow.OK()
213213
}

internal/controller/atlasdatafederation/private_endpoint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (r *AtlasDataFederationReconciler) ensurePrivateEndpoints(ctx *workflow.Con
3737

3838
func (r *AtlasDataFederationReconciler) privateEndpointsFailed(ctx *workflow.Context, err error) workflow.Result {
3939
ctx.Log.Errorw("getAllDataFederationPEs error", "err", err.Error())
40-
result := workflow.Terminate(workflow.Internal, err.Error())
40+
result := workflow.Terminate(workflow.Internal, err)
4141
ctx.SetConditionFromResult(api.DataFederationPEReadyType, result)
4242
return result
4343
}

0 commit comments

Comments
 (0)