...

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  	Latency string `json:"latency" webhook:"Duration"`
   144  	// +optional
   145  	Correlation string `json:"correlation,omitempty" default:"0" webhook:"FloatStr"`
   146  	// +optional
   147  	Jitter string `json:"jitter,omitempty" default:"0ms" webhook:"Duration"`
   148  	// +optional
   149  	Reorder *ReorderSpec `json:"reorder,omitempty"`
   150  }
   151  
   152  // LossSpec defines detail of a loss action
   153  type LossSpec struct {
   154  	Loss string `json:"loss" webhook:"FloatStr"`
   155  	// +optional
   156  	Correlation string `json:"correlation,omitempty" default:"0" webhook:"FloatStr"`
   157  }
   158  
   159  // DuplicateSpec defines detail of a duplicate action
   160  type DuplicateSpec struct {
   161  	Duplicate string `json:"duplicate" webhook:"FloatStr"`
   162  	// +optional
   163  	Correlation string `json:"correlation,omitempty" default:"0" webhook:"FloatStr"`
   164  }
   165  
   166  // CorruptSpec defines detail of a corrupt action
   167  type CorruptSpec struct {
   168  	Corrupt string `json:"corrupt" webhook:"FloatStr"`
   169  	// +optional
   170  	Correlation string `json:"correlation,omitempty" default:"0" webhook:"FloatStr"`
   171  }
   172  
   173  // BandwidthSpec defines detail of bandwidth limit.
   174  type BandwidthSpec struct {
   175  	// Rate is the speed knob. Allows bit, kbit, mbit, gbit, tbit, bps, kbps, mbps, gbps, tbps unit. bps means bytes per second.
   176  	Rate string `json:"rate" webhook:"Rate"`
   177  	// Limit is the number of bytes that can be queued waiting for tokens to become available.
   178  	// +kubebuilder:validation:Minimum=1
   179  	Limit uint32 `json:"limit"`
   180  	// Buffer is the maximum amount of bytes that tokens can be available for instantaneously.
   181  	// +kubebuilder:validation:Minimum=1
   182  	Buffer uint32 `json:"buffer"`
   183  	// Peakrate is the maximum depletion rate of the bucket.
   184  	// The peakrate does not need to be set, it is only necessary
   185  	// if perfect millisecond timescale shaping is required.
   186  	// +optional
   187  	// +kubebuilder:validation:Minimum=0
   188  	Peakrate *uint64 `json:"peakrate,omitempty"`
   189  	// Minburst specifies the size of the peakrate bucket. For perfect
   190  	// accuracy, should be set to the MTU of the interface.  If a
   191  	// peakrate is needed, but some burstiness is acceptable, this
   192  	// size can be raised. A 3000 byte minburst allows around 3mbit/s
   193  	// of peakrate, given 1000 byte packets.
   194  	// +optional
   195  	// +kubebuilder:validation:Minimum=0
   196  	Minburst *uint32 `json:"minburst,omitempty"`
   197  }
   198  
   199  // ReorderSpec defines details of packet reorder.
   200  type ReorderSpec struct {
   201  	Reorder string `json:"reorder" webhook:"FloatStr"`
   202  	// +optional
   203  	Correlation string `json:"correlation,omitempty" default:"0" webhook:"FloatStr"`
   204  	Gap         int    `json:"gap"`
   205  }
   206  
   207  // RateSpec defines details of rate limit.
   208  type RateSpec struct {
   209  	// Rate is the speed knob. Allows bit, kbit, mbit, gbit, tbit, bps, kbps, mbps, gbps, tbps unit. bps means bytes per second.
   210  	Rate string `json:"rate" webhook:"Rate"`
   211  }
   212  
   213  func (obj *NetworkChaos) GetSelectorSpecs() map[string]interface{} {
   214  	selectors := map[string]interface{}{
   215  		".": &obj.Spec.PodSelector,
   216  	}
   217  	if obj.Spec.Target != nil {
   218  		selectors[".Target"] = obj.Spec.Target
   219  	}
   220  	return selectors
   221  }
   222  
   223  func (obj *NetworkChaos) GetCustomStatus() interface{} {
   224  	return &obj.Status.Instances
   225  }
   226