Skip to content

Commit

Permalink
fix: Fix recover step for NodeSelectorChaos
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolaj-krzyzanowski-f3 committed Jan 14, 2025
1 parent bdc0e4f commit f8824f6
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 75 deletions.
3 changes: 2 additions & 1 deletion api/v1alpha1/nodeselectorchaos_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ type NodeSelectorChaosSpec struct {
DeploymentSelectorSpec `json:"selector"`
// Key is the name of the key that will be applied to the deployment's nodeSelector field.
Key string `json:"key"`
// Value is the value assigned to the provided key.
// Value is the value assigned to the provided key. If empty, the key will be removed.
// +optional
Value string `json:"value"`
// Duration represents the duration of the chaos
// +optional
Expand Down
4 changes: 2 additions & 2 deletions config/crd/bases/chaos-mesh.org_nodeselectorchaos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ spec:
type: object
type: object
value:
description: Value is the value assigned to the provided key.
description: Value is the value assigned to the provided key. If empty,
the key will be removed.
type: string
required:
- key
- selector
- value
type: object
status:
properties:
Expand Down
8 changes: 3 additions & 5 deletions config/crd/bases/chaos-mesh.org_schedules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2299,11 +2299,11 @@ spec:
type: object
value:
description: Value is the value assigned to the provided key.
If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired state of
Expand Down Expand Up @@ -6432,12 +6432,11 @@ spec:
type: object
value:
description: Value is the value assigned to the provided
key.
key. If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired
Expand Down Expand Up @@ -10333,12 +10332,11 @@ spec:
type: object
value:
description: Value is the value assigned to the
provided key.
provided key. If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired
Expand Down
11 changes: 5 additions & 6 deletions config/crd/bases/chaos-mesh.org_workflownodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2320,11 +2320,11 @@ spec:
type: object
value:
description: Value is the value assigned to the provided key.
If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired state of
Expand Down Expand Up @@ -5975,11 +5975,11 @@ spec:
type: object
value:
description: Value is the value assigned to the provided key.
If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired state
Expand Down Expand Up @@ -10207,12 +10207,11 @@ spec:
type: object
value:
description: Value is the value assigned to the
provided key.
provided key. If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired
Expand Down Expand Up @@ -14238,12 +14237,12 @@ spec:
type: object
value:
description: Value is the value assigned to
the provided key.
the provided key. If empty, the key will be
removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the
Expand Down
6 changes: 2 additions & 4 deletions config/crd/bases/chaos-mesh.org_workflows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2408,12 +2408,11 @@ spec:
type: object
value:
description: Value is the value assigned to the provided
key.
key. If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired state
Expand Down Expand Up @@ -6206,12 +6205,11 @@ spec:
type: object
value:
description: Value is the value assigned to the provided
key.
key. If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired
Expand Down
55 changes: 18 additions & 37 deletions controllers/chaosimpl/nodeselectorchaos/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,48 +60,29 @@ func (i *Impl) Apply(ctx context.Context, index int, records []*v1alpha1.Record,
return v1alpha1.NotInjected, err
}

data := []byte(fmt.Sprintf(`{"spec": {"template": {"spec": {"nodeSelector": {"%s" :"%s"}}}}}`, chaos.Spec.Key, chaos.Spec.Value))
patch := client.RawPatch(types.MergePatchType, data)
err = i.Client.Patch(ctx, &deployment, patch)
if err != nil {
i.Log.Error(err, "patching deployment")
return v1alpha1.NotInjected, err
if chaos.Spec.Value == "" {
escapedKey := strings.ReplaceAll(chaos.Spec.Key, "/", "~1")
data := []byte(fmt.Sprintf(`[{"op": "remove", "path": "/spec/template/spec/nodeSelector/%s"}]`, escapedKey))
patch := client.RawPatch(types.JSONPatchType, data)
err = i.Client.Patch(ctx, &deployment, patch)
if err != nil {
i.Log.Error(err, "patching deployment")
return v1alpha1.NotInjected, err
}
} else {
data := []byte(fmt.Sprintf(`{"spec": {"template": {"spec": {"nodeSelector": {"%s" :"%s"}}}}}`, chaos.Spec.Key, chaos.Spec.Value))
patch := client.RawPatch(types.MergePatchType, data)
err = i.Client.Patch(ctx, &deployment, patch)
if err != nil {
i.Log.Error(err, "patching deployment")
return v1alpha1.NotInjected, err
}
}

return v1alpha1.Injected, nil
}

func (i *Impl) Recover(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
chaos, ok := obj.(*v1alpha1.NodeSelectorChaos)

if !ok {
err := errors.New("not NodeSelectorChaos")
i.Log.Error(err, "casting InnerObject to NodeSelectorChaos")
return v1alpha1.NotInjected, err
}

name, err := controller.ParseNamespacedName(records[index].Id)
if err != nil {
i.Log.Error(err, "parsing record name")
return v1alpha1.Injected, err
}

var deployment v1.Deployment
err = i.Client.Get(ctx, name, &deployment)
if err != nil {
i.Log.Error(err, "getting deployment")
return v1alpha1.Injected, err
}

escapedKey := strings.ReplaceAll(chaos.Spec.Key, "/", "~1")
data := []byte(fmt.Sprintf(`[{"op": "remove", "path": "/spec/template/spec/nodeSelector/%s"}]`, escapedKey))
patch := client.RawPatch(types.JSONPatchType, data)
err = i.Client.Patch(ctx, &deployment, patch)
if err != nil {
i.Log.Error(err, "patching deployment")
return v1alpha1.Injected, err
}

func (i *Impl) Recover(_ context.Context, _ int, _ []*v1alpha1.Record, _ v1alpha1.InnerObject) (v1alpha1.Phase, error) {
return v1alpha1.NotInjected, nil
}

Expand Down
4 changes: 2 additions & 2 deletions helm/chaos-mesh/crds/chaos-mesh.org_nodeselectorchaos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ spec:
type: object
type: object
value:
description: Value is the value assigned to the provided key.
description: Value is the value assigned to the provided key. If empty,
the key will be removed.
type: string
required:
- key
- selector
- value
type: object
status:
properties:
Expand Down
8 changes: 3 additions & 5 deletions helm/chaos-mesh/crds/chaos-mesh.org_schedules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2299,11 +2299,11 @@ spec:
type: object
value:
description: Value is the value assigned to the provided key.
If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired state of
Expand Down Expand Up @@ -6432,12 +6432,11 @@ spec:
type: object
value:
description: Value is the value assigned to the provided
key.
key. If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired
Expand Down Expand Up @@ -10333,12 +10332,11 @@ spec:
type: object
value:
description: Value is the value assigned to the
provided key.
provided key. If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired
Expand Down
11 changes: 5 additions & 6 deletions helm/chaos-mesh/crds/chaos-mesh.org_workflownodes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2320,11 +2320,11 @@ spec:
type: object
value:
description: Value is the value assigned to the provided key.
If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired state of
Expand Down Expand Up @@ -5975,11 +5975,11 @@ spec:
type: object
value:
description: Value is the value assigned to the provided key.
If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired state
Expand Down Expand Up @@ -10207,12 +10207,11 @@ spec:
type: object
value:
description: Value is the value assigned to the
provided key.
provided key. If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired
Expand Down Expand Up @@ -14238,12 +14237,12 @@ spec:
type: object
value:
description: Value is the value assigned to
the provided key.
the provided key. If empty, the key will be
removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the
Expand Down
6 changes: 2 additions & 4 deletions helm/chaos-mesh/crds/chaos-mesh.org_workflows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2408,12 +2408,11 @@ spec:
type: object
value:
description: Value is the value assigned to the provided
key.
key. If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired state
Expand Down Expand Up @@ -6206,12 +6205,11 @@ spec:
type: object
value:
description: Value is the value assigned to the provided
key.
key. If empty, the key will be removed.
type: string
required:
- key
- selector
- value
type: object
physicalmachineChaos:
description: PhysicalMachineChaosSpec defines the desired
Expand Down
2 changes: 1 addition & 1 deletion pkg/dashboard/swaggerdocs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7302,7 +7302,7 @@ const docTemplate = `{
"type": "string"
},
"value": {
"description": "Value is the value assigned to the provided key.",
"description": "Value is the value assigned to the provided key. If empty, the key will be removed.\n+optional",
"type": "string"
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/dashboard/swaggerdocs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -7294,7 +7294,7 @@
"type": "string"
},
"value": {
"description": "Value is the value assigned to the provided key.",
"description": "Value is the value assigned to the provided key. If empty, the key will be removed.\n+optional",
"type": "string"
}
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/dashboard/swaggerdocs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5762,7 +5762,9 @@ definitions:
+optional
type: string
value:
description: Value is the value assigned to the provided key.
description: |-
Value is the value assigned to the provided key. If empty, the key will be removed.
+optional
type: string
type: object
v1alpha1.NodeSelectorSpec:
Expand Down

0 comments on commit f8824f6

Please sign in to comment.