Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
d08abdb1
Commit
d08abdb1
authored
Dec 30, 2016
by
Wojciech Tyczynski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow for returning map[string]interface{} from patch.
parent
8f99b744
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
39 additions
and
25 deletions
+39
-25
events_cache.go
pkg/client/record/events_cache.go
+1
-1
node_status_updater.go
.../volume/attachdetach/statusupdater/node_status_updater.go
+2
-2
rest.go
pkg/genericapiserver/api/handlers/rest.go
+3
-3
rest_test.go
pkg/genericapiserver/api/handlers/rest_test.go
+1
-1
patch.go
pkg/kubectl/cmd/patch.go
+1
-1
patch.go
pkg/util/strategicpatch/patch.go
+31
-17
No files found.
pkg/client/record/events_cache.go
View file @
d08abdb1
...
...
@@ -244,7 +244,7 @@ func (e *eventLogger) eventObserve(newEvent *v1.Event) (*v1.Event, []byte, error
newData
,
_
:=
json
.
Marshal
(
event
)
oldData
,
_
:=
json
.
Marshal
(
eventCopy2
)
patch
,
err
=
strategicpatch
.
Create
Strategic
MergePatch
(
oldData
,
newData
,
event
)
patch
,
err
=
strategicpatch
.
Create
TwoWay
MergePatch
(
oldData
,
newData
,
event
)
}
// record our new observation
...
...
pkg/controller/volume/attachdetach/statusupdater/node_status_updater.go
View file @
d08abdb1
...
...
@@ -111,10 +111,10 @@ func (nsu *nodeStatusUpdater) UpdateNodeStatuses() error {
}
patchBytes
,
err
:=
strategicpatch
.
Create
Strategic
MergePatch
(
oldData
,
newData
,
node
)
strategicpatch
.
Create
TwoWay
MergePatch
(
oldData
,
newData
,
node
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to Create
Strategic
MergePatch for node %q. %v"
,
"failed to Create
TwoWay
MergePatch for node %q. %v"
,
nodeName
,
err
)
}
...
...
pkg/genericapiserver/api/handlers/rest.go
View file @
d08abdb1
...
...
@@ -607,11 +607,11 @@ func patchResource(
if
err
!=
nil
{
return
nil
,
err
}
currentPatch
,
err
:=
strategicpatch
.
Create
Strategic
MergePatch
(
originalObjJS
,
currentObjectJS
,
versionedObj
)
currentPatch
,
err
:=
strategicpatch
.
Create
TwoWay
MergePatch
(
originalObjJS
,
currentObjectJS
,
versionedObj
)
if
err
!=
nil
{
return
nil
,
err
}
originalPatch
,
err
:=
strategicpatch
.
Create
Strategic
MergePatch
(
originalObjJS
,
originalPatchedObjJS
,
versionedObj
)
originalPatch
,
err
:=
strategicpatch
.
Create
TwoWay
MergePatch
(
originalObjJS
,
originalPatchedObjJS
,
versionedObj
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -1090,7 +1090,7 @@ func getPatchedJS(patchType types.PatchType, originalJS, patchJS []byte, obj run
case
types
.
MergePatchType
:
return
jsonpatch
.
MergePatch
(
originalJS
,
patchJS
)
case
types
.
StrategicMergePatchType
:
return
strategicpatch
.
StrategicMergePatch
Data
(
originalJS
,
patchJS
,
obj
)
return
strategicpatch
.
StrategicMergePatch
(
originalJS
,
patchJS
,
obj
)
default
:
// only here as a safety net - go-restful filters content-type
return
nil
,
fmt
.
Errorf
(
"unknown Content-Type header for patch: %v"
,
patchType
)
...
...
pkg/genericapiserver/api/handlers/rest_test.go
View file @
d08abdb1
...
...
@@ -215,7 +215,7 @@ func (tc *patchTestCase) Run(t *testing.T) {
continue
case
types
.
StrategicMergePatchType
:
patch
,
err
=
strategicpatch
.
Create
Strategic
MergePatch
(
originalObjJS
,
changedJS
,
versionedObj
)
patch
,
err
=
strategicpatch
.
Create
TwoWay
MergePatch
(
originalObjJS
,
changedJS
,
versionedObj
)
if
err
!=
nil
{
t
.
Errorf
(
"%s: unexpected error: %v"
,
tc
.
name
,
err
)
return
...
...
pkg/kubectl/cmd/patch.go
View file @
d08abdb1
...
...
@@ -261,7 +261,7 @@ func getPatchedJSON(patchType types.PatchType, originalJS, patchJS []byte, obj r
return
jsonpatch
.
MergePatch
(
originalJS
,
patchJS
)
case
types
.
StrategicMergePatchType
:
return
strategicpatch
.
StrategicMergePatch
Data
(
originalJS
,
patchJS
,
obj
)
return
strategicpatch
.
StrategicMergePatch
(
originalJS
,
patchJS
,
obj
)
default
:
// only here as a safety net - go-restful filters content-type
...
...
pkg/util/strategicpatch/patch.go
View file @
d08abdb1
...
...
@@ -48,6 +48,13 @@ const (
deleteFromPrimitiveListDirectivePrefix
=
"$deleteFromPrimitiveList"
)
// JSONMap is a representations of JSON object encoded as map[string]interface{}
// where the children can be either map[string]interface{}, []interface{} or
// primitive type).
// Operating on JSONMap representation is much faster as it doesn't require any
// json marshaling and/or unmarshaling operations.
type
JSONMap
map
[
string
]
interface
{}
// IsPreconditionFailed returns true if the provided error indicates
// a precondition failed.
func
IsPreconditionFailed
(
err
error
)
bool
{
...
...
@@ -136,11 +143,6 @@ func RequireMetadataKeyUnchanged(key string) PreconditionFunc {
}
}
// Deprecated: Use the synonym CreateTwoWayMergePatch, instead.
func
CreateStrategicMergePatch
(
original
,
modified
[]
byte
,
dataStruct
interface
{})
([]
byte
,
error
)
{
return
CreateTwoWayMergePatch
(
original
,
modified
,
dataStruct
)
}
// CreateTwoWayMergePatch creates a patch that can be passed to StrategicMergePatch from an original
// document and a modified document, which are passed to the method as json encoded content. It will
// return a patch that yields the modified document when applied to the original document, or an error
...
...
@@ -160,12 +162,24 @@ func CreateTwoWayMergePatch(original, modified []byte, dataStruct interface{}, f
}
}
patchMap
,
err
:=
CreateTwoWayMergeMapPatch
(
originalMap
,
modifiedMap
,
dataStruct
,
fns
...
)
if
err
!=
nil
{
return
nil
,
err
}
return
json
.
Marshal
(
patchMap
)
}
// CreateTwoWayMergeMapPatch creates a patch from an original and modified JSON objects,
// encoded JSONMap.
// The serialized version of the map can then be passed to StrategicMergeMapPatch.
func
CreateTwoWayMergeMapPatch
(
original
,
modified
JSONMap
,
dataStruct
interface
{},
fns
...
PreconditionFunc
)
(
JSONMap
,
error
)
{
t
,
err
:=
getTagStructType
(
dataStruct
)
if
err
!=
nil
{
return
nil
,
err
}
patchMap
,
err
:=
diffMaps
(
original
Map
,
modifiedMap
,
t
,
false
,
false
)
patchMap
,
err
:=
diffMaps
(
original
,
modified
,
t
,
false
,
false
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -177,7 +191,7 @@ func CreateTwoWayMergePatch(original, modified []byte, dataStruct interface{}, f
}
}
return
json
.
Marshal
(
patchMap
)
return
patchMap
,
nil
}
// Returns a (recursive) strategic merge patch that yields modified when applied to original.
...
...
@@ -494,12 +508,6 @@ loopB:
return
patch
,
nil
}
// Deprecated: StrategicMergePatchData is deprecated. Use the synonym StrategicMergePatch,
// instead, which follows the naming convention of evanphx/json-patch.
func
StrategicMergePatchData
(
original
,
patch
[]
byte
,
dataStruct
interface
{})
([]
byte
,
error
)
{
return
StrategicMergePatch
(
original
,
patch
,
dataStruct
)
}
// StrategicMergePatch applies a strategic merge patch. The patch and the original document
// must be json encoded content. A patch can be created from an original and a modified document
// by calling CreateStrategicMergePatch.
...
...
@@ -524,17 +532,23 @@ func StrategicMergePatch(original, patch []byte, dataStruct interface{}) ([]byte
return
nil
,
errBadJSONDoc
}
t
,
err
:=
getTagStructType
(
dataStruct
)
result
,
err
:=
StrategicMergeMapPatch
(
originalMap
,
patchMap
,
dataStruct
)
if
err
!=
nil
{
return
nil
,
err
}
result
,
err
:=
mergeMap
(
originalMap
,
patchMap
,
t
,
true
)
return
json
.
Marshal
(
result
)
}
// StrategicMergePatch applies a strategic merge patch. The original and patch documents
// must be JSONMap. A patch can be created from an original and modified document by
// calling CreateTwoWayMergeMapPatch.
func
StrategicMergeMapPatch
(
original
,
patch
JSONMap
,
dataStruct
interface
{})
(
JSONMap
,
error
)
{
t
,
err
:=
getTagStructType
(
dataStruct
)
if
err
!=
nil
{
return
nil
,
err
}
return
json
.
Marshal
(
result
)
return
mergeMap
(
original
,
patch
,
t
,
true
)
}
func
getTagStructType
(
dataStruct
interface
{})
(
reflect
.
Type
,
error
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment