Add app capabilities to /api endpoint

This commit is contained in:
Alfonso Acosta
2017-06-08 11:02:25 +00:00
parent d1489d4d85
commit 6be7aa8be2
5 changed files with 25 additions and 17 deletions

View File

@@ -14,7 +14,7 @@ import (
func topologyServer() *httptest.Server {
router := mux.NewRouter().SkipClean(true)
app.RegisterTopologyRoutes(router, app.StaticCollector(fixture.Report))
app.RegisterTopologyRoutes(router, app.StaticCollector(fixture.Report), map[string]bool{"foo_capability": true})
return httptest.NewServer(router)
}

View File

@@ -189,7 +189,7 @@ func TestAPITopologyAddsKubernetes(t *testing.T) {
router := mux.NewRouter()
c := app.NewCollector(1 * time.Minute)
app.RegisterReportPostHandler(c, router)
app.RegisterTopologyRoutes(router, c)
app.RegisterTopologyRoutes(router, c, map[string]bool{"foo_capability": true})
ts := httptest.NewServer(router)
defer ts.Close()

View File

@@ -90,10 +90,10 @@ func gzipHandler(h http.HandlerFunc) http.HandlerFunc {
}
// RegisterTopologyRoutes registers the various topology routes with a http mux.
func RegisterTopologyRoutes(router *mux.Router, r Reporter) {
func RegisterTopologyRoutes(router *mux.Router, r Reporter, capabilities map[string]bool) {
get := router.Methods("GET").Subrouter()
get.HandleFunc("/api",
gzipHandler(requestContextDecorator(apiHandler(r))))
gzipHandler(requestContextDecorator(apiHandler(r, capabilities))))
get.HandleFunc("/api/topology",
gzipHandler(requestContextDecorator(topologyRegistry.makeTopologyList(r))))
get.
@@ -177,7 +177,7 @@ func NewVersion(version, downloadURL string) {
}
}
func apiHandler(rep Reporter) CtxHandlerFunc {
func apiHandler(rep Reporter, capabilities map[string]bool) CtxHandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
report, err := rep.Report(ctx)
if err != nil {
@@ -187,11 +187,12 @@ func apiHandler(rep Reporter) CtxHandlerFunc {
newVersion.Lock()
defer newVersion.Unlock()
respondWith(w, http.StatusOK, xfer.Details{
ID: UniqueID,
Version: Version,
Hostname: hostname.Get(),
Plugins: report.Plugins,
NewVersion: newVersion.NewVersionInfo,
ID: UniqueID,
Version: Version,
Hostname: hostname.Get(),
Plugins: report.Plugins,
Capabilities: capabilities,
NewVersion: newVersion.NewVersionInfo,
})
}
}

View File

@@ -14,12 +14,15 @@ const (
ScopeProbeVersionHeader = "X-Scope-Probe-Version"
)
const ReportPersistenceCapability = "report_persistence"
// Details are some generic details that can be fetched from /api
type Details struct {
ID string `json:"id"`
Version string `json:"version"`
Hostname string `json:"hostname"`
Plugins PluginSpecs `json:"plugins,omitempty"`
ID string `json:"id"`
Version string `json:"version"`
Hostname string `json:"hostname"`
Plugins PluginSpecs `json:"plugins,omitempty"`
Capabilities map[string]bool `json:"capabilities,omitempty"`
NewVersion *NewVersionInfo `json:"newVersion,omitempty"`
}

View File

@@ -19,6 +19,7 @@ import (
"github.com/prometheus/client_golang/prometheus"
"github.com/tylerb/graceful"
"github.com/weaveworks/go-checkpoint"
"github.com/weaveworks/scope/common/xfer"
"github.com/weaveworks/weave/common"
billing "github.com/weaveworks/billing-client"
@@ -50,7 +51,7 @@ func init() {
}
// Router creates the mux for all the various app components.
func router(collector app.Collector, controlRouter app.ControlRouter, pipeRouter app.PipeRouter, externalUI bool) http.Handler {
func router(collector app.Collector, controlRouter app.ControlRouter, pipeRouter app.PipeRouter, externalUI bool, capabilities map[string]bool) http.Handler {
router := mux.NewRouter().SkipClean(true)
// We pull in the http.DefaultServeMux to get the pprof routes
@@ -60,7 +61,7 @@ func router(collector app.Collector, controlRouter app.ControlRouter, pipeRouter
app.RegisterReportPostHandler(collector, router)
app.RegisterControlRoutes(router, controlRouter)
app.RegisterPipeRoutes(router, pipeRouter)
app.RegisterTopologyRoutes(router, collector)
app.RegisterTopologyRoutes(router, collector, capabilities)
uiHandler := http.FileServer(GetFS(externalUI))
router.PathPrefix("/ui").Name("static").Handler(
@@ -294,7 +295,10 @@ func appMain(flags appFlags) {
}
}
handler := router(collector, controlRouter, pipeRouter, flags.externalUI)
capabilities := map[string]bool{
xfer.ReportPersistenceCapability: flags.s3URL != "local",
}
handler := router(collector, controlRouter, pipeRouter, flags.externalUI, capabilities)
if flags.logHTTP {
handler = middleware.Log{
LogRequestHeaders: flags.logHTTPHeaders,