@@ -54,6 +54,16 @@ type request struct {
5454 obj * unstructured.Unstructured
5555 oldObj * unstructured.Unstructured
5656 op k8sadm.Operation
57+ gvr metav1.GroupVersionResource
58+ }
59+
60+ func (req request ) gr () schema.GroupResource {
61+ return schema.GroupResource {Group : req .gvr .Group , Resource : req .gvr .Resource }
62+ }
63+
64+ func (req request ) name () string {
65+ name := types.NamespacedName {Namespace : req .obj .GetNamespace (), Name : req .obj .GetName ()}
66+ return name .String ()
5767}
5868
5969func (v * Validator ) Handle (ctx context.Context , req admission.Request ) admission.Response {
@@ -137,9 +147,9 @@ func (v *Validator) handle(ctx context.Context, req *request) admission.Response
137147 }
138148
139149 if yes , dnses := v .hasConflict (inst ); yes {
140- dnsesStr := strings .Join (dnses , "\n * " )
141- msg := fmt .Sprintf ( " \n Cannot create %q (%s) in namespace %q because it would overwrite objects in the following descendant namespace(s):\n * %s \n To fix this, choose a different name for the object, or remove the conflicting objects from the above namespaces." , inst . GetName (), inst . GroupVersionKind (), inst . GetNamespace () , dnsesStr )
142- return webhooks .Deny ( metav1 . StatusReasonConflict , msg )
150+ dnsesStr := strings .Join (dnses , ", " )
151+ err := fmt .Errorf ( " would overwrite objects in the following descendant namespace(s): %s. To fix this, choose a different name for the object, or remove the conflicting objects from the listed namespaces" , dnsesStr )
152+ return webhooks .DenyConflict ( req . gr (), req . name (), err )
143153 }
144154 return webhooks .Allow ("source object" )
145155 }
@@ -244,7 +254,8 @@ func (v *Validator) handleInherited(ctx context.Context, req *request, newSource
244254 // if this is an update, make sure that the canonical form of the object hasn't changed.
245255 switch op {
246256 case k8sadm .Create :
247- return webhooks .Deny (metav1 .StatusReasonForbidden , "Cannot create objects with the label \" " + api .LabelInheritedFrom + "\" " )
257+ err := fmt .Errorf ("cannot create objects with the label \" %s\" " , api .LabelInheritedFrom )
258+ return webhooks .DenyForbidden (req .gr (), req .name (), err )
248259
249260 case k8sadm .Delete :
250261 // There are few things more irritating in (K8s) life than having some stupid controller stop
@@ -256,7 +267,8 @@ func (v *Validator) handleInherited(ctx context.Context, req *request, newSource
256267 }
257268
258269 if ! isDeleting {
259- return webhooks .Deny (metav1 .StatusReasonForbidden , "Cannot delete object propagated from namespace \" " + oldSource + "\" " )
270+ err := fmt .Errorf ("cannot delete object propagated from namespace \" %s\" " , oldSource )
271+ return webhooks .DenyForbidden (req .gr (), req .name (), err )
260272 }
261273
262274 return webhooks .Allow ("allowing deletion of propagated object since namespace is being deleted" )
@@ -266,21 +278,22 @@ func (v *Validator) handleInherited(ctx context.Context, req *request, newSource
266278 // added or deleted. Note that this label is *not* included in canonical(), below, so we
267279 // need to check it manually.
268280 if newSource != oldSource {
269- return webhooks .Deny (metav1 .StatusReasonForbidden , "Cannot modify the label \" " + api .LabelInheritedFrom + "\" " )
281+ err := fmt .Errorf ("cannot modify the label \" %s\" " , api .LabelInheritedFrom )
282+ return webhooks .DenyForbidden (req .gr (), req .name (), err )
270283 }
271284
272285 // If the existing object has an inheritedFrom label, it's a propagated object. Any user changes
273286 // should be rejected. Note that canonical does *not* compare any HNC labels or
274287 // annotations.
275288 if ! reflect .DeepEqual (canonical (inst ), canonical (oldInst )) {
276- return webhooks . Deny ( metav1 . StatusReasonForbidden ,
277- "Cannot modify object propagated from namespace \" " + oldSource + " \" " )
289+ err := fmt . Errorf ( "cannot modify object propagated from namespace \" %s \" " , oldSource )
290+ return webhooks . DenyForbidden ( req . gr (), req . name (), err )
278291 }
279292
280293 // Check for all the labels and annotations (including HNC and non HNC)
281294 if ! reflect .DeepEqual (oldInst .GetLabels (), inst .GetLabels ()) || ! reflect .DeepEqual (oldInst .GetAnnotations (), inst .GetAnnotations ()) {
282- return webhooks . Deny ( metav1 . StatusReasonForbidden ,
283- "Cannot modify object propagated from namespace \" " + oldSource + " \" " )
295+ err := fmt . Errorf ( "cannot modify object propagated from namespace \" %s \" " , oldSource )
296+ return webhooks . DenyForbidden ( req . gr (), req . name (), err )
284297 }
285298
286299 return webhooks .Allow ("no illegal updates to propagated object" )
@@ -365,6 +378,7 @@ func (v *Validator) decodeRequest(log logr.Logger, req admission.Request) (*requ
365378 obj : inst ,
366379 oldObj : oldInst ,
367380 op : req .Operation ,
381+ gvr : req .Resource ,
368382 }, nil
369383}
370384
0 commit comments