diff --git a/pkg/providers/kube/kube.go b/pkg/providers/kube/kube.go index 89acea2..eef4245 100644 --- a/pkg/providers/kube/kube.go +++ b/pkg/providers/kube/kube.go @@ -20,6 +20,7 @@ import ( "context" _ "embed" "encoding/json" + "fmt" "cuelang.org/go/cue" "cuelang.org/go/cue/cuecontext" @@ -198,7 +199,11 @@ func ApplyInParallel(ctx context.Context, params *ApplyInParallelParams) (*Apply // Patch patch CR in cluster. func Patch(ctx context.Context, params *providertypes.Params[cue.Value]) (cue.Value, error) { handlers := getHandlers(params.RuntimeParams) - val := params.Params.LookupPath(cue.ParsePath("value")) + parameter := params.Params.LookupPath(cue.ParsePath("$params")) + if !parameter.Exists() { + return cue.Value{}, fmt.Errorf("$params not found") + } + val := parameter.LookupPath(cue.ParsePath("value")) obj := new(unstructured.Unstructured) b, err := val.MarshalJSON() if err != nil { @@ -211,7 +216,7 @@ func Patch(ctx context.Context, params *providertypes.Params[cue.Value]) (cue.Va if key.Namespace == "" { key.Namespace = "default" } - cluster, err := params.Params.LookupPath(cue.ParsePath("cluster")).String() + cluster, err := parameter.LookupPath(cue.ParsePath("cluster")).String() if err != nil { return cue.Value{}, err } @@ -220,7 +225,7 @@ func Patch(ctx context.Context, params *providertypes.Params[cue.Value]) (cue.Va return cue.Value{}, err } baseVal := cuecontext.New().CompileString("").FillPath(cue.ParsePath(""), obj) - patcher := params.Params.LookupPath(cue.ParsePath("patch")) + patcher := parameter.LookupPath(cue.ParsePath("patch")) base, err := model.NewBase(baseVal) if err != nil { diff --git a/pkg/providers/kube/kube_test.go b/pkg/providers/kube/kube_test.go index fba9791..49f1409 100644 --- a/pkg/providers/kube/kube_test.go +++ b/pkg/providers/kube/kube_test.go @@ -163,19 +163,21 @@ var _ = Describe("Test Workflow Provider Kube", func() { Expect(err).ToNot(HaveOccurred()) v := cuecontext.New().CompileString(` -value: { - apiVersion: "v1" - kind: "Pod" - metadata: name: "test-app-1" -} -cluster: "" -patch: { - metadata: name: "test-app-1" - spec: { - containers: [{ - // +patchStrategy=retainKeys - image: "nginx:notfound" - }] +$params: { + value: { + apiVersion: "v1" + kind: "Pod" + metadata: name: "test-app-1" + } + cluster: "" + patch: { + metadata: name: "test-app-1" + spec: { + containers: [{ + // +patchStrategy=retainKeys + image: "nginx:notfound" + }] + } } } `) diff --git a/pkg/providers/util/util.go b/pkg/providers/util/util.go index 0f05900..bac4f7c 100644 --- a/pkg/providers/util/util.go +++ b/pkg/providers/util/util.go @@ -43,17 +43,21 @@ type PatchVars struct { // PatchK8sObject patch k8s object func PatchK8sObject(_ context.Context, params *providertypes.Params[cue.Value]) (cue.Value, error) { - base, err := model.NewBase(params.Params.LookupPath(cue.ParsePath("value"))) + parameter := params.Params.LookupPath(cue.ParsePath("$params")) + if !parameter.Exists() { + return cue.Value{}, fmt.Errorf("$params not found") + } + base, err := model.NewBase(parameter.LookupPath(cue.ParsePath("value"))) if err != nil { return cue.Value{}, err } - if err = base.Unify(params.Params.LookupPath(cue.ParsePath("patch"))); err != nil { - return params.Params.FillPath(cue.ParsePath("err"), err.Error()), nil + if err = base.Unify(parameter.LookupPath(cue.ParsePath("patch"))); err != nil { + return params.Params.FillPath(value.FieldPath("$returns", "err"), err.Error()), nil } workload, err := base.Compile() if err != nil { - return params.Params.FillPath(cue.ParsePath("err"), err.Error()), nil + return params.Params.FillPath(value.FieldPath("$returns", "err"), err.Error()), nil } return params.Params.FillPath(value.FieldPath("$returns", "result"), params.Params.Context().CompileBytes(workload)), nil } diff --git a/pkg/providers/util/util_test.go b/pkg/providers/util/util_test.go index 134c16a..863e5f2 100644 --- a/pkg/providers/util/util_test.go +++ b/pkg/providers/util/util_test.go @@ -131,7 +131,7 @@ spec: template: metadata: name: "test-patchStrategy"`, t.Run(name, func(t *testing.T) { r := require.New(t) res, err := PatchK8sObject(ctx, &providertypes.Params[cue.Value]{ - Params: cuectx.CompileString(tc.value), + Params: cuectx.CompileString(fmt.Sprintf("$params:{%s}", tc.value)), }) if tc.expectedErr != nil { r.Equal(tc.expectedErr.Error(), err.Error())