ソースを参照

Provide a shortcut for proxying minikube

Lu Stadler 7 年 前
コミット
e73b27d8fe
共有2 個のファイルを変更した29 個の追加0 個の削除を含む
  1. 1 0
      go/fake-http/README.md
  2. 28 0
      go/fake-http/fake-http.go

+ 1 - 0
go/fake-http/README.md

@ -6,6 +6,7 @@ This needs a running Minikube instance, which can be started with
6 6
`minikube start`.
7 7
8 8
```
9
# shortcut for the cmdline below: fake-http -proxy-minikube
9 10
$ fake-http -proxy-url=https://$(minikube ip):8443 -proxy-client-cert ~/.minikube/client.crt -proxy-client-key ~/.minikube/client.key
10 11
2018/06/09 13:21:19 Listening on http://localhost:8080
11 12
2018/06/09 13:21:19 See http://localhost:8080/_help

+ 28 - 0
go/fake-http/fake-http.go

@ -13,6 +13,9 @@ import (
13 13
	"net/http"
14 14
	"net/http/httputil"
15 15
	"net/url"
16
	"os"
17
	"os/exec"
18
	"path"
16 19
	"strings"
17 20
18 21
	"gopkg.in/yaml.v2"
@ -23,6 +26,8 @@ var flags struct {
23 26
	proxyURL        string
24 27
	proxyClientCert string
25 28
	proxyClientKey  string
29
30
	proxyMinikube bool
26 31
}
27 32
28 33
func init() {
@ -30,6 +35,8 @@ func init() {
30 35
	flag.StringVar(&flags.proxyURL, "proxy-url", "", "Proxy requests to this URL")
31 36
	flag.StringVar(&flags.proxyClientCert, "proxy-client-cert", "", "Client certificate to use when connecting to proxy")
32 37
	flag.StringVar(&flags.proxyClientKey, "proxy-client-key", "", "Client key to use when connecting to proxy")
38
39
	flag.BoolVar(&flags.proxyMinikube, "proxy-minikube", false, "Shortcut for -proxy-url=https://$(minikube ip):8443 -proxy-client-cert ~/.minikube/client.crt -proxy-client-key ~/.minikube/client.key")
33 40
}
34 41
35 42
var responses = []Response{}
@ -37,6 +44,13 @@ var responses = []Response{}
37 44
func main() {
38 45
	flag.Parse()
39 46
47
	if flags.proxyMinikube {
48
		err := proxyMinikube()
49
		if err != nil {
50
			log.Fatalf("Error: Setting up Minikube proxy: %s", err)
51
		}
52
	}
53
40 54
	if flag.NArg() == 1 {
41 55
		rs, err := loadResponses(flag.Arg(0))
42 56
		if err != nil {
@ -165,6 +179,20 @@ func main() {
165 179
	log.Fatal(http.ListenAndServe(flags.addr, nil))
166 180
}
167 181
182
func proxyMinikube() error {
183
	out, err := exec.Command("minikube", "ip").Output()
184
	if err != nil {
185
		return err
186
	}
187
	flags.proxyURL = fmt.Sprintf("https://%s:8443", strings.TrimSpace(string(out)))
188
189
	homeDir := os.Getenv("HOME")
190
	flags.proxyClientCert = path.Join(homeDir, ".minikube/client.crt")
191
	flags.proxyClientKey = path.Join(homeDir, ".minikube/client.key")
192
193
	return nil
194
}
195
168 196
func matchResponse(req *http.Request, responses []Response) *Response {
169 197
	for _, resp := range responses {
170 198
		if req.Method == resp.Method && req.URL.Path == resp.Path {