@@ -54,10 +54,6 @@ func GetPatchStatusOperationsOnHookError(operations []sdkpkg.PatchCollectorOpera
5454 patchStatusOperations := make ([]sdkpkg.PatchCollectorOperation , 0 )
5555 for _ , op := range operations {
5656 switch operation := op .(type ) {
57- case * filterOperation :
58- if operation .subresource == "/status" && operation .ignoreHookError {
59- patchStatusOperations = append (patchStatusOperations , operation )
60- }
6157 case * patchOperation :
6258 if operation .subresource == "/status" && operation .ignoreHookError {
6359 patchStatusOperations = append (patchStatusOperations , operation )
@@ -143,14 +139,22 @@ type patchOperation struct {
143139 subresource string
144140
145141 // Patch options.
146- patchType types.PatchType
147- patch any
142+ patchType types.PatchType
143+ patch any
144+
145+ // using only if not JSON or Merge patch type
146+ filterFunc func (* unstructured.Unstructured ) (* unstructured.Unstructured , error )
147+
148148 ignoreMissingObject bool
149149 ignoreHookError bool
150150}
151151
152152func (op * patchOperation ) Description () string {
153- return fmt .Sprintf ("Patch object %s/%s/%s/%s using %s patch" , op .apiVersion , op .kind , op .namespace , op .name , op .patchType )
153+ return fmt .Sprintf ("Filter object %s/%s/%s/%s" , op .apiVersion , op .kind , op .namespace , op .name )
154+ }
155+
156+ func (op * patchOperation ) HasFilterFn () bool {
157+ return op .filterFunc != nil
154158}
155159
156160func (op * patchOperation ) WithSubresource (subresource string ) {
@@ -165,97 +169,61 @@ func (op *patchOperation) WithIgnoreHookError(ignore bool) {
165169 op .ignoreHookError = ignore
166170}
167171
168- type filterOperation struct {
169- // Object coordinates for patch and delete.
170- apiVersion string
171- kind string
172- namespace string
173- name string
174- subresource string
175-
176- // Patch options.
177- filterFunc func (* unstructured.Unstructured ) (* unstructured.Unstructured , error )
178- ignoreMissingObject bool
179- ignoreHookError bool
180- }
181-
182- func (op * filterOperation ) Description () string {
183- return fmt .Sprintf ("Filter object %s/%s/%s/%s" , op .apiVersion , op .kind , op .namespace , op .name )
184- }
185-
186- func (op * filterOperation ) WithSubresource (subresource string ) {
187- op .subresource = subresource
188- }
189-
190- func (op * filterOperation ) WithIgnoreMissingObject (ignore bool ) {
191- op .ignoreMissingObject = ignore
192- }
193-
194- func (op * filterOperation ) WithIgnoreHookError (ignore bool ) {
195- op .ignoreHookError = ignore
196- }
197-
198172func NewFromOperationSpec (spec OperationSpec ) sdkpkg.PatchCollectorOperation {
199173 switch spec .Operation {
200174 case Create :
201- return NewCreateOperation (spec .Object ,
202- CreateWithSubresource (spec .Subresource ))
175+ return NewCreateOperation (spec .Object )
203176 case CreateIfNotExists :
204- return NewCreateIfNotExistsOperation (spec .Object ,
205- CreateWithSubresource (spec .Subresource ))
177+ return NewCreateIfNotExistsOperation (spec .Object )
206178 case CreateOrUpdate :
207- return NewCreateOrUpdateOperation (spec .Object ,
208- CreateWithSubresource (spec .Subresource ))
179+ return NewCreateOrUpdateOperation (spec .Object )
209180 case Delete :
210- return NewDeleteOperation (spec .ApiVersion , spec .Kind , spec .Namespace , spec .Name ,
211- DeleteWithSubresource (spec .Subresource ))
181+ return NewDeleteOperation (spec .ApiVersion , spec .Kind , spec .Namespace , spec .Name )
212182 case DeleteInBackground :
213- return NewDeleteInBackgroundOperation (spec .ApiVersion , spec .Kind , spec .Namespace , spec .Name ,
214- DeleteWithSubresource (spec .Subresource ))
183+ return NewDeleteInBackgroundOperation (spec .ApiVersion , spec .Kind , spec .Namespace , spec .Name )
215184 case DeleteNonCascading :
216- return NewDeleteNonCascadingOperation (spec .ApiVersion , spec .Kind , spec .Namespace , spec .Name ,
217- DeleteWithSubresource (spec .Subresource ))
185+ return NewDeleteNonCascadingOperation (spec .ApiVersion , spec .Kind , spec .Namespace , spec .Name )
218186 case JQPatch :
219- return NewJQPatchOperation (
187+ return NewPatchWithJQOperation (
220188 spec .JQFilter ,
221189 spec .ApiVersion , spec .Kind , spec .Namespace , spec .Name ,
222- FilterWithSubresource (spec .Subresource ),
223- FilterWithIgnoreMissingObject (spec .IgnoreMissingObject ),
224- FilterWithIgnoreHookError (spec .IgnoreHookError ),
190+ WithSubresource (spec .Subresource ),
191+ WithIgnoreMissingObject (spec .IgnoreMissingObject ),
192+ WithIgnoreHookError (spec .IgnoreHookError ),
225193 )
226194 case MergePatch :
227195 return NewMergePatchOperation (spec .MergePatch ,
228196 spec .ApiVersion , spec .Kind , spec .Namespace , spec .Name ,
229- PatchWithSubresource (spec .Subresource ),
230- PatchWithIgnoreMissingObject (spec .IgnoreMissingObject ),
231- PatchWithIgnoreHookError (spec .IgnoreHookError ),
197+ WithSubresource (spec .Subresource ),
198+ WithIgnoreMissingObject (spec .IgnoreMissingObject ),
199+ WithIgnoreHookError (spec .IgnoreHookError ),
232200 )
233201 case JSONPatch :
234202 return NewJSONPatchOperation (spec .JSONPatch ,
235203 spec .ApiVersion , spec .Kind , spec .Namespace , spec .Name ,
236- PatchWithSubresource (spec .Subresource ),
237- PatchWithIgnoreMissingObject (spec .IgnoreMissingObject ),
238- PatchWithIgnoreHookError (spec .IgnoreHookError ),
204+ WithSubresource (spec .Subresource ),
205+ WithIgnoreMissingObject (spec .IgnoreMissingObject ),
206+ WithIgnoreHookError (spec .IgnoreHookError ),
239207 )
240208 }
241209
242210 // Should not be reached!
243211 return nil
244212}
245213
246- func NewCreateOperation (obj any , opts ... sdkpkg. PatchCollectorCreateOption ) sdkpkg.PatchCollectorOperation {
247- return newCreateOperation (Create , obj , opts ... )
214+ func NewCreateOperation (obj any ) sdkpkg.PatchCollectorOperation {
215+ return newCreateOperation (Create , obj )
248216}
249217
250- func NewCreateOrUpdateOperation (obj any , opts ... sdkpkg. PatchCollectorCreateOption ) sdkpkg.PatchCollectorOperation {
251- return newCreateOperation (CreateOrUpdate , obj , opts ... )
218+ func NewCreateOrUpdateOperation (obj any ) sdkpkg.PatchCollectorOperation {
219+ return newCreateOperation (CreateOrUpdate , obj )
252220}
253221
254- func NewCreateIfNotExistsOperation (obj any , opts ... sdkpkg. PatchCollectorCreateOption ) sdkpkg.PatchCollectorOperation {
255- return newCreateOperation (CreateIfNotExists , obj , opts ... )
222+ func NewCreateIfNotExistsOperation (obj any ) sdkpkg.PatchCollectorOperation {
223+ return newCreateOperation (CreateIfNotExists , obj )
256224}
257225
258- func newCreateOperation (operation OperationType , obj any , opts ... sdkpkg. PatchCollectorCreateOption ) sdkpkg.PatchCollectorOperation {
226+ func newCreateOperation (operation OperationType , obj any ) sdkpkg.PatchCollectorOperation {
259227 op := & createOperation {
260228 object : obj ,
261229 }
@@ -269,26 +237,22 @@ func newCreateOperation(operation OperationType, obj any, opts ...sdkpkg.PatchCo
269237 op .ignoreIfExists = true
270238 }
271239
272- for _ , opt := range opts {
273- opt .Apply (op )
274- }
275-
276240 return op
277241}
278242
279- func NewDeleteOperation (apiVersion string , kind string , namespace string , name string , opts ... sdkpkg. PatchCollectorDeleteOption ) sdkpkg.PatchCollectorOperation {
280- return newDeleteOperation (metav1 .DeletePropagationForeground , apiVersion , kind , namespace , name , opts ... )
243+ func NewDeleteOperation (apiVersion string , kind string , namespace string , name string ) sdkpkg.PatchCollectorOperation {
244+ return newDeleteOperation (metav1 .DeletePropagationForeground , apiVersion , kind , namespace , name )
281245}
282246
283- func NewDeleteInBackgroundOperation (apiVersion string , kind string , namespace string , name string , opts ... sdkpkg. PatchCollectorDeleteOption ) sdkpkg.PatchCollectorOperation {
284- return newDeleteOperation (metav1 .DeletePropagationBackground , apiVersion , kind , namespace , name , opts ... )
247+ func NewDeleteInBackgroundOperation (apiVersion string , kind string , namespace string , name string ) sdkpkg.PatchCollectorOperation {
248+ return newDeleteOperation (metav1 .DeletePropagationBackground , apiVersion , kind , namespace , name )
285249}
286250
287- func NewDeleteNonCascadingOperation (apiVersion string , kind string , namespace string , name string , opts ... sdkpkg. PatchCollectorDeleteOption ) sdkpkg.PatchCollectorOperation {
288- return newDeleteOperation (metav1 .DeletePropagationOrphan , apiVersion , kind , namespace , name , opts ... )
251+ func NewDeleteNonCascadingOperation (apiVersion string , kind string , namespace string , name string ) sdkpkg.PatchCollectorOperation {
252+ return newDeleteOperation (metav1 .DeletePropagationOrphan , apiVersion , kind , namespace , name )
289253}
290254
291- func newDeleteOperation (propagation metav1.DeletionPropagation , apiVersion , kind , namespace , name string , opts ... sdkpkg. PatchCollectorDeleteOption ) sdkpkg.PatchCollectorOperation {
255+ func newDeleteOperation (propagation metav1.DeletionPropagation , apiVersion , kind , namespace , name string ) sdkpkg.PatchCollectorOperation {
292256 op := & deleteOperation {
293257 apiVersion : apiVersion ,
294258 kind : kind ,
@@ -297,22 +261,18 @@ func newDeleteOperation(propagation metav1.DeletionPropagation, apiVersion, kind
297261 deletionPropagation : propagation ,
298262 }
299263
300- for _ , opt := range opts {
301- opt .Apply (op )
302- }
303-
304264 return op
305265}
306266
307- func NewMergePatchOperation (mergePatch any , apiVersion string , kind string , namespace string , name string , opts ... sdkpkg.PatchCollectorPatchOption ) sdkpkg.PatchCollectorOperation {
267+ func NewMergePatchOperation (mergePatch any , apiVersion string , kind string , namespace string , name string , opts ... sdkpkg.PatchCollectorOption ) sdkpkg.PatchCollectorOperation {
308268 return newPatchOperation (types .MergePatchType , mergePatch , apiVersion , kind , namespace , name , opts ... )
309269}
310270
311- func NewJSONPatchOperation (jsonpatch any , apiVersion string , kind string , namespace string , name string , opts ... sdkpkg.PatchCollectorPatchOption ) sdkpkg.PatchCollectorOperation {
271+ func NewJSONPatchOperation (jsonpatch any , apiVersion string , kind string , namespace string , name string , opts ... sdkpkg.PatchCollectorOption ) sdkpkg.PatchCollectorOperation {
312272 return newPatchOperation (types .JSONPatchType , jsonpatch , apiVersion , kind , namespace , name , opts ... )
313273}
314274
315- func newPatchOperation (patchType types.PatchType , patch any , apiVersion , kind , namespace , name string , opts ... sdkpkg.PatchCollectorPatchOption ) sdkpkg.PatchCollectorOperation {
275+ func newPatchOperation (patchType types.PatchType , patch any , apiVersion , kind , namespace , name string , opts ... sdkpkg.PatchCollectorOption ) sdkpkg.PatchCollectorOperation {
316276 op := & patchOperation {
317277 apiVersion : apiVersion ,
318278 kind : kind ,
@@ -329,24 +289,24 @@ func newPatchOperation(patchType types.PatchType, patch any, apiVersion, kind, n
329289 return op
330290}
331291
332- func NewJQPatchOperation (jqfilter string , apiVersion string , kind string , namespace string , name string , opts ... sdkpkg.PatchCollectorFilterOption ) sdkpkg.PatchCollectorOperation {
292+ func NewPatchWithJQOperation (jqfilter string , apiVersion string , kind string , namespace string , name string , opts ... sdkpkg.PatchCollectorOption ) sdkpkg.PatchCollectorOperation {
333293 return newFilterOperation (func (u * unstructured.Unstructured ) (* unstructured.Unstructured , error ) {
334294 filter := jq .NewFilter (app .JqLibraryPath )
335295 return applyJQPatch (jqfilter , filter , u )
336296 }, apiVersion , kind , namespace , name , opts ... )
337297}
338298
339- func NewFilterPatchOperation ( filterFunc func ( * unstructured. Unstructured ) ( * unstructured. Unstructured , error ), apiVersion string , kind string , namespace string , name string , opts ... sdkpkg.PatchCollectorFilterOption ) sdkpkg.PatchCollectorOperation {
340- return newFilterOperation (filterFunc , apiVersion , kind , namespace , name , opts ... )
299+ func NewPatchWithMutatingFuncOperation ( fn MutatingFunc , apiVersion string , kind string , namespace string , name string , opts ... sdkpkg.PatchCollectorOption ) sdkpkg.PatchCollectorOperation {
300+ return newFilterOperation (fn , apiVersion , kind , namespace , name , opts ... )
341301}
342302
343- func newFilterOperation (filterFunc func ( * unstructured. Unstructured ) ( * unstructured. Unstructured , error ), apiVersion , kind , namespace , name string , opts ... sdkpkg.PatchCollectorFilterOption ) sdkpkg.PatchCollectorOperation {
344- op := & filterOperation {
303+ func newFilterOperation (fn MutatingFunc , apiVersion , kind , namespace , name string , opts ... sdkpkg.PatchCollectorOption ) sdkpkg.PatchCollectorOperation {
304+ op := & patchOperation {
345305 apiVersion : apiVersion ,
346306 kind : kind ,
347307 namespace : namespace ,
348308 name : name ,
349- filterFunc : filterFunc ,
309+ filterFunc : fn ,
350310 }
351311
352312 for _ , opt := range opts {
0 commit comments