open-nomad/client/fingerprint/cni.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 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"
"github.com/hashicorp/go-hclog"
2020-05-15 15:09:01 +00:00
"github.com/hashicorp/nomad/nomad/structs"
)
// CNIFingerprint creates a fingerprint of the CNI configuration(s) on the
// Nomad client.
2020-05-15 15:09:01 +00:00
type CNIFingerprint struct {
StaticFingerprinter
logger hclog.Logger
2020-05-15 15:09:01 +00:00
}
func NewCNIFingerprint(logger hclog.Logger) Fingerprint {
2020-05-15 15:09:01 +00:00
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() {}