|
| 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 builtin |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + _ "embed" |
| 22 | + "encoding/json" |
| 23 | + "fmt" |
| 24 | + "strings" |
| 25 | + "time" |
| 26 | + |
| 27 | + "cuelang.org/go/cue/cuecontext" |
| 28 | + |
| 29 | + cuexruntime "github.com/kubevela/pkg/cue/cuex/runtime" |
| 30 | + |
| 31 | + "github.com/kubevela/workflow/api/v1alpha1" |
| 32 | + "github.com/kubevela/workflow/pkg/cue/model" |
| 33 | + "github.com/kubevela/workflow/pkg/errors" |
| 34 | + providertypes "github.com/kubevela/workflow/pkg/providers/types" |
| 35 | +) |
| 36 | + |
| 37 | +const ( |
| 38 | + // ProviderName is provider name. |
| 39 | + ProviderName = "builtin" |
| 40 | + // ResumeTimeStamp is resume time stamp. |
| 41 | + ResumeTimeStamp = "resumeTimeStamp" |
| 42 | + // SuspendTimeStamp is suspend time stamp. |
| 43 | + SuspendTimeStamp = "suspendTimeStamp" |
| 44 | +) |
| 45 | + |
| 46 | +// VarVars . |
| 47 | +type VarVars struct { |
| 48 | + Method string `json:"method"` |
| 49 | + Path string `json:"path"` |
| 50 | + Value any `json:"value"` |
| 51 | +} |
| 52 | + |
| 53 | +// VarReturnVars |
| 54 | +type VarReturnVars struct { |
| 55 | + Value any `json:"value"` |
| 56 | +} |
| 57 | + |
| 58 | +type VarReturns = providertypes.Returns[VarReturnVars] |
| 59 | + |
| 60 | +// VarParams . |
| 61 | +type VarParams = providertypes.Params[VarVars] |
| 62 | + |
| 63 | +// DoVar get & put variable from context. |
| 64 | +func DoVar(_ context.Context, params *VarParams) (*VarReturns, error) { |
| 65 | + wfCtx := params.RuntimeParams.WorkflowContext |
| 66 | + path := params.Params.Path |
| 67 | + |
| 68 | + switch params.Params.Method { |
| 69 | + case "Get": |
| 70 | + value, err := wfCtx.GetVar(strings.Split(path, ".")...) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + b, err := value.MarshalJSON() |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + var v any |
| 79 | + if err := json.Unmarshal(b, &v); err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + return &VarReturns{ |
| 83 | + Returns: VarReturnVars{ |
| 84 | + Value: v, |
| 85 | + }, |
| 86 | + }, nil |
| 87 | + case "Put": |
| 88 | + b, err := json.Marshal(params.Params.Value) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + if err := wfCtx.SetVar(cuecontext.New().CompileBytes(b), strings.Split(path, ".")...); err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + return nil, nil |
| 96 | + } |
| 97 | + return nil, nil |
| 98 | +} |
| 99 | + |
| 100 | +// ActionVars . |
| 101 | +type ActionVars struct { |
| 102 | + Message string `json:"message,omitempty"` |
| 103 | +} |
| 104 | + |
| 105 | +// ActionParams . |
| 106 | +type ActionParams = providertypes.Params[ActionVars] |
| 107 | + |
| 108 | +// WaitVars . |
| 109 | +type WaitVars struct { |
| 110 | + Continue bool `json:"continue"` |
| 111 | + ActionVars |
| 112 | +} |
| 113 | + |
| 114 | +// WaitParams . |
| 115 | +type WaitParams = providertypes.Params[WaitVars] |
| 116 | + |
| 117 | +// Wait let workflow wait. |
| 118 | +func Wait(_ context.Context, params *WaitParams) (*any, error) { |
| 119 | + if params.Params.Continue { |
| 120 | + return nil, nil |
| 121 | + } |
| 122 | + params.Action.Wait(params.Params.Message) |
| 123 | + return nil, errors.GenericActionError(errors.ActionWait) |
| 124 | +} |
| 125 | + |
| 126 | +// Break let workflow terminate. |
| 127 | +func Break(_ context.Context, params *ActionParams) (*any, error) { |
| 128 | + params.Action.Terminate(params.Params.Message) |
| 129 | + return nil, errors.GenericActionError(errors.ActionTerminate) |
| 130 | +} |
| 131 | + |
| 132 | +// Fail let the step fail, its status is failed and reason is Action |
| 133 | +func Fail(_ context.Context, params *ActionParams) (*any, error) { |
| 134 | + params.Action.Fail(params.Params.Message) |
| 135 | + return nil, errors.GenericActionError(errors.ActionTerminate) |
| 136 | +} |
| 137 | + |
| 138 | +// SuspendVars . |
| 139 | +type SuspendVars struct { |
| 140 | + Duration string `json:"duration,omitempty"` |
| 141 | + ActionVars |
| 142 | +} |
| 143 | + |
| 144 | +// SuspendParams . |
| 145 | +type SuspendParams = providertypes.Params[SuspendVars] |
| 146 | + |
| 147 | +// Suspend let the step suspend, its status is suspending and reason is Suspend |
| 148 | +func Suspend(_ context.Context, params *SuspendParams) (*any, error) { |
| 149 | + pCtx := params.ProcessContext |
| 150 | + wfCtx := params.WorkflowContext |
| 151 | + act := params.Action |
| 152 | + stepID := fmt.Sprint(pCtx.GetData(model.ContextStepSessionID)) |
| 153 | + timestamp := wfCtx.GetMutableValue(stepID, ResumeTimeStamp) |
| 154 | + |
| 155 | + var msg string |
| 156 | + if msg == "" { |
| 157 | + msg = fmt.Sprintf("Suspended by field %s", params.FieldLabel) |
| 158 | + } |
| 159 | + if timestamp != "" { |
| 160 | + t, err := time.Parse(time.RFC3339, timestamp) |
| 161 | + if err != nil { |
| 162 | + return nil, fmt.Errorf("failed to parse timestamp %s: %w", timestamp, err) |
| 163 | + } |
| 164 | + if time.Now().After(t) { |
| 165 | + act.Resume("") |
| 166 | + return nil, nil |
| 167 | + } |
| 168 | + act.Suspend(msg) |
| 169 | + return nil, errors.GenericActionError(errors.ActionSuspend) |
| 170 | + } |
| 171 | + if params.Params.Duration != "" { |
| 172 | + d, err := time.ParseDuration(params.Params.Duration) |
| 173 | + if err != nil { |
| 174 | + return nil, fmt.Errorf("failed to parse duration %s: %w", params.Params.Duration, err) |
| 175 | + } |
| 176 | + wfCtx.SetMutableValue(time.Now().Add(d).Format(time.RFC3339), stepID, ResumeTimeStamp) |
| 177 | + } |
| 178 | + if ts := wfCtx.GetMutableValue(stepID, params.FieldLabel, SuspendTimeStamp); ts != "" { |
| 179 | + if act.GetStatus().Phase == v1alpha1.WorkflowStepPhaseRunning { |
| 180 | + // if it is already suspended before and has been resumed, we should not suspend it again. |
| 181 | + return nil, nil |
| 182 | + } |
| 183 | + } else { |
| 184 | + wfCtx.SetMutableValue(time.Now().Format(time.RFC3339), stepID, params.FieldLabel, SuspendTimeStamp) |
| 185 | + } |
| 186 | + act.Suspend(msg) |
| 187 | + return nil, errors.GenericActionError(errors.ActionSuspend) |
| 188 | +} |
| 189 | + |
| 190 | +// Message writes message to step status, note that the message will be overwritten by the next message. |
| 191 | +func Message(_ context.Context, params *ActionParams) (*any, error) { |
| 192 | + params.Action.Message(params.Params.Message) |
| 193 | + return nil, nil |
| 194 | +} |
| 195 | + |
| 196 | +//go:embed workspace.cue |
| 197 | +var template string |
| 198 | + |
| 199 | +// GetTemplate returns the cue template. |
| 200 | +func GetTemplate() string { |
| 201 | + return template |
| 202 | +} |
| 203 | + |
| 204 | +// GetProviders returns the cue providers. |
| 205 | +func GetProviders() map[string]cuexruntime.ProviderFn { |
| 206 | + return map[string]cuexruntime.ProviderFn{ |
| 207 | + "wait": providertypes.GenericProviderFn[WaitVars, any](Wait), |
| 208 | + "break": providertypes.GenericProviderFn[ActionVars, any](Break), |
| 209 | + "fail": providertypes.GenericProviderFn[ActionVars, any](Fail), |
| 210 | + "message": providertypes.GenericProviderFn[ActionVars, any](Message), |
| 211 | + "var": providertypes.GenericProviderFn[VarVars, VarReturns](DoVar), |
| 212 | + "suspend": providertypes.GenericProviderFn[SuspendVars, any](Suspend), |
| 213 | + } |
| 214 | +} |
0 commit comments