Skip to content

Commit

Permalink
Use the appropriate port when redirecting node-agents to leader (Data…
Browse files Browse the repository at this point in the history
  • Loading branch information
xvello authored Jan 23, 2019
1 parent 728d66d commit 7f6f289
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/clusteragent/clusterchecks/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package clusterchecks

import (
"fmt"
"net/http"

"github.com/DataDog/datadog-agent/pkg/clusteragent/clusterchecks/types"
Expand All @@ -28,7 +29,7 @@ func (h *Handler) ShouldHandle() (int, string) {
case leader:
return http.StatusOK, ""
case follower:
return http.StatusFound, h.leaderIP
return http.StatusFound, fmt.Sprintf("%s:%d", h.leaderIP, h.port)
default:
return http.StatusServiceUnavailable, notReadyReason
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/clusteragent/clusterchecks/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
)

func TestShouldHandle(t *testing.T) {
h := &Handler{}
h := &Handler{
port: 5005,
}

// Initial state
code, reason := h.ShouldHandle()
Expand All @@ -33,5 +35,5 @@ func TestShouldHandle(t *testing.T) {
h.leaderIP = "1.2.3.4"
code, reason = h.ShouldHandle()
assert.Equal(t, http.StatusFound, code)
assert.Equal(t, "1.2.3.4", reason)
assert.Equal(t, "1.2.3.4:5005", reason)
}
2 changes: 2 additions & 0 deletions pkg/clusteragent/clusterchecks/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Handler struct {
m sync.RWMutex // Below fields protected by the mutex
state state
leaderIP string
port int
}

// NewHandler returns a populated Handler
Expand All @@ -67,6 +68,7 @@ func NewHandler(ac pluggableAutoConfig) (*Handler, error) {
warmupDuration: config.Datadog.GetDuration("cluster_checks.warmup_duration") * time.Second,
leadershipChan: make(chan state, 1),
dispatcher: newDispatcher(),
port: config.Datadog.GetInt("cluster_agent.cmd_port"),
}

if config.Datadog.GetBool("leader_election") {
Expand Down
5 changes: 3 additions & 2 deletions pkg/clusteragent/clusterchecks/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func TestHandlerRun(t *testing.T) {
leadershipChan: make(chan state, 1),
dispatcher: newDispatcher(),
leaderStatusCallback: le.get,
port: 5005,
}

//
Expand Down Expand Up @@ -179,7 +180,7 @@ func TestHandlerRun(t *testing.T) {
assertTrueBeforeTimeout(t, 10*time.Millisecond, 250*time.Millisecond, func() bool {
// API redirects to leader
code, reason := h.ShouldHandle()
return code == http.StatusFound && reason == "1.2.3.4"
return code == http.StatusFound && reason == "1.2.3.4:5005"
})

//
Expand Down Expand Up @@ -246,7 +247,7 @@ func TestHandlerRun(t *testing.T) {
assertTrueBeforeTimeout(t, 10*time.Millisecond, 250*time.Millisecond, func() bool {
// API redirects to leader again
code, reason := h.ShouldHandle()
return code == http.StatusFound && reason == "1.2.3.6"
return code == http.StatusFound && reason == "1.2.3.6:5005"
})
assertTrueBeforeTimeout(t, 10*time.Millisecond, 500*time.Millisecond, func() bool {
// RemoveScheduler is called
Expand Down

0 comments on commit 7f6f289

Please sign in to comment.