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

55 lines
1.6 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-21 11:00:09 -07:00
kubepolicy "github.com/nirmata/kyverno/pkg/apis/policy/v1alpha1"
"github.com/nirmata/kyverno/pkg/engine/mutation"
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-20 13:02:55 -07:00
func Mutate(policy kubepolicy.Policy, rawResource []byte, gvk metav1.GroupVersionKind) ([]mutation.PatchBytes, []byte) {
2019-05-09 22:26:22 -07:00
var policyPatches []mutation.PatchBytes
2019-05-20 13:02:55 -07:00
var processedPatches []mutation.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 := mutation.ProcessOverlay(rule.Mutation.Overlay, rawResource)
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-20 13:02:55 -07:00
processedPatches, patchedDocument, err = mutation.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
}