@@ -50,8 +50,8 @@ const (
5050
5151type Client interface {
5252 client.WithWatch
53- Apply (ctx context.Context , obj client.Object ) error
54- ApplyWithResult (ctx context.Context , obj client.Object ) (ApplyResult , error )
53+ Apply (ctx context.Context , obj client.Object , opts ... ApplyOption ) error
54+ ApplyWithResult (ctx context.Context , obj client.Object , opts ... ApplyOption ) (ApplyResult , error )
5555}
5656
5757type ApplyResult int
@@ -80,9 +80,9 @@ type applier struct {
8080 parser GVKParser
8181}
8282
83- func (p * applier ) Apply (ctx context.Context , obj client.Object ) error {
83+ func (p * applier ) Apply (ctx context.Context , obj client.Object , opts ... ApplyOption ) error {
8484 logger := logr .FromContextOrDiscard (ctx )
85- res , err := p .ApplyWithResult (ctx , obj )
85+ res , err := p .ApplyWithResult (ctx , obj , opts ... )
8686 if err == nil {
8787 logger .Info ("apply success" ,
8888 "kind" , reflect .TypeOf (obj ),
@@ -94,7 +94,12 @@ func (p *applier) Apply(ctx context.Context, obj client.Object) error {
9494 return err
9595}
9696
97- func (p * applier ) ApplyWithResult (ctx context.Context , obj client.Object ) (ApplyResult , error ) {
97+ func (p * applier ) ApplyWithResult (ctx context.Context , obj client.Object , opts ... ApplyOption ) (ApplyResult , error ) {
98+ o := & ApplyOptions {}
99+ for _ , opt := range opts {
100+ opt .With (o )
101+ }
102+
98103 gvks , _ , err := scheme .Scheme .ObjectKinds (obj )
99104 if err != nil {
100105 return ApplyResultUnchanged , fmt .Errorf ("cannot get gvks of the obj %T: %w" , obj , err )
@@ -122,6 +127,19 @@ func (p *applier) ApplyWithResult(ctx context.Context, obj client.Object) (Apply
122127 if err != nil {
123128 return ApplyResultUnchanged , err
124129 }
130+ for _ , fields := range o .Immutable {
131+ val , exists , err := unstructured .NestedFieldNoCopy (current .Object , fields ... )
132+ if err != nil {
133+ return ApplyResultUnchanged , fmt .Errorf ("cannot get immutable fields %v: %w" , fields , err )
134+ }
135+ if ! exists {
136+ unstructured .RemoveNestedField (expected .Object , fields ... )
137+ } else {
138+ if err := unstructured .SetNestedField (expected .Object , val , fields ... ); err != nil {
139+ return ApplyResultUnchanged , fmt .Errorf ("cannot set immutable fields %v: %w" , fields , err )
140+ }
141+ }
142+ }
125143 lastApplied , err := p .Extract (current , DefaultFieldManager , gvks [0 ], "" )
126144 if err != nil {
127145 return ApplyResultUnchanged , fmt .Errorf ("cannot extract last applied patch: %w" , err )
0 commit comments