github.com/kubevela/workflow@v0.6.0/pkg/hooks/data_passing.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 hooks
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"github.com/pkg/errors"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  
    27  	"github.com/kubevela/workflow/api/v1alpha1"
    28  	wfContext "github.com/kubevela/workflow/pkg/context"
    29  	"github.com/kubevela/workflow/pkg/cue/model/value"
    30  	wfTypes "github.com/kubevela/workflow/pkg/types"
    31  )
    32  
    33  // Input set data to parameter.
    34  func Input(ctx wfContext.Context, paramValue *value.Value, step v1alpha1.WorkflowStep) error {
    35  	for _, input := range step.Inputs {
    36  		inputValue, err := ctx.GetVar(strings.Split(input.From, ".")...)
    37  		if err != nil {
    38  			inputValue, err = paramValue.LookupByScript(input.From)
    39  			if err != nil {
    40  				return errors.WithMessagef(err, "get input from [%s]", input.From)
    41  			}
    42  		}
    43  		if input.ParameterKey != "" {
    44  			if err := paramValue.SetValueByScript(inputValue, strings.Join([]string{"parameter", input.ParameterKey}, ".")); err != nil || paramValue.Error() != nil {
    45  				if err != nil {
    46  					return err
    47  				}
    48  				if paramValue.Error() != nil {
    49  					return paramValue.Error()
    50  				}
    51  			}
    52  		}
    53  	}
    54  	return nil
    55  }
    56  
    57  // Output get data from task value.
    58  func Output(ctx wfContext.Context, taskValue *value.Value, step v1alpha1.WorkflowStep, status v1alpha1.StepStatus, stepStatus map[string]v1alpha1.StepStatus) error {
    59  	errMsg := ""
    60  	if wfTypes.IsStepFinish(status.Phase, status.Reason) {
    61  		SetAdditionalNameInStatus(stepStatus, step.Name, step.Properties, status)
    62  		for _, output := range step.Outputs {
    63  			v, err := taskValue.LookupByScript(output.ValueFrom)
    64  			// if the error is not nil and the step is not skipped, return the error
    65  			if err != nil && status.Phase != v1alpha1.WorkflowStepPhaseSkipped {
    66  				errMsg += fmt.Sprintf("failed to get output from %s: %s\n", output.ValueFrom, err.Error())
    67  			}
    68  			// if the error is not nil, set the value to null
    69  			if err != nil || v.Error() != nil {
    70  				v, _ = taskValue.MakeValue("null")
    71  			}
    72  			if err := ctx.SetVar(v, output.Name); err != nil {
    73  				errMsg += fmt.Sprintf("failed to set output %s: %s\n", output.Name, err.Error())
    74  			}
    75  		}
    76  	}
    77  
    78  	if errMsg != "" {
    79  		return errors.New(errMsg)
    80  	}
    81  	return nil
    82  }
    83  
    84  // SetAdditionalNameInStatus sets additional name from properties to status map
    85  func SetAdditionalNameInStatus(stepStatus map[string]v1alpha1.StepStatus, name string, properties *runtime.RawExtension, status v1alpha1.StepStatus) {
    86  	if stepStatus == nil || properties == nil {
    87  		return
    88  	}
    89  	o := struct {
    90  		Name      string `json:"name"`
    91  		Component string `json:"component"`
    92  	}{}
    93  	js, err := properties.MarshalJSON()
    94  	if err != nil {
    95  		return
    96  	}
    97  	if err := json.Unmarshal(js, &o); err != nil {
    98  		return
    99  	}
   100  	additionalName := ""
   101  	switch {
   102  	case o.Name != "":
   103  		additionalName = o.Name
   104  	case o.Component != "":
   105  		additionalName = o.Component
   106  	default:
   107  		return
   108  	}
   109  	if _, ok := stepStatus[additionalName]; !ok {
   110  		stepStatus[additionalName] = status
   111  		return
   112  	}
   113  }