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 <hidde@hhh.computer>
  • Loading branch information
hiddeco committed Dec 14, 2023
1 parent 603d321 commit e6d03d2
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion internal/controller/helmrelease_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
corev1 "k8s.io/api/core/v1"
apiequality "k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
apierrutil "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -55,6 +56,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 +144,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 +670,30 @@ 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 {
var filtered []metav1.ManagedFieldsEntry
for _, f := range obj.GetManagedFields() {
if f.APIVersion != v2beta1.GroupVersion.String() || f.Manager != r.FieldManager {
filtered = append(filtered, f)
}
}

if len(filtered) == len(obj.GetManagedFields()) {
return nil
}

mergeFrom := client.MergeFrom(obj.DeepCopy())
obj.SetManagedFields(filtered)

return r.Client.Patch(ctx, obj, mergeFrom)
}

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 e6d03d2

Please sign in to comment.