@@ -23,7 +23,7 @@ import (
23
23
"strings"
24
24
"time"
25
25
26
- "github.com/evanphx/json-patch"
26
+ jsonpatch "github.com/evanphx/json-patch"
27
27
"k8s.io/apimachinery/pkg/api/errors"
28
28
"k8s.io/apimachinery/pkg/api/meta"
29
29
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
@@ -118,6 +118,7 @@ func PatchResource(r rest.Patcher, scope *RequestScope, admit admission.Interfac
118
118
scope .err (err , w , req )
119
119
return
120
120
}
121
+ options .TypeMeta .SetGroupVersionKind (metav1 .SchemeGroupVersion .WithKind ("PatchOptions" ))
121
122
122
123
ae := request .AuditEventFrom (ctx )
123
124
admit = admission .WithAudit (admit , ae )
@@ -151,6 +152,7 @@ func PatchResource(r rest.Patcher, scope *RequestScope, admit admission.Interfac
151
152
scope .Resource ,
152
153
scope .Subresource ,
153
154
admission .Create ,
155
+ patchToCreateOptions (options ),
154
156
dryrun .IsDryRun (options .DryRun ),
155
157
userInfo )
156
158
staticUpdateAttributes := admission .NewAttributesRecord (
@@ -162,6 +164,7 @@ func PatchResource(r rest.Patcher, scope *RequestScope, admit admission.Interfac
162
164
scope .Resource ,
163
165
scope .Subresource ,
164
166
admission .Update ,
167
+ patchToUpdateOptions (options ),
165
168
dryrun .IsDryRun (options .DryRun ),
166
169
userInfo ,
167
170
)
@@ -489,9 +492,9 @@ func (p *patcher) applyPatch(_ context.Context, _, currentObject runtime.Object)
489
492
return objToUpdate , nil
490
493
}
491
494
492
- func (p * patcher ) admissionAttributes (ctx context.Context , updatedObject runtime.Object , currentObject runtime.Object , operation admission.Operation ) admission.Attributes {
495
+ func (p * patcher ) admissionAttributes (ctx context.Context , updatedObject runtime.Object , currentObject runtime.Object , operation admission.Operation , operationOptions runtime. Object ) admission.Attributes {
493
496
userInfo , _ := request .UserFrom (ctx )
494
- return admission .NewAttributesRecord (updatedObject , currentObject , p .kind , p .namespace , p .name , p .resource , p .subresource , operation , p .dryRun , userInfo )
497
+ return admission .NewAttributesRecord (updatedObject , currentObject , p .kind , p .namespace , p .name , p .resource , p .subresource , operation , operationOptions , p .dryRun , userInfo )
495
498
}
496
499
497
500
// applyAdmission is called every time GuaranteedUpdate asks for the updated object,
@@ -500,16 +503,19 @@ func (p *patcher) admissionAttributes(ctx context.Context, updatedObject runtime
500
503
func (p * patcher ) applyAdmission (ctx context.Context , patchedObject runtime.Object , currentObject runtime.Object ) (runtime.Object , error ) {
501
504
p .trace .Step ("About to check admission control" )
502
505
var operation admission.Operation
506
+ var options runtime.Object
503
507
if hasUID , err := hasUID (currentObject ); err != nil {
504
508
return nil , err
505
509
} else if ! hasUID {
506
510
operation = admission .Create
507
511
currentObject = nil
512
+ options = patchToCreateOptions (p .options )
508
513
} else {
509
514
operation = admission .Update
515
+ options = patchToUpdateOptions (p .options )
510
516
}
511
517
if p .admissionCheck != nil && p .admissionCheck .Handles (operation ) {
512
- attributes := p .admissionAttributes (ctx , patchedObject , currentObject , operation )
518
+ attributes := p .admissionAttributes (ctx , patchedObject , currentObject , operation , options )
513
519
return patchedObject , p .admissionCheck .Admit (attributes , p .objectInterfaces )
514
520
}
515
521
return patchedObject , nil
@@ -551,11 +557,8 @@ func (p *patcher) patchResource(ctx context.Context, scope *RequestScope) (runti
551
557
wasCreated := false
552
558
p .updatedObjectInfo = rest .DefaultUpdatedObjectInfo (nil , p .applyPatch , p .applyAdmission )
553
559
result , err := finishRequest (p .timeout , func () (runtime.Object , error ) {
554
- // TODO: Pass in UpdateOptions to override UpdateStrategy.AllowUpdateOnCreate
555
- options , err := patchToUpdateOptions (p .options )
556
- if err != nil {
557
- return nil , err
558
- }
560
+ // Pass in UpdateOptions to override UpdateStrategy.AllowUpdateOnCreate
561
+ options := patchToUpdateOptions (p .options )
559
562
updateObject , created , updateErr := p .restPatcher .Update (ctx , p .name , p .updatedObjectInfo , p .createValidation , p .updateValidation , p .forceAllowCreate , options )
560
563
wasCreated = created
561
564
return updateObject , updateErr
@@ -600,12 +603,28 @@ func interpretStrategicMergePatchError(err error) error {
600
603
}
601
604
}
602
605
603
- func patchToUpdateOptions (po * metav1.PatchOptions ) (* metav1.UpdateOptions , error ) {
604
- b , err := json .Marshal (po )
605
- if err != nil {
606
- return nil , err
606
+ // patchToUpdateOptions creates an UpdateOptions with the same field values as the provided PatchOptions.
607
+ func patchToUpdateOptions (po * metav1.PatchOptions ) * metav1.UpdateOptions {
608
+ if po == nil {
609
+ return nil
610
+ }
611
+ uo := & metav1.UpdateOptions {
612
+ DryRun : po .DryRun ,
613
+ FieldManager : po .FieldManager ,
614
+ }
615
+ uo .TypeMeta .SetGroupVersionKind (metav1 .SchemeGroupVersion .WithKind ("UpdateOptions" ))
616
+ return uo
617
+ }
618
+
619
+ // patchToCreateOptions creates an CreateOptions with the same field values as the provided PatchOptions.
620
+ func patchToCreateOptions (po * metav1.PatchOptions ) * metav1.CreateOptions {
621
+ if po == nil {
622
+ return nil
623
+ }
624
+ co := & metav1.CreateOptions {
625
+ DryRun : po .DryRun ,
626
+ FieldManager : po .FieldManager ,
607
627
}
608
- uo := metav1.UpdateOptions {}
609
- err = json .Unmarshal (b , & uo )
610
- return & uo , err
628
+ co .TypeMeta .SetGroupVersionKind (metav1 .SchemeGroupVersion .WithKind ("CreateOptions" ))
629
+ return co
611
630
}
0 commit comments