github.com/kubevela/workflow@v0.6.0/api/v1alpha1/types.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package v1alpha1
    18  
    19  import (
    20  	corev1 "k8s.io/api/core/v1"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  	"k8s.io/apimachinery/pkg/runtime"
    23  
    24  	"github.com/kubevela/workflow/api/condition"
    25  )
    26  
    27  // +kubebuilder:object:root=true
    28  
    29  // WorkflowRun is the Schema for the workflowRun API
    30  // +kubebuilder:storageversion
    31  // +kubebuilder:resource:categories={oam},shortName={wr}
    32  // +kubebuilder:subresource:status
    33  // +kubebuilder:printcolumn:name="PHASE",type=string,JSONPath=`.status.status`
    34  // +kubebuilder:printcolumn:name="AGE",type=date,JSONPath=".metadata.creationTimestamp"
    35  // +genclient
    36  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    37  type WorkflowRun struct {
    38  	metav1.TypeMeta   `json:",inline"`
    39  	metav1.ObjectMeta `json:"metadata,omitempty"`
    40  	Spec              WorkflowRunSpec   `json:"spec,omitempty"`
    41  	Status            WorkflowRunStatus `json:"status,omitempty"`
    42  }
    43  
    44  // +kubebuilder:object:root=true
    45  
    46  // WorkflowRunList contains a list of WorkflowRun
    47  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
    48  type WorkflowRunList struct {
    49  	metav1.TypeMeta `json:",inline"`
    50  	metav1.ListMeta `json:"metadata,omitempty"`
    51  	Items           []WorkflowRun `json:"items"`
    52  }
    53  
    54  func (w WorkflowRunList) Len() int {
    55  	return len(w.Items)
    56  }
    57  
    58  func (w WorkflowRunList) Swap(i, j int) {
    59  	w.Items[i], w.Items[j] = w.Items[j], w.Items[i]
    60  }
    61  
    62  func (w WorkflowRunList) Less(i, j int) bool {
    63  	if !w.Items[i].Status.Finished && !w.Items[j].Status.Finished {
    64  		return w.Items[i].CreationTimestamp.After(w.Items[j].CreationTimestamp.Time)
    65  	}
    66  	if !w.Items[i].Status.EndTime.IsZero() && !w.Items[j].Status.EndTime.IsZero() {
    67  		return w.Items[i].Status.EndTime.After(w.Items[j].Status.EndTime.Time)
    68  	}
    69  	return !w.Items[i].Status.EndTime.IsZero()
    70  }
    71  
    72  // WorkflowRunSpec is the spec for the WorkflowRun
    73  type WorkflowRunSpec struct {
    74  	// +kubebuilder:pruning:PreserveUnknownFields
    75  	Context      *runtime.RawExtension `json:"context,omitempty"`
    76  	Mode         *WorkflowExecuteMode  `json:"mode,omitempty"`
    77  	WorkflowSpec *WorkflowSpec         `json:"workflowSpec,omitempty"`
    78  	WorkflowRef  string                `json:"workflowRef,omitempty"`
    79  }
    80  
    81  // WorkflowRunStatus record the status of workflow run
    82  type WorkflowRunStatus struct {
    83  	condition.ConditionedStatus `json:",inline"`
    84  
    85  	Mode    WorkflowExecuteMode `json:"mode"`
    86  	Phase   WorkflowRunPhase    `json:"status"`
    87  	Message string              `json:"message,omitempty"`
    88  
    89  	Suspend      bool   `json:"suspend"`
    90  	SuspendState string `json:"suspendState,omitempty"`
    91  
    92  	Terminated bool `json:"terminated"`
    93  	Finished   bool `json:"finished"`
    94  
    95  	ContextBackend *corev1.ObjectReference `json:"contextBackend,omitempty"`
    96  	Steps          []WorkflowStepStatus    `json:"steps,omitempty"`
    97  
    98  	StartTime metav1.Time `json:"startTime,omitempty"`
    99  	EndTime   metav1.Time `json:"endTime,omitempty"`
   100  }
   101  
   102  // WorkflowSpec defines workflow steps and other attributes
   103  type WorkflowSpec struct {
   104  	Steps []WorkflowStep `json:"steps,omitempty"`
   105  }
   106  
   107  // WorkflowExecuteMode defines the mode of workflow execution
   108  type WorkflowExecuteMode struct {
   109  	// Steps is the mode of workflow steps execution
   110  	Steps WorkflowMode `json:"steps,omitempty"`
   111  	// SubSteps is the mode of workflow sub steps execution
   112  	SubSteps WorkflowMode `json:"subSteps,omitempty"`
   113  }
   114  
   115  // WorkflowRunPhase is a label for the condition of a WorkflowRun at the current time
   116  type WorkflowRunPhase string
   117  
   118  const (
   119  	// WorkflowStateInitializing means the workflow run is initializing
   120  	WorkflowStateInitializing WorkflowRunPhase = "initializing"
   121  	// WorkflowStateExecuting means the workflow run is executing
   122  	WorkflowStateExecuting WorkflowRunPhase = "executing"
   123  	// WorkflowStateSuspending means the workflow run is suspending
   124  	WorkflowStateSuspending WorkflowRunPhase = "suspending"
   125  	// WorkflowStateTerminated means the workflow run is terminated
   126  	WorkflowStateTerminated WorkflowRunPhase = "terminated"
   127  	// WorkflowStateFailed means the workflow run is failed
   128  	WorkflowStateFailed WorkflowRunPhase = "failed"
   129  	// WorkflowStateSucceeded means the workflow run is succeeded
   130  	WorkflowStateSucceeded WorkflowRunPhase = "succeeded"
   131  	// WorkflowStateSkipped means the workflow run is skipped
   132  	WorkflowStateSkipped WorkflowRunPhase = "skipped"
   133  )
   134  
   135  // +kubebuilder:object:root=true
   136  
   137  // Workflow is the Schema for the workflow API
   138  // +kubebuilder:storageversion
   139  // +kubebuilder:resource:categories={oam}
   140  // +genclient
   141  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
   142  type Workflow struct {
   143  	metav1.TypeMeta   `json:",inline"`
   144  	metav1.ObjectMeta `json:"metadata,omitempty"`
   145  
   146  	Mode         *WorkflowExecuteMode `json:"mode,omitempty"`
   147  	WorkflowSpec `json:",inline"`
   148  }
   149  
   150  // +kubebuilder:object:root=true
   151  
   152  // WorkflowList contains a list of Workflow
   153  // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
   154  type WorkflowList struct {
   155  	metav1.TypeMeta `json:",inline"`
   156  	metav1.ListMeta `json:"metadata,omitempty"`
   157  	Items           []Workflow `json:"items"`
   158  }
   159  
   160  // WorkflowStep defines how to execute a workflow step.
   161  type WorkflowStep struct {
   162  	WorkflowStepBase `json:",inline"`
   163  	// Mode is only valid for sub steps, it defines the mode of the sub steps
   164  	// +nullable
   165  	Mode     WorkflowMode       `json:"mode,omitempty"`
   166  	SubSteps []WorkflowStepBase `json:"subSteps,omitempty"`
   167  }
   168  
   169  // WorkflowStepMeta contains the meta data of a workflow step
   170  type WorkflowStepMeta struct {
   171  	Alias string `json:"alias,omitempty"`
   172  }
   173  
   174  // WorkflowStepBase defines the workflow step base
   175  type WorkflowStepBase struct {
   176  	// Name is the unique name of the workflow step.
   177  	Name string `json:"name,omitempty"`
   178  	// Type is the type of the workflow step.
   179  	Type string `json:"type"`
   180  	// Meta is the meta data of the workflow step.
   181  	Meta *WorkflowStepMeta `json:"meta,omitempty"`
   182  	// If is the if condition of the step
   183  	If string `json:"if,omitempty"`
   184  	// Timeout is the timeout of the step
   185  	Timeout string `json:"timeout,omitempty"`
   186  	// DependsOn is the dependency of the step
   187  	DependsOn []string `json:"dependsOn,omitempty"`
   188  	// Inputs is the inputs of the step
   189  	Inputs StepInputs `json:"inputs,omitempty"`
   190  	// Outputs is the outputs of the step
   191  	Outputs StepOutputs `json:"outputs,omitempty"`
   192  
   193  	// Properties is the properties of the step
   194  	// +kubebuilder:pruning:PreserveUnknownFields
   195  	Properties *runtime.RawExtension `json:"properties,omitempty"`
   196  }
   197  
   198  // WorkflowMode describes the mode of workflow
   199  type WorkflowMode string
   200  
   201  const (
   202  	// WorkflowModeDAG describes the DAG mode of workflow
   203  	WorkflowModeDAG WorkflowMode = "DAG"
   204  	// WorkflowModeStep describes the step by step mode of workflow
   205  	WorkflowModeStep WorkflowMode = "StepByStep"
   206  )
   207  
   208  // StepStatus record the base status of workflow step, which could be workflow step or subStep
   209  type StepStatus struct {
   210  	ID    string            `json:"id"`
   211  	Name  string            `json:"name,omitempty"`
   212  	Type  string            `json:"type,omitempty"`
   213  	Phase WorkflowStepPhase `json:"phase,omitempty"`
   214  	// A human readable message indicating details about why the workflowStep is in this state.
   215  	Message string `json:"message,omitempty"`
   216  	// A brief CamelCase message indicating details about why the workflowStep is in this state.
   217  	Reason string `json:"reason,omitempty"`
   218  	// FirstExecuteTime is the first time this step execution.
   219  	FirstExecuteTime metav1.Time `json:"firstExecuteTime,omitempty"`
   220  	// LastExecuteTime is the last time this step execution.
   221  	LastExecuteTime metav1.Time `json:"lastExecuteTime,omitempty"`
   222  }
   223  
   224  // WorkflowStepStatus record the status of a workflow step, include step status and subStep status
   225  type WorkflowStepStatus struct {
   226  	StepStatus     `json:",inline"`
   227  	SubStepsStatus []StepStatus `json:"subSteps,omitempty"`
   228  }
   229  
   230  // SetConditions set condition to workflow run
   231  func (wr *WorkflowRun) SetConditions(c ...condition.Condition) {
   232  	wr.Status.SetConditions(c...)
   233  }
   234  
   235  // GetCondition get condition by given condition type
   236  func (wr *WorkflowRun) GetCondition(t condition.ConditionType) condition.Condition {
   237  	return wr.Status.GetCondition(t)
   238  }
   239  
   240  // WorkflowRunConditionType is a valid condition type for a WorkflowRun
   241  const WorkflowRunConditionType string = "WorkflowRun"
   242  
   243  // WorkflowStepPhase describes the phase of a workflow step.
   244  type WorkflowStepPhase string
   245  
   246  const (
   247  	// WorkflowStepPhaseSucceeded will make the controller run the next step.
   248  	WorkflowStepPhaseSucceeded WorkflowStepPhase = "succeeded"
   249  	// WorkflowStepPhaseFailed will report error in `message`.
   250  	WorkflowStepPhaseFailed WorkflowStepPhase = "failed"
   251  	// WorkflowStepPhaseSkipped will make the controller skip the step.
   252  	WorkflowStepPhaseSkipped WorkflowStepPhase = "skipped"
   253  	// WorkflowStepPhaseRunning will make the controller continue the workflow.
   254  	WorkflowStepPhaseRunning WorkflowStepPhase = "running"
   255  	// WorkflowStepPhasePending will make the controller wait for the step to run.
   256  	WorkflowStepPhasePending WorkflowStepPhase = "pending"
   257  	// WorkflowStepPhaseSuspending will make the controller suspend the workflow.
   258  	WorkflowStepPhaseSuspending WorkflowStepPhase = "suspending"
   259  )
   260  
   261  // StepOutputs defines output variable of WorkflowStep
   262  type StepOutputs []OutputItem
   263  
   264  // StepInputs defines variable input of WorkflowStep
   265  type StepInputs []InputItem
   266  
   267  // InputItem defines an input variable of WorkflowStep
   268  type InputItem struct {
   269  	ParameterKey string `json:"parameterKey,omitempty"`
   270  	From         string `json:"from"`
   271  }
   272  
   273  // OutputItem defines an output variable of WorkflowStep
   274  type OutputItem struct {
   275  	ValueFrom string `json:"valueFrom"`
   276  	Name      string `json:"name"`
   277  }