Skip to content

Commit 52bbde7

Browse files
committed
use json patch api type from kustomize api
1 parent 40e2d53 commit 52bbde7

File tree

8 files changed

+95
-148
lines changed

8 files changed

+95
-148
lines changed

api/go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@ module github.com/openmcp-project/controller-utils/api
22

33
go 1.24.2
44

5-
require k8s.io/apiextensions-apiserver v0.33.2
5+
require github.com/fluxcd/pkg/apis/kustomize v1.10.0
66

77
require (
88
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
99
github.com/go-logr/logr v1.4.2 // indirect
1010
github.com/gogo/protobuf v1.3.2 // indirect
1111
github.com/json-iterator/go v1.1.12 // indirect
12+
github.com/kr/pretty v0.3.1 // indirect
1213
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
1314
github.com/modern-go/reflect2 v1.0.2 // indirect
1415
github.com/x448/float16 v0.8.4 // indirect
1516
golang.org/x/net v0.38.0 // indirect
1617
golang.org/x/text v0.23.0 // indirect
1718
gopkg.in/inf.v0 v0.9.1 // indirect
19+
k8s.io/apiextensions-apiserver v0.33.2 // indirect
1820
k8s.io/apimachinery v0.33.2 // indirect
1921
k8s.io/klog/v2 v2.130.1 // indirect
2022
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect

api/go.sum

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
12
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
23
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
34
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/fluxcd/pkg/apis/kustomize v1.10.0 h1:47EeSzkQvlQZdH92vHMe2lK2iR8aOSEJq95avw5idts=
6+
github.com/fluxcd/pkg/apis/kustomize v1.10.0/go.mod h1:UsqMV4sqNa1Yg0pmTsdkHRJr7bafBOENIJoAN+3ezaQ=
47
github.com/fxamacker/cbor/v2 v2.7.0 h1:iM5WgngdRBanHcxugY4JySA0nk1wZorNOpTgCMedv5E=
58
github.com/fxamacker/cbor/v2 v2.7.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
69
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
@@ -15,13 +18,21 @@ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnr
1518
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
1619
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
1720
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
21+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
22+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
23+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
24+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1825
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
1926
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
2027
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
2128
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
2229
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
30+
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
2331
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2432
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
33+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
34+
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
35+
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
2536
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
2637
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
2738
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -61,8 +72,9 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
6172
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6273
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
6374
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
64-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
6575
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
76+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
77+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
6678
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
6779
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
6880
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

api/jsonpatch/types.go

Lines changed: 8 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,94 +2,29 @@
22
package jsonpatch
33

44
import (
5-
"encoding/json"
6-
7-
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
5+
"github.com/fluxcd/pkg/apis/kustomize"
86
)
97

108
// JSONPatch represents a JSON patch operation.
119
// Technically, a single JSON patch (as defined by RFC 6902) is a list of patch operations.
1210
// Opposed to that, this type represents a single operation. Use the JSONPatches type for a list of operations instead.
13-
// +kubebuilder:validation:Schemaless
14-
// +kubebuilder:validation:Type=object
15-
type JSONPatch struct {
16-
// Operation is the operation to perform.
17-
// Valid values are: add, remove, replace, move, copy, test
18-
// +kubebuilder:validation:Enum=add;remove;replace;move;copy;test
19-
// +kubebuilder:validation:Required
20-
Operation Operation `json:"op"`
21-
22-
// Path is the path to the target location in the JSON document.
23-
// +kubebuilder:validation:Required
24-
Path string `json:"path"`
25-
26-
// Value is the value to set at the target location.
27-
// Required for add, replace, and test operations.
28-
// +kubebuilder:validation:Schemaless
29-
// +kubebuilder:pruning:PreserveUnknownFields
30-
// +optional
31-
Value *apiextensionsv1.JSON `json:"value,omitempty"`
32-
33-
// From is the source location for move and copy operations.
34-
// +optional
35-
From *string `json:"from,omitempty"`
36-
}
11+
type JSONPatch = kustomize.JSON6902
3712

3813
// JSONPatches is a list of JSON patch operations.
3914
// This is technically a 'JSON patch' as defined in RFC 6902.
4015
type JSONPatches []JSONPatch
4116

42-
type Operation string
43-
4417
const (
4518
// ADD is the constant for the JSONPatch 'add' operation.
46-
ADD Operation = "add"
19+
ADD = "add"
4720
// REMOVE is the constant for the JSONPatch 'remove' operation.
48-
REMOVE Operation = "remove"
21+
REMOVE = "remove"
4922
// REPLACE is the constant for the JSONPatch 'replace' operation.
50-
REPLACE Operation = "replace"
23+
REPLACE = "replace"
5124
// MOVE is the constant for the JSONPatch 'move' operation.
52-
MOVE Operation = "move"
25+
MOVE = "move"
5326
// COPY is the constant for the JSONPatch 'copy' operation.
54-
COPY Operation = "copy"
27+
COPY = "copy"
5528
// TEST is the constant for the JSONPatch 'test' operation.
56-
TEST Operation = "test"
29+
TEST = "test"
5730
)
58-
59-
// NewJSONPatch creates a new JSONPatch with the given values.
60-
// If value is non-nil, it is marshaled to JSON. Returns an error if the value cannot be marshaled.
61-
func NewJSONPatch(op Operation, path string, value any, from *string) (JSONPatch, error) {
62-
res := JSONPatch{
63-
Operation: op,
64-
Path: path,
65-
From: from,
66-
}
67-
var err error
68-
if value != nil {
69-
var valueJSON []byte
70-
valueJSON, err = json.Marshal(value)
71-
res.Value = &apiextensionsv1.JSON{
72-
Raw: valueJSON,
73-
}
74-
}
75-
return res, err
76-
}
77-
78-
// NewJSONPatchOrPanic works like NewJSONPatch, but instead of returning an error, it panics if the patch cannot be created.
79-
func NewJSONPatchOrPanic(op Operation, path string, value any, from *string) JSONPatch {
80-
patch, err := NewJSONPatch(op, path, value, from)
81-
if err != nil {
82-
panic(err)
83-
}
84-
return patch
85-
}
86-
87-
// NewJSONPatches combines multiple JSONPatch instances into a single JSONPatches instance.
88-
// This is a convenience function to create a JSONPatches instance from multiple JSONPatch instances.
89-
func NewJSONPatches(patches ...JSONPatch) JSONPatches {
90-
result := make(JSONPatches, 0, len(patches))
91-
for _, patch := range patches {
92-
result = append(result, patch)
93-
}
94-
return result
95-
}

api/jsonpatch/zz_generated.deepcopy.go

Lines changed: 0 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ require (
3333
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
3434
github.com/emicklei/go-restful/v3 v3.12.1 // indirect
3535
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
36+
github.com/fluxcd/pkg/apis/kustomize v1.10.0 // indirect
3637
github.com/fsnotify/fsnotify v1.8.0 // indirect
3738
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
3839
github.com/go-openapi/jsonpointer v0.21.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH
1414
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
1515
github.com/evanphx/json-patch/v5 v5.9.11 h1:/8HVnzMq13/3x9TPvjG08wUGqBTmZBsCWzjTM0wiaDU=
1616
github.com/evanphx/json-patch/v5 v5.9.11/go.mod h1:3j+LviiESTElxA4p3EMKAB9HXj3/XEtnUf6OZxqIQTM=
17+
github.com/fluxcd/pkg/apis/kustomize v1.10.0 h1:47EeSzkQvlQZdH92vHMe2lK2iR8aOSEJq95avw5idts=
18+
github.com/fluxcd/pkg/apis/kustomize v1.10.0/go.mod h1:UsqMV4sqNa1Yg0pmTsdkHRJr7bafBOENIJoAN+3ezaQ=
1719
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
1820
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
1921
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=

pkg/jsonpatch/patch.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ import (
77

88
jplib "github.com/evanphx/json-patch/v5"
99

10+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
11+
1012
jpapi "github.com/openmcp-project/controller-utils/api/jsonpatch"
1113
)
1214

15+
type PatchValueData = apiextensionsv1.JSON
16+
1317
type Untyped = []byte
1418

1519
type Patch = TypedPatch[Untyped]
@@ -32,14 +36,14 @@ type Option func(*Options)
3236
// This JSONPatch's Apply method works on plain JSON bytes.
3337
// To apply the patches to an arbitrary type (which is marshalled to JSON before and unmarshalled back afterwards),
3438
// use NewTyped instead.
35-
func New(patches jpapi.JSONPatches) *Patch {
39+
func New(patches ...jpapi.JSONPatch) *Patch {
3640
return &TypedPatch[Untyped]{
3741
JSONPatches: patches,
3842
}
3943
}
4044

4145
// NewTyped creates a new TypedJSONPatch with the given patches.
42-
func NewTyped[T any](patches jpapi.JSONPatches) *TypedPatch[T] {
46+
func NewTyped[T any](patches ...jpapi.JSONPatch) *TypedPatch[T] {
4347
return &TypedPatch[T]{
4448
JSONPatches: patches,
4549
}
@@ -165,13 +169,11 @@ func (p *TypedPatch[T]) MarshalJSON() ([]byte, error) {
165169
}
166170
p.Path = convertedPath
167171

168-
if p.From != nil {
169-
convertedFrom, iperr := ConvertPath(*p.From)
170-
if iperr != nil {
171-
return nil, fmt.Errorf("failed to convert 'from' path at index %d: %w", i, iperr)
172-
}
173-
p.From = &convertedFrom
172+
convertedFrom, iperr := ConvertPath(p.From)
173+
if iperr != nil {
174+
return nil, fmt.Errorf("failed to convert 'from' path at index %d: %w", i, iperr)
174175
}
176+
p.From = convertedFrom
175177

176178
patches[i] = *p
177179
}

0 commit comments

Comments
 (0)