Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix recover step for NodeSelectorChaos #100

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading
Loading