diff --git a/agent/agent.go b/agent/agent.go index c5fb3cc886..58367c856f 100644 --- a/agent/agent.go +++ b/agent/agent.go @@ -4708,6 +4708,11 @@ func (a *Agent) checkServerLastSeen(readFn consul.ServerMetadataReadFunc) error return fmt.Errorf("error reading server metadata: %w", err) } + // Return early if the file exists, but content is empty. + if md == nil { + return nil + } + maxAge := a.config.ServerRejoinAgeMax if md.IsLastSeenStale(maxAge) { return fmt.Errorf("refusing to rejoin cluster because server has been offline for more than the configured server_rejoin_age_max (%s) - consider wiping your data dir", maxAge) diff --git a/agent/agent_test.go b/agent/agent_test.go index 316adeb3dd..e932200dce 100644 --- a/agent/agent_test.go +++ b/agent/agent_test.go @@ -6412,6 +6412,15 @@ func TestAgent_checkServerLastSeen(t *testing.T) { require.ErrorContains(t, err, "refusing to rejoin cluster because server has been offline for more than the configured server_rejoin_age_max") }) + t.Run("TestIsLastSeenEmptyFile", func(t *testing.T) { + readFn := func(filename string) (*consul.ServerMetadata, error) { + return nil, nil + } + + err := agent.checkServerLastSeen(readFn) + require.NoError(t, err) + }) + // Test that a server with a 6h old last seen timestamp is not treated as an error. t.Run("TestNoErr", func(t *testing.T) { agent.config.ServerRejoinAgeMax = 24 * 7 * time.Hour diff --git a/agent/consul/server_metadata.go b/agent/consul/server_metadata.go index b9a4eaf2eb..dc3bade937 100644 --- a/agent/consul/server_metadata.go +++ b/agent/consul/server_metadata.go @@ -46,6 +46,10 @@ func ReadServerMetadata(filename string) (*ServerMetadata, error) { return nil, err } + if len(b) == 0 { + return nil, nil + } + var md ServerMetadata if err := json.Unmarshal(b, &md); err != nil { return nil, err