-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
- Loading branch information
1 parent
7faca6c
commit 741b0de
Showing
7 changed files
with
75 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package matching | ||
|
||
import ( | ||
"sync" | ||
|
||
lru "github.com/hashicorp/golang-lru/v2" | ||
"github.com/kyverno/kyverno-json/pkg/core/assertion" | ||
"github.com/kyverno/kyverno-json/pkg/core/templating" | ||
) | ||
|
||
type Compiler struct { | ||
templating.Compiler | ||
*lru.Cache[string, func() (assertion.Assertion, error)] | ||
} | ||
|
||
func NewCompiler(compiler templating.Compiler, cacheSize int) Compiler { | ||
out := Compiler{ | ||
Compiler: compiler, | ||
} | ||
if cache, err := lru.New[string, func() (assertion.Assertion, error)](cacheSize); err == nil { | ||
out.Cache = cache | ||
} | ||
return out | ||
} | ||
func (c Compiler) CompileAssertion(hash string, value any) (assertion.Assertion, error) { | ||
if c.Cache == nil { | ||
return assertion.Parse(value, c.Compiler) | ||
} | ||
entry, _ := c.Cache.Get(hash) | ||
if entry == nil { | ||
entry = sync.OnceValues(func() (assertion.Assertion, error) { | ||
return assertion.Parse(value, c.Compiler) | ||
}) | ||
c.Cache.Add(hash, entry) | ||
} | ||
return entry() | ||
} |
This file contains 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
This file contains 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