Fix cannot add object use plaintext overide regardless of whether the path exists or not

Fix cannot remove object use plaintext overide regardless of whether the path exists or not

Signed-off-by: xiaoqing <xiaoqingnb@gmail.com>
This commit is contained in:
xiaoqing 2023-08-09 22:15:28 +08:00
parent 37cee8d51d
commit 63b6411bc6
2 changed files with 116 additions and 1 deletions

View File

@ -255,7 +255,10 @@ func applyJSONPatch(obj *unstructured.Unstructured, overrides []overrideOption)
return err
}
patchedObjectJSONBytes, err := patch.Apply(objectJSONBytes)
applyOption := jsonpatch.NewApplyOptions()
applyOption.EnsurePathExistsOnAdd = true
applyOption.AllowMissingPathOnRemove = true
patchedObjectJSONBytes, err := patch.ApplyWithOptions(objectJSONBytes, applyOption)
if err != nil {
return err
}

View File

@ -87,6 +87,34 @@ func Test_overrideManagerImpl_ApplyOverridePolicies(t *testing.T) {
},
},
},
Plaintext: []policyv1alpha1.PlaintextOverrider{
{
// add on not exist path
Path: "/spec/template/metadata/annotations/testAddAnnotation",
Operator: policyv1alpha1.OverriderOpAdd,
Value: apiextensionsv1.JSON{
Raw: []byte(`"testAddAnnotation"`),
},
},
{
// add on exist path
Path: "/spec/template/metadata/labels/testAddLabel",
Operator: policyv1alpha1.OverriderOpAdd,
Value: apiextensionsv1.JSON{
Raw: []byte(`"testAddLabel"`),
},
},
{
// remove on not exist path
Path: "/notexist",
Operator: policyv1alpha1.OverriderOpRemove,
},
{
// remove on exist path
Path: "/spec/replicas",
Operator: policyv1alpha1.OverriderOpRemove,
},
},
},
},
},
@ -121,6 +149,34 @@ func Test_overrideManagerImpl_ApplyOverridePolicies(t *testing.T) {
},
},
},
Plaintext: []policyv1alpha1.PlaintextOverrider{
{
// add on not exist path
Path: "/spec/template/metadata/annotations/testAddAnnotation",
Operator: policyv1alpha1.OverriderOpAdd,
Value: apiextensionsv1.JSON{
Raw: []byte(`"testAddAnnotation"`),
},
},
{
// add on exist path
Path: "/spec/template/metadata/labels/testAddLabel",
Operator: policyv1alpha1.OverriderOpAdd,
Value: apiextensionsv1.JSON{
Raw: []byte(`"testAddLabel"`),
},
},
{
// remove on not exist path
Path: "/notexist",
Operator: policyv1alpha1.OverriderOpRemove,
},
{
// remove on exist path
Path: "/spec/replicas",
Operator: policyv1alpha1.OverriderOpRemove,
},
},
},
},
},
@ -161,6 +217,34 @@ func Test_overrideManagerImpl_ApplyOverridePolicies(t *testing.T) {
},
},
},
Plaintext: []policyv1alpha1.PlaintextOverrider{
{
// add on not exist path
Path: "/spec/template/metadata/annotations/testAddAnnotation",
Operator: policyv1alpha1.OverriderOpAdd,
Value: apiextensionsv1.JSON{
Raw: []byte(`"testAddAnnotation"`),
},
},
{
// add on exist path
Path: "/spec/template/metadata/labels/testAddLabel",
Operator: policyv1alpha1.OverriderOpAdd,
Value: apiextensionsv1.JSON{
Raw: []byte(`"testAddLabel"`),
},
},
{
// remove on not exist path
Path: "/notexist",
Operator: policyv1alpha1.OverriderOpRemove,
},
{
// remove on exist path
Path: "/spec/replicas",
Operator: policyv1alpha1.OverriderOpRemove,
},
},
},
},
},
@ -194,6 +278,34 @@ func Test_overrideManagerImpl_ApplyOverridePolicies(t *testing.T) {
},
},
},
Plaintext: []policyv1alpha1.PlaintextOverrider{
{
// add on not exist path
Path: "/spec/template/metadata/annotations/testAddAnnotation",
Operator: policyv1alpha1.OverriderOpAdd,
Value: apiextensionsv1.JSON{
Raw: []byte(`"testAddAnnotation"`),
},
},
{
// add on exist path
Path: "/spec/template/metadata/labels/testAddLabel",
Operator: policyv1alpha1.OverriderOpAdd,
Value: apiextensionsv1.JSON{
Raw: []byte(`"testAddLabel"`),
},
},
{
// remove on not exist path
Path: "/notexist",
Operator: policyv1alpha1.OverriderOpRemove,
},
{
// remove on exist path
Path: "/spec/replicas",
Operator: policyv1alpha1.OverriderOpRemove,
},
},
},
},
},