Skip to content

Commit

Permalink
support go plugin (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyang0 committed Nov 1, 2023
1 parent 435f703 commit 03b85ee
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ deps:
go mod vendor

binary:
CGO_ENABLED=0 go build -ldflags "$(GO_LDFLAGS)" -o eru-core
CGO_ENABLED=1 go build -mod=readonly -ldflags "$(GO_LDFLAGS)" -o eru-core

build: deps binary

Expand Down
17 changes: 17 additions & 0 deletions resource/cobalt/cobalt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/projecteru2/core/resource/plugins"
"github.com/projecteru2/core/resource/plugins/binary"
"github.com/projecteru2/core/resource/plugins/cpumem"
"github.com/projecteru2/core/resource/plugins/goplugin"
"github.com/projecteru2/core/types"
"github.com/projecteru2/core/utils"
)
Expand Down Expand Up @@ -64,7 +65,23 @@ func (m *Manager) LoadPlugins(ctx context.Context, t *testing.T) error {
}
m.AddPlugins(b)
}
shareLibFiles, err := utils.ListAllShareLibFiles(m.config.ResourcePlugin.Dir)
if err != nil {
logger.Errorf(ctx, err, "failed to list all share lib files dir: %+v", m.config.ResourcePlugin.Dir)
return err
}

for _, file := range shareLibFiles {
logger.Infof(ctx, "load go plugin: %+v", file)
b, err := goplugin.NewPlugin(ctx, file, m.config)
if err != nil {
return err
}
if _, ok := cache[b.Name()]; ok {
continue
}
m.AddPlugins(b)
}
return nil
}

Expand Down
34 changes: 34 additions & 0 deletions resource/plugins/goplugin/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package goplugin

import (
"context"
"fmt"
"path/filepath"

goplugin "plugin"

"github.com/pkg/errors"
"github.com/projecteru2/core/resource/plugins"
coretypes "github.com/projecteru2/core/types"
)

// NewPlugin .
func NewPlugin(ctx context.Context, path string, config coretypes.Config) (plugins.Plugin, error) {
pFname, err := filepath.Abs(path)
if err != nil {
return nil, err
}
gp, err := goplugin.Open(pFname)
if err != nil {
return nil, errors.Wrapf(err, "failed to open plugin %s", pFname)
}
sym, err := gp.Lookup("NewPlugin")
if err != nil {
return nil, errors.Wrapf(err, "failed to lookup NewPlugin %s", pFname)
}
fn, ok := sym.(func(context.Context, coretypes.Config) (plugins.Plugin, error))
if !ok {
return nil, fmt.Errorf("NewPlugin is not a function")
}
return fn(ctx, config)
}
18 changes: 18 additions & 0 deletions utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ func ListAllExecutableFiles(basedir string) ([]string, error) {
return files, err
}

func ListAllShareLibFiles(basedir string) ([]string, error) {
files := []string{}
err := filepath.Walk(basedir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && path != basedir {
return filepath.SkipDir
}
if !info.IsDir() && filepath.Ext(path) == ".so" {
files = append(files, path)
}
return nil
})

return files, err
}

func isExecutable(perm fs.FileMode) bool {
return perm&executablePerm == executablePerm
}
27 changes: 27 additions & 0 deletions utils/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -35,3 +36,29 @@ func TestListAllExecutableFiles(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, fs, 1)
}

func TestListAllShareLibFiles(t *testing.T) {
dir, err := os.MkdirTemp(os.TempDir(), "test*")
assert.NoError(t, err)
defer os.RemoveAll(dir)

_, err = os.Create(filepath.Join(dir, "abc"))
assert.NoError(t, err)

_, err = os.Create(filepath.Join(dir, "bcd.so"))
assert.NoError(t, err)

subdir, err := os.MkdirTemp(dir, "def")
assert.NoError(t, err)

_, err = os.Create(filepath.Join(subdir, "abc1"))
assert.NoError(t, err)

_, err = os.Create(filepath.Join(subdir, "bcd1.so"))
assert.NoError(t, err)

fs, err := ListAllShareLibFiles(dir)
assert.NoError(t, err)
assert.Len(t, fs, 1)
assert.Equal(t, filepath.Join(dir, "bcd.so"), fs[0])
}

0 comments on commit 03b85ee

Please sign in to comment.