Skip to content

Commit

Permalink
Merge pull request #28 from profefe/feature/kubectl-get-services
Browse files Browse the repository at this point in the history
feat(kubectl): Add new kubectl command "get profiles"
  • Loading branch information
gianarb authored Mar 6, 2020
2 parents 46533e4 + 3bc8b05 commit 407ccce
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ func NewGetCmd() *cobra.Command {

cmd.AddCommand(NewGetProfilesCmd())
cmd.AddCommand(NewGetProfileTypesCmd())
cmd.AddCommand(NewGetServicesCmd())
return cmd
}
43 changes: 43 additions & 0 deletions pkg/cmd/get_services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cmd

import (
"context"
"fmt"
"net/http"

"github.com/gianarb/kube-profefe/pkg/profefe"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func NewGetServicesCmd() *cobra.Command {
flags := pflag.NewFlagSet("kprofefe", pflag.ExitOnError)

cmd := &cobra.Command{
Use: "services",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
pClient := profefe.NewClient(profefe.Config{
HostPort: ProfefeHostPort,
}, http.Client{})

resp, err := pClient.GetServices(ctx)
if err != nil {
return err
}

fmt.Fprint(cmd.OutOrStdout(), "Services:\n")
for _, v := range resp.Body {
fmt.Fprintf(cmd.OutOrStdout(), "\t %s\n", v)
}
return nil
},
}

flags.AddFlagSet(cmd.PersistentFlags())
flags.StringVar(&ProfefeHostPort, "profefe-hostport", "http://localhost:10100", `where profefe is located`)

cmd.Flags().AddFlagSet(flags)

return cmd
}
46 changes: 46 additions & 0 deletions pkg/cmd/get_services_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cmd

import (
"bytes"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

func TestGetServices(t *testing.T) {
pprofServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(`{
"code": 200,
"body": [
"first",
"second"
]
}`))
}))

buf := new(bytes.Buffer)
cmd := NewGetServicesCmd()
cmd.SetOut(buf)
cmd.SetArgs([]string{
"--profefe-hostport",
pprofServer.URL,
})

err := cmd.Execute()
if err != nil {
t.Fatal(err)
}
exp := `Services:
first
second`

out := buf.String()
if strings.Contains(out, exp) {
t.Errorf(`expected "%s" got "%s"`, exp, out)
}
}

//Services:
//first
//second
25 changes: 25 additions & 0 deletions pkg/profefe/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,31 @@ type SavePprofResponse struct {
} `json:"body"`
}

// GET
// /api/0/services
func (c *Client) GetServices(ctx context.Context) (*GetServicesResponse, error) {
buf := bytes.NewBuffer([]byte{})
r, err := http.NewRequestWithContext(ctx, "GET", c.HostPort+"/api/0/services", buf)

resp, err := c.Do(r)
defer resp.Body.Close()
rr := &GetServicesResponse{}

err = json.NewDecoder(resp.Body).Decode(rr)
if err != nil {
return nil, err
}
if resp.StatusCode == http.StatusOK {
return rr, nil
}
return nil, fmt.Errorf(rr.Error)
}

type GetServicesResponse struct {
Body []string
Error string `json:"error"`
}

func NewClient(config Config, httpClient http.Client) *Client {
if config.HostPort == "" {
config.HostPort = "http://localhost:10100"
Expand Down
28 changes: 28 additions & 0 deletions pkg/profefe/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"reflect"
"runtime"
Expand Down Expand Up @@ -69,3 +70,30 @@ func TestSavePprof(t *testing.T) {
t.Errorf("expected serviec name %s got %s", funcName, resp.Body.Service)
}
}

func TestGetServices(t *testing.T) {
ctx := context.Background()

pprofServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(`{
"code": 200,
"body": [
"first",
"second"
]
}`))
}))

client := NewClient(Config{
HostPort: pprofServer.URL,
UserAgent: "test",
}, *pprofServer.Client())

rr, err := client.GetServices(ctx)
if err != nil {
t.Fatal(err)
}
if rr.Body[0] != "first" {
t.Errorf("expect first got %s", rr.Body[0])
}
}

0 comments on commit 407ccce

Please sign in to comment.