From 0797de5b2d173660f922df0305abb65be881da3d Mon Sep 17 00:00:00 2001 From: Ben Ye Date: Sun, 1 Mar 2020 22:42:46 -0500 Subject: [PATCH] Support reorder in network delay chaos (#252) --- api/v1alpha1/networkchaos_types.go | 40 +++++++++++++++---- api/v1alpha1/podchaos_types.go | 2 +- api/v1alpha1/zz_generated.deepcopy.go | 22 +++++++++- .../crd/bases/pingcap.com_networkchaos.yaml | 16 +++++++- config/crd/bases/pingcap.com_podchaos.yaml | 2 +- manifests/crd.yaml | 18 ++++++++- 6 files changed, 87 insertions(+), 13 deletions(-) diff --git a/api/v1alpha1/networkchaos_types.go b/api/v1alpha1/networkchaos_types.go index bb7c25b810..0eb35dc06c 100644 --- a/api/v1alpha1/networkchaos_types.go +++ b/api/v1alpha1/networkchaos_types.go @@ -32,7 +32,7 @@ const ( // DelayAction represents the chaos action of adding delay on pods. DelayAction NetworkChaosAction = "delay" - // LossAction represents the chaos action of lossing packets on pods. + // LossAction represents the chaos action of losing packets on pods. LossAction NetworkChaosAction = "loss" // DuplicateAction represents the chaos action of duplicating packets on pods. @@ -126,7 +126,7 @@ type NetworkChaosSpec struct { // DuplicateSpec represents the detail about loss action Duplicate *DuplicateSpec `json:"duplicate,omitempty"` - // Corrupt represents the detail about loss action + // Corrupt represents the detail about corrupt action Corrupt *CorruptSpec `json:"corrupt,omitempty"` // Direction represents the partition direction @@ -256,9 +256,10 @@ func (in *NetworkChaos) Validate() (bool, string, error) { // DelaySpec defines detail of a delay action type DelaySpec struct { - Latency string `json:"latency"` - Correlation string `json:"correlation"` - Jitter string `json:"jitter"` + Latency string `json:"latency"` + Correlation string `json:"correlation"` + Jitter string `json:"jitter"` + Reorder *ReorderSpec `json:"reorder,omitempty"` } func (delay *DelaySpec) ToNetem() (*chaosdaemon.Netem, error) { @@ -276,11 +277,29 @@ func (delay *DelaySpec) ToNetem() (*chaosdaemon.Netem, error) { return nil, err } - return &chaosdaemon.Netem{ + netem := &chaosdaemon.Netem{ Time: uint32(delayTime.Nanoseconds() / 1e3), DelayCorr: float32(corr), Jitter: uint32(jitter.Nanoseconds() / 1e3), - }, nil + } + + if delay.Reorder != nil { + reorderPercentage, err := strconv.ParseFloat(delay.Reorder.Reorder, 32) + if err != nil { + return nil, err + } + + corr, err := strconv.ParseFloat(delay.Reorder.Correlation, 32) + if err != nil { + return nil, err + } + + netem.Reorder = float32(reorderPercentage) + netem.ReorderCorr = float32(corr) + netem.Gap = uint32(delay.Reorder.Gap) + } + + return netem, nil } // LossSpec defines detail of a loss action @@ -352,6 +371,13 @@ func (corrupt *CorruptSpec) ToNetem() (*chaosdaemon.Netem, error) { }, nil } +// ReorderSpec defines details of packet reorder. +type ReorderSpec struct { + Reorder string `json:"reorder"` + Correlation string `json:"correlation"` + Gap int `json:"gap"` +} + // +kubebuilder:object:root=true // NetworkChaosList contains a list of NetworkChaos diff --git a/api/v1alpha1/podchaos_types.go b/api/v1alpha1/podchaos_types.go index af08ff517f..fd3923acb0 100644 --- a/api/v1alpha1/podchaos_types.go +++ b/api/v1alpha1/podchaos_types.go @@ -121,7 +121,7 @@ type PodChaosSpec struct { Scheduler *SchedulerSpec `json:"scheduler,omitempty"` // Action defines the specific pod chaos action. - // Supported action: pod-kill / pod-failure + // Supported action: pod-kill / pod-failure / container-kill // Default action: pod-kill Action PodChaosAction `json:"action"` diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 6685958e8e..eaa032b2d4 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -57,6 +57,11 @@ func (in *CorruptSpec) DeepCopy() *CorruptSpec { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *DelaySpec) DeepCopyInto(out *DelaySpec) { *out = *in + if in.Reorder != nil { + in, out := &in.Reorder, &out.Reorder + *out = new(ReorderSpec) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new DelaySpec. @@ -317,7 +322,7 @@ func (in *NetworkChaosSpec) DeepCopyInto(out *NetworkChaosSpec) { if in.Delay != nil { in, out := &in.Delay, &out.Delay *out = new(DelaySpec) - **out = **in + (*in).DeepCopyInto(*out) } if in.Loss != nil { in, out := &in.Loss, &out.Loss @@ -511,6 +516,21 @@ func (in *PodStatus) DeepCopy() *PodStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *ReorderSpec) DeepCopyInto(out *ReorderSpec) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ReorderSpec. +func (in *ReorderSpec) DeepCopy() *ReorderSpec { + if in == nil { + return nil + } + out := new(ReorderSpec) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *SchedulerSpec) DeepCopyInto(out *SchedulerSpec) { *out = *in diff --git a/config/crd/bases/pingcap.com_networkchaos.yaml b/config/crd/bases/pingcap.com_networkchaos.yaml index a185f90cad..7c076de521 100644 --- a/config/crd/bases/pingcap.com_networkchaos.yaml +++ b/config/crd/bases/pingcap.com_networkchaos.yaml @@ -39,7 +39,7 @@ spec: action: delay Default action: delay' type: string corrupt: - description: Corrupt represents the detail about loss action + description: Corrupt represents the detail about corrupt action properties: correlation: type: string @@ -58,6 +58,20 @@ spec: type: string latency: type: string + reorder: + description: ReorderSpec defines details of packet reorder. + properties: + correlation: + type: string + gap: + type: integer + reorder: + type: string + required: + - correlation + - gap + - reorder + type: object required: - correlation - jitter diff --git a/config/crd/bases/pingcap.com_podchaos.yaml b/config/crd/bases/pingcap.com_podchaos.yaml index 626f8cfc18..0e25c0f97e 100644 --- a/config/crd/bases/pingcap.com_podchaos.yaml +++ b/config/crd/bases/pingcap.com_podchaos.yaml @@ -36,7 +36,7 @@ spec: properties: action: description: 'Action defines the specific pod chaos action. Supported - action: pod-kill / pod-failure Default action: pod-kill' + action: pod-kill / pod-failure / container-kill Default action: pod-kill' type: string containerName: description: ContainerName indicates the name of the container. Needed diff --git a/manifests/crd.yaml b/manifests/crd.yaml index da9bc17d2b..8309f2cee1 100644 --- a/manifests/crd.yaml +++ b/manifests/crd.yaml @@ -286,7 +286,7 @@ spec: action: delay Default action: delay' type: string corrupt: - description: Corrupt represents the detail about loss action + description: Corrupt represents the detail about corrupt action properties: correlation: type: string @@ -305,6 +305,20 @@ spec: type: string latency: type: string + reorder: + description: ReorderSpec defines details of packet reorder. + properties: + correlation: + type: string + gap: + type: integer + reorder: + type: string + required: + - correlation + - gap + - reorder + type: object required: - correlation - jitter @@ -615,7 +629,7 @@ spec: properties: action: description: 'Action defines the specific pod chaos action. Supported - action: pod-kill / pod-failure Default action: pod-kill' + action: pod-kill / pod-failure / container-kill Default action: pod-kill' type: string containerName: description: ContainerName indicates the name of the container. Needed