Skip to content

Commit

Permalink
fix plugin bug (#1854)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stevent-fei authored Jan 10, 2023
1 parent 884513e commit af66a2c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion docs/design/global-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,5 @@ CMD helm install mysql -f etc/redis-config.yaml

Before mounting Rootfs, templates need to be rendered for the files in etc, charts, and manifest directories,
and render environment variables and annotations to the [configuration file](
https://github.com/sealerio/sealer/blob/main/pkg/filesystem/filesystem.go#L145).
https://github.com/sealerio/sealer/blob/release-v0.8.6/pkg/filesystem/filesystem.go#L145).
Generate the global.yaml file to the etc directory
2 changes: 1 addition & 1 deletion docs/design/global-config_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ CMD helm install dashboard dashboard-chart -f etc/global.yaml
## 开发文档

1. 在apply mountRootfs之前对etc,charts,manifest目录下的文件进行模板渲染,把环境变量和annotations渲染到[配置文件中](
https://github.com/sealerio/sealer/blob/main/pkg/filesystem/filesystem.go#L145) 。
https://github.com/sealerio/sealer/blob/release-v0.8.6/pkg/filesystem/filesystem.go#L145) 。
2. 生成global.yaml文件到etc目录下。
40 changes: 26 additions & 14 deletions pkg/plugin/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ import (
"plugin"
"strings"

"github.com/sealerio/sealer/utils"

"github.com/sealerio/sealer/utils/yaml"

"github.com/sealerio/sealer/common"
v1 "github.com/sealerio/sealer/types/api/v1"
v2 "github.com/sealerio/sealer/types/api/v2"
"github.com/sealerio/sealer/utils"
"github.com/sealerio/sealer/utils/platform"
strUtils "github.com/sealerio/sealer/utils/strings"
"github.com/sealerio/sealer/utils/yaml"
)

type InvalidPluginTypeError struct {
Expand Down Expand Up @@ -74,6 +72,7 @@ func (c *PluginsProcessor) Load() error {
if err != nil {
return fmt.Errorf("failed to load plugin dir: %v", err)
}

for _, f := range files {
// load shared object(.so) file
if filepath.Ext(f.Name()) == ".so" {
Expand All @@ -87,23 +86,36 @@ func (c *PluginsProcessor) Load() error {
if yaml.Matcher(f.Name()) {
plugins, err := utils.DecodeCRDFromFile(filepath.Join(path, f.Name()), common.Plugin)
if err != nil {
return fmt.Errorf("failed to load plugin: %v", err)
}
var plugs []v1.Plugin
for _, p := range plugins.([]v1.Plugin) {
for _, cp := range c.Plugins {
if !isSamePluginSpec(p, cp) {
plugs = append(plugs, p)
}
}
return fmt.Errorf("failed to load plugin %v", err)
}
c.Plugins = append(c.Plugins, plugs...)

c.Plugins = append(c.Plugins, plugins.([]v1.Plugin)...)
i := deduplication(c.Plugins)
c.Plugins = i
}
}

return nil
}

func deduplication(plugins []v1.Plugin) []v1.Plugin {
result := make([]v1.Plugin, len(plugins))
resultIdx := 0
for i := 0; i < len(plugins); i++ {
for j := 0; j <= i; j++ {
if plugins[i].Spec.Data == plugins[j].Spec.Data && plugins[i].Spec.Type == plugins[j].Spec.Type &&
plugins[i].Spec.On == plugins[j].Spec.On && plugins[i].Spec.Action == plugins[j].Spec.Action {
if i == j {
result[resultIdx] = plugins[i]
resultIdx++
}
break
}
}
}
return result[:resultIdx]
}

// Run execute each in-tree or out-of-tree plugin by traversing the plugin list.
func (c *PluginsProcessor) Run(host []net.IP, phase Phase) error {
for _, plug := range c.Plugins {
Expand Down
7 changes: 0 additions & 7 deletions pkg/plugin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import (
"net"
"strings"

v1 "github.com/sealerio/sealer/types/api/v1"

utilsnet "github.com/sealerio/sealer/utils/net"
"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -66,8 +64,3 @@ func GetIpsByOnField(on string, context Context, phase Phase) (ipList []net.IP,
}
return ipList, nil
}

func isSamePluginSpec(p1, p2 v1.Plugin) bool {
return p1.Spec.Type == p2.Spec.Type && p1.Spec.On == p2.Spec.On &&
p1.Spec.Data == p2.Spec.Data && p1.Spec.Action == p2.Spec.Action
}

0 comments on commit af66a2c

Please sign in to comment.