client: scan for drivers
This commit is contained in:
parent
a561fbb9a9
commit
c5553017be
|
@ -10,6 +10,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/nomad/client/driver"
|
||||
"github.com/hashicorp/nomad/client/fingerprint"
|
||||
"github.com/hashicorp/nomad/nomad"
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
|
@ -99,6 +100,11 @@ func NewClient(config *Config) (*Client, error) {
|
|||
if err := c.fingerprint(); err != nil {
|
||||
return nil, fmt.Errorf("fingerprinting failed: %v", err)
|
||||
}
|
||||
|
||||
// Scan for drivers
|
||||
if err := c.setupDrivers(); err != nil {
|
||||
return nil, fmt.Errorf("driver setup failed: %v", err)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
|
@ -239,3 +245,23 @@ func (c *Client) fingerprint() error {
|
|||
c.logger.Printf("[DEBUG] client: applied fingerprints %v", applied)
|
||||
return nil
|
||||
}
|
||||
|
||||
// setupDrivers is used to find the available drivers
|
||||
func (c *Client) setupDrivers() error {
|
||||
var avail []string
|
||||
for name := range driver.BuiltinDrivers {
|
||||
d, err := driver.NewDriver(name, c.logger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
applies, err := d.Fingerprint(c.config.Node)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if applies {
|
||||
avail = append(avail, name)
|
||||
}
|
||||
}
|
||||
c.logger.Printf("[DEBUG] client: available drivers %v", avail)
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -126,3 +126,14 @@ func TestClient_Fingerprint(t *testing.T) {
|
|||
t.Fatalf("missing arch")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_Drivers(t *testing.T) {
|
||||
c := testClient(t, nil)
|
||||
defer c.Shutdown()
|
||||
|
||||
// Ensure os and arch are always present
|
||||
node := c.Node()
|
||||
if node.Attributes["driver.exec"] == "" {
|
||||
t.Fatalf("missing exec driver")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,8 @@ func NewExecDriver(logger *log.Logger) Driver {
|
|||
return d
|
||||
}
|
||||
|
||||
func (d *ExecDriver) Fingerprint(*structs.Node) (bool, error) {
|
||||
func (d *ExecDriver) Fingerprint(node *structs.Node) (bool, error) {
|
||||
// We can always do a fork/exec
|
||||
node.Attributes["driver.exec"] = "1"
|
||||
return true, nil
|
||||
}
|
||||
|
|
|
@ -4,6 +4,8 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/nomad/nomad/structs"
|
||||
)
|
||||
|
||||
func testLogger() *log.Logger {
|
||||
|
@ -12,11 +14,17 @@ func testLogger() *log.Logger {
|
|||
|
||||
func TestExecDriver_Fingerprint(t *testing.T) {
|
||||
d := NewExecDriver(testLogger())
|
||||
apply, err := d.Fingerprint(nil)
|
||||
node := &structs.Node{
|
||||
Attributes: make(map[string]string),
|
||||
}
|
||||
apply, err := d.Fingerprint(node)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
if !apply {
|
||||
t.Fatalf("should apply")
|
||||
}
|
||||
if node.Attributes["driver.exec"] == "" {
|
||||
t.Fatalf("missing driver")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue