open-nomad/client/fingerprint/cni.go

84 lines
2.3 KiB
Go
Raw Normal View History

2020-05-15 15:09:01 +00:00
package fingerprint
import (
"fmt"
"os"
"strings"
"github.com/containernetworking/cni/libcni"
log "github.com/hashicorp/go-hclog"
"github.com/hashicorp/nomad/nomad/structs"
)
type CNIFingerprint struct {
StaticFingerprinter
logger log.Logger
}
func NewCNIFingerprint(logger log.Logger) Fingerprint {
return &CNIFingerprint{logger: logger}
}
func (f *CNIFingerprint) Fingerprint(req *FingerprintRequest, resp *FingerprintResponse) error {
confDir := req.Config.CNIConfigDir
networks := map[string]struct{}{}
if _, err := os.Stat(confDir); os.IsNotExist(err) {
f.logger.Debug("CNI config dir is not set or does not exist, skipping", "cni_config_dir", confDir)
resp.Detected = false
return nil
}
files, err := libcni.ConfFiles(confDir, []string{".conf", ".conflist", ".json"})
if err != nil {
return fmt.Errorf("failed to detect CNI conf files: %v", err)
}
for _, confFile := range files {
if strings.HasSuffix(confFile, ".conflist") {
confList, err := libcni.ConfListFromFile(confFile)
if err != nil {
return fmt.Errorf("failed to load CNI config list file %s: %v", confFile, err)
}
if _, ok := networks[confList.Name]; ok {
f.logger.Warn("duplicate CNI config names found, ignoring file", "name", confList.Name, "file", confFile)
continue
}
networks[confList.Name] = struct{}{}
} else {
conf, err := libcni.ConfFromFile(confFile)
if err != nil {
return fmt.Errorf("failed to load CNI config file %s: %v", confFile, err)
}
if _, ok := networks[conf.Network.Name]; ok {
f.logger.Warn("duplicate CNI config names found, ignoring file", "name", conf.Network.Name, "file", confFile)
continue
}
networks[conf.Network.Name] = struct{}{}
}
}
var nodeNetworks structs.Networks
2020-06-16 15:53:10 +00:00
var newNodeNetworks []*structs.NodeNetworkResource
2020-05-15 15:09:01 +00:00
for name := range networks {
2020-06-16 15:53:10 +00:00
mode := fmt.Sprintf("cni/%s", name)
2020-05-15 15:09:01 +00:00
nodeNetworks = append(nodeNetworks, &structs.NetworkResource{
2020-06-16 15:53:10 +00:00
Mode: mode,
})
newNodeNetworks = append(newNodeNetworks, &structs.NodeNetworkResource{
Mode: mode,
2020-05-15 15:09:01 +00:00
})
f.logger.Debug("detected CNI network", "name", name)
}
resp.NodeResources = &structs.NodeResources{
2020-06-16 15:53:10 +00:00
Networks: nodeNetworks,
NodeNetworks: newNodeNetworks,
2020-05-15 15:09:01 +00:00
}
resp.Detected = true
return nil
}
func (f *CNIFingerprint) Reload() {}