Skip to content

Commit

Permalink
controller: reset field managers for v2beta1
Browse files Browse the repository at this point in the history
Signed-off-by: Hidde Beydals <[email protected]>
  • Loading branch information
hiddeco committed Dec 14, 2023
1 parent 603d321 commit b91d5c6
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion internal/controller/helmrelease_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
"github.com/fluxcd/pkg/runtime/predicates"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"

"github.com/fluxcd/helm-controller/api/v2beta1"
v2 "github.com/fluxcd/helm-controller/api/v2beta2"
intacl "github.com/fluxcd/helm-controller/internal/acl"
"github.com/fluxcd/helm-controller/internal/action"
Expand Down Expand Up @@ -142,12 +143,17 @@ func (r *HelmReleaseReconciler) Reconcile(ctx context.Context, req ctrl.Request)
start := time.Now()
log := ctrl.LoggerFrom(ctx)

// Fetch the HelmRelease
// Fetch the HelmRelease.
obj := &v2.HelmRelease{}
if err := r.Get(ctx, req.NamespacedName, obj); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}

// Conditionally reset the field managers for the object.
if err := r.resetFieldManagers(ctx, obj); err != nil {
return ctrl.Result{}, err
}

// Initialize the patch helper with the current version of the object.
patchHelper := patch.NewSerialPatcher(obj, r.Client)

Expand Down Expand Up @@ -663,6 +669,24 @@ func (r *HelmReleaseReconciler) waitForHistoryCacheSync(obj *v2.HelmRelease) wai
}
}

// resetFieldManagers resets the field managers for the given v2beta2.HelmRelease
// when any of the fields are reported as v2beta1.HelmRelease fields.
// This is required to prevent any conversion conflicts when newly added fields
// are not present in the v2beta1.HelmRelease, or when the v2beta1.HelmRelease
// is eventually removed.
// xref: https://github.com/kubernetes/kubernetes/issues/111937
func (r *HelmReleaseReconciler) resetFieldManagers(ctx context.Context, obj *v2.HelmRelease) error {
for _, f := range obj.GetManagedFields() {
if f.APIVersion == v2beta1.GroupVersion.String() {
if err := r.Client.Patch(ctx, obj, client.RawPatch(types.MergePatchType, []byte(`{"metadata":{"managedFields": [{}]}}`))); err != nil {
return err
}
break
}
}
return nil
}

func (r *HelmReleaseReconciler) requestsForHelmChartChange(ctx context.Context, o client.Object) []reconcile.Request {
hc, ok := o.(*sourcev1.HelmChart)
if !ok {
Expand Down

0 comments on commit b91d5c6

Please sign in to comment.