Use preconfigured nodeID if there isn't a persisted node ID, and persist it if its not persisted.

This commit is contained in:
Preetha Appan 2018-04-10 08:47:33 -05:00
parent 216c053742
commit 6d0e1c9fea
No known key found for this signature in database
GPG Key ID: 9F7C19990A50EAFC
4 changed files with 28 additions and 24 deletions

View File

@ -533,26 +533,10 @@ func (a *Agent) setupServer() error {
// setupNodeID will pull the persisted node ID, if any, or create a random one
// and persist it.
func (a *Agent) setupNodeID(config *nomad.Config) error {
// If they've configured a node ID manually then just use that, as
// long as it's valid.
if config.NodeID != "" {
config.NodeID = strings.ToLower(string(config.NodeID))
if _, err := uuidparse.ParseUUID(string(config.NodeID)); err != nil {
return err
}
return nil
}
// For dev mode we have no filesystem access so just make one.
if a.config.DevMode {
config.NodeID = uuid.Generate()
return nil
}
// Load saved state, if any. Since a user could edit this, we also
// validate it.
// validate it. Saved state overwrites any configured node id
fileID := filepath.Join(config.DataDir, "node-id")
savedNodeID := false
if _, err := os.Stat(fileID); err == nil {
rawID, err := ioutil.ReadFile(fileID)
if err != nil {
@ -564,8 +548,31 @@ func (a *Agent) setupNodeID(config *nomad.Config) error {
if _, err := uuidparse.ParseUUID(nodeID); err != nil {
return err
}
config.NodeID = nodeID
savedNodeID = true
}
// If they've configured a node ID manually then just use that, as
// long as it's valid.
if !savedNodeID && config.NodeID != "" {
config.NodeID = strings.ToLower(string(config.NodeID))
if _, err := uuidparse.ParseUUID(string(config.NodeID)); err != nil {
return err
}
// Persist this configured nodeID to our data directory
if err := lib.EnsurePath(fileID, false); err != nil {
return err
}
if err := ioutil.WriteFile(fileID, []byte(config.NodeID), 0600); err != nil {
return err
}
return nil
}
// For dev mode we have no filesystem access so just make one.
if a.config.DevMode {
config.NodeID = uuid.Generate()
return nil
}
// If we still don't have a valid node ID, make one.

View File

@ -10,6 +10,7 @@ import (
"github.com/hashicorp/memberlist"
"github.com/hashicorp/nomad/helper/tlsutil"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/hashicorp/nomad/nomad/structs/config"
"github.com/hashicorp/nomad/scheduler"
@ -311,13 +312,12 @@ func DefaultConfig() *Config {
panic(err)
}
// TODO Add back random node ID and always persist in the agent startup
// TODO Remove places I added uuid.Generate()
c := &Config{
Region: DefaultRegion,
AuthoritativeRegion: DefaultRegion,
Datacenter: DefaultDC,
NodeName: hostname,
NodeID: uuid.Generate(),
ProtocolVersion: ProtocolVersionMax,
RaftConfig: raft.DefaultConfig(),
RaftTimeout: 10 * time.Second,

View File

@ -532,7 +532,6 @@ func TestServer_InvalidSchedulers(t *testing.T) {
require := require.New(t)
config := DefaultConfig()
config.NodeID = uuid.Generate()
config.DevMode = true
config.LogOutput = testlog.NewWriter(t)
config.SerfConfig.MemberlistConfig.BindAddr = "127.0.0.1"

View File

@ -11,7 +11,6 @@ import (
"github.com/hashicorp/consul/lib/freeport"
"github.com/hashicorp/nomad/command/agent/consul"
"github.com/hashicorp/nomad/helper/testlog"
"github.com/hashicorp/nomad/helper/uuid"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/mitchellh/go-testing-interface"
@ -39,7 +38,6 @@ func TestACLServer(t testing.T, cb func(*Config)) (*Server, *structs.ACLToken) {
func TestServer(t testing.T, cb func(*Config)) *Server {
// Setup the default settings
config := DefaultConfig()
config.NodeID = uuid.Generate()
config.Build = "0.8.0+unittest"
config.DevMode = true
nodeNum := atomic.AddUint32(&nodeNumber, 1)