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

54 lines
1.5 KiB
Go
Raw Normal View History

2019-05-13 18:17:28 -07:00
package engine
2019-05-09 22:26:22 -07:00
import (
"log"
2019-05-22 18:28:38 +01:00
kubepolicy "github.com/nirmata/kyverno/pkg/apis/policy/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2019-05-09 22:26:22 -07:00
)
// Mutate performs mutation. Overlay first and then mutation patches
2019-05-14 18:20:41 -07:00
// TODO: return events and violations
2019-05-21 18:27:56 +03:00
func Mutate(policy kubepolicy.Policy, rawResource []byte, gvk metav1.GroupVersionKind) ([]PatchBytes, []byte) {
var policyPatches []PatchBytes
var processedPatches []PatchBytes
var err error
2019-05-20 13:02:55 -07:00
patchedDocument := rawResource
2019-05-09 22:26:22 -07:00
for _, rule := range policy.Spec.Rules {
if rule.Mutation == nil {
continue
2019-05-09 22:26:22 -07:00
}
ok := ResourceMeetsDescription(rawResource, rule.ResourceDescription, gvk)
if !ok {
log.Printf("Rule \"%s\" is not applicable to resource\n", rule.Name)
continue
}
// Process Overlay
2019-05-09 22:26:22 -07:00
if rule.Mutation.Overlay != nil {
overlayPatches, err := ProcessOverlay(policy, rawResource, gvk)
if err != nil {
log.Printf("Overlay application has failed for rule %s in policy %s, err: %v\n", rule.Name, policy.ObjectMeta.Name, err)
} else {
policyPatches = append(policyPatches, overlayPatches...)
}
2019-05-09 22:26:22 -07:00
}
// Process Patches
2019-05-09 22:26:22 -07:00
if rule.Mutation.Patches != nil {
2019-05-21 18:27:56 +03:00
processedPatches, patchedDocument, err = ProcessPatches(rule.Mutation.Patches, patchedDocument)
if err != nil {
log.Printf("Patches application has failed for rule %s in policy %s, err: %v\n", rule.Name, policy.ObjectMeta.Name, err)
} else {
policyPatches = append(policyPatches, processedPatches...)
}
}
2019-05-09 22:26:22 -07:00
}
2019-05-20 13:02:55 -07:00
return policyPatches, patchedDocument
2019-05-09 22:26:22 -07:00
}