Fix: optimize skip reconcile and expose error if the traits patch an invalid workload like terraform (#5334)

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>

Signed-off-by: FogDong <dongtianxin.tx@alibaba-inc.com>
This commit is contained in:
Tianxin Dong
2023-01-13 17:04:22 +08:00
committed by GitHub
parent 9ab6fe4188
commit 5158fcf6c1
3 changed files with 38 additions and 3 deletions
@@ -254,7 +254,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
}
}
case workflowv1alpha1.WorkflowStateSkipped:
return ctrl.Result{}, nil
return r.result(nil).requeue(executor.GetBackoffWaitTime()).ret()
default:
}
+5 -2
View File
@@ -351,13 +351,16 @@ func (td *traitDef) Complete(ctx process.Context, abstractTemplate string, param
patcher := val.LookupPath(value.FieldPath(PatchFieldName))
base, auxiliaries := ctx.Output()
if base != nil && patcher.Exists() {
if patcher.Exists() {
if base == nil {
return fmt.Errorf("patch trait %s into an invalid workload", td.name)
}
if err := base.Unify(patcher, sets.CreateUnifyOptionsForPatcher(patcher)...); err != nil {
return errors.WithMessagef(err, "invalid patch trait %s into workload", td.name)
}
}
outputsPatcher := val.LookupPath(value.FieldPath(PatchOutputsFieldName))
if base != nil && outputsPatcher.Exists() {
if outputsPatcher.Exists() {
for _, auxiliary := range auxiliaries {
target := outputsPatcher.LookupPath(value.FieldPath(auxiliary.Name))
if !target.Exists() {
+32
View File
@@ -25,6 +25,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"github.com/kubevela/workflow/pkg/cue/packages"
wfprocess "github.com/kubevela/workflow/pkg/cue/process"
"github.com/oam-dev/kubevela/apis/types"
"github.com/oam-dev/kubevela/pkg/cue/process"
@@ -1534,3 +1535,34 @@ func TestTraitPatchSingleOutput(t *testing.T) {
r.True(ok)
r.Equal("val", val)
}
func TestTraitCompleteErrorCases(t *testing.T) {
cases := map[string]struct {
ctx wfprocess.Context
traitName string
template string
params map[string]interface{}
err string
}{
"patch trait": {
ctx: process.NewContext(process.ContextData{}),
template: `
patch: {
// +patchKey=name
spec: template: spec: containers: [parameter]
}
parameter: {
name: string
image: string
command?: [...string]
}`,
err: "patch trait patch trait into an invalid workload",
},
}
for k, v := range cases {
td := NewTraitAbstractEngine(k, &packages.PackageDiscover{})
err := td.Complete(v.ctx, v.template, v.params)
assert.Error(t, err)
assert.Contains(t, err.Error(), v.err)
}
}