From 4ee8c76d266c97e25996ec6621cf7582e595afa2 Mon Sep 17 00:00:00 2001
From: Pierre Souchay
Date: Wed, 17 Feb 2021 19:05:44 +0100
Subject: [PATCH] [Streaming] Set KnownLeader by default when streaming is
activated
This is not a real fix but will avoid breaking clients relying on this header
Fix https://github.com/hashicorp/consul/issues/9776
---
.changelog/9778.txt | 3 +++
agent/cache-types/streaming_health_services.go | 10 ++++++++--
agent/cache-types/streaming_health_services_test.go | 3 ++-
3 files changed, 13 insertions(+), 3 deletions(-)
create mode 100644 .changelog/9778.txt
diff --git a/.changelog/9778.txt b/.changelog/9778.txt
new file mode 100644
index 000000000000..29435f1c2b7c
--- /dev/null
+++ b/.changelog/9778.txt
@@ -0,0 +1,3 @@
+```release-note:bug
+streaming: return X-Consul-KnownLeader properly set when using streaming
+```
diff --git a/agent/cache-types/streaming_health_services.go b/agent/cache-types/streaming_health_services.go
index d150a43f48e3..c5bfe03e874c 100644
--- a/agent/cache-types/streaming_health_services.go
+++ b/agent/cache-types/streaming_health_services.go
@@ -156,12 +156,14 @@ func newHealthView(filterExpr string) (*healthView, error) {
// (IndexedCheckServiceNodes) and update it in place for each event - that
// involves re-sorting each time etc. though.
type healthView struct {
- state map[string]structs.CheckServiceNode
- filter filterEvaluator
+ state map[string]structs.CheckServiceNode
+ filter filterEvaluator
+ knownLeader bool
}
// Update implements View
func (s *healthView) Update(events []*pbsubscribe.Event) error {
+ s.knownLeader = true
for _, event := range events {
serviceHealth := event.GetServiceHealth()
if serviceHealth == nil {
@@ -227,6 +229,9 @@ func (s *healthView) Result(index uint64) (interface{}, error) {
Nodes: make(structs.CheckServiceNodes, 0, len(s.state)),
QueryMeta: structs.QueryMeta{
Index: index,
+ // TODO: fill properly those fields, see https://github.com/hashicorp/consul/issues/9776
+ KnownLeader: s.knownLeader,
+ LastContact: 0,
},
}
for _, node := range s.state {
@@ -238,5 +243,6 @@ func (s *healthView) Result(index uint64) (interface{}, error) {
}
func (s *healthView) Reset() {
+ s.knownLeader = false
s.state = make(map[string]structs.CheckServiceNode)
}
diff --git a/agent/cache-types/streaming_health_services_test.go b/agent/cache-types/streaming_health_services_test.go
index 4c16bd12c44f..14b0691e586f 100644
--- a/agent/cache-types/streaming_health_services_test.go
+++ b/agent/cache-types/streaming_health_services_test.go
@@ -48,7 +48,8 @@ func TestStreamingHealthServices_EmptySnapshot(t *testing.T) {
empty := &structs.IndexedCheckServiceNodes{
Nodes: structs.CheckServiceNodes{},
QueryMeta: structs.QueryMeta{
- Index: 1,
+ Index: 1,
+ KnownLeader: true,
},
}