1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-06 16:06:56 +00:00
kyverno/pkg/webhooks/annotations.go

150 lines
3.8 KiB
Go
Raw Normal View History

2019-08-19 16:10:10 -07:00
package webhooks
import (
"encoding/json"
"strings"
jsonpatch "github.com/evanphx/json-patch/v5"
2020-03-17 11:05:20 -07:00
"github.com/go-logr/logr"
"github.com/kyverno/kyverno/pkg/engine/response"
yamlv2 "gopkg.in/yaml.v2"
2019-08-19 16:10:10 -07:00
)
const (
policyAnnotation = "policies.kyverno.io~1patches"
2019-08-19 16:10:10 -07:00
)
type rulePatch struct {
RuleName string `json:"rulename"`
Op string `json:"op"`
Path string `json:"path"`
}
type annresponse struct {
2019-08-19 16:10:10 -07:00
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value"`
}
var operationToPastTense = map[string]string{
"add": "added",
"remove": "removed",
"replace": "replaced",
"move": "moved",
"copy": "copied",
"test": "tested",
}
2020-12-23 15:10:07 -08:00
func generateAnnotationPatches(engineResponses []*response.EngineResponse, log logr.Logger) []byte {
2019-11-11 18:52:26 -08:00
var annotations map[string]string
2019-11-13 17:56:56 -08:00
for _, er := range engineResponses {
if ann := er.PatchedResource.GetAnnotations(); ann != nil {
annotations = ann
break
}
2019-11-11 18:52:26 -08:00
}
2019-08-23 18:34:23 -07:00
if annotations == nil {
2019-10-07 18:31:14 -07:00
annotations = make(map[string]string)
2019-08-23 18:34:23 -07:00
}
2019-10-07 18:31:14 -07:00
var patchResponse annresponse
2020-03-17 11:05:20 -07:00
value := annotationFromEngineResponses(engineResponses, log)
2019-08-23 18:34:23 -07:00
if value == nil {
// no patches or error while processing patches
return nil
}
2019-10-07 18:31:14 -07:00
if _, ok := annotations[strings.ReplaceAll(policyAnnotation, "~1", "/")]; ok {
2019-08-23 18:34:23 -07:00
// create update patch string
patchResponse = annresponse{
2019-08-26 16:10:19 -07:00
Op: "replace",
2019-10-07 18:31:14 -07:00
Path: "/metadata/annotations/" + policyAnnotation,
2019-08-23 18:34:23 -07:00
Value: string(value),
}
} else {
2019-11-11 18:52:26 -08:00
// mutate rule has annotation patches
if len(annotations) > 0 {
patchResponse = annresponse{
2019-11-11 18:52:26 -08:00
Op: "add",
Path: "/metadata/annotations/" + policyAnnotation,
Value: string(value),
}
} else {
// insert 'policies.kyverno.patches' entry in annotation map
annotations[strings.ReplaceAll(policyAnnotation, "~1", "/")] = string(value)
patchResponse = annresponse{
2019-11-11 18:52:26 -08:00
Op: "add",
Path: "/metadata/annotations",
Value: annotations,
}
2019-08-23 18:34:23 -07:00
}
}
patchByte, _ := json.Marshal(patchResponse)
// check the patch
_, err := jsonpatch.DecodePatch([]byte("[" + string(patchByte) + "]"))
if err != nil {
2020-03-17 11:05:20 -07:00
log.Error(err, "failed o build JSON patch for annotation", "patch", string(patchByte))
2019-08-23 18:34:23 -07:00
}
return patchByte
}
2020-12-23 15:10:07 -08:00
func annotationFromEngineResponses(engineResponses []*response.EngineResponse, log logr.Logger) []byte {
var annotationContent = make(map[string]string)
2019-10-07 18:31:14 -07:00
for _, engineResponse := range engineResponses {
if !engineResponse.IsSuccessful() {
2020-03-17 11:05:20 -07:00
log.V(3).Info("skip building annotation; policy failed to apply", "policy", engineResponse.PolicyResponse.Policy)
2019-08-23 18:34:23 -07:00
continue
}
2020-03-17 11:05:20 -07:00
rulePatches := annotationFromPolicyResponse(engineResponse.PolicyResponse, log)
2019-10-07 18:31:14 -07:00
if rulePatches == nil {
continue
}
2019-08-23 18:34:23 -07:00
policyName := engineResponse.PolicyResponse.Policy
for _, rulePatch := range rulePatches {
annotationContent[rulePatch.RuleName+"."+policyName+".kyverno.io"] = operationToPastTense[rulePatch.Op] + " " + rulePatch.Path
}
2019-08-23 18:34:23 -07:00
}
2019-10-07 18:31:14 -07:00
// return nil if there's no patches
// otherwise result = null, len(result) = 4
if len(annotationContent) == 0 {
2019-08-23 18:34:23 -07:00
return nil
}
2019-08-19 16:10:10 -07:00
result, _ := yamlv2.Marshal(annotationContent)
2019-08-19 16:10:10 -07:00
2019-10-07 18:31:14 -07:00
return result
}
2019-08-19 16:10:10 -07:00
2020-03-17 11:05:20 -07:00
func annotationFromPolicyResponse(policyResponse response.PolicyResponse, log logr.Logger) []rulePatch {
2019-10-07 18:31:14 -07:00
var rulePatches []rulePatch
for _, ruleInfo := range policyResponse.Rules {
for _, patch := range ruleInfo.Patches {
var patchmap map[string]interface{}
if err := json.Unmarshal(patch, &patchmap); err != nil {
2020-03-17 11:05:20 -07:00
log.Error(err, "Failed to parse JSON patch bytes")
2019-10-07 18:31:14 -07:00
continue
}
rp := rulePatch{
RuleName: ruleInfo.Name,
Op: patchmap["op"].(string),
Path: patchmap["path"].(string)}
rulePatches = append(rulePatches, rp)
2020-03-17 11:05:20 -07:00
log.V(4).Info("annotation value prepared", "patches", rulePatches)
2019-10-07 18:31:14 -07:00
}
}
if len(rulePatches) == 0 {
return nil
}
return rulePatches
}