@@ -60,48 +60,29 @@ func (i *Impl) Apply(ctx context.Context, index int, records []*v1alpha1.Record,
6060 return v1alpha1 .NotInjected , err
6161 }
6262
63- data := []byte (fmt .Sprintf (`{"spec": {"template": {"spec": {"nodeSelector": {"%s" :"%s"}}}}}` , chaos .Spec .Key , chaos .Spec .Value ))
64- patch := client .RawPatch (types .MergePatchType , data )
65- err = i .Client .Patch (ctx , & deployment , patch )
66- if err != nil {
67- i .Log .Error (err , "patching deployment" )
68- return v1alpha1 .NotInjected , err
63+ if chaos .Spec .Value == "" {
64+ escapedKey := strings .ReplaceAll (chaos .Spec .Key , "/" , "~1" )
65+ data := []byte (fmt .Sprintf (`[{"op": "remove", "path": "/spec/template/spec/nodeSelector/%s"}]` , escapedKey ))
66+ patch := client .RawPatch (types .JSONPatchType , data )
67+ err = i .Client .Patch (ctx , & deployment , patch )
68+ if err != nil {
69+ i .Log .Error (err , "patching deployment" )
70+ return v1alpha1 .NotInjected , err
71+ }
72+ } else {
73+ data := []byte (fmt .Sprintf (`{"spec": {"template": {"spec": {"nodeSelector": {"%s" :"%s"}}}}}` , chaos .Spec .Key , chaos .Spec .Value ))
74+ patch := client .RawPatch (types .MergePatchType , data )
75+ err = i .Client .Patch (ctx , & deployment , patch )
76+ if err != nil {
77+ i .Log .Error (err , "patching deployment" )
78+ return v1alpha1 .NotInjected , err
79+ }
6980 }
7081
7182 return v1alpha1 .Injected , nil
7283}
7384
74- func (i * Impl ) Recover (ctx context.Context , index int , records []* v1alpha1.Record , obj v1alpha1.InnerObject ) (v1alpha1.Phase , error ) {
75- chaos , ok := obj .(* v1alpha1.NodeSelectorChaos )
76-
77- if ! ok {
78- err := errors .New ("not NodeSelectorChaos" )
79- i .Log .Error (err , "casting InnerObject to NodeSelectorChaos" )
80- return v1alpha1 .NotInjected , err
81- }
82-
83- name , err := controller .ParseNamespacedName (records [index ].Id )
84- if err != nil {
85- i .Log .Error (err , "parsing record name" )
86- return v1alpha1 .Injected , err
87- }
88-
89- var deployment v1.Deployment
90- err = i .Client .Get (ctx , name , & deployment )
91- if err != nil {
92- i .Log .Error (err , "getting deployment" )
93- return v1alpha1 .Injected , err
94- }
95-
96- escapedKey := strings .ReplaceAll (chaos .Spec .Key , "/" , "~1" )
97- data := []byte (fmt .Sprintf (`[{"op": "remove", "path": "/spec/template/spec/nodeSelector/%s"}]` , escapedKey ))
98- patch := client .RawPatch (types .JSONPatchType , data )
99- err = i .Client .Patch (ctx , & deployment , patch )
100- if err != nil {
101- i .Log .Error (err , "patching deployment" )
102- return v1alpha1 .Injected , err
103- }
104-
85+ func (i * Impl ) Recover (_ context.Context , _ int , _ []* v1alpha1.Record , _ v1alpha1.InnerObject ) (v1alpha1.Phase , error ) {
10586 return v1alpha1 .NotInjected , nil
10687}
10788
0 commit comments