2020-03-06 01:54:36 +05:30
|
|
|
package engine
|
|
|
|
|
|
|
|
import (
|
2022-12-12 07:20:20 -08:00
|
|
|
"github.com/go-logr/logr"
|
2022-05-17 13:12:43 +02:00
|
|
|
kyvernov1 "github.com/kyverno/kyverno/api/kyverno/v1"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/context"
|
2023-02-09 16:15:51 +01:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/internal"
|
2020-10-07 11:12:31 -07:00
|
|
|
"github.com/kyverno/kyverno/pkg/engine/mutate"
|
|
|
|
"github.com/kyverno/kyverno/pkg/engine/variables"
|
2022-12-12 07:20:20 -08:00
|
|
|
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
|
2020-03-06 01:54:36 +05:30
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ForceMutate does not check any conditions, it simply mutates the given resource
|
2021-03-26 10:47:59 -07:00
|
|
|
// It is used to validate mutation logic, and for tests.
|
2023-02-09 16:15:51 +01: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 11:48:34 -07:00
|
|
|
|
2022-01-04 17:36:33 -08:00
|
|
|
patchedResource := resource
|
2022-03-28 16:01:27 +02:00
|
|
|
// TODO: if we apply autogen, tests will fail
|
2022-03-30 15:04:30 +02:00
|
|
|
spec := policy.GetSpec()
|
|
|
|
for _, rule := range spec.Rules {
|
2020-03-06 01:54:36 +05:30
|
|
|
if !rule.HasMutate() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-02-09 16:15:51 +01:00
|
|
|
logger := internal.LoggerWithRule(logger, rule)
|
|
|
|
|
2022-01-04 17:36:33 -08:00
|
|
|
ruleCopy := rule.DeepCopy()
|
|
|
|
removeConditions(ruleCopy)
|
|
|
|
r, err := variables.SubstituteAllForceMutate(logger, ctx, *ruleCopy)
|
2021-03-11 22:06:04 +02:00
|
|
|
if err != nil {
|
2022-01-04 17:36:33 -08:00
|
|
|
return resource, err
|
2021-03-11 22:06:04 +02:00
|
|
|
}
|
|
|
|
|
2022-01-04 17:36:33 -08:00
|
|
|
if r.Mutation.ForEachMutation != nil {
|
2023-03-30 02:23:29 +08:00
|
|
|
patchedResource, err = applyForEachMutate(r.Name, r.Mutation.ForEachMutation, patchedResource, logger)
|
2022-12-12 07:20:20 -08:00
|
|
|
if err != nil {
|
|
|
|
return patchedResource, err
|
2020-03-06 01:54:36 +05:30
|
|
|
}
|
2022-01-04 17:36:33 -08:00
|
|
|
} else {
|
|
|
|
m := r.Mutation
|
2024-05-30 07:29:24 +08:00
|
|
|
patchedResource, err = applyPatches(m.GetPatchStrategicMerge(), m.PatchesJSON6902, patchedResource, logger)
|
2022-12-12 07:20:20 -08:00
|
|
|
if err != nil {
|
|
|
|
return patchedResource, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return patchedResource, nil
|
|
|
|
}
|
|
|
|
|
2023-03-30 02:23:29 +08:00
|
|
|
func applyForEachMutate(name string, foreach []kyvernov1.ForEachMutation, resource unstructured.Unstructured, logger logr.Logger) (patchedResource unstructured.Unstructured, err error) {
|
2022-12-12 07:20:20 -08:00
|
|
|
patchedResource = resource
|
|
|
|
for _, fe := range foreach {
|
2024-07-30 13:52:41 +03:00
|
|
|
fem := fe.GetForEachMutation()
|
|
|
|
if len(fem) > 0 {
|
|
|
|
return applyForEachMutate(name, fem, patchedResource, logger)
|
2022-12-12 07:20:20 -08:00
|
|
|
}
|
|
|
|
|
2024-05-30 07:29:24 +08:00
|
|
|
patchedResource, err = applyPatches(fe.GetPatchStrategicMerge(), fe.PatchesJSON6902, patchedResource, logger)
|
2022-12-12 07:20:20 -08:00
|
|
|
if err != nil {
|
|
|
|
return resource, err
|
2021-07-23 20:53:37 +03:00
|
|
|
}
|
2022-01-04 17:36:33 -08:00
|
|
|
}
|
2021-07-23 20:53:37 +03:00
|
|
|
|
2022-01-04 17:36:33 -08:00
|
|
|
return patchedResource, nil
|
|
|
|
}
|
2021-04-28 23:12:44 +03:00
|
|
|
|
2024-05-30 07:29:24 +08:00
|
|
|
func applyPatches(mergePatch apiextensions.JSON, jsonPatch string, resource unstructured.Unstructured, logger logr.Logger) (unstructured.Unstructured, error) {
|
2023-04-28 09:31:12 +02:00
|
|
|
patcher := mutate.NewPatcher(mergePatch, jsonPatch)
|
|
|
|
resourceBytes, err := resource.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return resource, err
|
2022-12-12 07:20:20 -08:00
|
|
|
}
|
2023-06-07 11:45:11 +02:00
|
|
|
resourceBytes, err = patcher.Patch(logger, resourceBytes)
|
2023-04-28 09:31:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return resource, err
|
|
|
|
}
|
|
|
|
if err := resource.UnmarshalJSON(resourceBytes); err != nil {
|
|
|
|
return resource, err
|
|
|
|
}
|
|
|
|
return resource, err
|
2022-12-12 07:20:20 -08:00
|
|
|
}
|
|
|
|
|
2022-01-04 17:36:33 -08:00
|
|
|
// removeConditions mutates the rule to remove AnyAllConditions
|
2022-05-17 13:12:43 +02:00
|
|
|
func removeConditions(rule *kyvernov1.Rule) {
|
2022-03-06 20:07:51 +01:00
|
|
|
if rule.GetAnyAllConditions() != nil {
|
|
|
|
rule.SetAnyAllConditions(nil)
|
2020-03-06 01:54:36 +05:30
|
|
|
}
|
|
|
|
|
2022-01-04 17:36:33 -08:00
|
|
|
for i, fem := range rule.Mutation.ForEachMutation {
|
|
|
|
if fem.AnyAllConditions != nil {
|
|
|
|
rule.Mutation.ForEachMutation[i].AnyAllConditions = nil
|
|
|
|
}
|
|
|
|
}
|
2020-03-06 01:54:36 +05:30
|
|
|
}
|