2020-03-05 20:24:36 +00:00
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-12-12 15:20:20 +00:00
|
|
|
"github.com/go-logr/logr"
|
2022-05-17 11:12:43 +00:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2020-10-07 18:12:31 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
2023-02-09 15:15:51 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/internal"
|
2020-10-07 18:12:31 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/mutate"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
2022-12-12 15:20:20 +00:00
|
|
|
"github.com/kyverno/kyverno/pkg/utils/api"
|
|
|
|
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
2020-03-05 20:24:36 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2023-02-09 15:15:51 +00:00
|
|
|
func ForceMutate(
|
|
|
|
ctx context.Interface,
|
|
|
|
logger logr.Logger,
|
|
|
|
policy kyvernov1.PolicyInterface,
|
|
|
|
resource unstructured.Unstructured,
|
|
|
|
) (unstructured.Unstructured, error) {
|
|
|
|
logger = internal.LoggerWithPolicy(logger, policy)
|
|
|
|
logger = internal.LoggerWithResource(logger, "resource", resource)
|
|
|
|
// logger := logging.WithName("EngineForceMutate").WithValues("policy", policy.GetName(), "kind", resource.GetKind(),
|
|
|
|
// "namespace", resource.GetNamespace(), "name", resource.GetName())
|
2020-07-09 18:48:34 +00:00
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
patchedResource := resource
|
2022-03-28 14:01:27 +00:00
|
|
|
// TODO: if we apply autogen, tests will fail
|
2022-03-30 13:04:30 +00:00
|
|
|
spec := policy.GetSpec()
|
|
|
|
for _, rule := range spec.Rules {
|
2020-03-05 20:24:36 +00:00
|
|
|
if !rule.HasMutate() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-02-09 15:15:51 +00:00
|
|
|
logger := internal.LoggerWithRule(logger, rule)
|
|
|
|
|
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 {
|
2023-03-29 18:23:29 +00:00
|
|
|
patchedResource, err = applyForEachMutate(r.Name, r.Mutation.ForEachMutation, patchedResource, logger)
|
2022-12-12 15:20:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return patchedResource, err
|
2020-03-05 20:24:36 +00:00
|
|
|
}
|
2022-01-05 01:36:33 +00:00
|
|
|
} else {
|
|
|
|
m := r.Mutation
|
2023-03-29 18:23:29 +00:00
|
|
|
patchedResource, err = applyPatches(r.Name, m.GetPatchStrategicMerge(), m.PatchesJSON6902, patchedResource, logger)
|
2022-12-12 15:20:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return patchedResource, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return patchedResource, nil
|
|
|
|
}
|
|
|
|
|
2023-03-29 18:23:29 +00:00
|
|
|
func applyForEachMutate(name string, foreach []kyvernov1.ForEachMutation, resource unstructured.Unstructured, logger logr.Logger) (patchedResource unstructured.Unstructured, err error) {
|
2022-12-12 15:20:20 +00:00
|
|
|
patchedResource = resource
|
|
|
|
for _, fe := range foreach {
|
|
|
|
if fe.ForEachMutation != nil {
|
2022-12-12 19:24:13 +00:00
|
|
|
nestedForEach, err := api.DeserializeJSONArray[kyvernov1.ForEachMutation](fe.ForEachMutation)
|
2022-12-12 15:20:20 +00:00
|
|
|
if err != nil {
|
2023-02-01 06:38:04 +00:00
|
|
|
return patchedResource, fmt.Errorf("failed to deserialize foreach: %w", err)
|
2020-03-05 20:24:36 +00:00
|
|
|
}
|
2021-04-28 20:12:44 +00:00
|
|
|
|
2023-03-29 18:23:29 +00:00
|
|
|
return applyForEachMutate(name, nestedForEach, patchedResource, logger)
|
2022-12-12 15:20:20 +00:00
|
|
|
}
|
|
|
|
|
2023-03-29 18:23:29 +00:00
|
|
|
patchedResource, err = applyPatches(name, fe.GetPatchStrategicMerge(), fe.PatchesJSON6902, patchedResource, logger)
|
2022-12-12 15:20:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return resource, err
|
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
|
|
|
|
2023-03-29 18:23:29 +00:00
|
|
|
func applyPatches(name string, mergePatch apiextensions.JSON, jsonPatch string, resource unstructured.Unstructured, logger logr.Logger) (unstructured.Unstructured, error) {
|
2023-04-28 07:31:12 +00:00
|
|
|
patcher := mutate.NewPatcher(mergePatch, jsonPatch)
|
|
|
|
resourceBytes, err := resource.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return resource, err
|
2022-12-12 15:20:20 +00:00
|
|
|
}
|
2023-06-07 09:45:11 +00:00
|
|
|
resourceBytes, err = patcher.Patch(logger, resourceBytes)
|
2023-04-28 07:31:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return resource, err
|
|
|
|
}
|
|
|
|
if err := resource.UnmarshalJSON(resourceBytes); err != nil {
|
|
|
|
return resource, err
|
|
|
|
}
|
|
|
|
return resource, err
|
2022-12-12 15:20:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 01:36:33 +00:00
|
|
|
// removeConditions mutates the rule to remove AnyAllConditions
|
2022-05-17 11:12:43 +00:00
|
|
|
func removeConditions(rule *kyvernov1.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
|
|
|
}
|