From 8252b1eada4be6097bbf6db32e0f058945c7474a Mon Sep 17 00:00:00 2001 From: Somefive Date: Tue, 22 Nov 2022 10:52:49 +0800 Subject: [PATCH] Fix: patchOutputs bug for multiple outputs (#5101) Signed-off-by: Somefive Signed-off-by: Somefive --- pkg/cue/definition/template.go | 5 ++- pkg/cue/definition/template_test.go | 54 +++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/pkg/cue/definition/template.go b/pkg/cue/definition/template.go index 78cfb2aa4..fb8747c74 100644 --- a/pkg/cue/definition/template.go +++ b/pkg/cue/definition/template.go @@ -365,10 +365,9 @@ func (td *traitDef) Complete(ctx process.Context, abstractTemplate string, param for _, auxiliary := range auxiliaries { target := outputsPatcher.LookupPath(value.FieldPath(auxiliary.Name)) if !target.Exists() { - return errors.WithMessagef(err, "trait=%s, to=%s, invalid patch trait into auxiliary workload", td.name, auxiliary.Name) + continue } - patcher := outputsPatcher.LookupPath(value.FieldPath(auxiliary.Name)) - if err := auxiliary.Ins.Unify(patcher); err != nil { + if err = auxiliary.Ins.Unify(target); err != nil { return errors.WithMessagef(err, "trait=%s, to=%s, invalid patch trait into auxiliary workload", td.name, auxiliary.Name) } } diff --git a/pkg/cue/definition/template_test.go b/pkg/cue/definition/template_test.go index fa943363a..00c9450ca 100644 --- a/pkg/cue/definition/template_test.go +++ b/pkg/cue/definition/template_test.go @@ -1480,3 +1480,57 @@ if len(context.outputs.ingress.status.loadBalancer.ingress) == 0 { assert.Equal(t, ca.expMessage, gotMessage, message) } } + +func TestTraitPatchSingleOutput(t *testing.T) { + baseTemplate := ` + output: { + apiVersion: "apps/v1" + kind: "Deployment" + spec: selector: matchLabels: "app.oam.dev/component": context.name + } + + outputs: gameconfig: { + apiVersion: "v1" + kind: "ConfigMap" + metadata: name: context.name + "game-config" + data: {} + } + + outputs: sideconfig: { + apiVersion: "v1" + kind: "ConfigMap" + metadata: name: context.name + "side-config" + data: {} + } + + parameter: {} +` + traitTemplate := ` + patchOutputs: sideconfig: data: key: "val" + parameter: {} +` + ctx := process.NewContext(process.ContextData{ + AppName: "myapp", + CompName: "test", + Namespace: "default", + AppRevisionName: "myapp-v1", + }) + wt := NewWorkloadAbstractEngine("-", &packages.PackageDiscover{}) + if err := wt.Complete(ctx, baseTemplate, map[string]interface{}{}); err != nil { + t.Error(err) + return + } + td := NewTraitAbstractEngine("single-patch", &packages.PackageDiscover{}) + r := require.New(t) + err := td.Complete(ctx, traitTemplate, map[string]string{}) + r.NoError(err) + base, assists := ctx.Output() + r.NotNil(base) + r.Equal(2, len(assists)) + got, err := assists[1].Ins.Unstructured() + r.NoError(err) + val, ok, err := unstructured.NestedString(got.Object, "data", "key") + r.NoError(err) + r.True(ok) + r.Equal("val", val) +}