Skip to content

Commit

Permalink
feat(cdsctl): GET on a service name (#3635)
Browse files Browse the repository at this point in the history
this will be useful to get the service goroutines

Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault authored and bnjjj committed Nov 26, 2018
1 parent 267f703 commit 335e6db
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
37 changes: 37 additions & 0 deletions cli/cdsctl/admin_services.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"reflect"

"github.com/spf13/cobra"
Expand All @@ -18,6 +19,7 @@ func adminServices() *cobra.Command {
return cli.NewCommand(adminServicesCmd, nil, []*cobra.Command{
cli.NewListCommand(adminServiceListCmd, adminServiceListRun, nil),
cli.NewListCommand(adminServiceStatusCmd, adminServiceStatusRun, nil),
cli.NewCommand(adminServiceGetCmd, adminServiceGetRun, nil),
})
}

Expand Down Expand Up @@ -63,6 +65,32 @@ var adminServiceStatusCmd = cli.Command{
},
}

var adminServiceGetCmd = cli.Command{
Name: "request",
Short: "request GET on a CDS service",
Example: `
## How to get the goroutine of the service named hatcheryLocal:
` + "```bash" + `
cdsctl admin services request --name hatcheryLocal --query /debug/pprof/goroutine\?debug\=2
` + "```" + `
`,
Flags: []cli.Flag{
{
Kind: reflect.String,
Name: "name",
Usage: "service name",
Default: "",
},
{
Kind: reflect.String,
Name: "query",
Usage: "http query, example: '/debug/pprof/goroutine?debug=2'",
Default: "",
},
},
}

func adminServiceStatusRun(v cli.Values) (cli.ListResult, error) {
lines := []sdk.MonitoringStatusLine{}
if v.GetString("name") != "" {
Expand Down Expand Up @@ -94,3 +122,12 @@ func adminServiceStatusRun(v cli.Values) (cli.ListResult, error) {

return cli.AsListResult(lines), nil
}

func adminServiceGetRun(v cli.Values) error {
btes, err := client.ServiceNameCallGET(v.GetString("name"), v.GetString("query"))
if err != nil {
return err
}
fmt.Println(string(btes))
return nil
}
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ services:
environment:
CDS_HATCHERY_LOCAL_COMMONCONFIGURATION_NAME: hatchery-local
CDS_HATCHERY_LOCAL_COMMONCONFIGURATION_API_TOKEN: changeitchangeitchangeitchangeitchangeitchangeitchangeitchangeit
CDS_HATCHERY_LOCAL_COMMONCONFIGURATION_URL: http://cds-hatchery-local:8086
CDS_HATCHERY_LOCAL_COMMONCONFIGURATION_API_HTTP_URL: http://cds-api:8081
CDS_HATCHERY_LOCAL_NBPROVISION: 5
links:
Expand Down
26 changes: 21 additions & 5 deletions engine/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"io/ioutil"
"net/http"
"strings"

"github.com/gorilla/mux"

Expand Down Expand Up @@ -96,12 +97,25 @@ func (api *API) putAdminServiceCallHandler() service.Handler {

func selectDeleteAdminServiceCallHandler(api *API, method string) service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
srvs, err := services.FindByType(api.mustDB(), r.FormValue("type"))
if err != nil {
return err
var srvs []sdk.Service
if r.FormValue("name") != "" {
srv, err := services.FindByName(api.mustDB(), r.FormValue("name"))
if err != nil {
return err
}
if srv != nil {
srvs = []sdk.Service{*srv}
}
} else {
var errFind error
srvs, errFind = services.FindByType(api.mustDB(), r.FormValue("type"))
if errFind != nil {
return errFind
}
}

if len(srvs) == 0 {
return sdk.WrapError(sdk.ErrNotFound, "No hooks service found")
return sdk.WrapError(sdk.ErrNotFound, "No service found")
}

query := r.FormValue("query")
Expand All @@ -115,7 +129,9 @@ func selectDeleteAdminServiceCallHandler(api *API, method string) service.Handle

log.Debug("selectDeleteAdminServiceCallHandler> %s : %s", query, string(btes))

//TODO: assuming it's only json...
if strings.HasPrefix(query, "/debug/pprof/") {
return service.Write(w, btes, code, "text/plain")
}
return service.Write(w, btes, code, "application/json")
}
}
Expand Down
5 changes: 5 additions & 0 deletions sdk/cdsclient/client_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func (c *client) ServicesByType(stype string) ([]sdk.Service, error) {
return srvs, nil
}

func (c *client) ServiceNameCallGET(name string, query string) ([]byte, error) {
btes, _, _, err := c.Request(context.Background(), "GET", "/admin/services/call?name="+name+"&query="+url.QueryEscape(query), nil)
return btes, err
}

func (c *client) ServiceCallGET(stype string, query string) ([]byte, error) {
btes, _, _, err := c.Request(context.Background(), "GET", "/admin/services/call?type="+stype+"&query="+url.QueryEscape(query), nil)
return btes, err
Expand Down
1 change: 1 addition & 0 deletions sdk/cdsclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type AdminService interface {
Services() ([]sdk.Service, error)
ServicesByName(name string) (*sdk.Service, error)
ServicesByType(stype string) ([]sdk.Service, error)
ServiceNameCallGET(name string, url string) ([]byte, error)
ServiceCallGET(stype string, url string) ([]byte, error)
ServiceCallPOST(stype string, url string, body []byte) ([]byte, error)
ServiceCallPUT(stype string, url string, body []byte) ([]byte, error)
Expand Down
6 changes: 5 additions & 1 deletion tests/clictl_admin_services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ testcases:
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldNotContainSubstring hatchery
- result.systemout ShouldContainSubstring api
- result.systemout ShouldContainSubstring api
- script: {{.cds.build.cdsctl}} admin services request --name `{{.cds.build.cdsctl}} admin services list -q|grep hatchery|head -n1` --query /debug/pprof/goroutine\?debug\=2
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldContainSubstring transport.go

0 comments on commit 335e6db

Please sign in to comment.