Merge pull request #1564 from weaveworks/probe-version-header

Add probe version header to probe requests
This commit is contained in:
Alfonso Acosta
2016-06-08 15:34:34 +01:00
4 changed files with 27 additions and 15 deletions

View File

@@ -9,6 +9,9 @@ const (
// ScopeProbeIDHeader is the header we use to carry the probe's unique ID. The
// ID is currently set to the a random string on probe startup.
ScopeProbeIDHeader = "X-Scope-Probe-ID"
// ScopeProbeVersionHeader is the header we use to carry the probe's version.
ScopeProbeVersionHeader = "X-Scope-Probe-Version"
)
// Details are some generic details that can be fetched from /api

View File

@@ -18,7 +18,7 @@ import (
"github.com/weaveworks/scope/test"
)
func dummyServer(t *testing.T, expectedToken, expectedID string, expectedReport report.Report, done chan struct{}) *httptest.Server {
func dummyServer(t *testing.T, expectedToken, expectedID string, expectedVersion string, expectedReport report.Report, done chan struct{}) *httptest.Server {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if have := r.Header.Get("Authorization"); fmt.Sprintf("Scope-Probe token=%s", expectedToken) != have {
t.Errorf("want %q, have %q", expectedToken, have)
@@ -28,6 +28,10 @@ func dummyServer(t *testing.T, expectedToken, expectedID string, expectedReport
t.Errorf("want %q, have %q", expectedID, have)
}
if have := r.Header.Get(xfer.ScopeProbeVersionHeader); expectedVersion != have {
t.Errorf("want %q, have %q", expectedID, have)
}
var have report.Report
reader := r.Body
@@ -59,10 +63,11 @@ func dummyServer(t *testing.T, expectedToken, expectedID string, expectedReport
func TestAppClientPublish(t *testing.T) {
var (
token = "abcdefg"
id = "1234567"
rpt = report.MakeReport()
done = make(chan struct{}, 10)
token = "abcdefg"
id = "1234567"
version = "0.18"
rpt = report.MakeReport()
done = make(chan struct{}, 10)
)
// marshalling->unmarshaling is not idempotent due to `json:"omitempty"`
@@ -89,7 +94,7 @@ func TestAppClientPublish(t *testing.T) {
rpt.Host.Controls = nil
rpt.Overlay.Controls = nil
s := dummyServer(t, token, id, rpt, done)
s := dummyServer(t, token, id, version, rpt, done)
defer s.Close()
u, err := url.Parse(s.URL)
@@ -98,9 +103,10 @@ func TestAppClientPublish(t *testing.T) {
}
pc := ProbeConfig{
Token: token,
ProbeID: id,
Insecure: false,
Token: token,
ProbeVersion: version,
ProbeID: id,
Insecure: false,
}
p, err := NewAppClient(pc, u.Host, s.URL, nil)

View File

@@ -24,14 +24,16 @@ func init() {
// ProbeConfig contains all the info needed for a probe to do HTTP requests
type ProbeConfig struct {
Token string
ProbeID string
Insecure bool
Token string
ProbeVersion string
ProbeID string
Insecure bool
}
func (pc ProbeConfig) authorizeHeaders(headers http.Header) {
headers.Set("Authorization", fmt.Sprintf("Scope-Probe token=%s", pc.Token))
headers.Set(xfer.ScopeProbeIDHeader, pc.ProbeID)
headers.Set(xfer.ScopeProbeVersionHeader, pc.ProbeVersion)
}
func (pc ProbeConfig) authorizedRequest(method string, urlStr string, body io.Reader) (*http.Request, error) {

View File

@@ -104,9 +104,10 @@ func probeMain(flags probeFlags) {
log.Infof("publishing to: %s", strings.Join(targets, ", "))
probeConfig := appclient.ProbeConfig{
Token: flags.token,
ProbeID: probeID,
Insecure: flags.insecure,
Token: flags.token,
ProbeVersion: version,
ProbeID: probeID,
Insecure: flags.insecure,
}
clients := appclient.NewMultiAppClient(func(hostname, endpoint string) (appclient.AppClient, error) {
return appclient.NewAppClient(