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:
parent
37cee8d51d
commit
63b6411bc6
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue