Skip to content

Commit 3baaac6

Browse files
authored
Merge pull request kubernetes#127400 from sttts/sttts-webhook-cel-compiler
apiserver/admission/webhook: construct static CEL compiler only once
2 parents 9844676 + 26aeda3 commit 3baaac6

File tree

1 file changed

+15
-6
lines changed
  • staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/generic

1 file changed

+15
-6
lines changed

staging/src/k8s.io/apiserver/pkg/admission/plugin/webhook/generic/webhook.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ import (
2020
"context"
2121
"fmt"
2222
"io"
23+
"sync"
2324

25+
"k8s.io/apiserver/pkg/cel/environment"
26+
"k8s.io/apiserver/pkg/features"
27+
utilfeature "k8s.io/apiserver/pkg/util/feature"
2428
"k8s.io/klog/v2"
2529

2630
admissionv1 "k8s.io/api/admission/v1"
@@ -38,14 +42,17 @@ import (
3842
"k8s.io/apiserver/pkg/admission/plugin/webhook/predicates/object"
3943
"k8s.io/apiserver/pkg/admission/plugin/webhook/predicates/rules"
4044
"k8s.io/apiserver/pkg/authorization/authorizer"
41-
"k8s.io/apiserver/pkg/cel/environment"
42-
"k8s.io/apiserver/pkg/features"
43-
utilfeature "k8s.io/apiserver/pkg/util/feature"
4445
webhookutil "k8s.io/apiserver/pkg/util/webhook"
4546
"k8s.io/client-go/informers"
4647
clientset "k8s.io/client-go/kubernetes"
4748
)
4849

50+
var (
51+
// filterCompiler is memory heavy, so we only want to create it once and share it.
52+
filterCompilerOnce sync.Once
53+
filterCompiler cel.FilterCompiler
54+
)
55+
4956
// Webhook is an abstract admission plugin with all the infrastructure to define Admit or Validate on-top.
5057
type Webhook struct {
5158
*admission.Handler
@@ -57,7 +64,6 @@ type Webhook struct {
5764
namespaceMatcher *namespace.Matcher
5865
objectMatcher *object.Matcher
5966
dispatcher Dispatcher
60-
filterCompiler cel.FilterCompiler
6167
authorizer authorizer.Authorizer
6268
}
6369

@@ -95,14 +101,17 @@ func NewWebhook(handler *admission.Handler, configFile io.Reader, sourceFactory
95101
cm.SetAuthenticationInfoResolver(authInfoResolver)
96102
cm.SetServiceResolver(webhookutil.NewDefaultServiceResolver())
97103

104+
filterCompilerOnce.Do(func() {
105+
filterCompiler = cel.NewFilterCompiler(environment.MustBaseEnvSet(environment.DefaultCompatibilityVersion(), utilfeature.DefaultFeatureGate.Enabled(features.StrictCostEnforcementForWebhooks)))
106+
})
107+
98108
return &Webhook{
99109
Handler: handler,
100110
sourceFactory: sourceFactory,
101111
clientManager: &cm,
102112
namespaceMatcher: &namespace.Matcher{},
103113
objectMatcher: &object.Matcher{},
104114
dispatcher: dispatcherFactory(&cm),
105-
filterCompiler: cel.NewFilterCompiler(environment.MustBaseEnvSet(environment.DefaultCompatibilityVersion(), utilfeature.DefaultFeatureGate.Enabled(features.StrictCostEnforcementForWebhooks))),
106115
}, nil
107116
}
108117

@@ -228,7 +237,7 @@ func (a *Webhook) ShouldCallHook(ctx context.Context, h webhook.WebhookAccessor,
228237
return nil, apierrors.NewInternalError(err)
229238
}
230239

231-
matcher := h.GetCompiledMatcher(a.filterCompiler)
240+
matcher := h.GetCompiledMatcher(filterCompiler)
232241
matchResult := matcher.Match(ctx, versionedAttr, nil, a.authorizer)
233242

234243
if matchResult.Error != nil {

0 commit comments

Comments
 (0)