|
| 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/go-logr/logr" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 13 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | + |
| 15 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/lifecycle/addons" |
| 16 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/lifecycle/config" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + defaultMultusReleaseName = "multus" |
| 21 | + defaultMultusNamespace = metav1.NamespaceSystem |
| 22 | +) |
| 23 | + |
| 24 | +type MultusDeployer struct { |
| 25 | + client ctrlclient.Client |
| 26 | + helmChartInfoGetter *config.HelmChartGetter |
| 27 | +} |
| 28 | + |
| 29 | +func NewMultusDeployer(client ctrlclient.Client, helmChartInfoGetter *config.HelmChartGetter) *MultusDeployer { |
| 30 | + return &MultusDeployer{ |
| 31 | + client: client, |
| 32 | + helmChartInfoGetter: helmChartInfoGetter, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (m *MultusDeployer) Deploy( |
| 37 | + ctx context.Context, |
| 38 | + cluster *clusterv1.Cluster, |
| 39 | + readinessSocketPath string, |
| 40 | + targetNamespace string, |
| 41 | + log logr.Logger, |
| 42 | +) error { |
| 43 | + // Check if Multus deployment is supported for this cloud provider |
| 44 | + isSupported, providerName := m.isCloudProviderSupported(cluster) |
| 45 | + |
| 46 | + // Currently, Multus is only supported for EKS and Nutanix clusters |
| 47 | + if !isSupported { |
| 48 | + log.Info("Multus deployment is only supported for EKS and Nutanix clusters. Skipping deployment.") |
| 49 | + return nil |
| 50 | + } |
| 51 | + |
| 52 | + log.Info(fmt.Sprintf("Cluster is %s. Proceeding with Multus deployment.", providerName)) |
| 53 | + |
| 54 | + // Get Multus Helm chart info |
| 55 | + helmChart, err := m.helmChartInfoGetter.For(ctx, log, config.Multus) |
| 56 | + if err != nil { |
| 57 | + // For local testing, provide a placeholder chart info |
| 58 | + log.Info("Multus not found in helm-config.yaml, using placeholder chart info for local development") |
| 59 | + helmChart = &config.HelmChart{ |
| 60 | + Name: "multus", |
| 61 | + Version: "dev", |
| 62 | + Repository: "", |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Deploy Multus using HelmAddonApplier with template function |
| 67 | + strategy := addons.NewHelmAddonApplier( |
| 68 | + addons.NewHelmAddonConfig( |
| 69 | + "default-multus-values-template", |
| 70 | + defaultMultusNamespace, |
| 71 | + defaultMultusReleaseName, |
| 72 | + ), |
| 73 | + m.client, |
| 74 | + helmChart, |
| 75 | + ).WithValueTemplater(templateValuesFunc(readinessSocketPath)). |
| 76 | + WithDefaultWaiter() |
| 77 | + |
| 78 | + if err := strategy.Apply(ctx, cluster, targetNamespace, log); err != nil { |
| 79 | + return fmt.Errorf("failed to apply Multus deployment: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + log.Info("Successfully deployed Multus") |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// isCloudProviderSupported checks if the cluster is a supported cloud provider |
| 87 | +// by inspecting the infrastructure reference. |
| 88 | +func (m *MultusDeployer) isCloudProviderSupported(cluster *clusterv1.Cluster) ( |
| 89 | + isSupported bool, |
| 90 | + providerName string, |
| 91 | +) { |
| 92 | + if cluster.Spec.InfrastructureRef == nil { |
| 93 | + return false, "" |
| 94 | + } |
| 95 | + |
| 96 | + switch cluster.Spec.InfrastructureRef.Kind { |
| 97 | + case "AWSManagedCluster": |
| 98 | + return true, "EKS" |
| 99 | + case "NutanixCluster": |
| 100 | + return true, "Nutanix" |
| 101 | + default: |
| 102 | + return false, "" |
| 103 | + } |
| 104 | +} |
0 commit comments