Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: fix native cue providers #193

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions pkg/providers/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
_ "embed"
"encoding/json"
"fmt"

"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand All @@ -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 {
Expand Down
28 changes: 15 additions & 13 deletions pkg/providers/kube/kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}]
}
}
}
`)
Expand Down
12 changes: 8 additions & 4 deletions pkg/providers/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/providers/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading