Skip to content

Commit

Permalink
server: add node id prefix flag
Browse files Browse the repository at this point in the history
  • Loading branch information
andydunstall committed May 1, 2024
1 parent 5a7015e commit aeed802
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
6 changes: 5 additions & 1 deletion cli/server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ default value can be given using form ${VAR:default}.`,
}

if conf.Cluster.NodeID == "" {
conf.Cluster.NodeID = netmap.GenerateNodeID()
nodeID := netmap.GenerateNodeID()
if conf.Cluster.NodeIDPrefix != "" {
nodeID = conf.Cluster.NodeIDPrefix + nodeID
}
conf.Cluster.NodeID = nodeID
}

if conf.Proxy.AdvertiseAddr == "" {
Expand Down
18 changes: 18 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ type ClusterConfig struct {
// NodeID is a unique identifier for this node in the cluster.
NodeID string `json:"node_id" yaml:"node_id"`

// NodeIDPrefix is a node ID prefix, where Pico will generate the rest of
// the node ID to ensure uniqueness.
NodeIDPrefix string `json:"node_id_prefix" yaml:"node_id_prefix"`

// Join contians a list of addresses of members in the cluster to join.
Join []string `json:"join" yaml:"join"`
}
Expand Down Expand Up @@ -249,6 +253,20 @@ A unique identifier for the node in the cluster.
By default a random ID will be generated for the node.`,
)
fs.StringVar(
&c.Cluster.NodeIDPrefix,
"cluster.node-id-prefix",
"",
`
A prefix for the node ID.
Pico will generate a unique random identifier for the node and append it to
the given prefix.
Such as you could use the node or pod name as a prefix, then add a unique
identifier to ensure the node ID is unique across restarts.`,
)

fs.StringSliceVar(
&c.Cluster.Join,
"cluster.join",
Expand Down
2 changes: 1 addition & 1 deletion server/netmap/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (n *Node) Copy() *Node {
}

func GenerateNodeID() string {
b := make([]byte, 10)
b := make([]byte, 6)
for i := range b {
n, err := rand.Int(rand.Reader, big.NewInt(int64(len(alphaNumericChars))))
if err != nil {
Expand Down

0 comments on commit aeed802

Please sign in to comment.