|
| 1 | +// Copyright 2025 Nutanix. All rights reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package distribution |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "fmt" |
| 10 | + "text/template" |
| 11 | + |
| 12 | + "github.com/go-logr/logr" |
| 13 | + "github.com/spf13/pflag" |
| 14 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 15 | + "sigs.k8s.io/cluster-api/controllers/remote" |
| 16 | + ctrlclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 17 | + |
| 18 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1" |
| 19 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/addons" |
| 20 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/config" |
| 21 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/registrymirror/utils" |
| 22 | + "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/options" |
| 23 | + handlersutils "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/utils" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + DefaultHelmReleaseName = "registry-mirror" |
| 28 | + DefaultHelmReleaseNamespace = "registry-mirror-system" |
| 29 | +) |
| 30 | + |
| 31 | +type Config struct { |
| 32 | + *options.GlobalOptions |
| 33 | + |
| 34 | + defaultValuesTemplateConfigMapName string |
| 35 | +} |
| 36 | + |
| 37 | +func (c *Config) AddFlags(prefix string, flags *pflag.FlagSet) { |
| 38 | + flags.StringVar( |
| 39 | + &c.defaultValuesTemplateConfigMapName, |
| 40 | + prefix+".default-values-template-configmap-name", |
| 41 | + "default-distribution-registry-mirror-helm-values-template", |
| 42 | + "default values ConfigMap name", |
| 43 | + ) |
| 44 | +} |
| 45 | + |
| 46 | +type Distribution struct { |
| 47 | + client ctrlclient.Client |
| 48 | + config *Config |
| 49 | + helmChartInfoGetter *config.HelmChartGetter |
| 50 | +} |
| 51 | + |
| 52 | +func New( |
| 53 | + c ctrlclient.Client, |
| 54 | + cfg *Config, |
| 55 | + helmChartInfoGetter *config.HelmChartGetter, |
| 56 | +) *Distribution { |
| 57 | + return &Distribution{ |
| 58 | + client: c, |
| 59 | + config: cfg, |
| 60 | + helmChartInfoGetter: helmChartInfoGetter, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +func (n *Distribution) Apply( |
| 65 | + ctx context.Context, |
| 66 | + _ v1alpha1.RegistryMirror, |
| 67 | + cluster *clusterv1.Cluster, |
| 68 | + log logr.Logger, |
| 69 | +) error { |
| 70 | + log.Info("Applying Distribution registry mirror installation") |
| 71 | + |
| 72 | + remoteClient, err := remote.NewClusterClient( |
| 73 | + ctx, |
| 74 | + "", |
| 75 | + n.client, |
| 76 | + ctrlclient.ObjectKeyFromObject(cluster), |
| 77 | + ) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("error creating remote cluster client: %w", err) |
| 80 | + } |
| 81 | + |
| 82 | + err = handlersutils.EnsureNamespaceWithMetadata( |
| 83 | + ctx, |
| 84 | + remoteClient, |
| 85 | + DefaultHelmReleaseNamespace, |
| 86 | + nil, |
| 87 | + nil, |
| 88 | + ) |
| 89 | + if err != nil { |
| 90 | + return fmt.Errorf( |
| 91 | + "failed to ensure release namespace %q exists: %w", |
| 92 | + DefaultHelmReleaseName, |
| 93 | + err, |
| 94 | + ) |
| 95 | + } |
| 96 | + |
| 97 | + helmChartInfo, err := n.helmChartInfoGetter.For(ctx, log, config.DistributionRegistryMirror) |
| 98 | + if err != nil { |
| 99 | + return fmt.Errorf("failed to get Distribution registry mirror helm chart: %w", err) |
| 100 | + } |
| 101 | + |
| 102 | + addonApplier := addons.NewHelmAddonApplier( |
| 103 | + addons.NewHelmAddonConfig( |
| 104 | + n.config.defaultValuesTemplateConfigMapName, |
| 105 | + DefaultHelmReleaseNamespace, |
| 106 | + DefaultHelmReleaseName, |
| 107 | + ), |
| 108 | + n.client, |
| 109 | + helmChartInfo, |
| 110 | + ).WithDefaultWaiter().WithValueTemplater(templateValues) |
| 111 | + |
| 112 | + if err := addonApplier.Apply(ctx, cluster, n.config.DefaultsNamespace(), log); err != nil { |
| 113 | + return fmt.Errorf("failed to apply Distribution registry mirror addon: %w", err) |
| 114 | + } |
| 115 | + |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func templateValues(cluster *clusterv1.Cluster, text string) (string, error) { |
| 120 | + valuesTemplate, err := template.New("").Parse(text) |
| 121 | + if err != nil { |
| 122 | + return "", fmt.Errorf("failed to parse template: %w", err) |
| 123 | + } |
| 124 | + |
| 125 | + serviceIP, err := utils.ServiceIPForCluster(cluster) |
| 126 | + if err != nil { |
| 127 | + return "", fmt.Errorf("error getting service IP for the distribution registry: %w", err) |
| 128 | + } |
| 129 | + |
| 130 | + type input struct { |
| 131 | + ServiceIP string |
| 132 | + } |
| 133 | + |
| 134 | + templateInput := input{ |
| 135 | + ServiceIP: serviceIP, |
| 136 | + } |
| 137 | + |
| 138 | + var b bytes.Buffer |
| 139 | + err = valuesTemplate.Execute(&b, templateInput) |
| 140 | + if err != nil { |
| 141 | + return "", fmt.Errorf( |
| 142 | + "failed template values: %w", |
| 143 | + err, |
| 144 | + ) |
| 145 | + } |
| 146 | + |
| 147 | + return b.String(), nil |
| 148 | +} |
0 commit comments