-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement patch function in ManifestWorkAgentClient
Signed-off-by: Wei Liu <liuweixa@redhat.com>
- Loading branch information
Showing
3 changed files
with
212 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
workv1 "open-cluster-management.io/api/work/v1" | ||
) | ||
|
||
func TestPatch(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
patchType types.PatchType | ||
work *workv1.ManifestWork | ||
patch []byte | ||
validate func(t *testing.T, work *workv1.ManifestWork) | ||
}{ | ||
{ | ||
name: "json patch", | ||
patchType: types.JSONPatchType, | ||
work: &workv1.ManifestWork{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
}, | ||
}, | ||
patch: []byte("[{\"op\":\"replace\",\"path\":\"/metadata/name\",\"value\":\"test1\"}]"), | ||
validate: func(t *testing.T, work *workv1.ManifestWork) { | ||
if work.Name != "test1" { | ||
t.Errorf("unexpected work %v", work) | ||
} | ||
}, | ||
}, | ||
{ | ||
name: "merge patch", | ||
patchType: types.MergePatchType, | ||
work: &workv1.ManifestWork{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test", | ||
}, | ||
}, | ||
patch: func() []byte { | ||
newWork := &workv1.ManifestWork{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test2", | ||
Namespace: "test2", | ||
}, | ||
} | ||
data, err := json.Marshal(newWork) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return data | ||
}(), | ||
validate: func(t *testing.T, work *workv1.ManifestWork) { | ||
if work.Name != "test2" { | ||
t.Errorf("unexpected work %v", work) | ||
} | ||
if work.Namespace != "test2" { | ||
t.Errorf("unexpected work %v", work) | ||
} | ||
}, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
work, err := Patch(c.patchType, c.work, c.patch) | ||
if err != nil { | ||
t.Errorf("unexpected error %v", err) | ||
} | ||
|
||
c.validate(t, work) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package utils | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
jsonpatch "github.com/evanphx/json-patch" | ||
"k8s.io/apimachinery/pkg/types" | ||
workv1 "open-cluster-management.io/api/work/v1" | ||
) | ||
|
||
// Patch applies the patch to a work with the patch type. | ||
func Patch(patchType types.PatchType, work *workv1.ManifestWork, patchData []byte) (*workv1.ManifestWork, error) { | ||
workData, err := json.Marshal(work) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var patchedData []byte | ||
switch patchType { | ||
case types.JSONPatchType: | ||
var patchObj jsonpatch.Patch | ||
patchObj, err = jsonpatch.DecodePatch(patchData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
patchedData, err = patchObj.Apply(workData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
case types.MergePatchType: | ||
patchedData, err = jsonpatch.MergePatch(workData, patchData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
default: | ||
return nil, fmt.Errorf("unsupported patch type: %s", patchType) | ||
} | ||
|
||
patchedWork := &workv1.ManifestWork{} | ||
if err := json.Unmarshal(patchedData, patchedWork); err != nil { | ||
return nil, err | ||
} | ||
|
||
return patchedWork, nil | ||
} |