Conversation
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Cache compiled JQ filters: parse and compile each
jqFilterexpression once at hook-config load time instead of on every Kubernetes event.What this PR does / why we need it
Every Kubernetes event that reaches a hook with a
jqFilterpreviously triggered a fullgojq.Parse+ implicit compilation cycle before running the expression against the object. For hooks watching high-churn resources (Pods, Events, custom CRDs with many replicas) this is a measurable, unnecessary CPU cost that accumulates proportionally to event rate × number of monitored bindings.This PR:
CompiledFilterinterface in filter.go — a pre-compiled filter with a singleApply(data) ([]byte, error)method.CompiledJqFilterstruct in apply.go backed by*gojq.Code(the compiled representation returned bygojq.Compile). ACompile(string)constructor parses and compiles the expression once. A sharedcollectResultshelper is extracted so both the interpreted (Filter.ApplyFilter) and compiled (CompiledJqFilter.Apply) paths stay DRY.CompiledJqFilter filter.CompiledFilterfield toMonitorConfigand aWithJqFilter(string) errormethod that atomically sets the raw string (kept for metadata/logging) and compiles it. Config parse errors for invalidjqFilterexpressions are caught immediately at startup, not at first event.monitor.WithJqFilter(...)instead of plain field assignment — invalid expressions are now rejected at hook-config load time with a descriptive error.jq.NewFilter()allocations and passesei.Monitor.CompiledJqFilterdirectly toapplyFilter. ThejqFilterstring is still forwarded as a separate argument purely forres.Metadata.JqFilter(used by the binding-context JSON marshaler).applyFiltersignature simplified from(jqFilter string, fl filter.Filter, ...)to(compiledFilter filter.CompiledFilter, jqFilterStr string, ...).Performance impact:
gojq.Parse+gojq.Compilerun once per binding at startup. Per-event cost is reduced tocode.Run(data)only.