-
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.
Merge branch 'main' into cel-support
Signed-off-by: Charles-Edouard Brétéché <[email protected]>
- Loading branch information
Showing
12 changed files
with
156 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package v1alpha1 | ||
|
||
import ( | ||
"github.com/kyverno/kyverno-json/pkg/message" | ||
"k8s.io/apimachinery/pkg/util/json" | ||
) | ||
|
||
type _message = message.Message | ||
|
||
// Message stores a message template. | ||
// +k8s:deepcopy-gen=false | ||
// +kubebuilder:validation:Type:=string | ||
type Message struct { | ||
_message | ||
} | ||
|
||
func (a *Message) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(a.Original()) | ||
} | ||
|
||
func (a *Message) UnmarshalJSON(data []byte) error { | ||
var v string | ||
err := json.Unmarshal(data, &v) | ||
if err != nil { | ||
return err | ||
} | ||
a._message = message.Parse(v) | ||
return nil | ||
} | ||
|
||
func (in *Message) DeepCopyInto(out *Message) { | ||
out._message = in._message | ||
} | ||
|
||
func (in *Message) DeepCopy() *Message { | ||
if in == nil { | ||
return nil | ||
} | ||
out := new(Message) | ||
in.DeepCopyInto(out) | ||
return out | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,76 @@ | ||
package message | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/jmespath-community/go-jmespath/pkg/binding" | ||
"github.com/jmespath-community/go-jmespath/pkg/parsing" | ||
"github.com/kyverno/kyverno-json/pkg/engine/template" | ||
) | ||
|
||
var variable = regexp.MustCompile(`{{(.*?)}}`) | ||
|
||
type Message interface { | ||
Original() string | ||
Format(any, binding.Bindings, ...template.Option) string | ||
} | ||
|
||
type substitution = func(string, any, binding.Bindings, ...template.Option) string | ||
|
||
type message struct { | ||
original string | ||
substitutions []substitution | ||
} | ||
|
||
func (m *message) Original() string { | ||
return m.original | ||
} | ||
|
||
func (m *message) Format(value any, bindings binding.Bindings, opts ...template.Option) string { | ||
out := m.original | ||
for _, substitution := range m.substitutions { | ||
out = substitution(out, value, bindings, opts...) | ||
} | ||
return out | ||
} | ||
|
||
func Parse(in string) *message { | ||
groups := variable.FindAllStringSubmatch(in, -1) | ||
var substitutions []func(string, any, binding.Bindings, ...template.Option) string | ||
for _, group := range groups { | ||
statement := strings.TrimSpace(group[1]) | ||
parse := sync.OnceValues(func() (parsing.ASTNode, error) { | ||
parser := parsing.NewParser() | ||
return parser.Parse(statement) | ||
}) | ||
evaluate := func(value any, bindings binding.Bindings, opts ...template.Option) (any, error) { | ||
ast, err := parse() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return template.ExecuteAST(context.TODO(), ast, value, bindings, opts...) | ||
} | ||
placeholder := group[0] | ||
substitutions = append(substitutions, func(out string, value any, bindings binding.Bindings, opts ...template.Option) string { | ||
result, err := evaluate(value, bindings, opts...) | ||
if err != nil { | ||
out = strings.ReplaceAll(out, placeholder, fmt.Sprintf("ERR (%s - %s)", statement, err)) | ||
} else if result == nil { | ||
out = strings.ReplaceAll(out, placeholder, fmt.Sprintf("ERR (%s not found)", statement)) | ||
} else if result, ok := result.(string); !ok { | ||
out = strings.ReplaceAll(out, placeholder, fmt.Sprintf("ERR (%s not a string)", statement)) | ||
} else { | ||
out = strings.ReplaceAll(out, placeholder, result) | ||
} | ||
return out | ||
}) | ||
} | ||
return &message{ | ||
original: in, | ||
substitutions: substitutions, | ||
} | ||
} |
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