From ccde8e8f5c31e66c323bbb32c9934217afe90c2d Mon Sep 17 00:00:00 2001 From: Jim Ma Date: Mon, 22 Nov 2021 19:29:26 +0800 Subject: [PATCH] Fix: source plugin not loaded (#811) * fix: source plugin not loaded Signed-off-by: Jim Ma --- hack/build.sh | 3 ++- hack/docker-build.sh | 2 ++ hack/env.sh | 3 ++- internal/dfplugin/dfplugin.go | 2 +- pkg/source/plugin.go | 16 +++++++++++++--- pkg/source/source_client.go | 4 ++++ 6 files changed, 24 insertions(+), 6 deletions(-) diff --git a/hack/build.sh b/hack/build.sh index ff56684a380..c50d3fd7581 100755 --- a/hack/build.sh +++ b/hack/build.sh @@ -15,6 +15,7 @@ BUILD_IMAGE=golang:1.16.6-alpine VERSION=$(git rev-parse --short HEAD) BUILD_TIME=$(date -u '+%Y-%m-%dT%H:%M:%SZ') +CGO_ENABLED=${CGO_ENABLED:-0} GOPROXY=${GOPROXY:-} GOTAGS=${GOTAGS:-} GOGCFLAGS=${GOGCFLAGS:-} @@ -75,7 +76,7 @@ build-docker() { -v "$(pwd)"/.cache:/.cache \ -e GOOS="${GOOS}" \ -e GOARCH="${GOARCH}" \ - -e CGO_ENABLED=0 \ + -e CGO_ENABLED="${CGO_ENABLED}" \ -e GO111MODULE=on \ -e GOPROXY="${GOPROXY}" \ -e GOTAGS="${GOTAGS}" \ diff --git a/hack/docker-build.sh b/hack/docker-build.sh index 1e441c1d798..1b8a396eb08 100755 --- a/hack/docker-build.sh +++ b/hack/docker-build.sh @@ -12,6 +12,7 @@ D7Y_REGISTRY=${D7Y_REGISTRY:-d7yio} IMAGES_DIR="build/images" BASE_IMAGE=${BASE_IMAGE:-alpine:3.12} +CGO_ENABLED=${CGO_ENABLED:-0} GOPROXY=${GOPROXY:-`go env GOPROXY`} GOTAGS=${GOTAGS:-} GOGCFLAGS=${GOGCFLAGS:-} @@ -26,6 +27,7 @@ fi docker-build() { name=$1 docker build \ + --build-arg CGO_ENABLED="${CGO_ENABLED}" \ --build-arg GOPROXY="${GOPROXY}" \ --build-arg GOTAGS="${GOTAGS}" \ --build-arg GOGCFLAGS="${GOGCFLAGS}" \ diff --git a/hack/env.sh b/hack/env.sh index 811b111ce7b..04494966ce0 100644 --- a/hack/env.sh +++ b/hack/env.sh @@ -11,6 +11,7 @@ export GO_SOURCE_EXCLUDES=( \ GOOS=$(go env GOOS) GOARCH=$(go env GOARCH) +CGO_ENABLED=${CGO_ENABLED:-0} export GOOS export GOARCH -export CGO_ENABLED=0 +export CGO_ENABLED diff --git a/internal/dfplugin/dfplugin.go b/internal/dfplugin/dfplugin.go index df7c905fd00..d95847fd6bc 100644 --- a/internal/dfplugin/dfplugin.go +++ b/internal/dfplugin/dfplugin.go @@ -32,7 +32,7 @@ const ( // PluginInitFuncName indicates the function `DragonflyPluginInit` must be implemented in plugin PluginInitFuncName = "DragonflyPluginInit" - // PluginMetaKeyType indicates the type of a plugin, currently support: resource + // PluginMetaKeyType indicates the type of plugin, currently support: resource PluginMetaKeyType = "type" // PluginMetaKeyName indicates the name of a plugin diff --git a/pkg/source/plugin.go b/pkg/source/plugin.go index 292cf421a07..2a3c061fcdb 100644 --- a/pkg/source/plugin.go +++ b/pkg/source/plugin.go @@ -19,6 +19,7 @@ package source import ( "errors" + logger "d7y.io/dragonfly/v2/internal/dflog" "d7y.io/dragonfly/v2/internal/dfplugin" ) @@ -28,15 +29,24 @@ const ( func LoadPlugin(schema string) (ResourceClient, error) { // TODO init option + logger.Debugf("try to load source plugin: %s", schema) client, meta, err := dfplugin.Load(dfplugin.PluginTypeResource, schema, map[string]string{}) if err != nil { + logger.Errorf("load source plugin error: %s", err) return nil, err } + if meta[pluginMetadataSchema] != schema { + logger.Errorf("load source plugin error: support schema not match") return nil, errors.New("support schema not match") } - if rc, ok := client.(ResourceClient); ok { - return rc, err + + rc, ok := client.(ResourceClient) + if !ok { + logger.Errorf("invalid client, not a ResourceClient") + return nil, errors.New("invalid client, not a ResourceClient") } - return nil, errors.New("invalid client, not a ResourceClient") + + logger.Debugf("loaded source plugin %s", schema) + return rc, nil } diff --git a/pkg/source/source_client.go b/pkg/source/source_client.go index b607cf0c3a5..5d82fb6a9d0 100644 --- a/pkg/source/source_client.go +++ b/pkg/source/source_client.go @@ -229,6 +229,10 @@ func (m *clientManager) getSourceClient(rawURL string) (ResourceClient, error) { client, ok := m.clients[strings.ToLower(parsedURL.Scheme)] m.RUnlock() if !ok || client == nil { + client, err = m.loadSourcePlugin(strings.ToLower(parsedURL.Scheme)) + if err == nil && client != nil { + return client, nil + } return nil, errors.Errorf("can not find client for supporting url %s, clients:%v", rawURL, m.clients) } return client, nil