...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/networkchaos_types.go

Documentation: github.com/chaos-mesh/chaos-mesh/api/v1alpha1

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package v1alpha1
    17  
    18  import (
    19  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    20  )
    21  
    22  // EDIT THIS FILE!  THIS IS SCAFFOLDING FOR YOU TO OWN!
    23  // NOTE: json tags are required.  Any new fields you add must have json tags for the fields to be serialized.
    24  
    25  // +kubebuilder:object:root=true
    26  // +kubebuilder:printcolumn:name="action",type=string,JSONPath=`.spec.action`
    27  // +kubebuilder:printcolumn:name="duration",type=string,JSONPath=`.spec.duration`
    28  // +chaos-mesh:experiment
    29  
    30  // NetworkChaos is the Schema for the networkchaos API
    31  type NetworkChaos struct {
    32  	metav1.TypeMeta   `json:",inline"`
    33  	metav1.ObjectMeta `json:"metadata,omitempty"`
    34  
    35  	// Spec defines the behavior of a pod chaos experiment
    36  	Spec NetworkChaosSpec `json:"spec"`
    37  
    38  	// +optional
    39  	// Most recently observed status of the chaos experiment about pods
    40  	Status NetworkChaosStatus `json:"status,omitempty"`
    41  }
    42  
    43  var _ InnerObjectWithCustomStatus = (*NetworkChaos)(nil)
    44  var _ InnerObjectWithSelector = (*NetworkChaos)(nil)
    45  var _ InnerObject = (*NetworkChaos)(nil)
    46  
    47  // NetworkChaosAction represents the chaos action about network.
    48  type NetworkChaosAction string
    49  
    50  const (
    51  	// NetemAction is a combination of several chaos actions i.e. delay, loss, duplicate, corrupt.
    52  	// When using this action multiple specs are merged into one Netem RPC and sends to chaos daemon.
    53  	NetemAction NetworkChaosAction = "netem"
    54  
    55  	// DelayAction represents the chaos action of adding delay on pods.
    56  	DelayAction NetworkChaosAction = "delay"
    57  
    58  	// LossAction represents the chaos action of losing packets on pods.
    59  	LossAction NetworkChaosAction = "loss"
    60  
    61  	// DuplicateAction represents the chaos action of duplicating packets on pods.
    62  	DuplicateAction NetworkChaosAction = "duplicate"
    63  
    64  	// CorruptAction represents the chaos action of corrupting packets on pods.
    65  	CorruptAction NetworkChaosAction = "corrupt"
    66  
    67  	// PartitionAction represents the chaos action of network partition of pods.
    68  	PartitionAction NetworkChaosAction = "partition"
    69  
    70  	// BandwidthAction represents the chaos action of network bandwidth of pods.
    71  	BandwidthAction NetworkChaosAction = "bandwidth"
    72  )
    73  
    74  // Direction represents traffic direction from source to target,
    75  // it could be netem, delay, loss, duplicate, corrupt or partition,
    76  // check comments below for detail direction flow.
    77  type Direction string
    78  
    79  const (
    80  	// To represents network packet from source to target
    81  	To Direction = "to"
    82  
    83  	// From represents network packet to source from target
    84  	From Direction = "from"
    85  
    86  	// Both represents both directions
    87  	Both Direction = "both"
    88  )
    89  
    90  // NetworkChaosSpec defines the desired state of NetworkChaos
    91  type NetworkChaosSpec struct {
    92  	PodSelector `json:",inline"`
    93  
    94  	// Action defines the specific network chaos action.
    95  	// Supported action: partition, netem, delay, loss, duplicate, corrupt
    96  	// Default action: delay
    97  	// +kubebuilder:validation:Enum=netem;delay;loss;duplicate;corrupt;partition;bandwidth
    98  	Action NetworkChaosAction `json:"action"`
    99  
   100  	// Device represents the network device to be affected.
   101  	// +optional
   102  	Device string `json:"device,omitempty"`
   103  
   104  	// Duration represents the duration of the chaos action
   105  	Duration *string `json:"duration,omitempty" webhook:"Duration"`
   106  
   107  	// TcParameter represents the traffic control definition
   108  	TcParameter `json:",inline"`
   109  
   110  	// Direction represents the direction, this applies on netem and network partition action
   111  	// +optional
   112  	// +kubebuilder:validation:Enum=to;from;both
   113  	// +kubebuilder:default=to
   114  	Direction Direction `json:"direction,omitempty"`
   115  
   116  	// Target represents network target, this applies on netem and network partition action
   117  	// +optional
   118  	Target *PodSelector `json:"target,omitempty" webhook:",nilable"`
   119  
   120  	// TargetDevice represents the network device to be affected in target scope.
   121  	// +optional
   122  	TargetDevice string `json:"targetDevice,omitempty"`
   123  
   124  	// ExternalTargets represents network targets outside k8s
   125  	// +optional
   126  	ExternalTargets []string `json:"externalTargets,omitempty"`
   127  
   128  	// RemoteCluster represents the remote cluster where the chaos will be deployed
   129  	// +optional
   130  	RemoteCluster string `json:"remoteCluster,omitempty"`
   131  }
   132  
   133  // NetworkChaosStatus defines the observed state of NetworkChaos
   134  type NetworkChaosStatus struct {
   135  	ChaosStatus `json:",inline"`
   136  	// Instances always specifies podnetworkchaos generation or empty
   137  	// +optional
   138  	Instances map[string]int64 `json:"instances,omitempty"`
   139  }
   140  
   141  // DelaySpec defines detail of a delay action
   142  type DelaySpec struct {
   143  	// +kubebuilder:validation:Pattern="^[0-9]+(\\.[0-9]+)?(ns|us|ms|s|m|h)$"
   144  	Latency string `json:"latency" webhook:"Duration"`
   145  	// +optional
   146  	Correlation string `json:"correlation,omitempty" default:"0" webhook:"FloatStr"`
   147  	// +kubebuilder:validation:Pattern="^[0-9]+(\\.[0-9]+)?(ns|us|ms|s|m|h)$"
   148  	// +optional
   149  	Jitter string `json:"jitter,omitempty" default:"0ms" webhook:"Duration"`
   150  	// +optional
   151  	Reorder *ReorderSpec `json:"reorder,omitempty"`
   152  }
   153  
   154  // LossSpec defines detail of a loss action
   155  type LossSpec struct {
   156  	Loss string `json:"loss" webhook:"FloatStr"`
   157  	// +optional
   158  	Correlation string `json:"correlation,omitempty" default:"0" webhook:"FloatStr"`
   159  }
   160  
   161  // DuplicateSpec defines detail of a duplicate action
   162  type DuplicateSpec struct {
   163  	Duplicate string `json:"duplicate" webhook:"FloatStr"`
   164  	// +optional
   165  	Correlation string `json:"correlation,omitempty" default:"0" webhook:"FloatStr"`
   166  }
   167  
   168  // CorruptSpec defines detail of a corrupt action
   169  type CorruptSpec struct {
   170  	Corrupt string `json:"corrupt" webhook:"FloatStr"`
   171  	// +optional
   172  	Correlation string `json:"correlation,omitempty" default:"0" webhook:"FloatStr"`
   173  }
   174  
   175  // BandwidthSpec defines detail of bandwidth limit.
   176  type BandwidthSpec struct {
   177  	// Rate is the speed knob. Allows bit, kbit, mbit, gbit, tbit, bps, kbps, mbps, gbps, tbps unit. bps means bytes per second.
   178  	Rate string `json:"rate" webhook:"Rate"`
   179  	// Limit is the number of bytes that can be queued waiting for tokens to become available.
   180  	// +kubebuilder:validation:Minimum=1
   181  	Limit uint32 `json:"limit"`
   182  	// Buffer is the maximum amount of bytes that tokens can be available for instantaneously.
   183  	// +kubebuilder:validation:Minimum=1
   184  	Buffer uint32 `json:"buffer"`
   185  	// Peakrate is the maximum depletion rate of the bucket.
   186  	// The peakrate does not need to be set, it is only necessary
   187  	// if perfect millisecond timescale shaping is required.
   188  	// +optional
   189  	// +kubebuilder:validation:Minimum=0
   190  	Peakrate *uint64 `json:"peakrate,omitempty"`
   191  	// Minburst specifies the size of the peakrate bucket. For perfect
   192  	// accuracy, should be set to the MTU of the interface.  If a
   193  	// peakrate is needed, but some burstiness is acceptable, this
   194  	// size can be raised. A 3000 byte minburst allows around 3mbit/s
   195  	// of peakrate, given 1000 byte packets.
   196  	// +optional
   197  	// +kubebuilder:validation:Minimum=0
   198  	Minburst *uint32 `json:"minburst,omitempty"`
   199  }
   200  
   201  // ReorderSpec defines details of packet reorder.
   202  type ReorderSpec struct {
   203  	Reorder string `json:"reorder" webhook:"FloatStr"`
   204  	// +optional
   205  	Correlation string `json:"correlation,omitempty" default:"0" webhook:"FloatStr"`
   206  	Gap         int    `json:"gap"`
   207  }
   208  
   209  // RateSpec defines details of rate limit.
   210  type RateSpec struct {
   211  	// Rate is the speed knob. Allows bit, kbit, mbit, gbit, tbit, bps, kbps, mbps, gbps, tbps unit. bps means bytes per second.
   212  	Rate string `json:"rate" webhook:"Rate"`
   213  }
   214  
   215  func (obj *NetworkChaos) GetSelectorSpecs() map[string]interface{} {
   216  	selectors := map[string]interface{}{
   217  		".": &obj.Spec.PodSelector,
   218  	}
   219  	if obj.Spec.Target != nil {
   220  		selectors[".Target"] = obj.Spec.Target
   221  	}
   222  	return selectors
   223  }
   224  
   225  func (obj *NetworkChaos) GetCustomStatus() interface{} {
   226  	return &obj.Status.Instances
   227  }
   228