2020-03-05 20:24:36 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-10-29 16:13:20 +00:00
|
|
|
kyverno "github.com/kyverno/kyverno/api/kyverno/v1"
|
2020-10-07 18:12:31 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/mutate"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/response"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
2020-03-05 20:24:36 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2020-03-18 00:23:18 +00:00
|
|
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
2020-03-05 20:24:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ForceMutate does not check any conditions, it simply mutates the given resource
|
2021-03-26 17:47:59 +00:00
|
|
|
// It is used to validate mutation logic, and for tests.
|
2022-01-05 01:36:33 +00:00
|
|
|
func ForceMutate(ctx *context.Context, policy kyverno.ClusterPolicy, resource unstructured.Unstructured) (unstructured.Unstructured, error) {
|
2020-07-09 18:48:34 +00:00
|
|
|
logger := log.Log.WithName("EngineForceMutate").WithValues("policy", policy.Name, "kind", resource.GetKind(),
|
|
|
|
"namespace", resource.GetNamespace(), "name", resource.GetName())
|
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
patchedResource := resource
|
2022-03-18 15:18:32 +00:00
|
|
|
for _, rule := range policy.GetRules() {
|
2020-03-05 20:24:36 +00:00
|
|
|
if !rule.HasMutate() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
ruleCopy := rule.DeepCopy()
|
|
|
|
removeConditions(ruleCopy)
|
|
|
|
r, err := variables.SubstituteAllForceMutate(logger, ctx, *ruleCopy)
|
2021-03-11 20:06:04 +00:00
|
|
|
if err != nil {
|
2022-01-05 01:36:33 +00:00
|
|
|
return resource, err
|
2021-03-11 20:06:04 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
if r.Mutation.ForEachMutation != nil {
|
|
|
|
for i, foreach := range r.Mutation.ForEachMutation {
|
2022-03-06 19:07:51 +00:00
|
|
|
patcher := mutate.NewPatcher(r.Name, foreach.GetPatchStrategicMerge(), foreach.PatchesJSON6902, patchedResource, ctx, logger)
|
2022-01-05 01:36:33 +00:00
|
|
|
resp, mutatedResource := patcher.Patch()
|
|
|
|
if resp.Status != response.RuleStatusPass {
|
|
|
|
return patchedResource, fmt.Errorf("foreach mutate result %q at index %d: %s", resp.Status.String(), i, resp.Message)
|
|
|
|
}
|
2020-03-05 20:24:36 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
patchedResource = mutatedResource
|
2020-03-05 20:24:36 +00:00
|
|
|
}
|
2022-01-05 01:36:33 +00:00
|
|
|
} else {
|
|
|
|
m := r.Mutation
|
2022-03-06 19:07:51 +00:00
|
|
|
patcher := mutate.NewPatcher(r.Name, m.GetPatchStrategicMerge(), m.PatchesJSON6902, patchedResource, ctx, logger)
|
2022-01-05 01:36:33 +00:00
|
|
|
resp, mutatedResource := patcher.Patch()
|
2021-09-26 09:12:31 +00:00
|
|
|
if resp.Status != response.RuleStatusPass {
|
2022-01-05 01:36:33 +00:00
|
|
|
return patchedResource, fmt.Errorf("mutate result %q: %s", resp.Status.String(), resp.Message)
|
2020-03-05 20:24:36 +00:00
|
|
|
}
|
2021-04-28 20:12:44 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
patchedResource = mutatedResource
|
2021-07-23 17:53:37 +00:00
|
|
|
}
|
2022-01-05 01:36:33 +00:00
|
|
|
}
|
2021-07-23 17:53:37 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
return patchedResource, nil
|
|
|
|
}
|
2021-04-28 20:12:44 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
// removeConditions mutates the rule to remove AnyAllConditions
|
|
|
|
func removeConditions(rule *kyverno.Rule) {
|
2022-03-06 19:07:51 +00:00
|
|
|
if rule.GetAnyAllConditions() != nil {
|
|
|
|
rule.SetAnyAllConditions(nil)
|
2020-03-05 20:24:36 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
for i, fem := range rule.Mutation.ForEachMutation {
|
|
|
|
if fem.AnyAllConditions != nil {
|
|
|
|
rule.Mutation.ForEachMutation[i].AnyAllConditions = nil
|
|
|
|
}
|
|
|
|
}
|
2020-03-05 20:24:36 +00:00
|
|
|
}
|