Skip to content

Commit bcc2a74

Browse files
committed
bump
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
1 parent d547cc8 commit bcc2a74

File tree

7 files changed

+114
-178
lines changed

7 files changed

+114
-178
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ require (
4545
github.com/cespare/xxhash/v2 v2.3.0 // indirect
4646
github.com/containerd/stargz-snapshotter/estargz v0.14.3 // indirect
4747
github.com/davecgh/go-spew v1.1.1 // indirect
48-
github.com/deckhouse/module-sdk v0.1.1-0.20250306155143-efc505c729d0 // indirect
48+
github.com/deckhouse/module-sdk v0.1.1-0.20250307130634-9796dba9b13b // indirect
4949
github.com/docker/cli v24.0.0+incompatible // indirect
5050
github.com/docker/distribution v2.8.2+incompatible // indirect
5151
github.com/docker/docker v24.0.0+incompatible // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ github.com/deckhouse/module-sdk v0.1.1-0.20250306145537-568677f7eac7 h1:piuIR7WR
3737
github.com/deckhouse/module-sdk v0.1.1-0.20250306145537-568677f7eac7/go.mod h1:xZuqvKXZunp9VNAiF70fgYiN/HQkLDo8tvGymXNpu0o=
3838
github.com/deckhouse/module-sdk v0.1.1-0.20250306155143-efc505c729d0 h1:sZfJrKecyiGCxAP8sS7442psVMib08YpjtoSZkfDHJk=
3939
github.com/deckhouse/module-sdk v0.1.1-0.20250306155143-efc505c729d0/go.mod h1:xZuqvKXZunp9VNAiF70fgYiN/HQkLDo8tvGymXNpu0o=
40+
github.com/deckhouse/module-sdk v0.1.1-0.20250307114432-dbe2909556e3 h1:rOahjUWSShztMsDp7QohP3OqkOZ5AI9aCndrPRxm5io=
41+
github.com/deckhouse/module-sdk v0.1.1-0.20250307114432-dbe2909556e3/go.mod h1:xZuqvKXZunp9VNAiF70fgYiN/HQkLDo8tvGymXNpu0o=
42+
github.com/deckhouse/module-sdk v0.1.1-0.20250307120731-88e19e901be5 h1:77S9lqE4y+7TpRDtZ0T1yJuYJgoIyKvgg+jfm3A9fgo=
43+
github.com/deckhouse/module-sdk v0.1.1-0.20250307120731-88e19e901be5/go.mod h1:xZuqvKXZunp9VNAiF70fgYiN/HQkLDo8tvGymXNpu0o=
44+
github.com/deckhouse/module-sdk v0.1.1-0.20250307125652-e02edd325a3a h1:1fiGJctX45krnbqZwrzYuSpIn0IwBHI7dGQHvmEiDHM=
45+
github.com/deckhouse/module-sdk v0.1.1-0.20250307125652-e02edd325a3a/go.mod h1:xZuqvKXZunp9VNAiF70fgYiN/HQkLDo8tvGymXNpu0o=
46+
github.com/deckhouse/module-sdk v0.1.1-0.20250307130634-9796dba9b13b h1:jfJJrdXq4I+OpXJGFo5ogDYnm04hXTKM45xmIYmIs1w=
47+
github.com/deckhouse/module-sdk v0.1.1-0.20250307130634-9796dba9b13b/go.mod h1:xZuqvKXZunp9VNAiF70fgYiN/HQkLDo8tvGymXNpu0o=
4048
github.com/docker/cli v24.0.0+incompatible h1:0+1VshNwBQzQAx9lOl+OYCTCEAD8fKs/qeXMx3O0wqM=
4149
github.com/docker/cli v24.0.0+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
4250
github.com/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8=

pkg/kube/object_patch/operation.go

Lines changed: 50 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -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

152152
func (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

156160
func (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-
198172
func 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 {

pkg/kube/object_patch/options.go

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,26 @@ import (
44
sdkpkg "github.com/deckhouse/module-sdk/pkg"
55
)
66

7-
type CreateOption func(o sdkpkg.PatchCollectorCreateOptionApplier)
7+
type Option func(o sdkpkg.PatchCollectorOptionApplier)
88

9-
func (opt CreateOption) Apply(o sdkpkg.PatchCollectorCreateOptionApplier) {
9+
func (opt Option) Apply(o sdkpkg.PatchCollectorOptionApplier) {
1010
opt(o)
1111
}
1212

13-
func CreateWithSubresource(subresource string) CreateOption {
14-
return func(o sdkpkg.PatchCollectorCreateOptionApplier) {
13+
func WithSubresource(subresource string) Option {
14+
return func(o sdkpkg.PatchCollectorOptionApplier) {
1515
o.WithSubresource(subresource)
1616
}
1717
}
1818

19-
type DeleteOption func(o sdkpkg.PatchCollectorDeleteOptionApplier)
20-
21-
func (opt DeleteOption) Apply(o sdkpkg.PatchCollectorDeleteOptionApplier) {
22-
opt(o)
23-
}
24-
25-
func DeleteWithSubresource(subresource string) DeleteOption {
26-
return func(o sdkpkg.PatchCollectorDeleteOptionApplier) {
27-
o.WithSubresource(subresource)
28-
}
29-
}
30-
31-
type PatchOption func(o sdkpkg.PatchCollectorPatchOptionApplier)
32-
33-
func (opt PatchOption) Apply(o sdkpkg.PatchCollectorPatchOptionApplier) {
34-
opt(o)
35-
}
36-
37-
func PatchWithSubresource(subresource string) PatchOption {
38-
return func(o sdkpkg.PatchCollectorPatchOptionApplier) {
39-
o.WithSubresource(subresource)
40-
}
41-
}
42-
43-
func PatchWithIgnoreMissingObject(ignore bool) PatchOption {
44-
return func(o sdkpkg.PatchCollectorPatchOptionApplier) {
45-
o.WithIgnoreMissingObject(ignore)
46-
}
47-
}
48-
49-
func PatchWithIgnoreHookError(ignore bool) PatchOption {
50-
return func(o sdkpkg.PatchCollectorPatchOptionApplier) {
51-
o.WithIgnoreHookError(ignore)
52-
}
53-
}
54-
55-
type FilterOption func(o sdkpkg.PatchCollectorFilterOptionApplier)
56-
57-
func (opt FilterOption) Apply(o sdkpkg.PatchCollectorFilterOptionApplier) {
58-
opt(o)
59-
}
60-
61-
func FilterWithSubresource(subresource string) FilterOption {
62-
return func(o sdkpkg.PatchCollectorFilterOptionApplier) {
63-
o.WithSubresource(subresource)
64-
}
65-
}
66-
67-
func FilterWithIgnoreMissingObject(ignore bool) FilterOption {
68-
return func(o sdkpkg.PatchCollectorFilterOptionApplier) {
19+
func WithIgnoreMissingObject(ignore bool) Option {
20+
return func(o sdkpkg.PatchCollectorOptionApplier) {
6921
o.WithIgnoreMissingObject(ignore)
7022
}
7123
}
7224

73-
func FilterWithIgnoreHookError(ignore bool) FilterOption {
74-
return func(o sdkpkg.PatchCollectorFilterOptionApplier) {
25+
func WithIgnoreHookError(ignore bool) Option {
26+
return func(o sdkpkg.PatchCollectorOptionApplier) {
7527
o.WithIgnoreHookError(ignore)
7628
}
7729
}

pkg/kube/object_patch/patch.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,11 @@ func (o *ObjectPatcher) ExecuteOperation(operation sdkpkg.PatchCollectorOperatio
6868
case *deleteOperation:
6969
return o.executeDeleteOperation(v)
7070
case *patchOperation:
71+
if v.HasFilterFn() {
72+
return o.executeFilterOperation(v)
73+
}
74+
7175
return o.executePatchOperation(v)
72-
case *filterOperation:
73-
return o.executeFilterOperation(v)
7476
}
7577

7678
return nil
@@ -193,12 +195,12 @@ func (o *ObjectPatcher) executePatchOperation(op *patchOperation) error {
193195

194196
// executeFilterOperation retrieves a specified object, modified it with
195197
// filterFunc and calls update.
196-
198+
//
197199
// Other options:
198200
// - WithSubresource — a subresource argument for Patch or Update API call.
199201
// - IgnoreMissingObject — do not return error if the specified object is missing.
200202
// - IgnoreHookError — allows applying patches for a Status subresource even if the hook fails
201-
func (o *ObjectPatcher) executeFilterOperation(op *filterOperation) error {
203+
func (o *ObjectPatcher) executeFilterOperation(op *patchOperation) error {
202204
var err error
203205

204206
if op.filterFunc == nil {

0 commit comments

Comments
 (0)