pkg icon indicating copy to clipboard operation
pkg copied to clipboard

Extend `ssa.Apply` with field ignore rules

Open stefanprodan opened this issue 2 years ago • 2 comments

To make kustomize-controller compatible with VPA, HPA for custom resources and other controllers which mutate Flux-managed fields in-cluster, we need to wire the ssa/jsondiff logic into the SSA apply functions.

Tasks:

  • [x] Move the saa functions used in jsondiff to a dedicated package
  • [ ] Add []jsondiff.IgnoreRule to the ssa.ApplyOptions
  • [ ] Remove the ignored fields before the final apply if they diverge

Example:

	appliedObject := object.DeepCopy()

	if existingObject.GetResourceVersion() != "" {
		var sm = make(map[*jsondiff.SelectorRegex][]string, len(opts.DriftIgnoreRule))
		for _, ips := range opts.DriftIgnoreRule {
			sr, err := jsondiff.NewSelectorRegex(ips.Selector)
			if err != nil {
				return nil, fmt.Errorf("failed to create ignore rule selector: %w", err)
			}
			sm[sr] = ips.Paths
		}

		var ignorePaths jsondiff.IgnorePaths
		for sr, paths := range sm {
			if sr.MatchUnstructured(appliedObject) {
				ignorePaths = append(ignorePaths, paths...)
			}
		}

		if len(ignorePaths) > 0 {
			patch := jsondiff.GenerateRemovePatch(ignorePaths...)
			if err := jsondiff.ApplyPatchToUnstructured(appliedObject, patch); err != nil {
				return nil, err
			}
		}
	}

	if err := m.apply(ctx, appliedObject); err != nil {
		return nil, fmt.Errorf("%s apply failed: %w", FmtUnstructured(appliedObject), err)
	}

stefanprodan avatar Dec 01 '23 09:12 stefanprodan

Would like to contribute to getting this solved as flux has been great for my use cases until I now need to manage a CRD with a conversion webhook and a cert-manager caBundle that would keep getting removed/added.

Is it "just" a lack of priority and contributions that is blocking this issue or is there something else related to adding the ignore rules that makes this extra complicated to solve?

alxbse avatar Oct 21 '24 12:10 alxbse

@alxbse the CRD conversion webhook CA bundle issue was fixed in #952 and will be available in Flux 2.7

stefanprodan avatar Jul 17 '25 10:07 stefanprodan