github.com/kubevela/workflow@v0.6.0/pkg/types/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 types 18 19 import ( 20 "context" 21 22 "cuelang.org/go/cue" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/types" 25 "k8s.io/apiserver/pkg/util/feature" 26 "sigs.k8s.io/controller-runtime/pkg/client" 27 28 monitorContext "github.com/kubevela/pkg/monitor/context" 29 30 "github.com/kubevela/workflow/api/v1alpha1" 31 wfContext "github.com/kubevela/workflow/pkg/context" 32 "github.com/kubevela/workflow/pkg/cue/model/value" 33 "github.com/kubevela/workflow/pkg/cue/packages" 34 "github.com/kubevela/workflow/pkg/cue/process" 35 "github.com/kubevela/workflow/pkg/features" 36 "github.com/kubevela/workflow/pkg/tasks/template" 37 ) 38 39 // WorkflowInstance is the instance for workflow engine to execute 40 type WorkflowInstance struct { 41 WorkflowMeta 42 OwnerInfo []metav1.OwnerReference 43 Debug bool 44 Context map[string]interface{} 45 Mode *v1alpha1.WorkflowExecuteMode 46 Steps []v1alpha1.WorkflowStep 47 Status v1alpha1.WorkflowRunStatus 48 } 49 50 // WorkflowMeta is the meta information for workflow instance 51 type WorkflowMeta struct { 52 Name string 53 Namespace string 54 Annotations map[string]string 55 Labels map[string]string 56 UID types.UID 57 ChildOwnerReferences []metav1.OwnerReference 58 } 59 60 // ContextDataResetter reset process.Context after the step is finished 61 type ContextDataResetter func(processCtx process.Context) 62 63 // TaskRunner is a task runner 64 type TaskRunner interface { 65 Name() string 66 Pending(ctx monitorContext.Context, wfCtx wfContext.Context, stepStatus map[string]v1alpha1.StepStatus) (bool, v1alpha1.StepStatus) 67 Run(ctx wfContext.Context, options *TaskRunOptions) (v1alpha1.StepStatus, *Operation, error) 68 FillContextData(ctx monitorContext.Context, processCtx process.Context) ContextDataResetter 69 } 70 71 // TaskDiscover is the interface to obtain the TaskGenerator 72 type TaskDiscover interface { 73 GetTaskGenerator(ctx context.Context, name string) (TaskGenerator, error) 74 } 75 76 // Engine is the engine to run workflow 77 type Engine interface { 78 Run(ctx monitorContext.Context, taskRunners []TaskRunner, dag bool) error 79 GetStepStatus(stepName string) v1alpha1.WorkflowStepStatus 80 GetCommonStepStatus(stepName string) v1alpha1.StepStatus 81 SetParentRunner(name string) 82 GetOperation() *Operation 83 } 84 85 // TaskRunOptions is the options for task run 86 type TaskRunOptions struct { 87 Data *value.Value 88 PCtx process.Context 89 PreCheckHooks []TaskPreCheckHook 90 PreStartHooks []TaskPreStartHook 91 PostStopHooks []TaskPostStopHook 92 GetTracer func(id string, step v1alpha1.WorkflowStep) monitorContext.Context 93 RunSteps func(isDag bool, runners ...TaskRunner) (*v1alpha1.WorkflowRunStatus, error) 94 Debug func(step string, v *value.Value) error 95 StepStatus map[string]v1alpha1.StepStatus 96 Engine Engine 97 } 98 99 // PreCheckResult is the result of pre check. 100 type PreCheckResult struct { 101 Skip bool 102 Timeout bool 103 } 104 105 // PreCheckOptions is the options for pre check. 106 type PreCheckOptions struct { 107 PackageDiscover *packages.PackageDiscover 108 BasicTemplate string 109 BasicValue *value.Value 110 } 111 112 // StatusPatcher is the interface to patch status 113 type StatusPatcher func(ctx context.Context, status *v1alpha1.WorkflowRunStatus, isUpdate bool) error 114 115 // TaskPreCheckHook is the hook for pre check. 116 type TaskPreCheckHook func(step v1alpha1.WorkflowStep, options *PreCheckOptions) (*PreCheckResult, error) 117 118 // TaskPreStartHook run before task execution. 119 type TaskPreStartHook func(ctx wfContext.Context, paramValue *value.Value, step v1alpha1.WorkflowStep) error 120 121 // TaskPostStopHook run after task execution. 122 type TaskPostStopHook func(ctx wfContext.Context, taskValue *value.Value, step v1alpha1.WorkflowStep, status v1alpha1.StepStatus, stepStatus map[string]v1alpha1.StepStatus) error 123 124 // Operation is workflow operation object. 125 type Operation struct { 126 Suspend bool 127 Terminated bool 128 Waiting bool 129 Skip bool 130 FailedAfterRetries bool 131 } 132 133 // TaskGenerator will generate taskRunner. 134 type TaskGenerator func(wfStep v1alpha1.WorkflowStep, options *TaskGeneratorOptions) (TaskRunner, error) 135 136 // TaskGeneratorOptions is the options for generate task. 137 type TaskGeneratorOptions struct { 138 ID string 139 PrePhase v1alpha1.WorkflowStepPhase 140 StepConvertor func(step v1alpha1.WorkflowStep) (v1alpha1.WorkflowStep, error) 141 SubTaskRunners []TaskRunner 142 SubStepExecuteMode v1alpha1.WorkflowMode 143 PackageDiscover *packages.PackageDiscover 144 ProcessContext process.Context 145 } 146 147 // Handler is provider's processing method. 148 type Handler func(ctx monitorContext.Context, wfCtx wfContext.Context, v *value.Value, act Action) error 149 150 // Providers is provider discover interface. 151 type Providers interface { 152 GetHandler(provider, name string) (Handler, bool) 153 Register(provider string, m map[string]Handler) 154 } 155 156 // StepGeneratorOptions is the options for generate step. 157 type StepGeneratorOptions struct { 158 Providers Providers 159 PackageDiscover *packages.PackageDiscover 160 ProcessCtx process.Context 161 TemplateLoader template.Loader 162 Client client.Client 163 StepConvertor map[string]func(step v1alpha1.WorkflowStep) (v1alpha1.WorkflowStep, error) 164 LogLevel int 165 } 166 167 // Action is that workflow provider can do. 168 type Action interface { 169 Suspend(message string) 170 Resume(message string) 171 Terminate(message string) 172 Wait(message string) 173 Fail(message string) 174 Message(message string) 175 GetStatus() v1alpha1.StepStatus 176 } 177 178 // Parameter defines a parameter for cli from capability template 179 type Parameter struct { 180 Name string `json:"name"` 181 Short string `json:"short,omitempty"` 182 Required bool `json:"required,omitempty"` 183 Default interface{} `json:"default,omitempty"` 184 Usage string `json:"usage,omitempty"` 185 Ignore bool `json:"ignore,omitempty"` 186 Type cue.Kind `json:"type,omitempty"` 187 Alias string `json:"alias,omitempty"` 188 JSONType string `json:"jsonType,omitempty"` 189 } 190 191 // Resource is the log resources 192 type Resource struct { 193 Name string `json:"name,omitempty"` 194 Namespace string `json:"namespace,omitempty"` 195 Cluster string `json:"cluster,omitempty"` 196 LabelSelector map[string]string `json:"labelSelector,omitempty"` 197 } 198 199 // LogSource is the source of the log 200 type LogSource struct { 201 URL string `json:"url,omitempty"` 202 Resources []Resource `json:"resources,omitempty"` 203 } 204 205 // LogConfig is the config of the log 206 type LogConfig struct { 207 Data bool `json:"data,omitempty"` 208 Source *LogSource `json:"source,omitempty"` 209 } 210 211 const ( 212 // ContextPrefixFailedTimes is the prefix that refer to the failed times of the step in workflow context config map. 213 ContextPrefixFailedTimes = "failed_times" 214 // ContextPrefixBackoffTimes is the prefix that refer to the backoff times in workflow context config map. 215 ContextPrefixBackoffTimes = "backoff_times" 216 // ContextPrefixBackoffReason is the prefix that refer to the current backoff reason in workflow context config map 217 ContextPrefixBackoffReason = "backoff_reason" 218 // ContextKeyLastExecuteTime is the key that refer to the last execute time in workflow context config map. 219 ContextKeyLastExecuteTime = "last_execute_time" 220 // ContextKeyNextExecuteTime is the key that refer to the next execute time in workflow context config map. 221 ContextKeyNextExecuteTime = "next_execute_time" 222 // ContextKeyLogConfig is key for log config. 223 ContextKeyLogConfig = "logConfig" 224 ) 225 226 const ( 227 // WorkflowStepTypeSuspend type suspend 228 WorkflowStepTypeSuspend = "suspend" 229 // WorkflowStepTypeApplyComponent type apply-component 230 WorkflowStepTypeApplyComponent = "apply-component" 231 // WorkflowStepTypeBuiltinApplyComponent type builtin-apply-component 232 WorkflowStepTypeBuiltinApplyComponent = "builtin-apply-component" 233 // WorkflowStepTypeStepGroup type step-group 234 WorkflowStepTypeStepGroup = "step-group" 235 ) 236 237 const ( 238 // LabelWorkflowRunName is the label key for workflow run name 239 LabelWorkflowRunName = "workflowrun.oam.dev/name" 240 // LabelWorkflowRunNamespace is the label key for workflow run namespace 241 LabelWorkflowRunNamespace = "workflowrun.oam.dev/namespace" 242 ) 243 244 var ( 245 // MaxWorkflowStepErrorRetryTimes is the max retry times of the failed workflow step. 246 MaxWorkflowStepErrorRetryTimes = 10 247 // MaxWorkflowWaitBackoffTime is the max time to wait before reconcile wait workflow again 248 MaxWorkflowWaitBackoffTime = 60 249 // MaxWorkflowFailedBackoffTime is the max time to wait before reconcile failed workflow again 250 MaxWorkflowFailedBackoffTime = 300 251 ) 252 253 const ( 254 // StatusReasonWait is the reason of the workflow progress condition which is Wait. 255 StatusReasonWait = "Wait" 256 // StatusReasonSkip is the reason of the workflow progress condition which is Skip. 257 StatusReasonSkip = "Skip" 258 // StatusReasonRendering is the reason of the workflow progress condition which is Rendering. 259 StatusReasonRendering = "Rendering" 260 // StatusReasonExecute is the reason of the workflow progress condition which is Execute. 261 StatusReasonExecute = "Execute" 262 // StatusReasonSuspend is the reason of the workflow progress condition which is Suspend. 263 StatusReasonSuspend = "Suspend" 264 // StatusReasonTerminate is the reason of the workflow progress condition which is Terminate. 265 StatusReasonTerminate = "Terminate" 266 // StatusReasonParameter is the reason of the workflow progress condition which is ProcessParameter. 267 StatusReasonParameter = "ProcessParameter" 268 // StatusReasonInput is the reason of the workflow progress condition which is Input. 269 StatusReasonInput = "Input" 270 // StatusReasonOutput is the reason of the workflow progress condition which is Output. 271 StatusReasonOutput = "Output" 272 // StatusReasonFailedAfterRetries is the reason of the workflow progress condition which is FailedAfterRetries. 273 StatusReasonFailedAfterRetries = "FailedAfterRetries" 274 // StatusReasonTimeout is the reason of the workflow progress condition which is Timeout. 275 StatusReasonTimeout = "Timeout" 276 // StatusReasonAction is the reason of the workflow progress condition which is Action. 277 StatusReasonAction = "Action" 278 ) 279 280 const ( 281 // MessageSuspendFailedAfterRetries is the message of failed after retries 282 MessageSuspendFailedAfterRetries = "The workflow suspends automatically because the failed times of steps have reached the limit" 283 ) 284 285 const ( 286 // AnnotationWorkflowRunDebug is the annotation for debug 287 AnnotationWorkflowRunDebug = "workflowrun.oam.dev/debug" 288 // AnnotationControllerRequirement indicates the controller version that can process the workflow run 289 AnnotationControllerRequirement = "workflowrun.oam.dev/controller-version-require" 290 ) 291 292 // IsStepFinish will decide whether step is finish. 293 func IsStepFinish(phase v1alpha1.WorkflowStepPhase, reason string) bool { 294 if feature.DefaultMutableFeatureGate.Enabled(features.EnableSuspendOnFailure) { 295 return phase == v1alpha1.WorkflowStepPhaseSucceeded 296 } 297 switch phase { 298 case v1alpha1.WorkflowStepPhaseFailed: 299 return reason != "" && reason != StatusReasonExecute 300 case v1alpha1.WorkflowStepPhaseSkipped: 301 return true 302 case v1alpha1.WorkflowStepPhaseSucceeded: 303 return true 304 default: 305 return false 306 } 307 } 308 309 // SetNamespaceInCtx set namespace in context. 310 func SetNamespaceInCtx(ctx context.Context, namespace string) context.Context { 311 if namespace == "" { 312 // compatible with some webhook handlers that maybe receive empty string as app namespace which means `default` namespace 313 namespace = "default" 314 } 315 ctx = context.WithValue(ctx, template.DefinitionNamespace, namespace) 316 return ctx 317 }