| 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 |
"errors" |
| 22 |
"fmt" |
| 23 |
|
| 24 |
"k8s.io/api/admissionregistration/v1alpha1" |
| 25 |
v1 "k8s.io/api/core/v1" |
| 26 |
apiequality "k8s.io/apimachinery/pkg/api/equality" |
| 27 |
k8serrors "k8s.io/apimachinery/pkg/api/errors" |
| 28 |
"k8s.io/apimachinery/pkg/api/meta" |
| 29 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 |
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 31 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 32 |
"k8s.io/apimachinery/pkg/types" |
| 33 |
"k8s.io/apiserver/pkg/admission" |
| 34 |
admissionauthorizer "k8s.io/apiserver/pkg/admission/plugin/authorizer" |
| 35 |
"k8s.io/apiserver/pkg/admission/plugin/cel" |
| 36 |
"k8s.io/apiserver/pkg/admission/plugin/policy/generic" |
| 37 |
"k8s.io/apiserver/pkg/admission/plugin/policy/matching" |
| 38 |
"k8s.io/apiserver/pkg/admission/plugin/policy/mutating/patch" |
| 39 |
webhookgeneric "k8s.io/apiserver/pkg/admission/plugin/webhook/generic" |
| 40 |
celconfig "k8s.io/apiserver/pkg/apis/cel" |
| 41 |
"k8s.io/apiserver/pkg/authorization/authorizer" |
| 42 |
) |
| 43 |
|
| 44 |
func NewDispatcher(a authorizer.Authorizer, m *matching.Matcher, tcm patch.TypeConverterManager) generic.Dispatcher[PolicyHook] { |
| 45 |
res := &dispatcher{ |
| 46 |
matcher: m, |
| 47 |
authz: a, |
| 48 |
typeConverterManager: tcm, |
| 49 |
} |
| 50 |
res.Dispatcher = generic.NewPolicyDispatcher[*Policy, *PolicyBinding, PolicyEvaluator]( |
| 51 |
NewMutatingAdmissionPolicyAccessor, |
| 52 |
NewMutatingAdmissionPolicyBindingAccessor, |
| 53 |
m, |
| 54 |
res.dispatchInvocations, |
| 55 |
) |
| 56 |
return res |
| 57 |
} |
| 58 |
|
| 59 |
type dispatcher struct { |
| 60 |
matcher *matching.Matcher |
| 61 |
authz authorizer.Authorizer |
| 62 |
typeConverterManager patch.TypeConverterManager |
| 63 |
generic.Dispatcher[PolicyHook] |
| 64 |
} |
| 65 |
|
| 66 |
func (d *dispatcher) Start(ctx context.Context) error { |
| 67 |
go d.typeConverterManager.Run(ctx) |
| 68 |
return d.Dispatcher.Start(ctx) |
| 69 |
} |
| 70 |
|
| 71 |
func (d *dispatcher) dispatchInvocations( |
| 72 |
ctx context.Context, |
| 73 |
a admission.Attributes, |
| 74 |
o admission.ObjectInterfaces, |
| 75 |
versionedAttributes webhookgeneric.VersionedAttributeAccessor, |
| 76 |
invocations []generic.PolicyInvocation[*Policy, *PolicyBinding, PolicyEvaluator], |
| 77 |
) ([]generic.PolicyError, *k8serrors.StatusError) { |
| 78 |
var lastVersionedAttr *admission.VersionedAttributes |
| 79 |
|
| 80 |
reinvokeCtx := a.GetReinvocationContext() |
| 81 |
var policyReinvokeCtx *policyReinvokeContext |
| 82 |
if v := reinvokeCtx.Value(PluginName); v != nil { |
| 83 |
policyReinvokeCtx = v.(*policyReinvokeContext) |
| 84 |
} else { |
| 85 |
policyReinvokeCtx = &policyReinvokeContext{} |
| 86 |
reinvokeCtx.SetValue(PluginName, policyReinvokeCtx) |
| 87 |
} |
| 88 |
|
| 89 |
if reinvokeCtx.IsReinvoke() && policyReinvokeCtx.IsOutputChangedSinceLastPolicyInvocation(a.GetObject()) { |
| 90 |
// If the object has changed, we know the in-tree plugin re-invocations have mutated the object, |
| 91 |
// and we need to reinvoke all eligible policies. |
| 92 |
policyReinvokeCtx.RequireReinvokingPreviouslyInvokedPlugins() |
| 93 |
} |
| 94 |
defer func() { |
| 95 |
policyReinvokeCtx.SetLastPolicyInvocationOutput(a.GetObject()) |
| 96 |
}() |
| 97 |
|
| 98 |
var policyErrors []generic.PolicyError |
| 99 |
addConfigError := func(err error, invocation generic.PolicyInvocation[*Policy, *PolicyBinding, PolicyEvaluator], reason metav1.StatusReason) { |
| 100 |
policyErrors = append(policyErrors, generic.PolicyError{ |
| 101 |
Message: err, |
| 102 |
Policy: NewMutatingAdmissionPolicyAccessor(invocation.Policy), |
| 103 |
Binding: NewMutatingAdmissionPolicyBindingAccessor(invocation.Binding), |
| 104 |
Reason: reason, |
| 105 |
}) |
| 106 |
} |
| 107 |
|
| 108 |
// There is at least one invocation to invoke. Make sure we have a namespace |
| 109 |
// object if the incoming object is not cluster scoped to pass into the evaluator. |
| 110 |
var namespace *v1.Namespace |
| 111 |
var err error |
| 112 |
namespaceName := a.GetNamespace() |
| 113 |
|
| 114 |
// Special case, the namespace object has the namespace of itself (maybe a bug). |
| 115 |
// unset it if the incoming object is a namespace |
| 116 |
if gvk := a.GetKind(); gvk.Kind == "Namespace" && gvk.Version == "v1" && gvk.Group == "" { |
| 117 |
namespaceName = "" |
| 118 |
} |
| 119 |
|
| 120 |
// if it is cluster scoped, namespaceName will be empty |
| 121 |
// Otherwise, get the Namespace resource. |
| 122 |
if namespaceName != "" { |
| 123 |
namespace, err = d.matcher.GetNamespace(namespaceName) |
| 124 |
if err != nil { |
| 125 |
return nil, k8serrors.NewNotFound(schema.GroupResource{Group: "", Resource: "namespaces"}, namespaceName) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
authz := admissionauthorizer.NewCachingAuthorizer(d.authz) |
| 130 |
|
| 131 |
// Should loop through invocations, handling possible error and invoking |
| 132 |
// evaluator to apply patch, also should handle re-invocations |
| 133 |
for _, invocation := range invocations { |
| 134 |
if invocation.Evaluator.CompositionEnv != nil { |
| 135 |
ctx = invocation.Evaluator.CompositionEnv.CreateContext(ctx) |
| 136 |
} |
| 137 |
if len(invocation.Evaluator.Mutators) != len(invocation.Policy.Spec.Mutations) { |
| 138 |
// This would be a bug. The compiler should always return exactly as |
| 139 |
// many evaluators as there are mutations |
| 140 |
return nil, k8serrors.NewInternalError(fmt.Errorf("expected %v compiled evaluators for policy %v, got %v", |
| 141 |
len(invocation.Policy.Spec.Mutations), invocation.Policy.Name, len(invocation.Evaluator.Mutators))) |
| 142 |
} |
| 143 |
|
| 144 |
versionedAttr, err := versionedAttributes.VersionedAttribute(invocation.Kind) |
| 145 |
if err != nil { |
| 146 |
// This should never happen, we pre-warm versoined attribute |
| 147 |
// accessors before starting the dispatcher |
| 148 |
return nil, k8serrors.NewInternalError(err) |
| 149 |
} |
| 150 |
|
| 151 |
if invocation.Evaluator.Matcher != nil { |
| 152 |
matchResults := invocation.Evaluator.Matcher.Match(ctx, versionedAttr, invocation.Param, authz) |
| 153 |
if matchResults.Error != nil { |
| 154 |
addConfigError(matchResults.Error, invocation, metav1.StatusReasonInvalid) |
| 155 |
continue |
| 156 |
} |
| 157 |
|
| 158 |
// if preconditions are not met, then skip mutations |
| 159 |
if !matchResults.Matches { |
| 160 |
continue |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
invocationKey, invocationKeyErr := keyFor(invocation) |
| 165 |
if invocationKeyErr != nil { |
| 166 |
// This should never happen. It occurs if there is a programming |
| 167 |
// error causing the Param not to be a valid object. |
| 168 |
return nil, k8serrors.NewInternalError(invocationKeyErr) |
| 169 |
} |
| 170 |
if reinvokeCtx.IsReinvoke() && !policyReinvokeCtx.ShouldReinvoke(invocationKey) { |
| 171 |
continue |
| 172 |
} |
| 173 |
|
| 174 |
objectBeforeMutations := versionedAttr.VersionedObject |
| 175 |
// Mutations for a single invocation of a MutatingAdmissionPolicy are evaluated |
| 176 |
// in order. |
| 177 |
for mutationIndex := range invocation.Policy.Spec.Mutations { |
| 178 |
lastVersionedAttr = versionedAttr |
| 179 |
if versionedAttr.VersionedObject == nil { // Do not call patchers if there is no object to patch. |
| 180 |
continue |
| 181 |
} |
| 182 |
|
| 183 |
patcher := invocation.Evaluator.Mutators[mutationIndex] |
| 184 |
optionalVariables := cel.OptionalVariableBindings{VersionedParams: invocation.Param, Authorizer: authz} |
| 185 |
err = d.dispatchOne(ctx, patcher, o, versionedAttr, namespace, invocation.Resource, optionalVariables) |
| 186 |
if err != nil { |
| 187 |
var statusError *k8serrors.StatusError |
| 188 |
if errors.As(err, &statusError) { |
| 189 |
return nil, statusError |
| 190 |
} |
| 191 |
|
| 192 |
addConfigError(err, invocation, metav1.StatusReasonInvalid) |
| 193 |
continue |
| 194 |
} |
| 195 |
} |
| 196 |
if !apiequality.Semantic.DeepEqual(objectBeforeMutations, versionedAttr.VersionedObject) { |
| 197 |
// The mutation has changed the object. Prepare to reinvoke all previous mutations that are eligible for re-invocation. |
| 198 |
policyReinvokeCtx.RequireReinvokingPreviouslyInvokedPlugins() |
| 199 |
reinvokeCtx.SetShouldReinvoke() |
| 200 |
} |
| 201 |
if invocation.Policy.Spec.ReinvocationPolicy == v1alpha1.IfNeededReinvocationPolicy { |
| 202 |
policyReinvokeCtx.AddReinvocablePolicyToPreviouslyInvoked(invocationKey) |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
if lastVersionedAttr != nil && lastVersionedAttr.VersionedObject != nil && lastVersionedAttr.Dirty { |
| 207 |
policyReinvokeCtx.RequireReinvokingPreviouslyInvokedPlugins() |
| 208 |
reinvokeCtx.SetShouldReinvoke() |
| 209 |
if err := o.GetObjectConvertor().Convert(lastVersionedAttr.VersionedObject, lastVersionedAttr.Attributes.GetObject(), nil); err != nil { |
| 210 |
return nil, k8serrors.NewInternalError(fmt.Errorf("failed to convert object: %w", err)) |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return policyErrors, nil |
| 215 |
} |
| 216 |
|
| 217 |
func (d *dispatcher) dispatchOne( |
| 218 |
ctx context.Context, |
| 219 |
patcher patch.Patcher, |
| 220 |
o admission.ObjectInterfaces, |
| 221 |
versionedAttributes *admission.VersionedAttributes, |
| 222 |
namespace *v1.Namespace, |
| 223 |
resource schema.GroupVersionResource, |
| 224 |
optionalVariables cel.OptionalVariableBindings, |
| 225 |
) (err error) { |
| 226 |
if patcher == nil { |
| 227 |
// internal error. this should not happen |
| 228 |
return k8serrors.NewInternalError(fmt.Errorf("policy evaluator is nil")) |
| 229 |
} |
| 230 |
|
| 231 |
// Find type converter for the invoked Group-Version. |
| 232 |
typeConverter := d.typeConverterManager.GetTypeConverter(versionedAttributes.VersionedKind) |
| 233 |
if typeConverter == nil { |
| 234 |
// This can happen if the request is for a resource whose schema |
| 235 |
// has not been registered with the type converter manager. |
| 236 |
return k8serrors.NewServiceUnavailable(fmt.Sprintf("Resource kind %s not found. There can be a delay between when CustomResourceDefinitions are created and when they are available.", versionedAttributes.VersionedKind)) |
| 237 |
} |
| 238 |
|
| 239 |
patchRequest := patch.Request{ |
| 240 |
MatchedResource: resource, |
| 241 |
VersionedAttributes: versionedAttributes, |
| 242 |
ObjectInterfaces: o, |
| 243 |
OptionalVariables: optionalVariables, |
| 244 |
Namespace: namespace, |
| 245 |
TypeConverter: typeConverter, |
| 246 |
} |
| 247 |
newVersionedObject, err := patcher.Patch(ctx, patchRequest, celconfig.RuntimeCELCostBudget) |
| 248 |
if err != nil { |
| 249 |
return err |
| 250 |
} |
| 251 |
|
| 252 |
switch versionedAttributes.VersionedObject.(type) { |
| 253 |
case *unstructured.Unstructured: |
| 254 |
// No conversion needed before defaulting for the patch object if the admitted object is unstructured. |
| 255 |
default: |
| 256 |
// Before defaulting, if the admitted object is a typed object, convert unstructured patch result back to a typed object. |
| 257 |
newVersionedObject, err = o.GetObjectConvertor().ConvertToVersion(newVersionedObject, versionedAttributes.GetKind().GroupVersion()) |
| 258 |
if err != nil { |
| 259 |
return err |
| 260 |
} |
| 261 |
} |
| 262 |
o.GetObjectDefaulter().Default(newVersionedObject) |
| 263 |
|
| 264 |
versionedAttributes.Dirty = true |
| 265 |
versionedAttributes.VersionedObject = newVersionedObject |
| 266 |
return nil |
| 267 |
} |
| 268 |
|
| 269 |
func keyFor(invocation generic.PolicyInvocation[*Policy, *PolicyBinding, PolicyEvaluator]) (key, error) { |
| 270 |
var paramUID types.NamespacedName |
| 271 |
if invocation.Param != nil { |
| 272 |
paramAccessor, err := meta.Accessor(invocation.Param) |
| 273 |
if err != nil { |
| 274 |
// This should never happen, as the param should have been validated |
| 275 |
// before being passed to the plugin. |
| 276 |
return key{}, err |
| 277 |
} |
| 278 |
paramUID = types.NamespacedName{ |
| 279 |
Name: paramAccessor.GetName(), |
| 280 |
Namespace: paramAccessor.GetNamespace(), |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
return key{ |
| 285 |
PolicyUID: types.NamespacedName{ |
| 286 |
Name: invocation.Policy.GetName(), |
| 287 |
Namespace: invocation.Policy.GetNamespace(), |
| 288 |
}, |
| 289 |
BindingUID: types.NamespacedName{ |
| 290 |
Name: invocation.Binding.GetName(), |
| 291 |
Namespace: invocation.Binding.GetNamespace(), |
| 292 |
}, |
| 293 |
ParamUID: paramUID, |
| 294 |
}, nil |
| 295 |
} |