Skip to content

Commit 9696bf7

Browse files
authored
Merge pull request #13 from fluxcd/helmchart-ownership
Set owner ref to HelmRepository on HelmChart
2 parents b7897b7 + 23f0b38 commit 9696bf7

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

config/rbac/role.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ rules:
3838
- patch
3939
- update
4040
- watch
41+
- apiGroups:
42+
- source.fluxcd.io
43+
resources:
44+
- helmcharts/finalizers
45+
verbs:
46+
- get
47+
- patch
48+
- update
4149
- apiGroups:
4250
- source.fluxcd.io
4351
resources:

controllers/helmchart_controller.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ func (r *HelmChartReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
8686
return ctrl.Result{Requeue: true}, err
8787
}
8888

89+
// set ownership reference so chart is garbage collected on
90+
// repository removal
91+
r.setOwnerRef(ctx, &chart, repository)
92+
8993
// try to pull chart
9094
pulledChart, err := r.sync(repository, *chart.DeepCopy())
9195
if err != nil {
@@ -282,3 +286,14 @@ func (r *HelmChartReconciler) gc(chart sourcev1.HelmChart) {
282286
}
283287
}
284288
}
289+
290+
func (r *HelmChartReconciler) setOwnerRef(ctx context.Context, chart *sourcev1.HelmChart, repository sourcev1.HelmRepository) {
291+
if metav1.IsControlledBy(chart.GetObjectMeta(), repository.GetObjectMeta()) {
292+
return
293+
}
294+
chart.SetOwnerReferences(append(chart.GetOwnerReferences(),
295+
*metav1.NewControllerRef(repository.GetObjectMeta(), repository.GroupVersionKind())))
296+
if err := r.Update(ctx, chart); err != nil {
297+
r.Log.Error(err, fmt.Sprintf("failed to set owner reference to HelmRepository '%s'", repository.Name))
298+
}
299+
}

controllers/helmrepository_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type HelmRepositoryReconciler struct {
5050

5151
// +kubebuilder:rbac:groups=source.fluxcd.io,resources=helmrepositories,verbs=get;list;watch;create;update;patch;delete
5252
// +kubebuilder:rbac:groups=source.fluxcd.io,resources=helmrepositories/status,verbs=get;update;patch
53+
// +kubebuilder:rbac:groups=source.fluxcd.io,resources=helmcharts/finalizers,verbs=get;update;patch
5354

5455
func (r *HelmRepositoryReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
5556
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)

0 commit comments

Comments
 (0)