Skip to content

Commit 3ee7232

Browse files
fix: Fix recover step for NodeSelectorChaos (#100)
1 parent bdc0e4f commit 3ee7232

File tree

14 files changed

+61
-92
lines changed

14 files changed

+61
-92
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

config/crd/bases/chaos-mesh.org_nodeselectorchaos.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ spec:
5555
type: object
5656
type: object
5757
value:
58-
description: Value is the value assigned to the provided key.
58+
description: Value is the value assigned to the provided key. If empty,
59+
the key will be removed.
5960
type: string
6061
required:
6162
- key
6263
- selector
63-
- value
6464
type: object
6565
status:
6666
properties:

config/crd/bases/chaos-mesh.org_schedules.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,11 +2299,11 @@ spec:
22992299
type: object
23002300
value:
23012301
description: Value is the value assigned to the provided key.
2302+
If empty, the key will be removed.
23022303
type: string
23032304
required:
23042305
- key
23052306
- selector
2306-
- value
23072307
type: object
23082308
physicalmachineChaos:
23092309
description: PhysicalMachineChaosSpec defines the desired state of
@@ -6432,12 +6432,11 @@ spec:
64326432
type: object
64336433
value:
64346434
description: Value is the value assigned to the provided
6435-
key.
6435+
key. If empty, the key will be removed.
64366436
type: string
64376437
required:
64386438
- key
64396439
- selector
6440-
- value
64416440
type: object
64426441
physicalmachineChaos:
64436442
description: PhysicalMachineChaosSpec defines the desired
@@ -10333,12 +10332,11 @@ spec:
1033310332
type: object
1033410333
value:
1033510334
description: Value is the value assigned to the
10336-
provided key.
10335+
provided key. If empty, the key will be removed.
1033710336
type: string
1033810337
required:
1033910338
- key
1034010339
- selector
10341-
- value
1034210340
type: object
1034310341
physicalmachineChaos:
1034410342
description: PhysicalMachineChaosSpec defines the desired

config/crd/bases/chaos-mesh.org_workflownodes.yaml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,11 +2320,11 @@ spec:
23202320
type: object
23212321
value:
23222322
description: Value is the value assigned to the provided key.
2323+
If empty, the key will be removed.
23232324
type: string
23242325
required:
23252326
- key
23262327
- selector
2327-
- value
23282328
type: object
23292329
physicalmachineChaos:
23302330
description: PhysicalMachineChaosSpec defines the desired state of
@@ -5975,11 +5975,11 @@ spec:
59755975
type: object
59765976
value:
59775977
description: Value is the value assigned to the provided key.
5978+
If empty, the key will be removed.
59785979
type: string
59795980
required:
59805981
- key
59815982
- selector
5982-
- value
59835983
type: object
59845984
physicalmachineChaos:
59855985
description: PhysicalMachineChaosSpec defines the desired state
@@ -10207,12 +10207,11 @@ spec:
1020710207
type: object
1020810208
value:
1020910209
description: Value is the value assigned to the
10210-
provided key.
10210+
provided key. If empty, the key will be removed.
1021110211
type: string
1021210212
required:
1021310213
- key
1021410214
- selector
10215-
- value
1021610215
type: object
1021710216
physicalmachineChaos:
1021810217
description: PhysicalMachineChaosSpec defines the desired
@@ -14238,12 +14237,12 @@ spec:
1423814237
type: object
1423914238
value:
1424014239
description: Value is the value assigned to
14241-
the provided key.
14240+
the provided key. If empty, the key will be
14241+
removed.
1424214242
type: string
1424314243
required:
1424414244
- key
1424514245
- selector
14246-
- value
1424714246
type: object
1424814247
physicalmachineChaos:
1424914248
description: PhysicalMachineChaosSpec defines the

config/crd/bases/chaos-mesh.org_workflows.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,12 +2408,11 @@ spec:
24082408
type: object
24092409
value:
24102410
description: Value is the value assigned to the provided
2411-
key.
2411+
key. If empty, the key will be removed.
24122412
type: string
24132413
required:
24142414
- key
24152415
- selector
2416-
- value
24172416
type: object
24182417
physicalmachineChaos:
24192418
description: PhysicalMachineChaosSpec defines the desired state
@@ -6206,12 +6205,11 @@ spec:
62066205
type: object
62076206
value:
62086207
description: Value is the value assigned to the provided
6209-
key.
6208+
key. If empty, the key will be removed.
62106209
type: string
62116210
required:
62126211
- key
62136212
- selector
6214-
- value
62156213
type: object
62166214
physicalmachineChaos:
62176215
description: PhysicalMachineChaosSpec defines the desired

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

helm/chaos-mesh/crds/chaos-mesh.org_nodeselectorchaos.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ spec:
5555
type: object
5656
type: object
5757
value:
58-
description: Value is the value assigned to the provided key.
58+
description: Value is the value assigned to the provided key. If empty,
59+
the key will be removed.
5960
type: string
6061
required:
6162
- key
6263
- selector
63-
- value
6464
type: object
6565
status:
6666
properties:

helm/chaos-mesh/crds/chaos-mesh.org_schedules.yaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,11 +2299,11 @@ spec:
22992299
type: object
23002300
value:
23012301
description: Value is the value assigned to the provided key.
2302+
If empty, the key will be removed.
23022303
type: string
23032304
required:
23042305
- key
23052306
- selector
2306-
- value
23072307
type: object
23082308
physicalmachineChaos:
23092309
description: PhysicalMachineChaosSpec defines the desired state of
@@ -6432,12 +6432,11 @@ spec:
64326432
type: object
64336433
value:
64346434
description: Value is the value assigned to the provided
6435-
key.
6435+
key. If empty, the key will be removed.
64366436
type: string
64376437
required:
64386438
- key
64396439
- selector
6440-
- value
64416440
type: object
64426441
physicalmachineChaos:
64436442
description: PhysicalMachineChaosSpec defines the desired
@@ -10333,12 +10332,11 @@ spec:
1033310332
type: object
1033410333
value:
1033510334
description: Value is the value assigned to the
10336-
provided key.
10335+
provided key. If empty, the key will be removed.
1033710336
type: string
1033810337
required:
1033910338
- key
1034010339
- selector
10341-
- value
1034210340
type: object
1034310341
physicalmachineChaos:
1034410342
description: PhysicalMachineChaosSpec defines the desired

helm/chaos-mesh/crds/chaos-mesh.org_workflownodes.yaml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2320,11 +2320,11 @@ spec:
23202320
type: object
23212321
value:
23222322
description: Value is the value assigned to the provided key.
2323+
If empty, the key will be removed.
23232324
type: string
23242325
required:
23252326
- key
23262327
- selector
2327-
- value
23282328
type: object
23292329
physicalmachineChaos:
23302330
description: PhysicalMachineChaosSpec defines the desired state of
@@ -5975,11 +5975,11 @@ spec:
59755975
type: object
59765976
value:
59775977
description: Value is the value assigned to the provided key.
5978+
If empty, the key will be removed.
59785979
type: string
59795980
required:
59805981
- key
59815982
- selector
5982-
- value
59835983
type: object
59845984
physicalmachineChaos:
59855985
description: PhysicalMachineChaosSpec defines the desired state
@@ -10207,12 +10207,11 @@ spec:
1020710207
type: object
1020810208
value:
1020910209
description: Value is the value assigned to the
10210-
provided key.
10210+
provided key. If empty, the key will be removed.
1021110211
type: string
1021210212
required:
1021310213
- key
1021410214
- selector
10215-
- value
1021610215
type: object
1021710216
physicalmachineChaos:
1021810217
description: PhysicalMachineChaosSpec defines the desired
@@ -14238,12 +14237,12 @@ spec:
1423814237
type: object
1423914238
value:
1424014239
description: Value is the value assigned to
14241-
the provided key.
14240+
the provided key. If empty, the key will be
14241+
removed.
1424214242
type: string
1424314243
required:
1424414244
- key
1424514245
- selector
14246-
- value
1424714246
type: object
1424814247
physicalmachineChaos:
1424914248
description: PhysicalMachineChaosSpec defines the

helm/chaos-mesh/crds/chaos-mesh.org_workflows.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,12 +2408,11 @@ spec:
24082408
type: object
24092409
value:
24102410
description: Value is the value assigned to the provided
2411-
key.
2411+
key. If empty, the key will be removed.
24122412
type: string
24132413
required:
24142414
- key
24152415
- selector
2416-
- value
24172416
type: object
24182417
physicalmachineChaos:
24192418
description: PhysicalMachineChaosSpec defines the desired state
@@ -6206,12 +6205,11 @@ spec:
62066205
type: object
62076206
value:
62086207
description: Value is the value assigned to the provided
6209-
key.
6208+
key. If empty, the key will be removed.
62106209
type: string
62116210
required:
62126211
- key
62136212
- selector
6214-
- value
62156213
type: object
62166214
physicalmachineChaos:
62176215
description: PhysicalMachineChaosSpec defines the desired

0 commit comments

Comments
 (0)