|
| 1 | +// Copyright 2025 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package backupcompliancepolicy |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + controllerruntime "sigs.k8s.io/controller-runtime" |
| 22 | + builder "sigs.k8s.io/controller-runtime/pkg/builder" |
| 23 | + client "sigs.k8s.io/controller-runtime/pkg/client" |
| 24 | + controller "sigs.k8s.io/controller-runtime/pkg/controller" |
| 25 | + reconcile "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 26 | + |
| 27 | + v1 "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/nextapi/generated/v1" |
| 28 | + ctrlstate "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/controller/state" |
| 29 | + result "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/result" |
| 30 | + state "github.com/mongodb/mongodb-atlas-kubernetes/v2/pkg/state" |
| 31 | +) |
| 32 | + |
| 33 | +// getHandlerForResource selects the appropriate version-specific handler based on which resource spec version is set |
| 34 | +func (h *BackupCompliancePolicyHandler) getHandlerForResource(backupcompliancepolicy *v1.BackupCompliancePolicy) (ctrlstate.StateHandler[v1.BackupCompliancePolicy], error) { |
| 35 | + // Check which resource spec version is set and validate that only one is specified |
| 36 | + var versionCount int |
| 37 | + var selectedHandler ctrlstate.StateHandler[v1.BackupCompliancePolicy] |
| 38 | + |
| 39 | + if backupcompliancepolicy.Spec.V20250312 != nil { |
| 40 | + versionCount++ |
| 41 | + selectedHandler = h.handlerv20250312 |
| 42 | + } |
| 43 | + |
| 44 | + if versionCount == 0 { |
| 45 | + return nil, fmt.Errorf("no resource spec version specified - please set one of the available spec versions") |
| 46 | + } |
| 47 | + if versionCount > 1 { |
| 48 | + return nil, fmt.Errorf("multiple resource spec versions specified - please set only one spec version") |
| 49 | + } |
| 50 | + return selectedHandler, nil |
| 51 | +} |
| 52 | + |
| 53 | +// HandleInitial delegates to the version-specific handler |
| 54 | +func (h *BackupCompliancePolicyHandler) HandleInitial(ctx context.Context, backupcompliancepolicy *v1.BackupCompliancePolicy) (ctrlstate.Result, error) { |
| 55 | + handler, err := h.getHandlerForResource(backupcompliancepolicy) |
| 56 | + if err != nil { |
| 57 | + return result.Error(state.StateInitial, err) |
| 58 | + } |
| 59 | + return handler.HandleInitial(ctx, backupcompliancepolicy) |
| 60 | +} |
| 61 | + |
| 62 | +// HandleImportRequested delegates to the version-specific handler |
| 63 | +func (h *BackupCompliancePolicyHandler) HandleImportRequested(ctx context.Context, backupcompliancepolicy *v1.BackupCompliancePolicy) (ctrlstate.Result, error) { |
| 64 | + handler, err := h.getHandlerForResource(backupcompliancepolicy) |
| 65 | + if err != nil { |
| 66 | + return result.Error(state.StateInitial, err) |
| 67 | + } |
| 68 | + return handler.HandleImportRequested(ctx, backupcompliancepolicy) |
| 69 | +} |
| 70 | + |
| 71 | +// HandleImported delegates to the version-specific handler |
| 72 | +func (h *BackupCompliancePolicyHandler) HandleImported(ctx context.Context, backupcompliancepolicy *v1.BackupCompliancePolicy) (ctrlstate.Result, error) { |
| 73 | + handler, err := h.getHandlerForResource(backupcompliancepolicy) |
| 74 | + if err != nil { |
| 75 | + return result.Error(state.StateInitial, err) |
| 76 | + } |
| 77 | + return handler.HandleImported(ctx, backupcompliancepolicy) |
| 78 | +} |
| 79 | + |
| 80 | +// HandleCreating delegates to the version-specific handler |
| 81 | +func (h *BackupCompliancePolicyHandler) HandleCreating(ctx context.Context, backupcompliancepolicy *v1.BackupCompliancePolicy) (ctrlstate.Result, error) { |
| 82 | + handler, err := h.getHandlerForResource(backupcompliancepolicy) |
| 83 | + if err != nil { |
| 84 | + return result.Error(state.StateInitial, err) |
| 85 | + } |
| 86 | + return handler.HandleCreating(ctx, backupcompliancepolicy) |
| 87 | +} |
| 88 | + |
| 89 | +// HandleCreated delegates to the version-specific handler |
| 90 | +func (h *BackupCompliancePolicyHandler) HandleCreated(ctx context.Context, backupcompliancepolicy *v1.BackupCompliancePolicy) (ctrlstate.Result, error) { |
| 91 | + handler, err := h.getHandlerForResource(backupcompliancepolicy) |
| 92 | + if err != nil { |
| 93 | + return result.Error(state.StateInitial, err) |
| 94 | + } |
| 95 | + return handler.HandleCreated(ctx, backupcompliancepolicy) |
| 96 | +} |
| 97 | + |
| 98 | +// HandleUpdating delegates to the version-specific handler |
| 99 | +func (h *BackupCompliancePolicyHandler) HandleUpdating(ctx context.Context, backupcompliancepolicy *v1.BackupCompliancePolicy) (ctrlstate.Result, error) { |
| 100 | + handler, err := h.getHandlerForResource(backupcompliancepolicy) |
| 101 | + if err != nil { |
| 102 | + return result.Error(state.StateInitial, err) |
| 103 | + } |
| 104 | + return handler.HandleUpdating(ctx, backupcompliancepolicy) |
| 105 | +} |
| 106 | + |
| 107 | +// HandleUpdated delegates to the version-specific handler |
| 108 | +func (h *BackupCompliancePolicyHandler) HandleUpdated(ctx context.Context, backupcompliancepolicy *v1.BackupCompliancePolicy) (ctrlstate.Result, error) { |
| 109 | + handler, err := h.getHandlerForResource(backupcompliancepolicy) |
| 110 | + if err != nil { |
| 111 | + return result.Error(state.StateInitial, err) |
| 112 | + } |
| 113 | + return handler.HandleUpdated(ctx, backupcompliancepolicy) |
| 114 | +} |
| 115 | + |
| 116 | +// HandleDeletionRequested delegates to the version-specific handler |
| 117 | +func (h *BackupCompliancePolicyHandler) HandleDeletionRequested(ctx context.Context, backupcompliancepolicy *v1.BackupCompliancePolicy) (ctrlstate.Result, error) { |
| 118 | + handler, err := h.getHandlerForResource(backupcompliancepolicy) |
| 119 | + if err != nil { |
| 120 | + return result.Error(state.StateInitial, err) |
| 121 | + } |
| 122 | + return handler.HandleDeletionRequested(ctx, backupcompliancepolicy) |
| 123 | +} |
| 124 | + |
| 125 | +// HandleDeleting delegates to the version-specific handler |
| 126 | +func (h *BackupCompliancePolicyHandler) HandleDeleting(ctx context.Context, backupcompliancepolicy *v1.BackupCompliancePolicy) (ctrlstate.Result, error) { |
| 127 | + handler, err := h.getHandlerForResource(backupcompliancepolicy) |
| 128 | + if err != nil { |
| 129 | + return result.Error(state.StateInitial, err) |
| 130 | + } |
| 131 | + return handler.HandleDeleting(ctx, backupcompliancepolicy) |
| 132 | +} |
| 133 | + |
| 134 | +// For returns the resource and predicates for the controller |
| 135 | +func (h *BackupCompliancePolicyHandler) For() (client.Object, builder.Predicates) { |
| 136 | + obj := &v1.BackupCompliancePolicy{} |
| 137 | + // TODO: Add appropriate predicates |
| 138 | + return obj, builder.WithPredicates() |
| 139 | +} |
| 140 | + |
| 141 | +// SetupWithManager sets up the controller with the Manager |
| 142 | +func (h *BackupCompliancePolicyHandler) SetupWithManager(mgr controllerruntime.Manager, rec reconcile.Reconciler, defaultOptions controller.Options) error { |
| 143 | + h.Client = mgr.GetClient() |
| 144 | + return controllerruntime.NewControllerManagedBy(mgr).Named("BackupCompliancePolicy").For(h.For()).WithOptions(defaultOptions).Complete(rec) |
| 145 | +} |
0 commit comments