Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ssa/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.25.0
require (
github.com/evanphx/json-patch/v5 v5.9.11
github.com/fluxcd/cli-utils v0.37.2-flux.1
github.com/go-openapi/jsonpointer v0.21.1
github.com/google/go-cmp v0.7.0
github.com/onsi/gomega v1.39.0
github.com/wI2L/jsondiff v0.6.1
Expand Down Expand Up @@ -34,7 +35,6 @@ require (
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
github.com/go-errors/errors v1.5.1 // indirect
github.com/go-logr/logr v1.4.3 // indirect
github.com/go-openapi/jsonpointer v0.21.1 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.1 // indirect
github.com/google/btree v1.1.3 // indirect
Expand Down
20 changes: 20 additions & 0 deletions ssa/jsondiff/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ func GenerateRemovePatch(paths ...string) jsondiff.Patch {
return patch
}

// ReplaceOp describes a single JSON Patch replace operation.
type ReplaceOp struct {
Path string
Value any
}

// GenerateReplacePatch generates a JSON patch that replaces the values at the
// given JSON pointer paths.
func GenerateReplacePatch(ops ...ReplaceOp) jsondiff.Patch {
var patch jsondiff.Patch
for _, op := range ops {
patch = append(patch, jsondiff.Operation{
Type: jsondiff.OperationReplace,
Path: op.Path,
Value: op.Value,
})
}
return patch
}

// ApplyPatchToUnstructured applies the given JSON patch to the given
// unstructured object. The patch is applied in-place.
// It permits the patch to contain "remove" operations that target non-existing
Expand Down
27 changes: 20 additions & 7 deletions ssa/jsondiff/unstructured.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ type IgnoreRule struct {
Selector *Selector
}

// CompiledIgnoreRules is a set of IgnoreRule with compiled selectors.
type CompiledIgnoreRules map[*SelectorRegex][]string

// CompileIgnoreRules compiles the selectors in the given IgnoreRule slice
// and returns a CompiledIgnoreRules.
func CompileIgnoreRules(rules []IgnoreRule) (CompiledIgnoreRules, error) {
compiled := make(CompiledIgnoreRules, len(rules))
for _, rule := range rules {
sr, err := NewSelectorRegex(rule.Selector)
if err != nil {
return nil, fmt.Errorf("failed to create ignore rule selector: %w", err)
}
compiled[sr] = rule.Paths
}
return compiled, nil
}

// UnstructuredList runs a dry-run patch for a list of Kubernetes resources
// against a Kubernetes cluster and compares the result against the original
// objects. It returns a DiffSet, which contains differences between the
Expand All @@ -59,13 +76,9 @@ func UnstructuredList(ctx context.Context, c client.Client, objs []*unstructured
o := &ListOptions{}
o.ApplyOptions(opts)

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

var resOpts []ResourceOption
Expand Down
Loading
Loading