github.com/kubevela/workflow@v0.6.0/pkg/webhook/v1alpha1/workflowrun/validation.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 workflowrun 18 19 import ( 20 "context" 21 "fmt" 22 "time" 23 24 "k8s.io/apimachinery/pkg/util/validation/field" 25 "sigs.k8s.io/controller-runtime/pkg/client" 26 27 "github.com/kubevela/workflow/api/v1alpha1" 28 ) 29 30 // ValidateWorkflow validates the Application workflow 31 func (h *ValidatingHandler) ValidateWorkflow(ctx context.Context, wr *v1alpha1.WorkflowRun) field.ErrorList { 32 var errs field.ErrorList 33 var steps []v1alpha1.WorkflowStep 34 if wr.Spec.WorkflowSpec != nil { 35 steps = wr.Spec.WorkflowSpec.Steps 36 } else { 37 w := &v1alpha1.Workflow{} 38 if err := h.Client.Get(ctx, client.ObjectKey{Namespace: wr.Namespace, Name: wr.Spec.WorkflowRef}, w); err != nil { 39 errs = append(errs, field.Invalid(field.NewPath("spec", "workflowRef"), wr.Spec.WorkflowRef, fmt.Sprintf("failed to get workflow ref: %v", err))) 40 return errs 41 } 42 steps = w.Steps 43 } 44 stepName := make(map[string]interface{}) 45 for _, step := range steps { 46 if step.Name == "" { 47 errs = append(errs, field.Invalid(field.NewPath("spec", "workflowSpec", "steps", "name"), step.Name, "empty step name")) 48 } 49 if _, ok := stepName[step.Name]; ok { 50 errs = append(errs, field.Invalid(field.NewPath("spec", "workflowSpec", "steps"), step.Name, "duplicated step name")) 51 } 52 stepName[step.Name] = nil 53 if step.Timeout != "" { 54 errs = append(errs, h.ValidateTimeout(step.Name, step.Timeout)...) 55 } 56 for _, sub := range step.SubSteps { 57 if sub.Name == "" { 58 errs = append(errs, field.Invalid(field.NewPath("spec", "workflowSpec", "steps", "subSteps", "name"), sub.Name, "empty step name")) 59 } 60 if _, ok := stepName[sub.Name]; ok { 61 errs = append(errs, field.Invalid(field.NewPath("spec", "workflowSpec", "steps", "subSteps"), sub.Name, "duplicated step name")) 62 } 63 stepName[sub.Name] = nil 64 if step.Timeout != "" { 65 errs = append(errs, h.ValidateTimeout(step.Name, step.Timeout)...) 66 } 67 } 68 } 69 return errs 70 } 71 72 // ValidateTimeout validates the timeout of steps 73 func (h *ValidatingHandler) ValidateTimeout(name, timeout string) field.ErrorList { 74 var errs field.ErrorList 75 _, err := time.ParseDuration(timeout) 76 if err != nil { 77 errs = append(errs, field.Invalid(field.NewPath("spec", "workflowSpec", "steps", "timeout"), name, "invalid timeout, please use the format of timeout like 1s, 1m, 1h or 1d")) 78 } 79 return errs 80 }