Skip to content

Commit

Permalink
Add app capabilities to /api endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfonso Acosta committed Jun 8, 2017
1 parent d1489d4 commit 6be7aa8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/api_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion app/api_topologies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
17 changes: 9 additions & 8 deletions app/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
})
}
}
11 changes: 7 additions & 4 deletions common/xfer/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
10 changes: 7 additions & 3 deletions prog/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 6be7aa8

Please sign in to comment.