Debian Codegraph @

pkg/admission/plugin/cel/mutation.go

raw
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 cel
18
19 import (
20 "context"
21
22 admissionv1 "k8s.io/api/admission/v1"
23 v1 "k8s.io/api/core/v1"
24 "k8s.io/apiserver/pkg/admission"
25 "k8s.io/apiserver/pkg/cel/environment"
26 )
27
28 // mutatingCompiler provides a MutatingCompiler implementation.
29 type mutatingCompiler struct {
30 compiler Compiler
31 }
32
33 // CompileMutatingEvaluator compiles a CEL expression for admission plugins and returns an MutatingEvaluator for executing the
34 // compiled CEL expression.
35 func (p *mutatingCompiler) CompileMutatingEvaluator(expressionAccessor ExpressionAccessor, options OptionalVariableDeclarations, mode environment.Type) MutatingEvaluator {
36 compilationResult := p.compiler.CompileCELExpression(expressionAccessor, options, mode)
37 return NewMutatingEvaluator(compilationResult)
38 }
39
40 type mutatingEvaluator struct {
41 compilationResult CompilationResult
42 }
43
44 func NewMutatingEvaluator(compilationResult CompilationResult) MutatingEvaluator {
45 return &mutatingEvaluator{compilationResult}
46 }
47
48 // ForInput evaluates the compiled CEL expression and returns an evaluation result
49 // errors per evaluation are returned in the evaluation result
50 // runtimeCELCostBudget was added for testing purpose only. Callers should always use const RuntimeCELCostBudget from k8s.io/apiserver/pkg/apis/cel/config.go as input.
51 func (p *mutatingEvaluator) ForInput(ctx context.Context, versionedAttr *admission.VersionedAttributes, request *admissionv1.AdmissionRequest, inputs OptionalVariableBindings, namespace *v1.Namespace, runtimeCELCostBudget int64) (EvaluationResult, int64, error) {
52 // if this activation supports composition, we will need the compositionCtx. It may be nil.
53 compositionCtx, _ := ctx.(CompositionContext)
54
55 activation, err := newActivation(compositionCtx, versionedAttr, request, inputs, namespace)
56 if err != nil {
57 return EvaluationResult{}, -1, err
58 }
59 evaluation, remainingBudget, err := activation.Evaluate(ctx, compositionCtx, p.compilationResult, runtimeCELCostBudget)
60 if err != nil {
61 return evaluation, -1, err
62 }
63 return evaluation, remainingBudget, nil
64
65 }
66
67 // CompilationErrors returns a list of all the errors from the compilation of the mutatingEvaluator
68 func (p *mutatingEvaluator) CompilationErrors() (compilationErrors []error) {
69 if p.compilationResult.Error != nil {
70 return []error{p.compilationResult.Error}
71 }
72 return nil
73 }