| 1 |
/* |
| 2 |
Copyright 2017 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 handlers |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"net/http" |
| 23 |
"time" |
| 24 |
|
| 25 |
"go.opentelemetry.io/otel/attribute" |
| 26 |
|
| 27 |
"k8s.io/apimachinery/pkg/api/errors" |
| 28 |
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" |
| 29 |
metainternalversionscheme "k8s.io/apimachinery/pkg/apis/meta/internalversion/scheme" |
| 30 |
metainternalversionvalidation "k8s.io/apimachinery/pkg/apis/meta/internalversion/validation" |
| 31 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 32 |
"k8s.io/apimachinery/pkg/apis/meta/v1/validation" |
| 33 |
"k8s.io/apimachinery/pkg/fields" |
| 34 |
"k8s.io/apimachinery/pkg/labels" |
| 35 |
"k8s.io/apimachinery/pkg/runtime" |
| 36 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 37 |
"k8s.io/apimachinery/pkg/util/validation/field" |
| 38 |
"k8s.io/apiserver/pkg/admission" |
| 39 |
"k8s.io/apiserver/pkg/audit" |
| 40 |
"k8s.io/apiserver/pkg/authorization/authorizer" |
| 41 |
"k8s.io/apiserver/pkg/endpoints/handlers/finisher" |
| 42 |
requestmetrics "k8s.io/apiserver/pkg/endpoints/handlers/metrics" |
| 43 |
"k8s.io/apiserver/pkg/endpoints/handlers/negotiation" |
| 44 |
"k8s.io/apiserver/pkg/endpoints/request" |
| 45 |
"k8s.io/apiserver/pkg/features" |
| 46 |
"k8s.io/apiserver/pkg/registry/rest" |
| 47 |
"k8s.io/apiserver/pkg/util/apihelpers" |
| 48 |
"k8s.io/apiserver/pkg/util/dryrun" |
| 49 |
utilfeature "k8s.io/apiserver/pkg/util/feature" |
| 50 |
"k8s.io/component-base/tracing" |
| 51 |
|
| 52 |
"k8s.io/klog/v2" |
| 53 |
"k8s.io/utils/ptr" |
| 54 |
) |
| 55 |
|
| 56 |
// DeleteResource returns a function that will handle a resource deletion |
| 57 |
// TODO admission here becomes solely validating admission |
| 58 |
func DeleteResource(r rest.GracefulDeleter, allowsOptions bool, scope *RequestScope, admit admission.Interface) http.HandlerFunc { |
| 59 |
return func(w http.ResponseWriter, req *http.Request) { |
| 60 |
ctx := req.Context() |
| 61 |
// For performance tracking purposes. |
| 62 |
ctx, span := tracing.Start(ctx, "Delete", traceFields(req)...) |
| 63 |
req = req.WithContext(ctx) |
| 64 |
defer span.End(500 * time.Millisecond) |
| 65 |
|
| 66 |
namespace, name, err := scope.Namer.Name(req) |
| 67 |
if err != nil { |
| 68 |
scope.err(err, w, req) |
| 69 |
return |
| 70 |
} |
| 71 |
|
| 72 |
// enforce a timeout of at most requestTimeoutUpperBound (34s) or less if the user-provided |
| 73 |
// timeout inside the parent context is lower than requestTimeoutUpperBound. |
| 74 |
ctx, cancel := context.WithTimeout(ctx, requestTimeoutUpperBound) |
| 75 |
defer cancel() |
| 76 |
|
| 77 |
ctx = request.WithNamespace(ctx, namespace) |
| 78 |
admit = admission.WithAudit(admit) |
| 79 |
|
| 80 |
outputMediaType, _, err := negotiation.NegotiateOutputMediaType(req, scope.Serializer, scope) |
| 81 |
if err != nil { |
| 82 |
scope.err(err, w, req) |
| 83 |
return |
| 84 |
} |
| 85 |
|
| 86 |
options := &metav1.DeleteOptions{} |
| 87 |
if allowsOptions { |
| 88 |
body, err := limitedReadBodyWithRecordMetric(ctx, req, scope.MaxRequestBodyBytes, scope.Resource.GroupResource().String(), requestmetrics.Delete) |
| 89 |
if err != nil { |
| 90 |
span.AddEvent("limitedReadBody failed", attribute.Int("len", len(body)), attribute.String("err", err.Error())) |
| 91 |
scope.err(err, w, req) |
| 92 |
return |
| 93 |
} |
| 94 |
span.AddEvent("limitedReadBody succeeded", attribute.Int("len", len(body))) |
| 95 |
if len(body) > 0 { |
| 96 |
s, err := negotiation.NegotiateInputSerializer(req, false, apihelpers.GetMetaInternalVersionCodecs()) |
| 97 |
if err != nil { |
| 98 |
scope.err(err, w, req) |
| 99 |
return |
| 100 |
} |
| 101 |
// For backwards compatibility, we need to allow existing clients to submit per group DeleteOptions |
| 102 |
// It is also allowed to pass a body with meta.k8s.io/v1.DeleteOptions |
| 103 |
defaultGVK := scope.MetaGroupVersion.WithKind("DeleteOptions") |
| 104 |
obj, gvk, err := apihelpers.GetMetaInternalVersionCodecs().DecoderToVersion(s.Serializer, defaultGVK.GroupVersion()).Decode(body, &defaultGVK, options) |
| 105 |
if err != nil { |
| 106 |
scope.err(err, w, req) |
| 107 |
return |
| 108 |
} |
| 109 |
if obj != options { |
| 110 |
scope.err(fmt.Errorf("decoded object cannot be converted to DeleteOptions"), w, req) |
| 111 |
return |
| 112 |
} |
| 113 |
span.AddEvent("Decoded delete options") |
| 114 |
|
| 115 |
objGV := gvk.GroupVersion() |
| 116 |
audit.LogRequestObject(req.Context(), obj, objGV, scope.Resource, scope.Subresource, apihelpers.GetMetaInternalVersionCodecs()) |
| 117 |
span.AddEvent("Recorded the audit event") |
| 118 |
} else { |
| 119 |
if err := metainternalversionscheme.ParameterCodec.DecodeParameters(req.URL.Query(), scope.MetaGroupVersion, options); err != nil { |
| 120 |
err = errors.NewBadRequest(err.Error()) |
| 121 |
scope.err(err, w, req) |
| 122 |
return |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
if !utilfeature.DefaultFeatureGate.Enabled(features.AllowUnsafeMalformedObjectDeletion) && options != nil { |
| 127 |
options.IgnoreStoreReadErrorWithClusterBreakingPotential = nil |
| 128 |
} |
| 129 |
if errs := validation.ValidateDeleteOptions(options); len(errs) > 0 { |
| 130 |
err := errors.NewInvalid(schema.GroupKind{Group: metav1.GroupName, Kind: "DeleteOptions"}, "", errs) |
| 131 |
scope.err(err, w, req) |
| 132 |
return |
| 133 |
} |
| 134 |
options.TypeMeta.SetGroupVersionKind(metav1.SchemeGroupVersion.WithKind("DeleteOptions")) |
| 135 |
|
| 136 |
userInfo, _ := request.UserFrom(ctx) |
| 137 |
staticAdmissionAttrs := admission.NewAttributesRecord(nil, nil, scope.Kind, namespace, name, scope.Resource, scope.Subresource, admission.Delete, options, dryrun.IsDryRun(options.DryRun), userInfo) |
| 138 |
|
| 139 |
if utilfeature.DefaultFeatureGate.Enabled(features.AllowUnsafeMalformedObjectDeletion) { |
| 140 |
if options != nil && ptr.Deref(options.IgnoreStoreReadErrorWithClusterBreakingPotential, false) { |
| 141 |
// let's make sure that the audit will reflect that this delete request |
| 142 |
// was tried with ignoreStoreReadErrorWithClusterBreakingPotential enabled |
| 143 |
audit.AddAuditAnnotation(ctx, "apiserver.k8s.io/unsafe-delete-ignore-read-error", "") |
| 144 |
|
| 145 |
p, ok := r.(rest.CorruptObjectDeleterProvider) |
| 146 |
if !ok || p.GetCorruptObjDeleter() == nil { |
| 147 |
// this is a developer error |
| 148 |
scope.err(errors.NewInternalError(fmt.Errorf("no unsafe deleter provided, can not honor ignoreStoreReadErrorWithClusterBreakingPotential")), w, req) |
| 149 |
return |
| 150 |
} |
| 151 |
if scope.Authorizer == nil { |
| 152 |
scope.err(errors.NewInternalError(fmt.Errorf("no authorizer provided, unable to authorize unsafe delete")), w, req) |
| 153 |
return |
| 154 |
} |
| 155 |
if err := authorizeUnsafeDelete(ctx, staticAdmissionAttrs, scope.Authorizer); err != nil { |
| 156 |
scope.err(err, w, req) |
| 157 |
return |
| 158 |
} |
| 159 |
|
| 160 |
r = p.GetCorruptObjDeleter() |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
span.AddEvent("About to delete object from database") |
| 165 |
wasDeleted := true |
| 166 |
result, err := finisher.FinishRequest(ctx, func() (runtime.Object, error) { |
| 167 |
obj, deleted, err := r.Delete(ctx, name, rest.AdmissionToValidateObjectDeleteFunc(admit, staticAdmissionAttrs, scope), options) |
| 168 |
wasDeleted = deleted |
| 169 |
return obj, err |
| 170 |
}) |
| 171 |
if err != nil { |
| 172 |
scope.err(err, w, req) |
| 173 |
return |
| 174 |
} |
| 175 |
span.AddEvent("Object deleted from database") |
| 176 |
|
| 177 |
status := http.StatusOK |
| 178 |
// Return http.StatusAccepted if the resource was not deleted immediately and |
| 179 |
// user requested cascading deletion by setting OrphanDependents=false. |
| 180 |
// Note: We want to do this always if resource was not deleted immediately, but |
| 181 |
// that will break existing clients. |
| 182 |
// Other cases where resource is not instantly deleted are: namespace deletion |
| 183 |
// and pod graceful deletion. |
| 184 |
//nolint:staticcheck // SA1019 backwards compatibility |
| 185 |
//nolint: staticcheck |
| 186 |
if !wasDeleted && options.OrphanDependents != nil && !*options.OrphanDependents { |
| 187 |
status = http.StatusAccepted |
| 188 |
} |
| 189 |
// if the rest.Deleter returns a nil object, fill out a status. Callers may return a valid |
| 190 |
// object with the response. |
| 191 |
if result == nil { |
| 192 |
result = &metav1.Status{ |
| 193 |
Status: metav1.StatusSuccess, |
| 194 |
Code: int32(status), |
| 195 |
Details: &metav1.StatusDetails{ |
| 196 |
Name: name, |
| 197 |
Kind: scope.Kind.Kind, |
| 198 |
}, |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
span.AddEvent("About to write a response") |
| 203 |
defer span.AddEvent("Writing http response done") |
| 204 |
transformResponseObject(ctx, scope, req, w, status, outputMediaType, result) |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
// DeleteCollection returns a function that will handle a collection deletion |
| 209 |
func DeleteCollection(r rest.CollectionDeleter, checkBody bool, scope *RequestScope, admit admission.Interface) http.HandlerFunc { |
| 210 |
return func(w http.ResponseWriter, req *http.Request) { |
| 211 |
ctx := req.Context() |
| 212 |
ctx, span := tracing.Start(ctx, "Delete", traceFields(req)...) |
| 213 |
req = req.WithContext(ctx) |
| 214 |
defer span.End(500 * time.Millisecond) |
| 215 |
|
| 216 |
namespace, err := scope.Namer.Namespace(req) |
| 217 |
if err != nil { |
| 218 |
scope.err(err, w, req) |
| 219 |
return |
| 220 |
} |
| 221 |
|
| 222 |
// DELETECOLLECTION can be a lengthy operation, |
| 223 |
// we should not impose any 34s timeout here. |
| 224 |
// NOTE: This is similar to LIST which does not enforce a 34s timeout. |
| 225 |
ctx = request.WithNamespace(ctx, namespace) |
| 226 |
|
| 227 |
outputMediaType, _, err := negotiation.NegotiateOutputMediaType(req, scope.Serializer, scope) |
| 228 |
if err != nil { |
| 229 |
scope.err(err, w, req) |
| 230 |
return |
| 231 |
} |
| 232 |
|
| 233 |
listOptions := metainternalversion.ListOptions{} |
| 234 |
if err := metainternalversionscheme.ParameterCodec.DecodeParameters(req.URL.Query(), scope.MetaGroupVersion, &listOptions); err != nil { |
| 235 |
err = errors.NewBadRequest(err.Error()) |
| 236 |
scope.err(err, w, req) |
| 237 |
return |
| 238 |
} |
| 239 |
|
| 240 |
metainternalversion.SetListOptionsDefaults(&listOptions, utilfeature.DefaultFeatureGate.Enabled(features.WatchList)) |
| 241 |
if errs := metainternalversionvalidation.ValidateListOptions(&listOptions, utilfeature.DefaultFeatureGate.Enabled(features.WatchList)); len(errs) > 0 { |
| 242 |
err := errors.NewInvalid(schema.GroupKind{Group: metav1.GroupName, Kind: "ListOptions"}, "", errs) |
| 243 |
scope.err(err, w, req) |
| 244 |
return |
| 245 |
} |
| 246 |
|
| 247 |
// transform fields |
| 248 |
// TODO: DecodeParametersInto should do this. |
| 249 |
if listOptions.FieldSelector != nil { |
| 250 |
fn := func(label, value string) (newLabel, newValue string, err error) { |
| 251 |
return scope.Convertor.ConvertFieldLabel(scope.Kind, label, value) |
| 252 |
} |
| 253 |
if listOptions.FieldSelector, err = listOptions.FieldSelector.Transform(fn); err != nil { |
| 254 |
// TODO: allow bad request to set field causes based on query parameters |
| 255 |
err = errors.NewBadRequest(err.Error()) |
| 256 |
scope.err(err, w, req) |
| 257 |
return |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
options := &metav1.DeleteOptions{} |
| 262 |
if checkBody { |
| 263 |
body, err := limitedReadBodyWithRecordMetric(ctx, req, scope.MaxRequestBodyBytes, scope.Resource.GroupResource().String(), requestmetrics.DeleteCollection) |
| 264 |
if err != nil { |
| 265 |
span.AddEvent("limitedReadBody failed", attribute.Int("len", len(body)), attribute.String("err", err.Error())) |
| 266 |
scope.err(err, w, req) |
| 267 |
return |
| 268 |
} |
| 269 |
span.AddEvent("limitedReadBody succeeded", attribute.Int("len", len(body))) |
| 270 |
if len(body) > 0 { |
| 271 |
s, err := negotiation.NegotiateInputSerializer(req, false, apihelpers.GetMetaInternalVersionCodecs()) |
| 272 |
if err != nil { |
| 273 |
scope.err(err, w, req) |
| 274 |
return |
| 275 |
} |
| 276 |
// For backwards compatibility, we need to allow existing clients to submit per group DeleteOptions |
| 277 |
// It is also allowed to pass a body with meta.k8s.io/v1.DeleteOptions |
| 278 |
defaultGVK := scope.MetaGroupVersion.WithKind("DeleteOptions") |
| 279 |
obj, gvk, err := apihelpers.GetMetaInternalVersionCodecs().DecoderToVersion(s.Serializer, defaultGVK.GroupVersion()).Decode(body, &defaultGVK, options) |
| 280 |
if err != nil { |
| 281 |
scope.err(err, w, req) |
| 282 |
return |
| 283 |
} |
| 284 |
if obj != options { |
| 285 |
scope.err(fmt.Errorf("decoded object cannot be converted to DeleteOptions"), w, req) |
| 286 |
return |
| 287 |
} |
| 288 |
|
| 289 |
objGV := gvk.GroupVersion() |
| 290 |
audit.LogRequestObject(req.Context(), obj, objGV, scope.Resource, scope.Subresource, apihelpers.GetMetaInternalVersionCodecs()) |
| 291 |
} else { |
| 292 |
if err := metainternalversionscheme.ParameterCodec.DecodeParameters(req.URL.Query(), scope.MetaGroupVersion, options); err != nil { |
| 293 |
err = errors.NewBadRequest(err.Error()) |
| 294 |
scope.err(err, w, req) |
| 295 |
return |
| 296 |
} |
| 297 |
} |
| 298 |
} |
| 299 |
if !utilfeature.DefaultFeatureGate.Enabled(features.AllowUnsafeMalformedObjectDeletion) && options != nil { |
| 300 |
options.IgnoreStoreReadErrorWithClusterBreakingPotential = nil |
| 301 |
} |
| 302 |
if errs := validation.ValidateDeleteOptions(options); len(errs) > 0 { |
| 303 |
err := errors.NewInvalid(schema.GroupKind{Group: metav1.GroupName, Kind: "DeleteOptions"}, "", errs) |
| 304 |
scope.err(err, w, req) |
| 305 |
return |
| 306 |
} |
| 307 |
|
| 308 |
if utilfeature.DefaultFeatureGate.Enabled(features.AllowUnsafeMalformedObjectDeletion) { |
| 309 |
if options != nil && ptr.Deref(options.IgnoreStoreReadErrorWithClusterBreakingPotential, false) { |
| 310 |
fieldErrList := field.ErrorList{ |
| 311 |
field.Invalid(field.NewPath("ignoreStoreReadErrorWithClusterBreakingPotential"), true, "is not allowed with DELETECOLLECTION, try again after removing the option"), |
| 312 |
} |
| 313 |
err := errors.NewInvalid(schema.GroupKind{Group: metav1.GroupName, Kind: "DeleteOptions"}, "", fieldErrList) |
| 314 |
scope.err(err, w, req) |
| 315 |
return |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
options.TypeMeta.SetGroupVersionKind(metav1.SchemeGroupVersion.WithKind("DeleteOptions")) |
| 320 |
|
| 321 |
admit = admission.WithAudit(admit) |
| 322 |
userInfo, _ := request.UserFrom(ctx) |
| 323 |
staticAdmissionAttrs := admission.NewAttributesRecord(nil, nil, scope.Kind, namespace, "", scope.Resource, scope.Subresource, admission.Delete, options, dryrun.IsDryRun(options.DryRun), userInfo) |
| 324 |
result, err := finisher.FinishRequest(ctx, func() (runtime.Object, error) { |
| 325 |
return r.DeleteCollection(ctx, rest.AdmissionToValidateObjectDeleteFunc(admit, staticAdmissionAttrs, scope), options, &listOptions) |
| 326 |
}) |
| 327 |
if err != nil { |
| 328 |
scope.err(err, w, req) |
| 329 |
return |
| 330 |
} |
| 331 |
|
| 332 |
// if the rest.Deleter returns a nil object, fill out a status. Callers may return a valid |
| 333 |
// object with the response. |
| 334 |
if result == nil { |
| 335 |
result = &metav1.Status{ |
| 336 |
Status: metav1.StatusSuccess, |
| 337 |
Code: http.StatusOK, |
| 338 |
Details: &metav1.StatusDetails{ |
| 339 |
Kind: scope.Kind.Kind, |
| 340 |
}, |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
span.AddEvent("About to write a response") |
| 345 |
defer span.AddEvent("Writing http response done") |
| 346 |
transformResponseObject(ctx, scope, req, w, http.StatusOK, outputMediaType, result) |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
// authorizeUnsafeDelete ensures that the user has permission to do |
| 351 |
// 'unsafe-delete-ignore-read-errors' on the resource being deleted when |
| 352 |
// ignoreStoreReadErrorWithClusterBreakingPotential is enabled |
| 353 |
func authorizeUnsafeDelete(ctx context.Context, attr admission.Attributes, authz authorizer.Authorizer) (err error) { |
| 354 |
if attr.GetOperation() != admission.Delete || attr.GetOperationOptions() == nil { |
| 355 |
return nil |
| 356 |
} |
| 357 |
options, ok := attr.GetOperationOptions().(*metav1.DeleteOptions) |
| 358 |
if !ok { |
| 359 |
return errors.NewInternalError(fmt.Errorf("expected an option of type: %T, but got: %T", &metav1.DeleteOptions{}, attr.GetOperationOptions())) |
| 360 |
} |
| 361 |
if !ptr.Deref(options.IgnoreStoreReadErrorWithClusterBreakingPotential, false) { |
| 362 |
return nil |
| 363 |
} |
| 364 |
|
| 365 |
requestInfo, found := request.RequestInfoFrom(ctx) |
| 366 |
if !found { |
| 367 |
return admission.NewForbidden(attr, fmt.Errorf("no RequestInfo found in the context")) |
| 368 |
} |
| 369 |
if !requestInfo.IsResourceRequest || len(attr.GetSubresource()) > 0 { |
| 370 |
return admission.NewForbidden(attr, fmt.Errorf("ignoreStoreReadErrorWithClusterBreakingPotential delete option is not allowed on a subresource or non-resource request")) |
| 371 |
} |
| 372 |
|
| 373 |
// if we are here, IgnoreStoreReadErrorWithClusterBreakingPotential |
| 374 |
// is set to true in the delete options, the user must have permission |
| 375 |
// to do 'unsafe-delete-ignore-read-errors' on the given resource. |
| 376 |
record := authorizer.AttributesRecord{ |
| 377 |
User: attr.GetUserInfo(), |
| 378 |
Verb: "unsafe-delete-ignore-read-errors", |
| 379 |
Namespace: attr.GetNamespace(), |
| 380 |
Name: attr.GetName(), |
| 381 |
APIGroup: attr.GetResource().Group, |
| 382 |
APIVersion: attr.GetResource().Version, |
| 383 |
Resource: attr.GetResource().Resource, |
| 384 |
ResourceRequest: true, |
| 385 |
} |
| 386 |
// TODO: can't use ResourceAttributesFrom from k8s.io/kubernetes/pkg/registry/authorization/util |
| 387 |
// due to prevent staging --> k8s.io/kubernetes dep issue |
| 388 |
if utilfeature.DefaultFeatureGate.Enabled(features.AuthorizeWithSelectors) { |
| 389 |
if len(requestInfo.FieldSelector) > 0 { |
| 390 |
fieldSelector, err := fields.ParseSelector(requestInfo.FieldSelector) |
| 391 |
if err != nil { |
| 392 |
record.FieldSelectorRequirements, record.FieldSelectorParsingErr = nil, err |
| 393 |
} else { |
| 394 |
if requirements := fieldSelector.Requirements(); len(requirements) > 0 { |
| 395 |
record.FieldSelectorRequirements, record.FieldSelectorParsingErr = fieldSelector.Requirements(), nil |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
if len(requestInfo.LabelSelector) > 0 { |
| 400 |
labelSelector, err := labels.Parse(requestInfo.LabelSelector) |
| 401 |
if err != nil { |
| 402 |
record.LabelSelectorRequirements, record.LabelSelectorParsingErr = nil, err |
| 403 |
} else { |
| 404 |
if requirements, _ /*selectable*/ := labelSelector.Requirements(); len(requirements) > 0 { |
| 405 |
record.LabelSelectorRequirements, record.LabelSelectorParsingErr = requirements, nil |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
decision, reason, err := authz.Authorize(ctx, record) |
| 412 |
if err != nil { |
| 413 |
err = fmt.Errorf("error while checking permission for %q, %w", record.Verb, err) |
| 414 |
klog.FromContext(ctx).V(1).Error(err, "failed to authorize") |
| 415 |
return admission.NewForbidden(attr, err) |
| 416 |
} |
| 417 |
if decision == authorizer.DecisionAllow { |
| 418 |
return nil |
| 419 |
} |
| 420 |
|
| 421 |
return admission.NewForbidden(attr, fmt.Errorf("not permitted to do %q, reason: %s", record.Verb, reason)) |
| 422 |
} |