|
| 1 | +// Copyright 2024 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package multus |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/spf13/pflag" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 13 | + runtimehooksv1 "sigs.k8s.io/cluster-api/exp/runtime/hooks/api/v1alpha1" |
| 14 | + ctrl "sigs.k8s.io/controller-runtime" |
| 15 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 16 | + |
| 17 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1" |
| 18 | + commonhandlers "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers" |
| 19 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/handlers/lifecycle" |
| 20 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/clustertopology/variables" |
| 21 | + capiutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/common/pkg/capi/utils" |
| 22 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/lifecycle/addons" |
| 23 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/lifecycle/config" |
| 24 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/options" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + defaultMultusReleaseName = "multus" |
| 29 | + defaultMultusNamespace = metav1.NamespaceSystem |
| 30 | +) |
| 31 | + |
| 32 | +type MultusConfig struct { |
| 33 | + *options.GlobalOptions |
| 34 | + |
| 35 | + helmAddonConfig *addons.HelmAddonConfig |
| 36 | +} |
| 37 | + |
| 38 | +func NewMultusConfig(globalOptions *options.GlobalOptions) *MultusConfig { |
| 39 | + return &MultusConfig{ |
| 40 | + GlobalOptions: globalOptions, |
| 41 | + helmAddonConfig: addons.NewHelmAddonConfig( |
| 42 | + "default-multus-values-template", |
| 43 | + defaultMultusNamespace, |
| 44 | + defaultMultusReleaseName, |
| 45 | + ), |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func (m *MultusConfig) AddFlags(prefix string, flags *pflag.FlagSet) { |
| 50 | + m.helmAddonConfig.AddFlags(prefix+".helm-addon", flags) |
| 51 | +} |
| 52 | + |
| 53 | +type MultusHandler struct { |
| 54 | + client ctrlclient.Client |
| 55 | + config *MultusConfig |
| 56 | + helmChartInfoGetter *config.HelmChartGetter |
| 57 | +} |
| 58 | + |
| 59 | +var ( |
| 60 | + _ commonhandlers.Named = &MultusHandler{} |
| 61 | + _ lifecycle.AfterControlPlaneInitialized = &MultusHandler{} |
| 62 | + _ lifecycle.BeforeClusterUpgrade = &MultusHandler{} |
| 63 | +) |
| 64 | + |
| 65 | +func New( |
| 66 | + c ctrlclient.Client, |
| 67 | + cfg *MultusConfig, |
| 68 | + helmChartInfoGetter *config.HelmChartGetter, |
| 69 | +) *MultusHandler { |
| 70 | + return &MultusHandler{ |
| 71 | + client: c, |
| 72 | + config: cfg, |
| 73 | + helmChartInfoGetter: helmChartInfoGetter, |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (m *MultusHandler) Name() string { |
| 78 | + return "MultusHandler" |
| 79 | +} |
| 80 | + |
| 81 | +func (m *MultusHandler) AfterControlPlaneInitialized( |
| 82 | + ctx context.Context, |
| 83 | + req *runtimehooksv1.AfterControlPlaneInitializedRequest, |
| 84 | + resp *runtimehooksv1.AfterControlPlaneInitializedResponse, |
| 85 | +) { |
| 86 | + commonResponse := &runtimehooksv1.CommonResponse{} |
| 87 | + m.apply(ctx, &req.Cluster, commonResponse) |
| 88 | + resp.Status = commonResponse.GetStatus() |
| 89 | + resp.Message = commonResponse.GetMessage() |
| 90 | +} |
| 91 | + |
| 92 | +func (m *MultusHandler) BeforeClusterUpgrade( |
| 93 | + ctx context.Context, |
| 94 | + req *runtimehooksv1.BeforeClusterUpgradeRequest, |
| 95 | + resp *runtimehooksv1.BeforeClusterUpgradeResponse, |
| 96 | +) { |
| 97 | + commonResponse := &runtimehooksv1.CommonResponse{} |
| 98 | + m.apply(ctx, &req.Cluster, commonResponse) |
| 99 | + resp.Status = commonResponse.GetStatus() |
| 100 | + resp.Message = commonResponse.GetMessage() |
| 101 | +} |
| 102 | + |
| 103 | +func (m *MultusHandler) apply( |
| 104 | + ctx context.Context, |
| 105 | + cluster *clusterv1.Cluster, |
| 106 | + resp *runtimehooksv1.CommonResponse, |
| 107 | +) { |
| 108 | + clusterKey := ctrlclient.ObjectKeyFromObject(cluster) |
| 109 | + |
| 110 | + log := ctrl.LoggerFrom(ctx).WithValues( |
| 111 | + "cluster", |
| 112 | + clusterKey, |
| 113 | + ) |
| 114 | + |
| 115 | + // Check if Multus is supported for this cloud provider |
| 116 | + provider := capiutils.GetProvider(cluster) |
| 117 | + if provider != "eks" { |
| 118 | + log.V(5).Info( |
| 119 | + "Multus is not supported for this cloud provider. Skipping Multus deployment.", |
| 120 | + ) |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + log.Info(fmt.Sprintf("Cluster is %s. Checking CNI configuration for Multus deployment.", provider)) |
| 125 | + |
| 126 | + // Read CNI configuration to detect which CNI is deployed |
| 127 | + varMap := variables.ClusterVariablesToVariablesMap(cluster.Spec.Topology.Variables) |
| 128 | + |
| 129 | + cniVar, err := variables.Get[v1alpha1.CNI]( |
| 130 | + varMap, |
| 131 | + v1alpha1.ClusterConfigVariableName, |
| 132 | + []string{"addons", v1alpha1.CNIVariableName}...) |
| 133 | + if err != nil { |
| 134 | + if variables.IsNotFoundError(err) { |
| 135 | + log.V(5).Info("No CNI specified in cluster config. Skipping Multus deployment.") |
| 136 | + return |
| 137 | + } |
| 138 | + log.Error(err, "failed to read CNI configuration from cluster definition") |
| 139 | + resp.SetStatus(runtimehooksv1.ResponseStatusFailure) |
| 140 | + resp.SetMessage(fmt.Sprintf("failed to read CNI configuration: %v", err)) |
| 141 | + return |
| 142 | + } |
| 143 | + |
| 144 | + log.Info(fmt.Sprintf("Auto-deploying Multus for %s cluster with %s CNI", provider, cniVar.Provider)) |
| 145 | + |
| 146 | + // Get helm chart configuration |
| 147 | + helmChart, err := m.helmChartInfoGetter.For(ctx, log, config.Multus) |
| 148 | + if err != nil { |
| 149 | + log.Error( |
| 150 | + err, |
| 151 | + "failed to get configmap with helm settings", |
| 152 | + ) |
| 153 | + resp.SetStatus(runtimehooksv1.ResponseStatusFailure) |
| 154 | + resp.SetMessage( |
| 155 | + fmt.Sprintf("failed to get configuration to create helm addon: %v", |
| 156 | + err, |
| 157 | + ), |
| 158 | + ) |
| 159 | + return |
| 160 | + } |
| 161 | + |
| 162 | + // Create and apply helm addon using existing addons package |
| 163 | + targetNamespace := m.config.DefaultsNamespace() |
| 164 | + strategy := addons.NewHelmAddonApplier( |
| 165 | + m.config.helmAddonConfig, |
| 166 | + m.client, |
| 167 | + helmChart, |
| 168 | + ). |
| 169 | + WithValueTemplater(templateValuesFunc(cniVar)). |
| 170 | + WithDefaultWaiter() |
| 171 | + |
| 172 | + if err := strategy.Apply(ctx, cluster, targetNamespace, log); err != nil { |
| 173 | + log.Error(err, "failed to deploy Multus") |
| 174 | + resp.SetStatus(runtimehooksv1.ResponseStatusFailure) |
| 175 | + resp.SetMessage(fmt.Sprintf("failed to deploy Multus: %v", err)) |
| 176 | + return |
| 177 | + } |
| 178 | + |
| 179 | + resp.SetStatus(runtimehooksv1.ResponseStatusSuccess) |
| 180 | + resp.SetMessage("Multus deployed successfully") |
| 181 | +} |
0 commit comments