| 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 |
"context" |
| 21 |
celgo "github.com/google/cel-go/cel" |
| 22 |
"io" |
| 23 |
|
| 24 |
"k8s.io/api/admissionregistration/v1alpha1" |
| 25 |
corev1 "k8s.io/api/core/v1" |
| 26 |
"k8s.io/apimachinery/pkg/api/meta" |
| 27 |
"k8s.io/apimachinery/pkg/runtime" |
| 28 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 29 |
"k8s.io/apimachinery/pkg/util/managedfields" |
| 30 |
"k8s.io/apiserver/pkg/admission" |
| 31 |
"k8s.io/apiserver/pkg/admission/plugin/cel" |
| 32 |
"k8s.io/apiserver/pkg/admission/plugin/policy/generic" |
| 33 |
"k8s.io/apiserver/pkg/admission/plugin/policy/matching" |
| 34 |
"k8s.io/apiserver/pkg/admission/plugin/policy/mutating/patch" |
| 35 |
"k8s.io/apiserver/pkg/admission/plugin/webhook/matchconditions" |
| 36 |
"k8s.io/apiserver/pkg/authorization/authorizer" |
| 37 |
"k8s.io/apiserver/pkg/features" |
| 38 |
"k8s.io/client-go/dynamic" |
| 39 |
"k8s.io/client-go/informers" |
| 40 |
"k8s.io/client-go/kubernetes" |
| 41 |
"k8s.io/component-base/featuregate" |
| 42 |
) |
| 43 |
|
| 44 |
const ( |
| 45 |
// PluginName indicates the name of admission plug-in |
| 46 |
PluginName = "MutatingAdmissionPolicy" |
| 47 |
) |
| 48 |
|
| 49 |
// Register registers a plugin |
| 50 |
func Register(plugins *admission.Plugins) { |
| 51 |
plugins.Register(PluginName, func(configFile io.Reader) (admission.Interface, error) { |
| 52 |
return NewPlugin(configFile), nil |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
type Policy = v1alpha1.MutatingAdmissionPolicy |
| 57 |
type PolicyBinding = v1alpha1.MutatingAdmissionPolicyBinding |
| 58 |
type PolicyMutation = v1alpha1.Mutation |
| 59 |
type PolicyHook = generic.PolicyHook[*Policy, *PolicyBinding, PolicyEvaluator] |
| 60 |
|
| 61 |
type Mutator struct { |
| 62 |
} |
| 63 |
type MutationEvaluationFunc func( |
| 64 |
ctx context.Context, |
| 65 |
matchedResource schema.GroupVersionResource, |
| 66 |
versionedAttr *admission.VersionedAttributes, |
| 67 |
o admission.ObjectInterfaces, |
| 68 |
versionedParams runtime.Object, |
| 69 |
namespace *corev1.Namespace, |
| 70 |
typeConverter managedfields.TypeConverter, |
| 71 |
runtimeCELCostBudget int64, |
| 72 |
authorizer authorizer.Authorizer, |
| 73 |
) (runtime.Object, error) |
| 74 |
|
| 75 |
type PolicyEvaluator struct { |
| 76 |
Matcher matchconditions.Matcher |
| 77 |
Mutators []patch.Patcher |
| 78 |
CompositionEnv *cel.CompositionEnv |
| 79 |
Error error |
| 80 |
} |
| 81 |
|
| 82 |
// Plugin is an implementation of admission.Interface. |
| 83 |
type Plugin struct { |
| 84 |
*generic.Plugin[PolicyHook] |
| 85 |
} |
| 86 |
|
| 87 |
var _ admission.Interface = &Plugin{} |
| 88 |
var _ admission.MutationInterface = &Plugin{} |
| 89 |
|
| 90 |
// NewPlugin returns a generic admission webhook plugin. |
| 91 |
func NewPlugin(_ io.Reader) *Plugin { |
| 92 |
// There is no request body to mutate for DELETE, so this plugin never handles that operation. |
| 93 |
handler := admission.NewHandler(admission.Create, admission.Update, admission.Connect) |
| 94 |
res := &Plugin{} |
| 95 |
res.Plugin = generic.NewPlugin( |
| 96 |
handler, |
| 97 |
func(f informers.SharedInformerFactory, client kubernetes.Interface, dynamicClient dynamic.Interface, restMapper meta.RESTMapper) generic.Source[PolicyHook] { |
| 98 |
return generic.NewPolicySource( |
| 99 |
f.Admissionregistration().V1alpha1().MutatingAdmissionPolicies().Informer(), |
| 100 |
f.Admissionregistration().V1alpha1().MutatingAdmissionPolicyBindings().Informer(), |
| 101 |
NewMutatingAdmissionPolicyAccessor, |
| 102 |
NewMutatingAdmissionPolicyBindingAccessor, |
| 103 |
compilePolicy, |
| 104 |
//!TODO: Create a way to share param informers between |
| 105 |
// mutating/validating plugins |
| 106 |
f, |
| 107 |
dynamicClient, |
| 108 |
restMapper, |
| 109 |
) |
| 110 |
}, |
| 111 |
func(a authorizer.Authorizer, m *matching.Matcher, client kubernetes.Interface) generic.Dispatcher[PolicyHook] { |
| 112 |
return NewDispatcher(a, m, patch.NewTypeConverterManager(nil, client.Discovery().OpenAPIV3())) |
| 113 |
}, |
| 114 |
) |
| 115 |
return res |
| 116 |
} |
| 117 |
|
| 118 |
// Admit makes an admission decision based on the request attributes. |
| 119 |
func (a *Plugin) Admit(ctx context.Context, attr admission.Attributes, o admission.ObjectInterfaces) error { |
| 120 |
return a.Plugin.Dispatch(ctx, attr, o) |
| 121 |
} |
| 122 |
|
| 123 |
func (a *Plugin) InspectFeatureGates(featureGates featuregate.FeatureGate) { |
| 124 |
a.Plugin.SetEnabled(featureGates.Enabled(features.MutatingAdmissionPolicy)) |
| 125 |
} |
| 126 |
|
| 127 |
// Variable is a named expression for composition. |
| 128 |
type Variable struct { |
| 129 |
Name string |
| 130 |
Expression string |
| 131 |
} |
| 132 |
|
| 133 |
func (v *Variable) GetExpression() string { |
| 134 |
return v.Expression |
| 135 |
} |
| 136 |
|
| 137 |
func (v *Variable) ReturnTypes() []*celgo.Type { |
| 138 |
return []*celgo.Type{celgo.AnyType, celgo.DynType} |
| 139 |
} |
| 140 |
|
| 141 |
func (v *Variable) GetName() string { |
| 142 |
return v.Name |
| 143 |
} |
| 144 |
|
| 145 |
func convertv1alpha1Variables(variables []v1alpha1.Variable) []cel.NamedExpressionAccessor { |
| 146 |
namedExpressions := make([]cel.NamedExpressionAccessor, len(variables)) |
| 147 |
for i, variable := range variables { |
| 148 |
namedExpressions[i] = &Variable{Name: variable.Name, Expression: variable.Expression} |
| 149 |
} |
| 150 |
return namedExpressions |
| 151 |
} |