-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
117 lines (109 loc) · 2.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"context"
"fmt"
"log"
"os"
"os/exec"
"strings"
"time"
"github.com/chromedp/chromedp"
"golang.org/x/sync/errgroup"
)
func init() {
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
}
func main() {
ctx := context.Background()
eg, ctx := errgroup.WithContext(ctx)
chInterrupt := make(chan struct{})
eg.Go(func() error {
defer close(chInterrupt)
return runBrowser(ctx)
})
eg.Go(func() error {
return runKauthproxy(chInterrupt)
})
if err := eg.Wait(); err != nil {
log.Fatal(err)
}
}
func runKauthproxy(chInterrupt <-chan struct{}) error {
c := exec.Command("output/kauthproxy",
"--namespace=kubernetes-dashboard",
"--user=tester",
"--skip-open-browser",
"https://kubernetes-dashboard.svc",
)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
if err := c.Start(); err != nil {
return fmt.Errorf("could not start a process: %w", err)
}
log.Printf("started %s", c.String())
<-chInterrupt
if err := c.Process.Signal(os.Interrupt); err != nil {
return fmt.Errorf("could not send SIGINT to the process: %w", err)
}
if err := c.Wait(); err != nil {
return fmt.Errorf("could not wait for the process: %w", err)
}
return nil
}
func runBrowser(ctx context.Context) error {
execOpts := chromedp.DefaultExecAllocatorOptions[:]
execOpts = append(execOpts, chromedp.NoSandbox)
ctx, cancel := chromedp.NewExecAllocator(ctx, execOpts...)
defer cancel()
ctx, cancel = chromedp.NewContext(ctx, chromedp.WithLogf(log.Printf))
defer cancel()
ctx, cancel = context.WithTimeout(ctx, 30*time.Second)
defer cancel()
err := chromedp.Run(ctx,
chromedp.EmulateViewport(2048, 1152),
// open the page of pod list
navigate("http://localhost:18000/#/pod?namespace=kube-system"),
// wait for a link on the page
chromedp.WaitReady(`a[href^='#/pod/kube-system']`, chromedp.ByQuery),
takeScreenshot("output/screenshot.png"),
)
if err != nil {
return fmt.Errorf("could not run the browser: %w", err)
}
return nil
}
// navigate to the URL and retry on network errors
func navigate(urlstr string) chromedp.Action {
return chromedp.ActionFunc(func(ctx context.Context) error {
for {
var l string
if err := chromedp.Run(ctx,
chromedp.Navigate(urlstr),
chromedp.Location(&l),
); err != nil {
return err
}
log.Printf("opened %s", l)
if strings.HasPrefix(l, "http://") {
return nil
}
if err := chromedp.Sleep(1 * time.Second).Do(ctx); err != nil {
return err
}
}
})
}
// https://github.com/chromedp/examples/blob/master/screenshot/main.go
func takeScreenshot(name string) chromedp.Action {
return chromedp.ActionFunc(func(ctx context.Context) error {
var b []byte
if err := chromedp.FullScreenshot(&b, 90).Do(ctx); err != nil {
return fmt.Errorf("could not capture a screenshot: %w", err)
}
if err := os.WriteFile(name, b, 0644); err != nil {
return fmt.Errorf("could not write: %w", err)
}
log.Printf("saved screenshot to %s", name)
return nil
})
}