diff --git a/api/v1alpha1/nodeselectorchaos_types.go b/api/v1alpha1/nodeselectorchaos_types.go index 5279c9e9b1..6599fccf6e 100644 --- a/api/v1alpha1/nodeselectorchaos_types.go +++ b/api/v1alpha1/nodeselectorchaos_types.go @@ -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 diff --git a/config/crd/bases/chaos-mesh.org_nodeselectorchaos.yaml b/config/crd/bases/chaos-mesh.org_nodeselectorchaos.yaml index 9efba320a9..ee6eedc9a5 100644 --- a/config/crd/bases/chaos-mesh.org_nodeselectorchaos.yaml +++ b/config/crd/bases/chaos-mesh.org_nodeselectorchaos.yaml @@ -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: diff --git a/config/crd/bases/chaos-mesh.org_schedules.yaml b/config/crd/bases/chaos-mesh.org_schedules.yaml index a216dcdf1c..6b4d46745a 100644 --- a/config/crd/bases/chaos-mesh.org_schedules.yaml +++ b/config/crd/bases/chaos-mesh.org_schedules.yaml @@ -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 @@ -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 @@ -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 diff --git a/config/crd/bases/chaos-mesh.org_workflownodes.yaml b/config/crd/bases/chaos-mesh.org_workflownodes.yaml index e0f8dd7686..c616cafdca 100644 --- a/config/crd/bases/chaos-mesh.org_workflownodes.yaml +++ b/config/crd/bases/chaos-mesh.org_workflownodes.yaml @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/config/crd/bases/chaos-mesh.org_workflows.yaml b/config/crd/bases/chaos-mesh.org_workflows.yaml index 87250c8018..6888a1b79a 100644 --- a/config/crd/bases/chaos-mesh.org_workflows.yaml +++ b/config/crd/bases/chaos-mesh.org_workflows.yaml @@ -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 @@ -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 diff --git a/controllers/chaosimpl/nodeselectorchaos/impl.go b/controllers/chaosimpl/nodeselectorchaos/impl.go index 2449a2dbf7..1cb44b1bb4 100644 --- a/controllers/chaosimpl/nodeselectorchaos/impl.go +++ b/controllers/chaosimpl/nodeselectorchaos/impl.go @@ -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 } diff --git a/helm/chaos-mesh/crds/chaos-mesh.org_nodeselectorchaos.yaml b/helm/chaos-mesh/crds/chaos-mesh.org_nodeselectorchaos.yaml index 9efba320a9..ee6eedc9a5 100644 --- a/helm/chaos-mesh/crds/chaos-mesh.org_nodeselectorchaos.yaml +++ b/helm/chaos-mesh/crds/chaos-mesh.org_nodeselectorchaos.yaml @@ -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: diff --git a/helm/chaos-mesh/crds/chaos-mesh.org_schedules.yaml b/helm/chaos-mesh/crds/chaos-mesh.org_schedules.yaml index a216dcdf1c..6b4d46745a 100644 --- a/helm/chaos-mesh/crds/chaos-mesh.org_schedules.yaml +++ b/helm/chaos-mesh/crds/chaos-mesh.org_schedules.yaml @@ -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 @@ -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 @@ -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 diff --git a/helm/chaos-mesh/crds/chaos-mesh.org_workflownodes.yaml b/helm/chaos-mesh/crds/chaos-mesh.org_workflownodes.yaml index e0f8dd7686..c616cafdca 100644 --- a/helm/chaos-mesh/crds/chaos-mesh.org_workflownodes.yaml +++ b/helm/chaos-mesh/crds/chaos-mesh.org_workflownodes.yaml @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/helm/chaos-mesh/crds/chaos-mesh.org_workflows.yaml b/helm/chaos-mesh/crds/chaos-mesh.org_workflows.yaml index 87250c8018..6888a1b79a 100644 --- a/helm/chaos-mesh/crds/chaos-mesh.org_workflows.yaml +++ b/helm/chaos-mesh/crds/chaos-mesh.org_workflows.yaml @@ -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 @@ -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 diff --git a/manifests/crd.yaml b/manifests/crd.yaml index 0a24076173..8eefc7e5ad 100644 --- a/manifests/crd.yaml +++ b/manifests/crd.yaml @@ -4257,12 +4257,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: @@ -9433,11 +9433,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 @@ -13566,12 +13566,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 @@ -17467,12 +17466,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 @@ -27738,11 +27736,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 @@ -31393,11 +31391,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 @@ -35625,12 +35623,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 @@ -39656,12 +39653,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 @@ -54304,12 +54301,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 @@ -58102,12 +58098,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 diff --git a/pkg/dashboard/swaggerdocs/docs.go b/pkg/dashboard/swaggerdocs/docs.go index fbd098db4d..863aa8417f 100644 --- a/pkg/dashboard/swaggerdocs/docs.go +++ b/pkg/dashboard/swaggerdocs/docs.go @@ -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" } } diff --git a/pkg/dashboard/swaggerdocs/swagger.json b/pkg/dashboard/swaggerdocs/swagger.json index e822b56c59..7b313ec1ad 100644 --- a/pkg/dashboard/swaggerdocs/swagger.json +++ b/pkg/dashboard/swaggerdocs/swagger.json @@ -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" } } diff --git a/pkg/dashboard/swaggerdocs/swagger.yaml b/pkg/dashboard/swaggerdocs/swagger.yaml index fcdd22e992..64f55b2d96 100644 --- a/pkg/dashboard/swaggerdocs/swagger.yaml +++ b/pkg/dashboard/swaggerdocs/swagger.yaml @@ -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: