Skip to content

Commit 27cd51c

Browse files
committed
operator: Delete HelmChart custom resource when HelmRelease is suspended
When Redpanda resource is migrated from using flux to defluxed mode the HelmRelease custom resource is put into suspend mode. That suspend mode can affect deletion process as HelmChart (a child resource that doesn't have ownerReference) is not fully reconciled by helmrelease_controller. Redpanda operator now try to delete HelmChart. Reference https://github.com/fluxcd/helm-controller/blob/2d335f2aa0e2e0df2a631ebf19394aed07c556f3/internal/reconcile/helmchart_template.go#L163-L166 https://github.com/fluxcd/helm-controller/blob/2d335f2aa0e2e0df2a631ebf19394aed07c556f3/internal/controller/helmrelease_controller.go#L375-L388
1 parent 5dd27cc commit 27cd51c

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

operator/config/rbac/v2-manager-role/role.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ rules:
188188
- patch
189189
- update
190190
- watch
191+
- apiGroups:
192+
- coordination.k8s.io
193+
resources:
194+
- leases
195+
verbs:
196+
- create
197+
- delete
198+
- get
199+
- list
200+
- patch
201+
- update
202+
- watch
191203
- apiGroups:
192204
- helm.toolkit.fluxcd.io
193205
resources:

operator/internal/controller/redpanda/redpanda_controller.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ import (
5454
)
5555

5656
const (
57-
FinalizerKey = "operator.redpanda.com/finalizer"
57+
FinalizerKey = "operator.redpanda.com/finalizer"
58+
FluxFinalizerKey = "finalizers.fluxcd.io"
5859

5960
NotManaged = "false"
6061

@@ -135,6 +136,10 @@ type RedpandaReconciler struct {
135136
// +kubebuilder:rbac:groups=cluster.redpanda.com,resources=redpandas/finalizers,verbs=update
136137
// +kubebuilder:rbac:groups=core,namespace=default,resources=events,verbs=create;patch
137138

139+
// sidecar resources
140+
// The leases is used by controller-runtime in sidecar. Operator main reconciliation needs to have leases permissions in order to create role that have the same permissions.
141+
// +kubebuilder:rbac:groups=coordination.k8s.io,namespace=default,resources=leases,verbs=get;list;watch;create;update;patch;delete
142+
138143
// SetupWithManager sets up the controller with the Manager.
139144
func (r *RedpandaReconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager) error {
140145
if err := registerHelmReferencedIndex(ctx, mgr, "statefulset", &appsv1.StatefulSet{}); err != nil {
@@ -861,6 +866,10 @@ func (r *RedpandaReconciler) reconcileHelmRepository(ctx context.Context, rp *re
861866
}
862867

863868
func (r *RedpandaReconciler) reconcileDelete(ctx context.Context, rp *redpandav1alpha2.Redpanda) error {
869+
if err := r.deleteHelmChart(ctx, rp); err != nil {
870+
return err
871+
}
872+
864873
if controllerutil.ContainsFinalizer(rp, FinalizerKey) {
865874
controllerutil.RemoveFinalizer(rp, FinalizerKey)
866875
if err := r.Client.Update(ctx, rp); err != nil {
@@ -870,6 +879,35 @@ func (r *RedpandaReconciler) reconcileDelete(ctx context.Context, rp *redpandav1
870879
return nil
871880
}
872881

882+
func (r *RedpandaReconciler) deleteHelmChart(ctx context.Context, rp *redpandav1alpha2.Redpanda) error {
883+
// When HelmRelease is suspended it will not delete child resource HelmChart. In this function there is attempt
884+
// to delete HelmChart custom resource.
885+
// Reference:
886+
// https://github.com/fluxcd/helm-controller/blob/2d335f2aa0e2e0df2a631ebf19394aed07c556f3/internal/reconcile/helmchart_template.go#L163-L166
887+
// https://github.com/fluxcd/helm-controller/blob/2d335f2aa0e2e0df2a631ebf19394aed07c556f3/internal/controller/helmrelease_controller.go#L375-L388
888+
namespacedName := client.ObjectKey{Namespace: rp.Namespace, Name: rp.Namespace + "-" + rp.Name}
889+
var chart sourcev1.HelmChart
890+
err := r.Client.Get(ctx, namespacedName, &chart)
891+
if err != nil && !apierrors.IsNotFound(err) {
892+
// Return error to retry until we succeed.
893+
return fmt.Errorf("failed to get flux HelmChart '%s': %w", namespacedName.Name, err)
894+
} else if err == nil {
895+
if controllerutil.ContainsFinalizer(&chart, FluxFinalizerKey) {
896+
controllerutil.RemoveFinalizer(&chart, FluxFinalizerKey)
897+
if err := r.Client.Update(ctx, &chart); err != nil {
898+
return fmt.Errorf("failed to update flux HelmChart '%s': %w", namespacedName.Name, err)
899+
}
900+
}
901+
902+
// Delete the HelmChart.
903+
if err = r.Client.Delete(ctx, &chart); err != nil {
904+
return fmt.Errorf("failed to delete flux HelmChart '%s': %w", namespacedName.Name, err)
905+
}
906+
}
907+
908+
return nil
909+
}
910+
873911
func (r *RedpandaReconciler) createHelmReleaseFromTemplate(ctx context.Context, rp *redpandav1alpha2.Redpanda) (*helmv2beta2.HelmRelease, error) {
874912
log := ctrl.LoggerFrom(ctx).WithName("RedpandaReconciler.createHelmReleaseFromTemplate")
875913

operator/internal/controller/redpanda/redpanda_controller_test.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,10 @@ func (s *RedpandaControllerSuite) TestStableUIDAndGeneration() {
739739
s.compareSnapshot(flipped, flippedBack, isStable)
740740

741741
s.deleteAndWait(rp)
742+
743+
var hr v2beta2.HelmRelease
744+
err := s.client.Get(s.ctx, types.NamespacedName{Name: rp.Namespace + "-" + rp.Name, Namespace: rp.Namespace}, &hr)
745+
assert.True(s.T(), apierrors.IsNotFound(err))
742746
}
743747
}
744748

@@ -831,15 +835,6 @@ Starting helm repository that serves %q as the development version of the redpan
831835
for _, rp := range redpandas.Items {
832836
s.deleteAndWait(&rp)
833837
}
834-
835-
// For some reason, it seems that HelmCharts can get abandoned. Clean
836-
// them up to prevent hanging the NS deletion.
837-
var helmCharts sourcecontrollerv1beta2.HelmChartList
838-
s.NoError(s.env.Client().List(s.ctx, &helmCharts))
839-
840-
for _, chart := range helmCharts.Items {
841-
s.deleteAndWait(&chart)
842-
}
843838
})
844839
}
845840

@@ -956,7 +951,7 @@ func (s *RedpandaControllerSuite) minimalRP(useFlux bool) *redpandav1alpha2.Redp
956951
},
957952
},
958953
RBAC: &redpandav1alpha2.RBAC{
959-
Enabled: ptr.To(true),
954+
Enabled: ptr.To(false),
960955
},
961956
},
962957
},

operator/internal/controller/redpanda/role.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ rules:
188188
- patch
189189
- update
190190
- watch
191+
- apiGroups:
192+
- coordination.k8s.io
193+
resources:
194+
- leases
195+
verbs:
196+
- create
197+
- delete
198+
- get
199+
- list
200+
- patch
201+
- update
202+
- watch
191203
- apiGroups:
192204
- helm.toolkit.fluxcd.io
193205
resources:

0 commit comments

Comments
 (0)