...

Source file src/github.com/chaos-mesh/chaos-mesh/api/v1alpha1/workflow_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  	"github.com/pkg/errors"
    20  	corev1 "k8s.io/api/core/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  )
    24  
    25  // +kubebuilder:object:root=true
    26  // +kubebuilder:resource:shortName=wf
    27  // +kubebuilder:subresource:status
    28  // +chaos-mesh:experiment
    29  type Workflow struct {
    30  	metav1.TypeMeta   `json:",inline"`
    31  	metav1.ObjectMeta `json:"metadata,omitempty"`
    32  
    33  	// Spec defines the behavior of a workflow
    34  	Spec WorkflowSpec `json:"spec"`
    35  
    36  	// +optional
    37  	// Most recently observed status of the workflow
    38  	Status WorkflowStatus `json:"status"`
    39  }
    40  
    41  func (in *Workflow) GetObjectMeta() *metav1.ObjectMeta {
    42  	return &in.ObjectMeta
    43  }
    44  
    45  const KindWorkflow = "Workflow"
    46  
    47  type WorkflowSpec struct {
    48  	Entry     string     `json:"entry"`
    49  	Templates []Template `json:"templates"`
    50  }
    51  
    52  type WorkflowStatus struct {
    53  	// +optional
    54  	EntryNode *string `json:"entryNode,omitempty"`
    55  	// +optional
    56  	StartTime *metav1.Time `json:"startTime,omitempty"`
    57  	// +optional
    58  	EndTime *metav1.Time `json:"endTime,omitempty"`
    59  	// Represents the latest available observations of a workflow's current state.
    60  	// +optional
    61  	// +patchMergeKey=type
    62  	// +patchStrategy=merge
    63  	Conditions []WorkflowCondition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
    64  }
    65  
    66  type WorkflowConditionType string
    67  
    68  const (
    69  	WorkflowConditionAccomplished WorkflowConditionType = "Accomplished"
    70  	WorkflowConditionScheduled    WorkflowConditionType = "Scheduled"
    71  )
    72  
    73  type WorkflowCondition struct {
    74  	Type      WorkflowConditionType  `json:"type"`
    75  	Status    corev1.ConditionStatus `json:"status"`
    76  	Reason    string                 `json:"reason"`
    77  	StartTime *metav1.Time           `json:"startTime,omitempty"`
    78  }
    79  
    80  type TemplateType string
    81  
    82  const (
    83  	TypeTask        TemplateType = "Task"
    84  	TypeSerial      TemplateType = "Serial"
    85  	TypeParallel    TemplateType = "Parallel"
    86  	TypeSuspend     TemplateType = "Suspend"
    87  	TypeSchedule    TemplateType = "Schedule"
    88  	TypeStatusCheck TemplateType = "StatusCheck"
    89  )
    90  
    91  func IsChaosTemplateType(target TemplateType) bool {
    92  	return contains(allChaosTemplateType, target)
    93  }
    94  
    95  func contains(arr []TemplateType, target TemplateType) bool {
    96  	for _, item := range arr {
    97  		if item == target {
    98  			return true
    99  		}
   100  	}
   101  	return false
   102  }
   103  
   104  type Template struct {
   105  	Name string       `json:"name"`
   106  	Type TemplateType `json:"templateType"`
   107  	// +optional
   108  	Deadline *string `json:"deadline,omitempty"`
   109  	// Task describes the behavior of the custom task. Only used when Type is TypeTask.
   110  	// +optional
   111  	Task *Task `json:"task,omitempty"`
   112  	// Children describes the children steps of serial or parallel node. Only used when Type is TypeSerial or TypeParallel.
   113  	// +optional
   114  	Children []string `json:"children,omitempty"`
   115  	// ConditionalBranches describes the conditional branches of custom tasks. Only used when Type is TypeTask.
   116  	// +optional
   117  	ConditionalBranches []ConditionalBranch `json:"conditionalBranches,omitempty"`
   118  	// EmbedChaos describe the chaos to be injected with chaos nodes. Only used when Type is Type<Something>Chaos.
   119  	// +optional
   120  	*EmbedChaos `json:",inline"`
   121  	// Schedule describe the Schedule(describing scheduled chaos) to be injected with chaos nodes. Only used when Type is TypeSchedule.
   122  	// +optional
   123  	Schedule *ChaosOnlyScheduleSpec `json:"schedule,omitempty"`
   124  	// StatusCheck describe the behavior of StatusCheck. Only used when Type is TypeStatusCheck.
   125  	// +optional
   126  	StatusCheck *StatusCheckSpec `json:"statusCheck,omitempty"`
   127  	// AbortWithStatusCheck describe whether to abort the workflow when the failure threshold of StatusCheck is exceeded.
   128  	// Only used when Type is TypeStatusCheck.
   129  	// +optional
   130  	AbortWithStatusCheck bool `json:"abortWithStatusCheck,omitempty"`
   131  }
   132  
   133  // ChaosOnlyScheduleSpec is very similar with ScheduleSpec, but it could not schedule Workflow
   134  // because we could not resolve nested CRD now
   135  type ChaosOnlyScheduleSpec struct {
   136  	Schedule string `json:"schedule"`
   137  
   138  	// +optional
   139  	// +nullable
   140  	// +kubebuilder:validation:Minimum=0
   141  	StartingDeadlineSeconds *int64 `json:"startingDeadlineSeconds"`
   142  
   143  	// +optional
   144  	// +kubebuilder:validation:Enum=Forbid;Allow
   145  	ConcurrencyPolicy ConcurrencyPolicy `json:"concurrencyPolicy"`
   146  
   147  	// +optional
   148  	// +kubebuilder:validation:Minimum=1
   149  	HistoryLimit int `json:"historyLimit,omitempty"`
   150  
   151  	Type ScheduleTemplateType `json:"type"`
   152  
   153  	EmbedChaos `json:",inline"`
   154  }
   155  
   156  type Task struct {
   157  	// Container is the main container image to run in the pod
   158  	Container *corev1.Container `json:"container,omitempty"`
   159  
   160  	// Volumes is a list of volumes that can be mounted by containers in a template.
   161  	// +patchStrategy=merge
   162  	// +patchMergeKey=name
   163  	Volumes []corev1.Volume `json:"volumes,omitempty" patchStrategy:"merge" patchMergeKey:"name"`
   164  
   165  	// TODO: maybe we could specify parameters in other ways, like loading context from file
   166  }
   167  
   168  // +kubebuilder:object:root=true
   169  type WorkflowList struct {
   170  	metav1.TypeMeta `json:",inline"`
   171  	metav1.ListMeta `json:"metadata,omitempty"`
   172  	Items           []Workflow `json:"items"`
   173  }
   174  
   175  func (in *WorkflowList) GetItems() []GenericChaos {
   176  	var result []GenericChaos
   177  	for _, item := range in.Items {
   178  		item := item
   179  		result = append(result, &item)
   180  	}
   181  	return result
   182  }
   183  
   184  // TODO: refactor: not so accurate
   185  func (in *WorkflowList) DeepCopyList() GenericChaosList {
   186  	return in.DeepCopy()
   187  }
   188  
   189  func init() {
   190  	SchemeBuilder.Register(&Workflow{}, &WorkflowList{})
   191  }
   192  
   193  func FetchChaosByTemplateType(templateType TemplateType) (runtime.Object, error) {
   194  	if kind, ok := all.kinds[string(templateType)]; ok {
   195  		return kind.SpawnObject(), nil
   196  	}
   197  	return nil, errors.Wrapf(errInvalidValue, "unknown template type %s", templateType)
   198  }
   199