-
Notifications
You must be signed in to change notification settings - Fork 0
/
cni_loader.go
50 lines (39 loc) · 953 Bytes
/
cni_loader.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
package cni
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/containernetworking/cni/libcni"
)
type CNILoader struct {
PluginDir string
ConfigDir string
}
func (l *CNILoader) GetCNIConfig() *libcni.CNIConfig {
return &libcni.CNIConfig{Path: []string{l.PluginDir}}
}
func (l *CNILoader) GetNetworkConfigs() ([]*libcni.NetworkConfig, error) {
networkConfigs := []*libcni.NetworkConfig{}
err := filepath.Walk(l.ConfigDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(path, ".conf") {
return nil
}
conf, err := libcni.ConfFromFile(path)
if err != nil {
return fmt.Errorf("unable to load config from %s: %s", path, err)
}
networkConfigs = append(networkConfigs, conf)
return nil
})
if err != nil {
return networkConfigs, fmt.Errorf("error loading config: %s", err)
}
return networkConfigs, nil
}