Skip to content
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

[FIXED] LeafNode: Don't report cluster name in varz/banner when none defined #5931

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions server/leafnode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7968,3 +7968,38 @@ func TestLeafNodeServerKickClient(t *testing.T) {
require_True(t, lid != ln.Cid)
require_True(t, ln.Start.After(disconnectTime))
}

func TestLeafNodeBannerNoClusterNameIfNoCluster(t *testing.T) {
u, err := url.Parse("nats://127.0.0.1:1234")
require_NoError(t, err)

opts := DefaultOptions()
opts.ServerName = "LEAF_SERVER"
opts.Cluster.Name = _EMPTY_
opts.Cluster.Port = 0
opts.LeafNode.Remotes = []*RemoteLeafOpts{{URLs: []*url.URL{u}}}
opts.NoLog = false

s, err := NewServer(opts)
require_NoError(t, err)
defer func() {
s.Shutdown()
s.WaitForShutdown()
}()
l := &captureNoticeLogger{}
s.SetLogger(l, false, false)
s.Start()

if !s.ReadyForConnections(time.Second) {
t.Fatal("Server not ready!")
}

l.Lock()
for _, n := range l.notices {
if strings.Contains(n, "Cluster: ") {
l.Unlock()
t.Fatalf("Cluster name should not be displayed, got %q", n)
}
}
l.Unlock()
}
5 changes: 5 additions & 0 deletions server/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1592,6 +1592,11 @@ func (s *Server) createVarz(pcpu float64, rss int64) *Varz {
TrustedOperatorsJwt: opts.operatorJWT,
TrustedOperatorsClaim: opts.TrustedOperators,
}
// If this is a leaf without cluster, reset the cluster name (that is otherwise
// set to the server name).
if s.leafNoCluster {
varz.Cluster.Name = _EMPTY_
}
if len(opts.Routes) > 0 {
varz.Cluster.URLs = urlsToStrings(opts.Routes)
}
Expand Down
8 changes: 8 additions & 0 deletions server/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3138,6 +3138,14 @@ func TestMonitorLeafNode(t *testing.T) {
for mode := 0; mode < 2; mode++ {
check := func(t *testing.T, v *Varz) {
t.Helper()
// Issue 5913. When we have solicited leafnodes but no clustering
// and no clustername, we may need a stable clustername so we use
// the server name as cluster name. However, we should not expose
// it in /varz.
if v.Cluster.Name != _EMPTY_ {
t.Fatalf("mode=%v - unexpected cluster name: %s", mode, v.Cluster.Name)
}
// Check rest is as expected.
if !reflect.DeepEqual(v.LeafNode, expected) {
t.Fatalf("mode=%v - expected %+v, got %+v", mode, expected, v.LeafNode)
}
Expand Down
14 changes: 13 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ type Server struct {
leafRemoteAccounts sync.Map
leafNodeEnabled bool
leafDisableConnect bool // Used in test only
leafNoCluster bool // Indicate that this server has only remotes and no cluster defined

quitCh chan struct{}
startupComplete chan struct{}
Expand Down Expand Up @@ -750,6 +751,7 @@ func NewServer(opts *Options) (*Server, error) {
// If we have solicited leafnodes but no clustering and no clustername.
// However we may need a stable clustername so use the server name.
if len(opts.LeafNode.Remotes) > 0 && opts.Cluster.Port == 0 && opts.Cluster.Name == _EMPTY_ {
s.leafNoCluster = true
opts.Cluster.Name = opts.ServerName
}

Expand Down Expand Up @@ -2178,7 +2180,17 @@ func (s *Server) Start() {

// Snapshot server options.
opts := s.getOpts()
clusterName := s.ClusterName()

// Capture if this server is a leaf that has no cluster, so we don't
// display the cluster name if that is the case.
s.mu.RLock()
leafNoCluster := s.leafNoCluster
s.mu.RUnlock()

var clusterName string
if !leafNoCluster {
clusterName = s.ClusterName()
}

s.Noticef(" Version: %s", VERSION)
s.Noticef(" Git: [%s]", gc)
Expand Down