Skip to content

Commit a00d0ce

Browse files
committed
feat: add registryMirror handler
1 parent a17b14e commit a00d0ce

File tree

7 files changed

+479
-12
lines changed

7 files changed

+479
-12
lines changed

pkg/handlers/generic/lifecycle/config/cm.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ import (
1717
type Component string
1818

1919
const (
20-
Autoscaler Component = "cluster-autoscaler"
21-
Tigera Component = "tigera-operator"
22-
Cilium Component = "cilium"
23-
NFD Component = "nfd"
24-
NutanixStorageCSI Component = "nutanix-storage-csi"
25-
SnapshotController Component = "snapshot-controller"
26-
NutanixCCM Component = "nutanix-ccm"
27-
MetalLB Component = "metallb"
28-
LocalPathProvisionerCSI Component = "local-path-provisioner-csi"
29-
AWSEBSCSI Component = "aws-ebs-csi"
30-
AWSCCM Component = "aws-ccm"
31-
COSIController Component = "cosi-controller"
20+
Autoscaler Component = "cluster-autoscaler"
21+
Tigera Component = "tigera-operator"
22+
Cilium Component = "cilium"
23+
NFD Component = "nfd"
24+
NutanixStorageCSI Component = "nutanix-storage-csi"
25+
SnapshotController Component = "snapshot-controller"
26+
NutanixCCM Component = "nutanix-ccm"
27+
MetalLB Component = "metallb"
28+
LocalPathProvisionerCSI Component = "local-path-provisioner-csi"
29+
AWSEBSCSI Component = "aws-ebs-csi"
30+
AWSCCM Component = "aws-ccm"
31+
COSIController Component = "cosi-controller"
32+
DistributionRegistryMirror Component = "distribution-registry-mirror"
3233
)
3334

3435
type HelmChartGetter struct {

pkg/handlers/generic/lifecycle/handlers.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
nutanixcsi "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/csi/nutanix"
2626
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/csi/snapshotcontroller"
2727
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/nfd"
28+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/registrymirror"
29+
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/registrymirror/distribution"
2830
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/servicelbgc"
2931
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/serviceloadbalancer"
3032
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/pkg/handlers/generic/lifecycle/serviceloadbalancer/metallb"
@@ -45,6 +47,7 @@ type Handlers struct {
4547
localPathCSIConfig *localpath.Config
4648
snapshotControllerConfig *snapshotcontroller.Config
4749
cosiControllerConfig *cosi.ControllerConfig
50+
distributionConfig *distribution.Config
4851
}
4952

5053
func New(
@@ -66,6 +69,7 @@ func New(
6669
localPathCSIConfig: localpath.NewConfig(globalOptions),
6770
snapshotControllerConfig: snapshotcontroller.NewConfig(globalOptions),
6871
cosiControllerConfig: cosi.NewControllerConfig(globalOptions),
72+
distributionConfig: &distribution.Config{GlobalOptions: globalOptions},
6973
}
7074
}
7175

@@ -100,6 +104,13 @@ func (h *Handlers) AllHandlers(mgr manager.Manager) []handlers.Named {
100104
helmChartInfoGetter,
101105
),
102106
}
107+
registryMirrorHandlers := map[string]registrymirror.RegistryMirrorProvider{
108+
v1alpha1.RegistryMirrorProviderDistribution: distribution.New(
109+
mgr.GetClient(),
110+
h.distributionConfig,
111+
helmChartInfoGetter,
112+
),
113+
}
103114
serviceLoadBalancerHandlers := map[string]serviceloadbalancer.ServiceLoadBalancerProvider{
104115
v1alpha1.ServiceLoadBalancerProviderMetalLB: metallb.New(
105116
mgr.GetClient(),
@@ -117,6 +128,7 @@ func (h *Handlers) AllHandlers(mgr manager.Manager) []handlers.Named {
117128
snapshotcontroller.New(mgr.GetClient(), h.snapshotControllerConfig, helmChartInfoGetter),
118129
cosi.New(mgr.GetClient(), h.cosiControllerConfig, helmChartInfoGetter),
119130
servicelbgc.New(mgr.GetClient()),
131+
registrymirror.New(mgr.GetClient(), registryMirrorHandlers),
120132
// The order of the handlers in the list is important and are called consecutively.
121133
// The MetalLB provider may be configured to create a IPAddressPool on the remote cluster.
122134
// However, the MetalLB provider also has a webhook that validates IPAddressPool requests.
@@ -218,4 +230,5 @@ func (h *Handlers) AddFlags(flagSet *pflag.FlagSet) {
218230
h.nutanixCCMConfig.AddFlags("ccm.nutanix", flagSet)
219231
h.metalLBConfig.AddFlags("metallb", flagSet)
220232
h.cosiControllerConfig.AddFlags("cosi.controller", flagSet)
233+
h.distributionConfig.AddFlags("registry.distribution", flagSet)
221234
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Copyright 2025 Nutanix. All rights reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package registrymirror

0 commit comments

Comments
 (0)