|
| 1 | +// Copyright 2025 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package awsloadbalancercontroller |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/spf13/pflag" |
| 11 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 12 | + runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" |
| 13 | + ctrl "sigs.k8s.io/controller-runtime" |
| 14 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 15 | + |
| 16 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1" |
| 17 | + commonhandlers "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers" |
| 18 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/lifecycle" |
| 19 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/utils" |
| 20 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/lifecycle/addons" |
| 21 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/lifecycle/config" |
| 22 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/options" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + // Abbreviate name to be <63 characters with the UUID suffix. |
| 27 | + defaultHelmReleaseName = "aws-lb-controller" |
| 28 | + defaultHelmReleaseNamespace = "kube-system" |
| 29 | +) |
| 30 | + |
| 31 | +type ControllerConfig struct { |
| 32 | + *options.GlobalOptions |
| 33 | + |
| 34 | + helmAddonConfig *addons.HelmAddonConfig |
| 35 | +} |
| 36 | + |
| 37 | +func NewControllerConfig(globalOptions *options.GlobalOptions) *ControllerConfig { |
| 38 | + return &ControllerConfig{ |
| 39 | + GlobalOptions: globalOptions, |
| 40 | + helmAddonConfig: addons.NewHelmAddonConfig( |
| 41 | + "default-aws-load-balancer-controller-helm-values-template", |
| 42 | + defaultHelmReleaseNamespace, |
| 43 | + defaultHelmReleaseName, |
| 44 | + ), |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func (c *ControllerConfig) AddFlags(prefix string, flags *pflag.FlagSet) { |
| 49 | + c.helmAddonConfig.AddFlags(prefix+".helm-addon", flags) |
| 50 | +} |
| 51 | + |
| 52 | +type DefaultAWSLoadBalancerController struct { |
| 53 | + client ctrlclient.Client |
| 54 | + config *ControllerConfig |
| 55 | + helmChartInfoGetter *config.HelmChartGetter |
| 56 | + |
| 57 | + variableName string // points to the global config variable |
| 58 | + variablePath []string // path of this variable on the global config variable |
| 59 | +} |
| 60 | + |
| 61 | +var ( |
| 62 | + _ commonhandlers.Named = &DefaultAWSLoadBalancerController{} |
| 63 | + _ lifecycle.AfterControlPlaneInitialized = &DefaultAWSLoadBalancerController{} |
| 64 | + _ lifecycle.BeforeClusterUpgrade = &DefaultAWSLoadBalancerController{} |
| 65 | +) |
| 66 | + |
| 67 | +func New( |
| 68 | + c ctrlclient.Client, |
| 69 | + cfg *ControllerConfig, |
| 70 | + helmChartInfoGetter *config.HelmChartGetter, |
| 71 | +) *DefaultAWSLoadBalancerController { |
| 72 | + return &DefaultAWSLoadBalancerController{ |
| 73 | + client: c, |
| 74 | + config: cfg, |
| 75 | + helmChartInfoGetter: helmChartInfoGetter, |
| 76 | + variableName: v1alpha1.ClusterConfigVariableName, |
| 77 | + variablePath: []string{"addons", "loadBalancerController"}, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func (n *DefaultAWSLoadBalancerController) Name() string { |
| 82 | + return "AWSLoadBalancerControllerHandler" |
| 83 | +} |
| 84 | + |
| 85 | +func (n *DefaultAWSLoadBalancerController) AfterControlPlaneInitialized( |
| 86 | + ctx context.Context, |
| 87 | + req *runtimehooksv1.AfterControlPlaneInitializedRequest, |
| 88 | + resp *runtimehooksv1.AfterControlPlaneInitializedResponse, |
| 89 | +) { |
| 90 | + commonResponse := &runtimehooksv1.CommonResponse{} |
| 91 | + n.apply(ctx, &req.Cluster, commonResponse) |
| 92 | + resp.Status = commonResponse.GetStatus() |
| 93 | + resp.Message = commonResponse.GetMessage() |
| 94 | +} |
| 95 | + |
| 96 | +func (n *DefaultAWSLoadBalancerController) BeforeClusterUpgrade( |
| 97 | + ctx context.Context, |
| 98 | + req *runtimehooksv1.BeforeClusterUpgradeRequest, |
| 99 | + resp *runtimehooksv1.BeforeClusterUpgradeResponse, |
| 100 | +) { |
| 101 | + commonResponse := &runtimehooksv1.CommonResponse{} |
| 102 | + n.apply(ctx, &req.Cluster, commonResponse) |
| 103 | + resp.Status = commonResponse.GetStatus() |
| 104 | + resp.Message = commonResponse.GetMessage() |
| 105 | +} |
| 106 | + |
| 107 | +func (n *DefaultAWSLoadBalancerController) apply( |
| 108 | + ctx context.Context, |
| 109 | + cluster *clusterv1.Cluster, |
| 110 | + resp *runtimehooksv1.CommonResponse, |
| 111 | +) { |
| 112 | + clusterKey := ctrlclient.ObjectKeyFromObject(cluster) |
| 113 | + |
| 114 | + log := ctrl.LoggerFrom(ctx).WithValues( |
| 115 | + "cluster", |
| 116 | + clusterKey, |
| 117 | + ) |
| 118 | + |
| 119 | + // For now, always enable the AWS Load Balancer Controller |
| 120 | + // FIXME: Add proper variable checking when APIs are added |
| 121 | + if provider := utils.GetProvider(cluster); provider != "eks" { |
| 122 | + log.V(5).Info("Skipping AWS Load Balancer Controller handler,not an EKS cluster", provider) |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + log.Info("Installing AWS Load Balancer Controller addon") |
| 127 | + |
| 128 | + helmChart, err := n.helmChartInfoGetter.For(ctx, log, config.AWSLoadBalancerController) |
| 129 | + if err != nil { |
| 130 | + log.Error( |
| 131 | + err, |
| 132 | + "failed to get configmap with helm settings", |
| 133 | + ) |
| 134 | + resp.SetStatus(runtimehooksv1.ResponseStatusFailure) |
| 135 | + resp.SetMessage( |
| 136 | + fmt.Sprintf("failed to get configuration to create helm addon: %v", |
| 137 | + err, |
| 138 | + ), |
| 139 | + ) |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + strategy := addons.NewHelmAddonApplier( |
| 144 | + n.config.helmAddonConfig, |
| 145 | + n.client, |
| 146 | + helmChart, |
| 147 | + ) |
| 148 | + |
| 149 | + if err := strategy.Apply(ctx, cluster, n.config.DefaultsNamespace(), log); err != nil { |
| 150 | + err = fmt.Errorf("failed to apply AWS Load Balancer Controller addon: %w", err) |
| 151 | + resp.SetStatus(runtimehooksv1.ResponseStatusFailure) |
| 152 | + resp.SetMessage(err.Error()) |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + resp.SetStatus(runtimehooksv1.ResponseStatusSuccess) |
| 157 | +} |
0 commit comments