|
| 1 | +package atlasproject |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + |
| 7 | + "go.mongodb.org/atlas/mongodbatlas" |
| 8 | + |
| 9 | + mdbv1 "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1" |
| 10 | + "github.com/mongodb/mongodb-atlas-kubernetes/pkg/api/v1/project" |
| 11 | + "github.com/mongodb/mongodb-atlas-kubernetes/pkg/controller/workflow" |
| 12 | +) |
| 13 | + |
| 14 | +// ensureMaintenanceWindow ensures that the state of the Atlas Maintenance Window matches the |
| 15 | +// state of the Maintenance Window specified in the project CR. If a Maintenance Window exists |
| 16 | +// in Atlas but is not specified in the CR, it is deleted. |
| 17 | +func ensureMaintenanceWindow(ctx *workflow.Context, projectID string, atlasProject *mdbv1.AtlasProject) workflow.Result { |
| 18 | + windowSpec := atlasProject.Spec.MaintenanceWindow |
| 19 | + if err := validateMaintenanceWindow(windowSpec); err != nil { |
| 20 | + return workflow.Terminate(workflow.ProjectWindowInvalid, err.Error()) |
| 21 | + } |
| 22 | + |
| 23 | + if isEmptyWindow(windowSpec) { |
| 24 | + ctx.Log.Debugw("Window empty or undefined, deleting in Atlas") |
| 25 | + if result := deleteInAtlas(ctx.Client, projectID); !result.IsOk() { |
| 26 | + return result |
| 27 | + } |
| 28 | + return workflow.OK() |
| 29 | + } |
| 30 | + |
| 31 | + if windowSpecified(windowSpec) { |
| 32 | + ctx.Log.Debugw("Checking if window needs update") |
| 33 | + windowInAtlas, result := getInAtlas(ctx.Client, projectID) |
| 34 | + if !result.IsOk() { |
| 35 | + return result |
| 36 | + } |
| 37 | + |
| 38 | + if windowInAtlas.DayOfWeek == 0 || windowInAtlas.HourOfDay == nil || |
| 39 | + *windowInAtlas.HourOfDay != windowSpec.HourOfDay || windowInAtlas.DayOfWeek != windowSpec.DayOfWeek { |
| 40 | + ctx.Log.Debugw("Creating or updating window") |
| 41 | + // We set startASAP to false because the operator takes care of calling the API a second time if both |
| 42 | + // startASAP and the new maintenance timeslots are defined |
| 43 | + if result := createOrUpdateInAtlas(ctx.Client, projectID, windowSpec.WithStartASAP(false)); !result.IsOk() { |
| 44 | + return result |
| 45 | + } |
| 46 | + } else if *windowInAtlas.AutoDeferOnceEnabled != windowSpec.AutoDefer { |
| 47 | + // If autoDefer flag is different in Atlas, and we haven't updated the window previously, we toggle the flag |
| 48 | + ctx.Log.Debugw("Toggling autoDefer") |
| 49 | + if result := toggleAutoDeferInAtlas(ctx.Client, projectID); !result.IsOk() { |
| 50 | + return result |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if windowSpec.StartASAP { |
| 56 | + ctx.Log.Debugw("Starting maintenance ASAP") |
| 57 | + // To avoid any unexpected behavior, we send a request to the API containing only the StartASAP flag, |
| 58 | + // although the API should ignore other fields in that case |
| 59 | + if result := createOrUpdateInAtlas(ctx.Client, projectID, project.NewMaintenanceWindow().WithStartASAP(true)); !result.IsOk() { |
| 60 | + return result |
| 61 | + } |
| 62 | + // Nothing else should be done after sending a StartASAP request |
| 63 | + return workflow.OK() |
| 64 | + } |
| 65 | + |
| 66 | + if windowSpec.Defer { |
| 67 | + ctx.Log.Debugw("Deferring scheduled maintenance") |
| 68 | + if result := deferInAtlas(ctx.Client, projectID); !result.IsOk() { |
| 69 | + return result |
| 70 | + } |
| 71 | + // Nothing else should be done after deferring |
| 72 | + return workflow.OK() |
| 73 | + } |
| 74 | + |
| 75 | + return workflow.OK() |
| 76 | +} |
| 77 | + |
| 78 | +func isEmpty(i int) bool { |
| 79 | + return i == 0 |
| 80 | +} |
| 81 | + |
| 82 | +func isEmptyWindow(window project.MaintenanceWindow) bool { |
| 83 | + return isEmpty(window.DayOfWeek) && isEmpty(window.HourOfDay) && !window.StartASAP && !window.Defer && !window.AutoDefer |
| 84 | +} |
| 85 | + |
| 86 | +func windowSpecified(window project.MaintenanceWindow) bool { |
| 87 | + return !isEmpty(window.DayOfWeek) |
| 88 | +} |
| 89 | + |
| 90 | +func maxOneFlag(window project.MaintenanceWindow) bool { |
| 91 | + return !(window.StartASAP && window.Defer) |
| 92 | +} |
| 93 | + |
| 94 | +// validateMaintenanceWindow performs validation of the Maintenance Window. Note, that we intentionally don't validate |
| 95 | +// that hour of day and day of week are in the bounds - this will be done by Atlas. |
| 96 | +func validateMaintenanceWindow(window project.MaintenanceWindow) error { |
| 97 | + if isEmptyWindow(window) || (windowSpecified(window) && maxOneFlag(window)) { |
| 98 | + return nil |
| 99 | + } |
| 100 | + errorString := "projectMaintenanceWindow must respect the following constraints, or be empty : " + |
| 101 | + "1) dayOfWeek must be specified (hourOfDay is 0 by default, autoDeferral is false by default) " + |
| 102 | + "2) only one of (startASAP, defer) is true" |
| 103 | + return errors.New(errorString) |
| 104 | +} |
| 105 | + |
| 106 | +// operatorToAtlasMaintenanceWindow converts the maintenanceWindow specified in the project CR to the format |
| 107 | +// expected by the Atlas API. |
| 108 | +func operatorToAtlasMaintenanceWindow(maintenanceWindow project.MaintenanceWindow) (*mongodbatlas.MaintenanceWindow, workflow.Result) { |
| 109 | + operatorWindow := maintenanceWindow.ToAtlas() |
| 110 | + return operatorWindow, workflow.OK() |
| 111 | +} |
| 112 | + |
| 113 | +func getInAtlas(client mongodbatlas.Client, projectID string) (*mongodbatlas.MaintenanceWindow, workflow.Result) { |
| 114 | + window, _, err := client.MaintenanceWindows.Get(context.Background(), projectID) |
| 115 | + if err != nil { |
| 116 | + return nil, workflow.Terminate(workflow.ProjectWindowNotObtainedFromAtlas, err.Error()) |
| 117 | + } |
| 118 | + return window, workflow.OK() |
| 119 | +} |
| 120 | + |
| 121 | +func createOrUpdateInAtlas(client mongodbatlas.Client, projectID string, maintenanceWindow project.MaintenanceWindow) workflow.Result { |
| 122 | + operatorWindow, status := operatorToAtlasMaintenanceWindow(maintenanceWindow) |
| 123 | + if !status.IsOk() { |
| 124 | + return status |
| 125 | + } |
| 126 | + |
| 127 | + if _, err := client.MaintenanceWindows.Update(context.Background(), projectID, operatorWindow); err != nil { |
| 128 | + return workflow.Terminate(workflow.ProjectWindowNotCreatedInAtlas, err.Error()) |
| 129 | + } |
| 130 | + return workflow.OK() |
| 131 | +} |
| 132 | + |
| 133 | +func deleteInAtlas(client mongodbatlas.Client, projectID string) workflow.Result { |
| 134 | + if _, err := client.MaintenanceWindows.Reset(context.Background(), projectID); err != nil { |
| 135 | + return workflow.Terminate(workflow.ProjectWindowNotDeletedInAtlas, err.Error()) |
| 136 | + } |
| 137 | + return workflow.OK() |
| 138 | +} |
| 139 | + |
| 140 | +func deferInAtlas(client mongodbatlas.Client, projectID string) workflow.Result { |
| 141 | + if _, err := client.MaintenanceWindows.Defer(context.Background(), projectID); err != nil { |
| 142 | + return workflow.Terminate(workflow.ProjectWindowNotDeferredInAtlas, err.Error()) |
| 143 | + } |
| 144 | + return workflow.OK() |
| 145 | +} |
| 146 | + |
| 147 | +// toggleAutoDeferInAtlas toggles the field "autoDeferOnceEnabled" by sending a POST /autoDefer request to the API |
| 148 | +func toggleAutoDeferInAtlas(client mongodbatlas.Client, projectID string) workflow.Result { |
| 149 | + if _, err := client.MaintenanceWindows.AutoDefer(context.Background(), projectID); err != nil { |
| 150 | + return workflow.Terminate(workflow.ProjectWindowNotAutoDeferredInAtlas, err.Error()) |
| 151 | + } |
| 152 | + return workflow.OK() |
| 153 | +} |
0 commit comments