Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CEL Admission Plugin #112858

Merged
merged 5 commits into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions staging/src/k8s.io/apiserver/pkg/admission/plugin/cel/OWNERS
@@ -0,0 +1,10 @@
# See the OWNERS docs at https://go.k8s.io/owners

approvers:
- jpbetz
- cici37
- alexzielenski
reviewers:
- jpbetz
- cici37
- alexzielenski
103 changes: 103 additions & 0 deletions staging/src/k8s.io/apiserver/pkg/admission/plugin/cel/admission.go
@@ -0,0 +1,103 @@
/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cel

import (
"context"
"errors"
"fmt"
"io"
"time"

"k8s.io/apiserver/pkg/admission"
"k8s.io/client-go/tools/cache"
)

////////////////////////////////////////////////////////////////////////////////
// Plugin Definition
////////////////////////////////////////////////////////////////////////////////

// Definition for CEL admission plugin. This is the entry point into the
// CEL admission control system.
//
// Each plugin is asked to validate every object update.

const (
// PluginName indicates the name of admission plug-in
PluginName = "CEL"
)

// Register registers a plugin
func Register(plugins *admission.Plugins) {
alexzielenski marked this conversation as resolved.
Show resolved Hide resolved
plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
return NewPlugin()
})
}

////////////////////////////////////////////////////////////////////////////////
// Plugin Initialization & Dependency Injection
////////////////////////////////////////////////////////////////////////////////

type celAdmissionPlugin struct {
evaluator CELPolicyEvaluator
}

var _ WantsCELPolicyEvaluator = &celAdmissionPlugin{}
var _ admission.ValidationInterface = &celAdmissionPlugin{}

func NewPlugin() (*celAdmissionPlugin, error) {
result := &celAdmissionPlugin{}
return result, nil
}

func (c *celAdmissionPlugin) SetCELPolicyEvaluator(evaluator CELPolicyEvaluator) {
c.evaluator = evaluator
}

// Once clientset and informer factory are provided, creates and starts the
// admission controller
func (c *celAdmissionPlugin) ValidateInitialization() error {
if c.evaluator != nil {
return nil
}

return errors.New("CELPolicyEvaluator not injected")
}

////////////////////////////////////////////////////////////////////////////////
// admission.ValidationInterface
////////////////////////////////////////////////////////////////////////////////

func (c *celAdmissionPlugin) Handles(operation admission.Operation) bool {
return true
}

func (c *celAdmissionPlugin) Validate(
ctx context.Context,
a admission.Attributes,
o admission.ObjectInterfaces,
) (err error) {
alexzielenski marked this conversation as resolved.
Show resolved Hide resolved

deadlined, cancel := context.WithTimeout(ctx, 2*time.Second)
defer cancel()

if !cache.WaitForNamedCacheSync("cel-admission-plugin", deadlined.Done(), c.evaluator.HasSynced) {
return admission.NewForbidden(a, fmt.Errorf("not yet ready to handle request"))
}

return c.evaluator.Validate(ctx, a, o)
}