Browse code

daemon/cluster: rename Cluster.root to Cluster.stateDir

This matches the name used by Swarm in swarmnode.Config. While updating,
also remove code from Cluster.Start that replicated the logic to construct
the path, in favor of using the `stateDir` field.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2025/02/26 23:01:37
Showing 3 changed files
... ...
@@ -113,7 +113,7 @@ type Cluster struct {
113 113
 	mu           sync.RWMutex
114 114
 	controlMutex sync.RWMutex // protect init/join/leave user operations
115 115
 	nr           *nodeRunner
116
-	root         string
116
+	stateDir     string
117 117
 	runtimeRoot  string
118 118
 	config       Config
119 119
 	configEvent  chan lncluster.ConfigEventType // todo: make this array and goroutine safe
... ...
@@ -136,12 +136,12 @@ type attacher struct {
136 136
 
137 137
 // New creates a new Cluster instance using provided config.
138 138
 func New(config Config) (*Cluster, error) {
139
-	root := filepath.Join(config.Root, swarmDirName)
140
-	if err := os.MkdirAll(root, 0o700); err != nil {
139
+	stateDir := filepath.Join(config.Root, swarmDirName)
140
+	if err := os.MkdirAll(stateDir, 0o700); err != nil {
141 141
 		return nil, err
142 142
 	}
143 143
 	if config.RuntimeRoot == "" {
144
-		config.RuntimeRoot = root
144
+		config.RuntimeRoot = stateDir
145 145
 	}
146 146
 	if config.RaftHeartbeatTick == 0 {
147 147
 		config.RaftHeartbeatTick = 1
... ...
@@ -155,7 +155,7 @@ func New(config Config) (*Cluster, error) {
155 155
 		return nil, err
156 156
 	}
157 157
 	c := &Cluster{
158
-		root:        root,
158
+		stateDir:    stateDir,
159 159
 		config:      config,
160 160
 		configEvent: make(chan lncluster.ConfigEventType, 10),
161 161
 		runtimeRoot: config.RuntimeRoot,
... ...
@@ -172,9 +172,7 @@ func New(config Config) (*Cluster, error) {
172 172
 // TODO The split between New and Start can be join again when the SendClusterEvent
173 173
 // method is no longer required
174 174
 func (c *Cluster) Start() error {
175
-	root := filepath.Join(c.config.Root, swarmDirName)
176
-
177
-	nodeConfig, err := loadPersistentState(root)
175
+	nodeConfig, err := loadPersistentState(c.stateDir)
178 176
 	if err != nil {
179 177
 		if os.IsNotExist(err) {
180 178
 			return nil
... ...
@@ -131,7 +131,7 @@ func (n *nodeRunner) start(conf nodeStartConfig) error {
131 131
 			VXLANUDPPort:    conf.DataPathPort,
132 132
 		},
133 133
 		JoinAddr:  joinAddr,
134
-		StateDir:  n.cluster.root,
134
+		StateDir:  n.cluster.stateDir,
135 135
 		JoinToken: conf.joinToken,
136 136
 		Executor: container.NewExecutor(
137 137
 			n.cluster.config.Backend,
... ...
@@ -171,7 +171,7 @@ func (n *nodeRunner) start(conf nodeStartConfig) error {
171 171
 		conf.JoinInProgress = true
172 172
 	}
173 173
 	n.config = conf
174
-	savePersistentState(n.cluster.root, conf)
174
+	savePersistentState(n.cluster.stateDir, conf)
175 175
 
176 176
 	ctx, cancel := context.WithCancel(context.Background())
177 177
 
... ...
@@ -262,7 +262,7 @@ func (n *nodeRunner) handleReadyEvent(ctx context.Context, node *swarmnode.Node,
262 262
 		n.err = nil
263 263
 		if n.config.JoinInProgress {
264 264
 			n.config.JoinInProgress = false
265
-			savePersistentState(n.cluster.root, n.config)
265
+			savePersistentState(n.cluster.stateDir, n.config)
266 266
 		}
267 267
 		n.mu.Unlock()
268 268
 		close(ready)
... ...
@@ -128,7 +128,7 @@ func (c *Cluster) Init(req types.InitRequest) (string, error) {
128 128
 		c.nr = nil
129 129
 		c.mu.Unlock()
130 130
 		if !req.ForceNewCluster { // if failure on first attempt don't keep state
131
-			if err := clearPersistentState(c.root); err != nil {
131
+			if err := clearPersistentState(c.stateDir); err != nil {
132 132
 				return "", err
133 133
 			}
134 134
 		}
... ...
@@ -207,7 +207,7 @@ func (c *Cluster) Join(req types.JoinRequest) error {
207 207
 			c.mu.Lock()
208 208
 			c.nr = nil
209 209
 			c.mu.Unlock()
210
-			if err := clearPersistentState(c.root); err != nil {
210
+			if err := clearPersistentState(c.stateDir); err != nil {
211 211
 				return err
212 212
 			}
213 213
 		}
... ...
@@ -421,7 +421,7 @@ func (c *Cluster) Leave(ctx context.Context, force bool) error {
421 421
 	}
422 422
 
423 423
 	// todo: cleanup optional?
424
-	if err := clearPersistentState(c.root); err != nil {
424
+	if err := clearPersistentState(c.stateDir); err != nil {
425 425
 		return err
426 426
 	}
427 427
 	c.config.Backend.DaemonLeavesCluster()