-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add cluster join command line options and configuration options #527
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0d0fe0a
Add cluster join command line options and configuration options
ChrisAubuchon 63bdb79
Set defaults for `retry_max` and `retry_interval` options
ChrisAubuchon 2fbafc1
Add new configuration options to docs
ChrisAubuchon b2d5741
Fix links to configuration options
ChrisAubuchon c971f7b
Fix broken link to `start_join`
ChrisAubuchon 53b23cd
`30 * time.Second` -> `"30s"` in DefaultConfig
ChrisAubuchon fd3765f
Restore AtlasConfig
ChrisAubuchon 07563b9
Move retryJoin error channel to Command structure
ChrisAubuchon 5336df6
Indicate which configuration option failed to parse properly
ChrisAubuchon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,11 @@ func (c *Command) readConfig() *Config { | |
|
||
// Server-only options | ||
flags.IntVar(&cmdConfig.Server.BootstrapExpect, "bootstrap-expect", 0, "") | ||
flags.BoolVar(&cmdConfig.Server.RejoinAfterLeave, "rejoin", false, "") | ||
flags.Var((*sliceflag.StringFlag)(&cmdConfig.Server.StartJoin), "join", "") | ||
flags.Var((*sliceflag.StringFlag)(&cmdConfig.Server.RetryJoin), "retry-join", "") | ||
flags.IntVar(&cmdConfig.Server.RetryMaxAttempts, "retry-max", 0, "") | ||
flags.StringVar(&cmdConfig.Server.RetryInterval, "retry-interval", "", "") | ||
|
||
// Client-only options | ||
flags.StringVar(&cmdConfig.Client.StateDir, "state-dir", "", "") | ||
|
@@ -100,6 +105,15 @@ func (c *Command) readConfig() *Config { | |
return nil | ||
} | ||
|
||
if cmdConfig.Server.RetryInterval != "" { | ||
dur, err := time.ParseDuration(cmdConfig.Server.RetryInterval) | ||
if err != nil { | ||
c.Ui.Error(fmt.Sprintf("Error: %s", err)) | ||
return nil | ||
} | ||
cmdConfig.Server.retryInterval = dur | ||
} | ||
|
||
// Split the servers. | ||
if servers != "" { | ||
cmdConfig.Client.Servers = strings.Split(servers, ",") | ||
|
@@ -358,6 +372,12 @@ func (c *Command) Run(args []string) int { | |
} | ||
}() | ||
|
||
// Join startup nodes if specified | ||
if err := c.startupJoin(config); err != nil { | ||
c.Ui.Error(err.Error()) | ||
return 1 | ||
} | ||
|
||
// Compile agent information for output later | ||
info := make(map[string]string) | ||
info["client"] = strconv.FormatBool(config.Client.Enabled) | ||
|
@@ -396,12 +416,16 @@ func (c *Command) Run(args []string) int { | |
// Enable log streaming | ||
logGate.Flush() | ||
|
||
// Start retry join process | ||
errCh := make(chan struct{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add the retryJoinErr channel to the Command struct so we don't have to pass the channel into the functions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added to the Command struct |
||
go c.retryJoin(config, errCh) | ||
|
||
// Wait for exit | ||
return c.handleSignals(config) | ||
return c.handleSignals(config, errCh) | ||
} | ||
|
||
// handleSignals blocks until we get an exit-causing signal | ||
func (c *Command) handleSignals(config *Config) int { | ||
func (c *Command) handleSignals(config *Config, retryJoin <-chan struct{}) int { | ||
signalCh := make(chan os.Signal, 4) | ||
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP) | ||
|
||
|
@@ -413,6 +437,8 @@ WAIT: | |
sig = s | ||
case <-c.ShutdownCh: | ||
sig = os.Interrupt | ||
case <-retryJoin: | ||
return 1 | ||
} | ||
c.Ui.Output(fmt.Sprintf("Caught signal: %v", sig)) | ||
|
||
|
@@ -559,6 +585,52 @@ func (c *Command) setupSCADA(config *Config) error { | |
return nil | ||
} | ||
|
||
func (c *Command) startupJoin(config *Config) error { | ||
if len(config.Server.StartJoin) == 0 || !config.Server.Enabled { | ||
return nil | ||
} | ||
|
||
c.Ui.Output("Joining cluster...") | ||
n, err := c.agent.server.Join(config.Server.StartJoin) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.Ui.Info(fmt.Sprintf("Join completed. Synced with %d initial agents", n)) | ||
return nil | ||
} | ||
|
||
// retryJoin is used to handle retrying a join until it succeeds or all retries | ||
// are exhausted. | ||
func (c *Command) retryJoin(config *Config, errCh chan<- struct{}) { | ||
if len(config.Server.RetryJoin) == 0 || !config.Server.Enabled { | ||
return | ||
} | ||
|
||
logger := c.agent.logger | ||
logger.Printf("[INFO] agent: Joining cluster...") | ||
|
||
attempt := 0 | ||
for { | ||
n, err := c.agent.server.Join(config.Server.RetryJoin) | ||
if err == nil { | ||
logger.Printf("[INFO] agent: Join completed. Synced with %d initial agents", n) | ||
return | ||
} | ||
|
||
attempt++ | ||
if config.Server.RetryMaxAttempts > 0 && attempt > config.Server.RetryMaxAttempts { | ||
logger.Printf("[ERROR] agent: max join retry exhausted, exiting") | ||
close(errCh) | ||
return | ||
} | ||
|
||
logger.Printf("[WARN] agent: Join failed: %v, retrying in %v", err, | ||
config.Server.RetryInterval) | ||
time.Sleep(config.Server.retryInterval) | ||
} | ||
} | ||
|
||
func (c *Command) Synopsis() string { | ||
return "Runs a Nomad agent" | ||
} | ||
|
@@ -632,6 +704,24 @@ Server Options: | |
bootstrapping the cluster. Once <num> servers have joined eachother, | ||
Nomad initiates the bootstrap process. | ||
|
||
-join=<address> | ||
Address of an agent to join at start time. Can be specified | ||
multiple times. | ||
|
||
-retry-join=<address> | ||
Address of an agent to join at start time with retries enabled. | ||
Can be specified multiple times. | ||
|
||
-retry-max=<num> | ||
Maximum number of join attempts. Defaults to 0, which will retry | ||
indefinitely. | ||
|
||
-retry-interval=<dur> | ||
Time to wait between join attempts. | ||
|
||
-rejoin | ||
Ignore a previous leave and attempts to rejoin the cluster. | ||
|
||
Client Options: | ||
|
||
-client | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add some extra information in the error message to indicate which field Nomad was not about to parse? I think that would help the user to quickly debug what's wrong in the configuration file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the error message to
Error parsing retry interval