...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/common_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  	"time"
    20  
    21  	corev1 "k8s.io/api/core/v1"
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	ctrl "sigs.k8s.io/controller-runtime"
    24  	"sigs.k8s.io/controller-runtime/pkg/webhook"
    25  )
    26  
    27  const (
    28  	// PauseAnnotationKey defines the annotation used to pause a chaos
    29  	PauseAnnotationKey = "experiment.chaos-mesh.org/pause"
    30  	LabelManagedBy     = "managed-by"
    31  )
    32  
    33  type ChaosStatus struct {
    34  	// Conditions represents the current global condition of the chaos
    35  	// +optional
    36  	Conditions []ChaosCondition `json:"conditions,omitempty"`
    37  
    38  	// Experiment records the last experiment state.
    39  	Experiment ExperimentStatus `json:"experiment"`
    40  }
    41  
    42  type ChaosConditionType string
    43  
    44  const (
    45  	ConditionSelected     ChaosConditionType = "Selected"
    46  	ConditionAllInjected  ChaosConditionType = "AllInjected"
    47  	ConditionAllRecovered ChaosConditionType = "AllRecovered"
    48  	ConditionPaused       ChaosConditionType = "Paused"
    49  )
    50  
    51  type ChaosCondition struct {
    52  	Type   ChaosConditionType     `json:"type"`
    53  	Status corev1.ConditionStatus `json:"status"`
    54  	// +optional
    55  	Reason string `json:"reason,omitempty"`
    56  }
    57  
    58  type DesiredPhase string
    59  
    60  const (
    61  	// The target of `RunningPhase` is to make all selected targets (container or pod) into "Injected" phase
    62  	RunningPhase DesiredPhase = "Run"
    63  	// The target of `StoppedPhase` is to make all selected targets (container or pod) into "NotInjected" phase
    64  	StoppedPhase DesiredPhase = "Stop"
    65  )
    66  
    67  type ExperimentStatus struct {
    68  	// +kubebuilder:validation:Enum=Run;Stop
    69  	DesiredPhase `json:"desiredPhase,omitempty"`
    70  	// +optional
    71  	// Records are used to track the running status
    72  	Records []*Record `json:"containerRecords,omitempty"`
    73  }
    74  
    75  type Record struct {
    76  	Id          string `json:"id"`
    77  	SelectorKey string `json:"selectorKey"`
    78  	Phase       Phase  `json:"phase"`
    79  	// InjectedCount is a counter to record the sum of successful injections
    80  	InjectedCount int `json:"injectedCount"`
    81  	// RecoveredCount is a counter to record the sum of successful recoveries
    82  	RecoveredCount int `json:"recoveredCount"`
    83  	// Events are the essential details about the injections and recoveries
    84  	Events []RecordEvent `json:"events,omitempty"`
    85  }
    86  
    87  type Phase string
    88  
    89  const (
    90  	// NotInjected means the target is not injected yet. The controller could call "Inject" on the target
    91  	NotInjected Phase = "Not Injected"
    92  	// Injected means the target is injected. It's safe to recover it.
    93  	Injected Phase = "Injected"
    94  )
    95  
    96  type RecordEvent struct {
    97  	// Type means the stage of this event
    98  	Type RecordEventType `json:"type"`
    99  	// Operation represents the operation we are doing, when we crate this event
   100  	Operation RecordEventOperation `json:"operation"`
   101  	// Message is the detail message, e.g. the reason why we failed to inject the chaos
   102  	Message string `json:"message,omitempty"`
   103  	// Timestamp is time when we create this event
   104  	Timestamp *metav1.Time `json:"timestamp"`
   105  }
   106  
   107  type RecordEventType string
   108  
   109  const (
   110  	// TypeSucceeded means the stage of this event is successful
   111  	TypeSucceeded RecordEventType = "Succeeded"
   112  	// TypeFailed means the stage of this event is failed
   113  	TypeFailed RecordEventType = "Failed"
   114  )
   115  
   116  type RecordEventOperation string
   117  
   118  const (
   119  	// Apply means this event is recorded, when we inject the chaos
   120  	// typically, when we call impl.Apply()
   121  	Apply RecordEventOperation = "Apply"
   122  	// Recover means this event is recorded, when we recover the chaos
   123  	// typically, when we call impl.Recover()
   124  	Recover RecordEventOperation = "Recover"
   125  )
   126  
   127  // NewRecordEvent is a constructor of RecordEvent in status
   128  func NewRecordEvent(eventType RecordEventType, eventStage RecordEventOperation,
   129  	msg string, time metav1.Time) *RecordEvent {
   130  	return &RecordEvent{
   131  		Type:      eventType,
   132  		Operation: eventStage,
   133  		Message:   msg,
   134  		Timestamp: &time,
   135  	}
   136  }
   137  
   138  var log = ctrl.Log.WithName("api")
   139  
   140  // +kubebuilder:object:generate=false
   141  
   142  // InnerObject is basic Object for the Reconciler
   143  type InnerObject interface {
   144  	StatefulObject
   145  	IsDeleted() bool
   146  	IsPaused() bool
   147  	DurationExceeded(time.Time) (bool, time.Duration, error)
   148  	IsOneShot() bool
   149  }
   150  
   151  // +kubebuilder:object:generate=false
   152  
   153  // StatefulObject defines a basic Object that can get the status
   154  type StatefulObject interface {
   155  	GenericChaos
   156  	GetStatus() *ChaosStatus
   157  }
   158  
   159  // +kubebuilder:object:generate=false
   160  type InnerObjectWithCustomStatus interface {
   161  	InnerObject
   162  
   163  	GetCustomStatus() interface{}
   164  }
   165  
   166  // +kubebuilder:object:generate=false
   167  type InnerObjectWithSelector interface {
   168  	InnerObject
   169  
   170  	GetSelectorSpecs() map[string]interface{}
   171  }
   172  
   173  // +kubebuilder:object:generate=false
   174  
   175  // WebhookObject is basic Object which implement `webhook.Validator` and `webhook.Defaulter`
   176  type WebhookObject interface {
   177  	webhook.Validator
   178  	webhook.Defaulter
   179  }
   180  
   181  // +kubebuilder:object:generate=false
   182  type RemoteObject interface {
   183  	StatefulObject
   184  	GetRemoteCluster() string
   185  }
   186