Skip to content

Commit 2ac1879

Browse files
fix: Fix recover step for NodeSelectorChaos
1 parent bdc0e4f commit 2ac1879

File tree

2 files changed

+20
-38
lines changed

2 files changed

+20
-38
lines changed

api/v1alpha1/nodeselectorchaos_types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ type NodeSelectorChaosSpec struct {
3838
DeploymentSelectorSpec `json:"selector"`
3939
// Key is the name of the key that will be applied to the deployment's nodeSelector field.
4040
Key string `json:"key"`
41-
// Value is the value assigned to the provided key.
41+
// Value is the value assigned to the provided key. If empty, the key will be removed.
42+
// +optional
4243
Value string `json:"value"`
4344
// Duration represents the duration of the chaos
4445
// +optional

controllers/chaosimpl/nodeselectorchaos/impl.go

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)