-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
51 lines (42 loc) · 1.43 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"fmt"
"os"
"github.com/go-logr/logr"
"gopkg.in/yaml.v3"
)
type Config struct {
ResourceName string `yaml:"resourceName"`
NumberDevicesOnNode int `yaml:"numberDevicesOnNode"`
PluginName string `yaml:"pluginName"`
DeviceIDPrefix string `yaml:"deviceIDPrefix"`
AnnotationPrefix string `yaml:"annotationPrefix,omitempty"`
EnvPrefix string `yaml:"envPrefix,omitempty"`
DeviceFilePrefix string `yaml:"deviceFilePrefix,omitempty"`
deviceIDs []string
}
func preparePluginConfiguration(path string, cfg *Config, logger logr.Logger) error {
fd, err := os.Open(path)
if err != nil {
return fmt.Errorf("could not open the configuration file: %v", err)
}
defer fd.Close()
if err = yaml.NewDecoder(fd).Decode(cfg); err != nil {
return fmt.Errorf("could not decode configuration file: %v", err)
}
setDeviceIDs(cfg)
logger.Info("Plugin configuration:", "ResourceName", cfg.ResourceName,
"NumberDevicesOnNode", cfg.NumberDevicesOnNode,
"PluginName", cfg.PluginName,
"DeviceIDPrefix", cfg.DeviceIDPrefix,
"EnvPrefix", cfg.EnvPrefix,
"DeviceFilePrefix", cfg.DeviceFilePrefix)
return nil
}
func setDeviceIDs(config *Config) {
nodeName := os.Getenv("NODE_NAME")
config.deviceIDs = make([]string, config.NumberDevicesOnNode)
for i := 0; i < config.NumberDevicesOnNode; i++ {
config.deviceIDs[i] = fmt.Sprintf("%s-%s-%d", config.DeviceIDPrefix, nodeName, i)
}
}