Skip to content

Commit 8c7c2b2

Browse files
committed
feat: delete registry syncer HCP
1 parent a25cb8e commit 8c7c2b2

File tree

5 files changed

+161
-2
lines changed

5 files changed

+161
-2
lines changed

pkg/handlers/generic/lifecycle/registry/cncfdistribution/handler.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,26 @@ func (n *CNCFDistribution) Apply(
155155
return nil
156156
}
157157

158+
func (n *CNCFDistribution) Cleanup(
159+
ctx context.Context,
160+
_ v1alpha1.RegistryAddon,
161+
cluster *clusterv1.Cluster,
162+
log logr.Logger,
163+
) error {
164+
// Delete any registry syncer artifacts.
165+
registrySyncer := syncer.New(
166+
n.client,
167+
&syncer.Config{GlobalOptions: n.config.GlobalOptions},
168+
n.helmChartInfoGetter,
169+
)
170+
err := registrySyncer.Cleanup(ctx, cluster, log)
171+
if err != nil {
172+
return fmt.Errorf("failed to clean up CNCF Distribution registry syncer: %w", err)
173+
}
174+
175+
return nil
176+
}
177+
158178
func templateValues(cluster *clusterv1.Cluster, text string) (string, error) {
159179
valuesTemplate, err := template.New("").Parse(text)
160180
if err != nil {

pkg/handlers/generic/lifecycle/registry/handler.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ type RegistryProvider interface {
3232
cluster *clusterv1.Cluster,
3333
log logr.Logger,
3434
) error
35+
Cleanup(
36+
ctx context.Context,
37+
registryVar v1alpha1.RegistryAddon,
38+
cluster *clusterv1.Cluster,
39+
log logr.Logger,
40+
) error
3541
}
3642

3743
type RegistryHandler struct {
@@ -266,3 +272,84 @@ func (r *RegistryHandler) apply(
266272
),
267273
)
268274
}
275+
276+
func (r *RegistryHandler) BeforeClusterDelete(
277+
ctx context.Context,
278+
req *runtimehooksv1.BeforeClusterDeleteRequest,
279+
resp *runtimehooksv1.BeforeClusterDeleteResponse,
280+
) {
281+
cluster := &req.Cluster
282+
283+
clusterKey := ctrlclient.ObjectKeyFromObject(cluster)
284+
285+
log := ctrl.LoggerFrom(ctx).WithValues(
286+
"cluster",
287+
clusterKey,
288+
)
289+
290+
varMap := variables.ClusterVariablesToVariablesMap(cluster.Spec.Topology.Variables)
291+
registryVar, err := variables.Get[v1alpha1.RegistryAddon](
292+
varMap,
293+
r.variableName,
294+
r.variablePath...)
295+
if err != nil {
296+
if variables.IsNotFoundError(err) {
297+
log.V(5).
298+
Info(
299+
"Skipping RegistryAddon, field is not specified",
300+
"error",
301+
err,
302+
)
303+
return
304+
}
305+
log.Error(
306+
err,
307+
"failed to read RegistryAddon provider from cluster definition",
308+
)
309+
resp.SetStatus(runtimehooksv1.ResponseStatusFailure)
310+
resp.SetMessage(
311+
fmt.Sprintf("failed to read RegistryAddon provider from cluster definition: %v",
312+
err,
313+
),
314+
)
315+
return
316+
}
317+
318+
handler, ok := r.ProviderHandler[registryVar.Provider]
319+
if !ok {
320+
err = fmt.Errorf("unknown RegistryAddon Provider")
321+
log.Error(err, "provider", registryVar.Provider)
322+
resp.SetStatus(runtimehooksv1.ResponseStatusFailure)
323+
resp.SetMessage(
324+
fmt.Sprintf("%s %s", err, registryVar.Provider),
325+
)
326+
return
327+
}
328+
329+
log.Info(fmt.Sprintf("Clean up RegistryAddon provider %s", registryVar.Provider))
330+
err = handler.Cleanup(
331+
ctx,
332+
registryVar,
333+
cluster,
334+
log,
335+
)
336+
if err != nil {
337+
log.Error(
338+
err,
339+
fmt.Sprintf(
340+
"failed to clean up RegistryAddon provider %s",
341+
registryVar.Provider,
342+
),
343+
)
344+
resp.SetStatus(runtimehooksv1.ResponseStatusFailure)
345+
resp.SetMessage(
346+
fmt.Sprintf(
347+
"failed to clean up RegistryAddon provider: %v",
348+
err,
349+
),
350+
)
351+
return
352+
}
353+
354+
resp.SetStatus(runtimehooksv1.ResponseStatusSuccess)
355+
}

pkg/handlers/generic/lifecycle/registry/syncer/applier.go renamed to pkg/handlers/generic/lifecycle/registry/syncer/syncer.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import (
1010
"text/template"
1111

1212
"github.com/go-logr/logr"
13+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
1415
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
1516

17+
caaphv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/external/sigs.k8s.io/cluster-api-addon-provider-helm/api/v1alpha1"
1618
_ "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1719
carenv1 "github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/v1alpha1"
1820
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/api/variables"
@@ -102,6 +104,47 @@ func (n *RegistrySyncer) Apply(
102104
return nil
103105
}
104106

107+
// Cleanup cleans up the HCP on the management cluster for the cluster.
108+
// The syncer is applied against the management cluster can be in a different namespace from cluster.
109+
// Since cross-namespace owner references are not allowed, we need to delete the HelmChartProxy directly.
110+
func (n *RegistrySyncer) Cleanup(
111+
ctx context.Context,
112+
cluster *clusterv1.Cluster,
113+
log logr.Logger,
114+
) error {
115+
log.Info("Checking if registry syncer needs to be cleaned up")
116+
managementCluster, err := capiutils.ManagementCluster(ctx, n.client)
117+
if err != nil {
118+
return fmt.Errorf("failed to get management cluster: %w", err)
119+
}
120+
shouldApply, err := shouldApplyRegistrySyncer(cluster, managementCluster)
121+
if err != nil {
122+
return fmt.Errorf("failed to check if registry syncer should be cleaned up: %w", err)
123+
}
124+
if !shouldApply {
125+
log.Info("Skipping registry syncer cleanup as it is not required")
126+
return nil
127+
}
128+
129+
log.Info("Cleaning up registry syncer for cluster")
130+
hcp := &caaphv1.HelmChartProxy{
131+
ObjectMeta: metav1.ObjectMeta{
132+
Name: addonResourceNameForCluster(cluster),
133+
Namespace: managementCluster.Namespace,
134+
},
135+
}
136+
137+
err = ctrlclient.IgnoreNotFound(n.client.Delete(ctx, hcp))
138+
if err != nil {
139+
return fmt.Errorf(
140+
"failed to delete regystry syncer installation HelmChartProxy: %w",
141+
err,
142+
)
143+
}
144+
145+
return nil
146+
}
147+
105148
// shouldApply returns false if:
106149
// - the management cluster is nil, ie. this cluster will become the management cluster
107150
// - the management cluster name and namespace matches the workload cluster

pkg/handlers/generic/lifecycle/registry/syncer/applier_integration_test.go renamed to pkg/handlers/generic/lifecycle/registry/syncer/syncer_integration_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
. "github.com/onsi/ginkgo/v2"
1111
. "github.com/onsi/gomega"
1212
corev1 "k8s.io/api/core/v1"
13+
apierrors "k8s.io/apimachinery/pkg/api/errors"
1314
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1415
"k8s.io/apimachinery/pkg/runtime"
1516
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -27,13 +28,13 @@ import (
2728
"github.com/nutanix-cloud-native/cluster-api-runtime-extensions-nutanix/test/helpers"
2829
)
2930

30-
var _ = Describe("Test Syncer Apply", func() {
31+
var _ = Describe("Test Syncer", func() {
3132
clientScheme := runtime.NewScheme()
3233
utilruntime.Must(clientgoscheme.AddToScheme(clientScheme))
3334
utilruntime.Must(clusterv1.AddToScheme(clientScheme))
3435
utilruntime.Must(caaphv1.AddToScheme(clientScheme))
3536

36-
It("Should create HelmChartProxy", func(ctx SpecContext) {
37+
It("Should create HelmChartProxy and then delete it", func(ctx SpecContext) {
3738
t := GinkgoT()
3839
featuregatetesting.SetFeatureGateDuringTest(
3940
t,
@@ -101,6 +102,14 @@ var _ = Describe("Test Syncer Apply", func() {
101102

102103
expectedMatchLabels := map[string]string{clusterv1.ClusterNameLabel: managementCluster.Name}
103104
Expect(registrySyncerHelmChartProxy.Spec.ClusterSelector.MatchLabels).To(Equal(expectedMatchLabels))
105+
106+
// Run the cleanup and verify that the HelmChartProxy is deleted.
107+
err = syncer.Cleanup(ctx, workloadCluster, logr.Discard())
108+
Expect(err).To(BeNil())
109+
110+
err = c.Get(ctx, registrySyncerHelmChartProxyKey, registrySyncerHelmChartProxy)
111+
Expect(err).ToNot(BeNil())
112+
Expect(apierrors.IsNotFound(err)).To(BeTrue())
104113
})
105114
AfterEach(func(ctx SpecContext) {
106115
c, err := helpers.TestEnv.GetK8sClientWithScheme(clientScheme)

0 commit comments

Comments
 (0)