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

129 lines
3.1 KiB
Go
Raw Normal View History

2019-08-19 16:10:10 -07:00
package webhooks
import (
"encoding/json"
2019-08-23 18:34:23 -07:00
"github.com/nirmata/kyverno/pkg/engine"
2019-08-19 16:10:10 -07:00
jsonpatch "github.com/evanphx/json-patch"
"github.com/golang/glog"
)
const (
policyAnnotation = "policies.kyverno.io/patches"
2019-08-19 16:10:10 -07:00
)
type policyPatch struct {
2019-10-07 18:31:14 -07:00
PolicyName string `json:"policyname"`
2019-08-19 16:10:10 -07:00
RulePatches interface{} `json:"patches"`
}
type rulePatch struct {
RuleName string `json:"rulename"`
Op string `json:"op"`
Path string `json:"path"`
}
type response struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value"`
}
func generateAnnotationPatches(annotations map[string]string, engineResponses []engine.EngineResponse) []byte {
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
2019-08-23 18:34:23 -07:00
var patchResponse response
2019-10-08 14:21:47 -07:00
value := annotationFromEngineResponses(engineResponses)
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
2019-08-23 18:34:23 -07:00
if _, ok := annotations[policyAnnotation]; ok {
// create update patch string
patchResponse = response{
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-10-07 18:31:14 -07:00
// insert 'policies.kyverno.io' entry in annotation map
annotations[policyAnnotation] = string(value)
2019-08-23 18:34:23 -07:00
patchResponse = response{
2019-08-26 16:10:19 -07:00
Op: "add",
2019-10-07 18:31:14 -07:00
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 {
glog.Errorf("Failed to make patch from annotation'%s', err: %v\n ", string(patchByte), err)
}
return patchByte
}
func annotationFromEngineResponses(engineResponses []engine.EngineResponse) []byte {
2019-10-07 18:31:14 -07:00
var policyPatches []policyPatch
for _, engineResponse := range engineResponses {
if !engineResponse.IsSuccesful() {
glog.V(3).Infof("Policy %s failed, skip preparing annotation\n", engineResponse.PolicyResponse.Policy)
2019-08-23 18:34:23 -07:00
continue
}
2019-10-07 18:31:14 -07:00
var pp policyPatch
rulePatches := annotationFromPolicyResponse(engineResponse.PolicyResponse)
if rulePatches == nil {
continue
}
2019-08-23 18:34:23 -07:00
2019-10-07 18:31:14 -07:00
pp.RulePatches = rulePatches
pp.PolicyName = engineResponse.PolicyResponse.Policy
policyPatches = append(policyPatches, pp)
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 policyPatches == nil {
2019-08-23 18:34:23 -07:00
return nil
}
2019-08-19 16:10:10 -07:00
2019-10-07 18:31:14 -07:00
result, _ := json.Marshal(policyPatches)
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
2019-10-07 18:31:14 -07:00
func annotationFromPolicyResponse(policyResponse engine.PolicyResponse) []rulePatch {
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 {
glog.Errorf("Failed to parse patch bytes, err: %v\n", err)
continue
}
rp := rulePatch{
RuleName: ruleInfo.Name,
Op: patchmap["op"].(string),
Path: patchmap["path"].(string)}
rulePatches = append(rulePatches, rp)
glog.V(4).Infof("Annotation value prepared: %v\n", rulePatches)
}
}
2019-08-19 16:10:10 -07:00
2019-10-07 18:31:14 -07:00
if len(rulePatches) == 0 {
return nil
}
2019-08-19 16:10:10 -07:00
2019-10-07 18:31:14 -07:00
return rulePatches
}