Skip to content

Commit 48c2c5f

Browse files
committed
fix plugin-config
1 parent 35ef183 commit 48c2c5f

File tree

5 files changed

+105
-17
lines changed

5 files changed

+105
-17
lines changed

cloud/scheduler/plugins/registry.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import (
1515
)
1616

1717
type PluginConfigs struct {
18-
filterPlugins map[string]pluginConfig `yaml:"filters"`
19-
scorePlugins map[string]pluginConfig `yaml:"scores"`
20-
vmidPlugins map[string]pluginConfig `yaml:"vmids"`
18+
FilterPlugins map[string]PluginConfig `yaml:"filters,omitempty"`
19+
ScorePlugins map[string]PluginConfig `yaml:"scores,omitempty"`
20+
VMIDPlugins map[string]PluginConfig `yaml:"vmids,omitempty"`
2121
}
2222

23-
type pluginConfig struct {
23+
type PluginConfig struct {
2424
Enable bool `yaml:"enable,omitempty"`
2525
Config map[string]interface{} `yaml:"config,omitempty"`
2626
}
@@ -45,14 +45,14 @@ func (r *PluginRegistry) VMIDPlugins() []framework.VMIDPlugin {
4545

4646
func NewRegistry(configs PluginConfigs) PluginRegistry {
4747
r := PluginRegistry{
48-
filterPlugins: NewNodeFilterPlugins(configs.filterPlugins),
49-
scorePlugins: NewNodeScorePlugins(configs.scorePlugins),
50-
vmidPlugins: NewVMIDPlugins(configs.vmidPlugins),
48+
filterPlugins: NewNodeFilterPlugins(configs.FilterPlugins),
49+
scorePlugins: NewNodeScorePlugins(configs.ScorePlugins),
50+
vmidPlugins: NewVMIDPlugins(configs.VMIDPlugins),
5151
}
5252
return r
5353
}
5454

55-
func NewNodeFilterPlugins(config map[string]pluginConfig) []framework.NodeFilterPlugin {
55+
func NewNodeFilterPlugins(config map[string]PluginConfig) []framework.NodeFilterPlugin {
5656
pls := []framework.NodeFilterPlugin{
5757
&nodename.NodeName{},
5858
&overcommit.CPUOvercommit{},
@@ -70,7 +70,7 @@ func NewNodeFilterPlugins(config map[string]pluginConfig) []framework.NodeFilter
7070
return plugins
7171
}
7272

73-
func NewNodeScorePlugins(config map[string]pluginConfig) []framework.NodeScorePlugin {
73+
func NewNodeScorePlugins(config map[string]PluginConfig) []framework.NodeScorePlugin {
7474
pls := []framework.NodeScorePlugin{
7575
&random.Random{},
7676
&noderesource.NodeResource{},
@@ -86,7 +86,7 @@ func NewNodeScorePlugins(config map[string]pluginConfig) []framework.NodeScorePl
8686
return plugins
8787
}
8888

89-
func NewVMIDPlugins(config map[string]pluginConfig) []framework.VMIDPlugin {
89+
func NewVMIDPlugins(config map[string]PluginConfig) []framework.VMIDPlugin {
9090
pls := []framework.VMIDPlugin{
9191
&idrange.Range{},
9292
&regex.Regex{},
@@ -104,7 +104,7 @@ func NewVMIDPlugins(config map[string]pluginConfig) []framework.VMIDPlugin {
104104

105105
// Read config file and unmarshal it to PluginConfig type
106106
func GetPluginConfigFromFile(path string) (PluginConfigs, error) {
107-
config := PluginConfigs{}
107+
var config PluginConfigs
108108
if path == "" {
109109
return config, nil
110110
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package plugins_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
10+
"github.com/sp-yduck/cluster-api-provider-proxmox/cloud/scheduler/plugins"
11+
)
12+
13+
func TestPlugins(t *testing.T) {
14+
RegisterFailHandler(Fail)
15+
RunSpecs(t, "Plugins Suite")
16+
}
17+
18+
var _ = Describe("GetPluginConfigFromFile", Label("unit", "scheduler"), func() {
19+
path := "./test-plugin-config.yaml"
20+
BeforeEach(func() {
21+
content := `scores:
22+
Random:
23+
enable: false`
24+
err := stringToFile(content, path)
25+
Expect(err).ToNot(HaveOccurred())
26+
})
27+
28+
AfterEach(func() {
29+
err := rm(path)
30+
Expect(err).NotTo(HaveOccurred())
31+
})
32+
33+
Context("with empty file path", func() {
34+
path := ""
35+
It("should not error", func() {
36+
config, err := plugins.GetPluginConfigFromFile(path)
37+
Expect(err).NotTo(HaveOccurred())
38+
Expect(config).To(Equal(plugins.PluginConfigs{}))
39+
})
40+
})
41+
42+
Context("with non-empty file path", func() {
43+
It("should not error", func() {
44+
config, err := plugins.GetPluginConfigFromFile(path)
45+
Expect(err).NotTo(HaveOccurred())
46+
scores := map[string]plugins.PluginConfig{}
47+
scores["Random"] = plugins.PluginConfig{Enable: false}
48+
Expect(config).To(Equal(plugins.PluginConfigs{ScorePlugins: scores}))
49+
})
50+
})
51+
52+
Context("with wrong file path", func() {
53+
It("shold error", func() {
54+
path := "./wrong-plugin-config.yaml"
55+
config, err := plugins.GetPluginConfigFromFile(path)
56+
Expect(err).To(HaveOccurred())
57+
Expect(config).To(Equal(plugins.PluginConfigs{}))
58+
})
59+
})
60+
})
61+
62+
func stringToFile(str string, path string) error {
63+
b := []byte(str)
64+
return os.WriteFile(path, b, 0666)
65+
}
66+
67+
func rm(path string) error {
68+
return os.Remove(path)
69+
}

cloud/scheduler/scheduler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func NewManager(params SchedulerParams) (*Manager, error) {
4242
return nil, fmt.Errorf("failed to read plugin config: %v", err)
4343
}
4444
params.pluginconfigs = config
45+
params.Logger.Info(fmt.Sprintf("load plugin config: %v", config))
4546
return &Manager{ctx: context.Background(), params: params, table: table}, nil
4647
}
4748

cloud/scheduler/scheduler_test.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,29 @@ import (
1414
)
1515

1616
var _ = Describe("NewManager", Label("unit", "scheduler"), func() {
17-
It("should not error", func() {
18-
params := scheduler.SchedulerParams{}
19-
manager, err := scheduler.NewManager(params)
20-
Expect(err).NotTo(HaveOccurred())
21-
Expect(manager).NotTo(BeNil())
17+
Context("with empty params", func() {
18+
It("should not error", func() {
19+
params := scheduler.SchedulerParams{}
20+
manager, err := scheduler.NewManager(params)
21+
Expect(err).NotTo(HaveOccurred())
22+
Expect(manager).NotTo(BeNil())
23+
})
24+
})
25+
Context("with only logger", func() {
26+
It("should not error", func() {
27+
params := scheduler.SchedulerParams{Logger: zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))}
28+
manager, err := scheduler.NewManager(params)
29+
Expect(err).NotTo(HaveOccurred())
30+
Expect(manager).NotTo(BeNil())
31+
})
32+
})
33+
Context("with plugin-config", func() {
34+
It("should not error", func() {
35+
params := scheduler.SchedulerParams{PluginConfigFile: ""}
36+
manager, err := scheduler.NewManager(params)
37+
Expect(err).NotTo(HaveOccurred())
38+
Expect(manager).NotTo(BeNil())
39+
})
2240
})
2341
})
2442

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func main() {
6262
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
6363
"Enable leader election for controller manager. "+
6464
"Enabling this will ensure there is only one active controller manager.")
65-
flag.StringVar(&pluginConfig, "scheduler-plugin-config", "/etc/qemu-scheduler/plugin-config.yaml", "The config file path for qemu-scheduler plugins")
65+
flag.StringVar(&pluginConfig, "scheduler-plugin-config", "", "The config file path for qemu-scheduler plugins")
6666
opts := zap.Options{
6767
Development: true,
6868
}

0 commit comments

Comments
 (0)