This repository has been archived by the owner on Mar 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add library for profiling downloader
update go vendor Signed-off-by: Lance Liu <[email protected]>
- Loading branch information
Lance Liu
committed
Jul 21, 2020
1 parent
20a456c
commit 2ae278a
Showing
28 changed files
with
4,875 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"strconv" | ||
"time" | ||
|
||
"k8s.io/client-go/rest" | ||
"k8s.io/client-go/tools/portforward" | ||
"k8s.io/client-go/transport/spdy" | ||
) | ||
|
||
const ( | ||
pprofPort uint32 = 8008 | ||
) | ||
|
||
func NewDownloader(cfg *rest.Config, podName, namespace string, endCh chan<- struct{}) *Downloader { | ||
return &Downloader{ | ||
PodName: podName, | ||
Namespace: namespace, | ||
ReadyCh: make(chan struct{}), | ||
RestConfig: cfg, | ||
LocalPort: 18008, | ||
Client: http.DefaultClient, | ||
} | ||
} | ||
|
||
type Downloader struct { | ||
PodName string | ||
Namespace string | ||
StopCh chan struct{} | ||
ReadyCh chan struct{} | ||
RestConfig *rest.Config | ||
LocalPort uint32 | ||
Client *http.Client | ||
} | ||
|
||
// non-blocking call to forward remote port in pod to localhost | ||
func (d *Downloader) Connect() error { | ||
path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", d.Namespace, d.PodName) | ||
transport, upgrader, err := spdy.RoundTripperFor(d.RestConfig) | ||
if err != nil { | ||
return err | ||
} | ||
u, err := url.Parse(d.RestConfig.Host) | ||
if err != nil { | ||
return err | ||
} | ||
url := &url.URL{ | ||
Host: u.Host, | ||
Scheme: u.Scheme, | ||
Path: path, | ||
} | ||
dialer := spdy.NewDialer(upgrader, &http.Client{Transport: transport}, http.MethodPost, url) | ||
fw, err := portforward.New(dialer, []string{fmt.Sprintf("%d:%d", d.LocalPort, pprofPort)}, d.StopCh, d.ReadyCh, os.Stdout, os.Stderr) | ||
if err != nil { | ||
return err | ||
} | ||
go fw.ForwardPorts() | ||
return nil | ||
} | ||
|
||
type ProfileType int | ||
|
||
const ( | ||
ProfileTypeUnknown ProfileType = iota | ||
ProfileTypeHeap | ||
ProfileTypeProfile | ||
ProfileTypeBlock | ||
ProfileTypeTrace | ||
ProfileTypeAllocs | ||
ProfileTypeMutex | ||
ProfileTypeGoroutine | ||
ProfileTypeThreadCreat | ||
) | ||
|
||
var ProfileEndpoints = [...]string{ | ||
ProfileTypeUnknown: "", | ||
ProfileTypeHeap: "heap", | ||
ProfileTypeProfile: "profile", | ||
ProfileTypeBlock: "block", | ||
ProfileTypeTrace: "trace", | ||
ProfileTypeAllocs: "allocs", | ||
ProfileTypeMutex: "mutex", | ||
ProfileTypeGoroutine: "goroutine", | ||
ProfileTypeThreadCreat: "threadcreate", | ||
} | ||
|
||
type DownloadOptions interface { | ||
Apply(*http.Request) error | ||
} | ||
|
||
type profilingTime time.Duration | ||
|
||
const secondsKey = "seconds" | ||
|
||
func (pr profilingTime) Apply(req *http.Request) error { | ||
query := req.URL.Query() | ||
seconds := int64(time.Duration(pr) / time.Second) | ||
query.Set(secondsKey, strconv.FormatInt(seconds, 10)) | ||
req.URL.RawQuery = query.Encode() | ||
return nil | ||
} | ||
|
||
func (d *Downloader) Download(t ProfileType, output io.Writer, options ...DownloadOptions) error { | ||
if t <= ProfileTypeUnknown || t >= ProfileType(len(ProfileEndpoints)) { | ||
return fmt.Errorf("unknown profiling type %d", t) | ||
} | ||
// wait for readyCh closing | ||
<-d.ReadyCh | ||
var err error | ||
url := &url.URL{ | ||
Scheme: "http", | ||
Host: fmt.Sprintf("127.0.0.1:%d", d.LocalPort), | ||
Path: fmt.Sprintf("/debug/pprof/%s", ProfileEndpoints[t]), | ||
} | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
go func() { | ||
select { | ||
// request succeeded | ||
case <-ctx.Done(): | ||
break | ||
case <-d.StopCh: | ||
cancel() | ||
} | ||
}() | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url.String(), nil) | ||
if err != nil { | ||
return err | ||
} | ||
for _, o := range options { | ||
if err = o.Apply(req); err != nil { | ||
return err | ||
} | ||
} | ||
resp, err := d.Client.Do(req) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
_, err = io.Copy(output, resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc" | ||
"k8s.io/client-go/tools/clientcmd" | ||
) | ||
|
||
// DO NOT COMMIT | ||
// sample code to download profile using profile downloader | ||
func main() { | ||
|
||
kubeconfig := os.Getenv("KUBECONFIG") | ||
|
||
// If we have an explicit indication of where the kubernetes config lives, read that. | ||
if kubeconfig == "" { | ||
log.Fatalf("please set KUBECONFIG") | ||
} | ||
cfg, err := clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
end := make(chan struct{}) | ||
c, err := kubernetes.NewForConfig(cfg) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
// find autoscaler pod name | ||
pods, err := c.CoreV1().Pods("knative-serving").List(metav1.ListOptions{ | ||
LabelSelector: "app=autoscaler", | ||
}) | ||
if err != nil || len(pods.Items) == 0 { | ||
log.Fatal(err) | ||
} | ||
|
||
podName := pods.Items[0].Name | ||
d := NewDownloader(cfg, podName, "knative-serving", end) | ||
log.Printf("getting profile for pod %s", podName) | ||
err = d.Connect() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
f1, _ := ioutil.TempFile("", "") | ||
defer f1.Close() | ||
log.Println("downloading 5s CPU profile") | ||
err = d.Download(ProfileTypeProfile, f1, profilingTime(5*time.Second)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Printf("cpu profile saved to %s", f1.Name()) | ||
f2, _ := ioutil.TempFile("", "") | ||
defer f2.Close() | ||
log.Println("downloading heap profile") | ||
err = d.Download(ProfileTypeHeap, f2) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Printf("heap saved to %s", f2.Name()) | ||
} |
13 changes: 13 additions & 0 deletions
13
plugins/admin/vendor/github.com/docker/spdystream/CONTRIBUTING.md
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.