Merge pull request #13 from fluxcd/helmchart-ownership

Set owner ref to HelmRepository on HelmChart
This commit is contained in:
Hidde Beydals 2020-04-13 13:41:28 +02:00 committed by GitHub
commit 9696bf7f29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 0 deletions

View File

@ -38,6 +38,14 @@ rules:
- patch
- update
- watch
- apiGroups:
- source.fluxcd.io
resources:
- helmcharts/finalizers
verbs:
- get
- patch
- update
- apiGroups:
- source.fluxcd.io
resources:

View File

@ -86,6 +86,10 @@ func (r *HelmChartReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
return ctrl.Result{Requeue: true}, err
}
// set ownership reference so chart is garbage collected on
// repository removal
r.setOwnerRef(ctx, &chart, repository)
// try to pull chart
pulledChart, err := r.sync(repository, *chart.DeepCopy())
if err != nil {
@ -282,3 +286,14 @@ func (r *HelmChartReconciler) gc(chart sourcev1.HelmChart) {
}
}
}
func (r *HelmChartReconciler) setOwnerRef(ctx context.Context, chart *sourcev1.HelmChart, repository sourcev1.HelmRepository) {
if metav1.IsControlledBy(chart.GetObjectMeta(), repository.GetObjectMeta()) {
return
}
chart.SetOwnerReferences(append(chart.GetOwnerReferences(),
*metav1.NewControllerRef(repository.GetObjectMeta(), repository.GroupVersionKind())))
if err := r.Update(ctx, chart); err != nil {
r.Log.Error(err, fmt.Sprintf("failed to set owner reference to HelmRepository '%s'", repository.Name))
}
}

View File

@ -50,6 +50,7 @@ type HelmRepositoryReconciler struct {
// +kubebuilder:rbac:groups=source.fluxcd.io,resources=helmrepositories,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=source.fluxcd.io,resources=helmrepositories/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=source.fluxcd.io,resources=helmcharts/finalizers,verbs=get;update;patch
func (r *HelmRepositoryReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)