...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/podchaos_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  // +kubebuilder:object:root=true
    23  // +chaos-mesh:experiment
    24  // +chaos-mesh:oneshot=in.Spec.Action==PodKillAction || in.Spec.Action==ContainerKillAction
    25  
    26  // PodChaos is the control script`s spec.
    27  type PodChaos struct {
    28  	metav1.TypeMeta   `json:",inline"`
    29  	metav1.ObjectMeta `json:"metadata,omitempty"`
    30  
    31  	// Spec defines the behavior of a pod chaos experiment
    32  	Spec PodChaosSpec `json:"spec"`
    33  
    34  	// +optional
    35  	// Most recently observed status of the chaos experiment about pods
    36  	Status PodChaosStatus `json:"status,omitempty"`
    37  }
    38  
    39  var _ InnerObjectWithSelector = (*PodChaos)(nil)
    40  var _ InnerObject = (*PodChaos)(nil)
    41  
    42  // PodChaosAction represents the chaos action about pods.
    43  type PodChaosAction string
    44  
    45  const (
    46  	// PodKillAction represents the chaos action of killing pods.
    47  	PodKillAction PodChaosAction = "pod-kill"
    48  	// PodFailureAction represents the chaos action of injecting errors to pods.
    49  	// This action will cause the pod to not be created for a while.
    50  	PodFailureAction PodChaosAction = "pod-failure"
    51  	// ContainerKillAction represents the chaos action of killing the container
    52  	ContainerKillAction PodChaosAction = "container-kill"
    53  )
    54  
    55  // PodChaosSpec defines the attributes that a user creates on a chaos experiment about pods.
    56  type PodChaosSpec struct {
    57  	ContainerSelector `json:",inline"`
    58  
    59  	// Action defines the specific pod chaos action.
    60  	// Supported action: pod-kill / pod-failure / container-kill
    61  	// Default action: pod-kill
    62  	// +kubebuilder:validation:Enum=pod-kill;pod-failure;container-kill
    63  	Action PodChaosAction `json:"action"`
    64  
    65  	// Duration represents the duration of the chaos action.
    66  	// It is required when the action is `PodFailureAction`.
    67  	// A duration string is a possibly signed sequence of
    68  	// decimal numbers, each with optional fraction and a unit suffix,
    69  	// such as "300ms", "-1.5h" or "2h45m".
    70  	// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
    71  	// +optional
    72  	Duration *string `json:"duration,omitempty" webhook:"Duration"`
    73  
    74  	// GracePeriod is used in pod-kill action. It represents the duration in seconds before the pod should be deleted.
    75  	// Value must be non-negative integer. The default value is zero that indicates delete immediately.
    76  	// +optional
    77  	// +kubebuilder:validation:Minimum=0
    78  	GracePeriod int64 `json:"gracePeriod,omitempty"`
    79  
    80  	// RemoteCluster represents the remote cluster where the chaos will be deployed
    81  	// +optional
    82  	RemoteCluster string `json:"remoteCluster,omitempty"`
    83  }
    84  
    85  // PodChaosStatus represents the current status of the chaos experiment about pods.
    86  type PodChaosStatus struct {
    87  	ChaosStatus `json:",inline"`
    88  }
    89  
    90  func (obj *PodChaos) GetSelectorSpecs() map[string]interface{} {
    91  	switch obj.Spec.Action {
    92  	case PodKillAction, PodFailureAction:
    93  		return map[string]interface{}{
    94  			".": &obj.Spec.PodSelector,
    95  		}
    96  	case ContainerKillAction:
    97  		return map[string]interface{}{
    98  			".": &obj.Spec.ContainerSelector,
    99  		}
   100  	}
   101  
   102  	return nil
   103  }
   104