Skip to content

Extend ssa.Apply with field ignore rules #696

@stefanprodan

Description

@stefanprodan

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:

  • 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)
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions