|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "github.com/go-logr/logr" |
| 23 | + corev1 "k8s.io/api/core/v1" |
| 24 | + "k8s.io/apimachinery/pkg/types" |
| 25 | + "k8s.io/client-go/tools/record" |
| 26 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/shared_constants" |
| 27 | + ctrl "sigs.k8s.io/controller-runtime" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | + "sigs.k8s.io/controller-runtime/pkg/controller" |
| 30 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 31 | + |
| 32 | + agaapi "sigs.k8s.io/aws-load-balancer-controller/apis/aga/v1beta1" |
| 33 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/config" |
| 34 | + ctrlerrors "sigs.k8s.io/aws-load-balancer-controller/pkg/error" |
| 35 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/k8s" |
| 36 | + lbcmetrics "sigs.k8s.io/aws-load-balancer-controller/pkg/metrics/lbc" |
| 37 | + metricsutil "sigs.k8s.io/aws-load-balancer-controller/pkg/metrics/util" |
| 38 | + "sigs.k8s.io/aws-load-balancer-controller/pkg/runtime" |
| 39 | +) |
| 40 | + |
| 41 | +const ( |
| 42 | + controllerName = "globalAccelerator" |
| 43 | + |
| 44 | + // Metric stage constants |
| 45 | + MetricStageFetchGlobalAccelerator = "fetch_globalAccelerator" |
| 46 | + MetricStageAddFinalizers = "add_finalizers" |
| 47 | + MetricStageReconcileGlobalAccelerator = "reconcile_globalaccelerator" |
| 48 | + |
| 49 | + // Metric error constants |
| 50 | + MetricErrorAddFinalizers = "add_finalizers_error" |
| 51 | + MetricErrorReconcileGlobalAccelerator = "reconcile_globalaccelerator_error" |
| 52 | +) |
| 53 | + |
| 54 | +// NewGlobalAcceleratorReconciler constructs new globalAcceleratorReconciler |
| 55 | +func NewGlobalAcceleratorReconciler(k8sClient client.Client, eventRecorder record.EventRecorder, finalizerManager k8s.FinalizerManager, |
| 56 | + config config.ControllerConfig, logger logr.Logger, metricsCollector lbcmetrics.MetricCollector, reconcileCounters *metricsutil.ReconcileCounters) *globalAcceleratorReconciler { |
| 57 | + |
| 58 | + return &globalAcceleratorReconciler{ |
| 59 | + k8sClient: k8sClient, |
| 60 | + eventRecorder: eventRecorder, |
| 61 | + finalizerManager: finalizerManager, |
| 62 | + logger: logger, |
| 63 | + metricsCollector: metricsCollector, |
| 64 | + reconcileTracker: reconcileCounters.IncrementAGA, |
| 65 | + |
| 66 | + maxConcurrentReconciles: config.GlobalAcceleratorMaxConcurrentReconciles, |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// globalAcceleratorReconciler reconciles a GlobalAccelerator object |
| 71 | +type globalAcceleratorReconciler struct { |
| 72 | + k8sClient client.Client |
| 73 | + eventRecorder record.EventRecorder |
| 74 | + finalizerManager k8s.FinalizerManager |
| 75 | + logger logr.Logger |
| 76 | + metricsCollector lbcmetrics.MetricCollector |
| 77 | + reconcileTracker func(namespaceName types.NamespacedName) |
| 78 | + |
| 79 | + maxConcurrentReconciles int |
| 80 | +} |
| 81 | + |
| 82 | +// +kubebuilder:rbac:groups=aga.k8s.aws,resources=globalaccelerators,verbs=get;list;watch;patch |
| 83 | +// +kubebuilder:rbac:groups=aga.k8s.aws,resources=globalaccelerators/status,verbs=update;patch |
| 84 | +// +kubebuilder:rbac:groups=aga.k8s.aws,resources=globalaccelerators/finalizers,verbs=update;patch |
| 85 | +func (r *globalAcceleratorReconciler) Reconcile(ctx context.Context, req reconcile.Request) (ctrl.Result, error) { |
| 86 | + r.reconcileTracker(req.NamespacedName) |
| 87 | + r.logger.V(1).Info("Reconcile request", "name", req.Name) |
| 88 | + err := r.reconcile(ctx, req) |
| 89 | + return runtime.HandleReconcileError(err, r.logger) |
| 90 | +} |
| 91 | + |
| 92 | +func (r *globalAcceleratorReconciler) reconcile(ctx context.Context, req reconcile.Request) error { |
| 93 | + ga := &agaapi.GlobalAccelerator{} |
| 94 | + var err error |
| 95 | + fetchGlobalAcceleratorFn := func() { |
| 96 | + err = r.k8sClient.Get(ctx, req.NamespacedName, ga) |
| 97 | + } |
| 98 | + r.metricsCollector.ObserveControllerReconcileLatency(controllerName, MetricStageFetchGlobalAccelerator, fetchGlobalAcceleratorFn) |
| 99 | + if err != nil { |
| 100 | + return client.IgnoreNotFound(err) |
| 101 | + } |
| 102 | + |
| 103 | + if ga.DeletionTimestamp != nil && !ga.DeletionTimestamp.IsZero() { |
| 104 | + return r.cleanupGlobalAccelerator(ctx, ga) |
| 105 | + } |
| 106 | + return r.reconcileGlobalAccelerator(ctx, ga) |
| 107 | +} |
| 108 | + |
| 109 | +func (r *globalAcceleratorReconciler) reconcileGlobalAccelerator(ctx context.Context, ga *agaapi.GlobalAccelerator) error { |
| 110 | + var err error |
| 111 | + finalizerFn := func() { |
| 112 | + if !k8s.HasFinalizer(ga, shared_constants.GlobalAcceleratorFinalizer) { |
| 113 | + err = r.finalizerManager.AddFinalizers(ctx, ga, shared_constants.GlobalAcceleratorFinalizer) |
| 114 | + } |
| 115 | + } |
| 116 | + r.metricsCollector.ObserveControllerReconcileLatency(controllerName, MetricStageAddFinalizers, finalizerFn) |
| 117 | + if err != nil { |
| 118 | + r.eventRecorder.Event(ga, corev1.EventTypeWarning, k8s.GlobalAcceleratorEventReasonFailedAddFinalizer, fmt.Sprintf("Failed add finalizer due to %v", err)) |
| 119 | + return ctrlerrors.NewErrorWithMetrics(controllerName, MetricErrorAddFinalizers, err, r.metricsCollector) |
| 120 | + } |
| 121 | + |
| 122 | + // TODO: Implement GlobalAccelerator resource management |
| 123 | + // This would include: |
| 124 | + // 1. Creating/updating AWS Global Accelerator |
| 125 | + // 2. Managing listeners and endpoint groups |
| 126 | + // 3. Handling endpoint discovery from Services/Ingresses/Gateways |
| 127 | + reconcileResourceFn := func() { |
| 128 | + err = r.reconcileGlobalAcceleratorResources(ctx, ga) |
| 129 | + } |
| 130 | + r.metricsCollector.ObserveControllerReconcileLatency(controllerName, MetricStageReconcileGlobalAccelerator, reconcileResourceFn) |
| 131 | + if err != nil { |
| 132 | + return ctrlerrors.NewErrorWithMetrics(controllerName, MetricErrorReconcileGlobalAccelerator, err, r.metricsCollector) |
| 133 | + } |
| 134 | + |
| 135 | + r.eventRecorder.Event(ga, corev1.EventTypeNormal, k8s.GlobalAcceleratorEventReasonSuccessfullyReconciled, "Successfully reconciled") |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func (r *globalAcceleratorReconciler) cleanupGlobalAccelerator(ctx context.Context, ga *agaapi.GlobalAccelerator) error { |
| 140 | + if k8s.HasFinalizer(ga, shared_constants.GlobalAcceleratorFinalizer) { |
| 141 | + // TODO: Implement cleanup logic for AWS Global Accelerator resources |
| 142 | + if err := r.cleanupGlobalAcceleratorResources(ctx, ga); err != nil { |
| 143 | + r.eventRecorder.Event(ga, corev1.EventTypeWarning, k8s.GlobalAcceleratorEventReasonFailedCleanup, fmt.Sprintf("Failed cleanup due to %v", err)) |
| 144 | + return err |
| 145 | + } |
| 146 | + if err := r.finalizerManager.RemoveFinalizers(ctx, ga, shared_constants.GlobalAcceleratorFinalizer); err != nil { |
| 147 | + r.eventRecorder.Event(ga, corev1.EventTypeWarning, k8s.GlobalAcceleratorEventReasonFailedRemoveFinalizer, fmt.Sprintf("Failed remove finalizer due to %v", err)) |
| 148 | + return err |
| 149 | + } |
| 150 | + } |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +func (r *globalAcceleratorReconciler) reconcileGlobalAcceleratorResources(ctx context.Context, ga *agaapi.GlobalAccelerator) error { |
| 155 | + // TODO: Implement the actual AWS Global Accelerator resource management |
| 156 | + // This is a placeholder implementation |
| 157 | + r.logger.Info("Reconciling GlobalAccelerator resources", "name", ga.Name, "namespace", ga.Namespace) |
| 158 | + |
| 159 | + return nil |
| 160 | +} |
| 161 | + |
| 162 | +func (r *globalAcceleratorReconciler) cleanupGlobalAcceleratorResources(ctx context.Context, ga *agaapi.GlobalAccelerator) error { |
| 163 | + // TODO: Implement the actual AWS Global Accelerator resource cleanup |
| 164 | + // This is a placeholder implementation |
| 165 | + r.logger.Info("Cleaning up GlobalAccelerator resources", "name", ga.Name, "namespace", ga.Namespace) |
| 166 | + return nil |
| 167 | +} |
| 168 | + |
| 169 | +func (r *globalAcceleratorReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error { |
| 170 | + if err := r.setupIndexes(ctx, mgr.GetFieldIndexer()); err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + |
| 174 | + // TODO: Add event handlers for Services, Ingresses, and Gateways |
| 175 | + // that are referenced by GlobalAccelerator endpoints |
| 176 | + |
| 177 | + return ctrl.NewControllerManagedBy(mgr). |
| 178 | + For(&agaapi.GlobalAccelerator{}). |
| 179 | + Named(controllerName). |
| 180 | + WithOptions(controller.Options{ |
| 181 | + MaxConcurrentReconciles: r.maxConcurrentReconciles, |
| 182 | + }). |
| 183 | + Complete(r) |
| 184 | +} |
| 185 | + |
| 186 | +func (r *globalAcceleratorReconciler) setupIndexes(ctx context.Context, fieldIndexer client.FieldIndexer) error { |
| 187 | + // TODO: Add field indexes for efficient lookups |
| 188 | + return nil |
| 189 | +} |
0 commit comments