2020-05-15 15:09:01 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/containernetworking/cni/libcni"
|
2022-12-05 20:22:47 +00:00
|
|
|
"github.com/hashicorp/go-hclog"
|
2020-05-15 15:09:01 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2022-12-05 20:22:47 +00:00
|
|
|
// 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
|
2022-12-05 20:22:47 +00:00
|
|
|
logger hclog.Logger
|
2020-05-15 15:09:01 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 20:22:47 +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() {}
|