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: unify stdlib for workflow and kubevela #85

Merged
merged 1 commit into from
Nov 11, 2022
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
16 changes: 8 additions & 8 deletions makefiles/e2e.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ e2e-setup-controller-pre-hook:

.PHONY: e2e-setup-controller
e2e-setup-controller:
helm upgrade --install \
--create-namespace \
--namespace vela-system \
--set image.repository=oamdev/vela-workflow \
--set image.tag=latest \
--set image.pullPolicy=IfNotPresent \
--wait vela-workflow \
./charts/vela-workflow
helm upgrade --install \
--create-namespace \
--namespace vela-system \
--set image.repository=oamdev/vela-workflow \
--set image.tag=latest \
--set image.pullPolicy=IfNotPresent \
--wait vela-workflow \
./charts/vela-workflow

.PHONY: end-e2e
end-e2e:
Expand Down
85 changes: 46 additions & 39 deletions pkg/stdlib/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,20 @@ import (
"path/filepath"
"strings"

"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/parser"
"k8s.io/klog/v2"
)

func init() {
var err error
builtinImport, err = initBuiltinImports()
pkgs, err := GetPackages()
if err != nil {
klog.ErrorS(err, "Unable to init builtin packages for imports")
os.Exit(1)
}
builtinImport, err = InitBuiltinImports(pkgs)
if err != nil {
klog.ErrorS(err, "Unable to init builtin imports")
os.Exit(1)
Expand All @@ -42,8 +48,6 @@ var (
fs embed.FS
// builtinImport is the builtin import for cue
builtinImport []*build.Instance
// GeneralImports is the general imports for cue
GeneralImports []*build.Instance
)

const (
Expand All @@ -53,18 +57,37 @@ const (
defaultVersion = "v1"
)

// SetupGeneralImports setup general imports
func SetupGeneralImports(general []*build.Instance) {
GeneralImports = general
// SetupBuiltinImports setup builtin imports
func SetupBuiltinImports(pkgs map[string]string) error {
builtin, err := GetPackages()
if err != nil {
return err
}
for k, v := range pkgs {
file, err := parser.ParseFile("-", v, parser.ParseComments)
if err != nil {
return err
}
if original, ok := builtin[k]; ok {
builtin[k] = mergeFiles(original, file)
} else {
builtin[k] = file
}
}
builtinImport, err = InitBuiltinImports(builtin)
if err != nil {
return err
}
return nil
}

// GetPackages Get Stdlib packages
func GetPackages() (map[string]string, error) {
func GetPackages() (map[string]*ast.File, error) {
versions, err := fs.ReadDir(builtinActionPath)
if err != nil {
return nil, err
}
ret := make(map[string]string)
ret := make(map[string]*ast.File)

for _, dirs := range versions {
pathPrefix := fmt.Sprintf("%s/%s", builtinActionPath, dirs.Name())
Expand All @@ -85,35 +108,21 @@ func GetPackages() (map[string]string, error) {
pkgContent := fmt.Sprintf("%s: {\n%s\n}\n", strings.TrimSuffix(file.Name(), ".cue"), string(body))
opContent += pkgContent
}
f, err := parser.ParseFile("-", opContent, parser.ParseComments)
if err != nil {
return nil, err
}
if dirs.Name() == defaultVersion {
ret[builtinPackageName] = opContent
ret[builtinPackageName] = f
}
ret[filepath.Join(builtinPackageName, dirs.Name())] = opContent
ret[filepath.Join(builtinPackageName, dirs.Name())] = f
}
return ret, nil
}

// AddImportsFor install imports for build.Instance.
func AddImportsFor(inst *build.Instance, tagTempl string) error {
inst.Imports = append(inst.Imports, GeneralImports...)
addDefault := make(map[string]bool)
for _, builtin := range builtinImport {
addDefault[builtin.PkgName] = true
}

for _, a := range inst.Imports {
for _, builtin := range builtinImport {
if a.PkgName == builtin.PkgName {
addDefault[builtin.PkgName] = false
}
}
}

for _, builtin := range builtinImport {
if add := addDefault[builtin.PkgName]; add {
inst.Imports = append(inst.Imports, builtin)
}
}
inst.Imports = append(inst.Imports, builtinImport...)
if tagTempl != "" {
p := &build.Instance{
PkgName: filepath.Base("vela/custom"),
Expand All @@ -131,25 +140,23 @@ func AddImportsFor(inst *build.Instance, tagTempl string) error {
return nil
}

func initBuiltinImports() ([]*build.Instance, error) {
// InitBuiltinImports init built in imports
func InitBuiltinImports(pkgs map[string]*ast.File) ([]*build.Instance, error) {
imports := make([]*build.Instance, 0)
pkgs, err := GetPackages()
if err != nil {
return nil, err
}
for path, content := range pkgs {
p := &build.Instance{
PkgName: filepath.Base(path),
ImportPath: path,
}
file, err := parser.ParseFile("-", content, parser.ParseComments)
if err != nil {
return nil, err
}
if err := p.AddSyntax(file); err != nil {
if err := p.AddSyntax(content); err != nil {
return nil, err
}
imports = append(imports, p)
}
return imports, nil
}

func mergeFiles(base, file *ast.File) *ast.File {
base.Decls = append(base.Decls, file.Decls...)
return base
}
44 changes: 37 additions & 7 deletions pkg/stdlib/packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"cuelang.org/go/cue/build"
"cuelang.org/go/cue/cuecontext"
"cuelang.org/go/cue/parser"
"github.com/kubevela/workflow/pkg/cue/model/sets"
"github.com/stretchr/testify/require"
)

Expand All @@ -32,11 +33,9 @@ func TestGetPackages(t *testing.T) {
pkg, err := GetPackages()
r.NoError(err)
cuectx := cuecontext.New()
file, err := parser.ParseFile(builtinPackageName, pkg[builtinPackageName])
r.NoError(err)
_ = cuectx.BuildFile(file)
_ = cuectx.BuildFile(pkg[builtinPackageName])

file, err = parser.ParseFile("-", `
file, err := parser.ParseFile("-", `
import "vela/custom"
out: custom.context`)
r.NoError(err)
Expand All @@ -54,9 +53,7 @@ out: custom.context`)
//test vela/op/v1
testVersion := "vela/op/v1"
cuectx1 := cuecontext.New()
file, err = parser.ParseFile(testVersion, pkg[testVersion])
r.NoError(err)
_ = cuectx1.BuildFile(file)
_ = cuectx1.BuildFile(pkg[testVersion])

file, err = parser.ParseFile("-", `
import "vela/op/v1"
Expand All @@ -75,3 +72,36 @@ out: v1.#Break & {
r.NoError(err)
r.Equal(str1, "break")
}

func TestSetupBuiltinImports(t *testing.T) {
//test vela/op
r := require.New(t)
err := SetupBuiltinImports(map[string]string{"vela/op": "test: kube.#Apply"})
r.NoError(err)
file, err := parser.ParseFile("-", `
import "vela/op"
step: op.#Steps
apply: op.test`)
r.NoError(err)
builder := &build.Instance{}
err = builder.AddSyntax(file)
r.NoError(err)
err = AddImportsFor(builder, "")
r.NoError(err)

cuectx := cuecontext.New()
inst := cuectx.BuildInstance(builder)
r.NoError(inst.Value().Err())
v := inst.LookupPath(cue.ParsePath("step"))
r.NoError(err)
r.NoError(v.Err())
s, err := sets.ToString(v)
r.NoError(err)
r.Equal(s, "#do: \"steps\"\n")
v = inst.LookupPath(cue.ParsePath("apply"))
r.NoError(err)
r.NoError(v.Err())
s, err = sets.ToString(v)
r.NoError(err)
r.Equal(s, "#do: \"apply\"\n#provider: \"kube\"\n\n// +usage=The cluster to use\ncluster: *\"\" | string\n// +usage=The resource to apply\nvalue: {}\n")
}