| 1 |
/* |
| 2 |
Copyright 2024 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package mutating |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
|
| 22 |
"k8s.io/api/admissionregistration/v1alpha1" |
| 23 |
plugincel "k8s.io/apiserver/pkg/admission/plugin/cel" |
| 24 |
"k8s.io/apiserver/pkg/admission/plugin/policy/mutating/patch" |
| 25 |
"k8s.io/apiserver/pkg/admission/plugin/webhook/matchconditions" |
| 26 |
apiservercel "k8s.io/apiserver/pkg/cel" |
| 27 |
"k8s.io/apiserver/pkg/cel/environment" |
| 28 |
) |
| 29 |
|
| 30 |
// compilePolicy compiles the policy into a PolicyEvaluator |
| 31 |
// any error is stored and delayed until invocation. |
| 32 |
// |
| 33 |
// Each individual mutation is compiled into MutationEvaluationFunc and |
| 34 |
// returned is a PolicyEvaluator in the same order as the mutations appeared in the policy. |
| 35 |
func compilePolicy(policy *Policy) PolicyEvaluator { |
| 36 |
opts := plugincel.OptionalVariableDeclarations{HasParams: policy.Spec.ParamKind != nil, StrictCost: true, HasAuthorizer: true} |
| 37 |
compiler, err := plugincel.NewCompositedCompiler(environment.MustBaseEnvSet(environment.DefaultCompatibilityVersion(), true)) |
| 38 |
if err != nil { |
| 39 |
return PolicyEvaluator{Error: &apiservercel.Error{ |
| 40 |
Type: apiservercel.ErrorTypeInternal, |
| 41 |
Detail: fmt.Sprintf("failed to initialize CEL compiler: %v", err), |
| 42 |
}} |
| 43 |
} |
| 44 |
|
| 45 |
// Compile and store variables |
| 46 |
compiler.CompileAndStoreVariables(convertv1alpha1Variables(policy.Spec.Variables), opts, environment.StoredExpressions) |
| 47 |
|
| 48 |
// Compile matchers |
| 49 |
var matcher matchconditions.Matcher = nil |
| 50 |
matchConditions := policy.Spec.MatchConditions |
| 51 |
if len(matchConditions) > 0 { |
| 52 |
matchExpressionAccessors := make([]plugincel.ExpressionAccessor, len(matchConditions)) |
| 53 |
for i := range matchConditions { |
| 54 |
matchExpressionAccessors[i] = (*matchconditions.MatchCondition)(&matchConditions[i]) |
| 55 |
} |
| 56 |
matcher = matchconditions.NewMatcher(compiler.CompileCondition(matchExpressionAccessors, opts, environment.StoredExpressions), toV1FailurePolicy(policy.Spec.FailurePolicy), "policy", "validate", policy.Name) |
| 57 |
} |
| 58 |
|
| 59 |
// Compiler patchers |
| 60 |
var patchers []patch.Patcher |
| 61 |
patchOptions := opts |
| 62 |
patchOptions.HasPatchTypes = true |
| 63 |
for _, m := range policy.Spec.Mutations { |
| 64 |
switch m.PatchType { |
| 65 |
case v1alpha1.PatchTypeJSONPatch: |
| 66 |
if m.JSONPatch != nil { |
| 67 |
accessor := &patch.JSONPatchCondition{Expression: m.JSONPatch.Expression} |
| 68 |
compileResult := compiler.CompileMutatingEvaluator(accessor, patchOptions, environment.StoredExpressions) |
| 69 |
patchers = append(patchers, patch.NewJSONPatcher(compileResult)) |
| 70 |
} |
| 71 |
case v1alpha1.PatchTypeApplyConfiguration: |
| 72 |
if m.ApplyConfiguration != nil { |
| 73 |
accessor := &patch.ApplyConfigurationCondition{Expression: m.ApplyConfiguration.Expression} |
| 74 |
compileResult := compiler.CompileMutatingEvaluator(accessor, patchOptions, environment.StoredExpressions) |
| 75 |
patchers = append(patchers, patch.NewApplyConfigurationPatcher(compileResult)) |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return PolicyEvaluator{Matcher: matcher, Mutators: patchers, CompositionEnv: compiler.CompositionEnv} |
| 81 |
} |