This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
root.go
78 lines (70 loc) · 1.8 KB
/
root.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
package app
import (
"fmt"
"net"
"net/http"
"strings"
"time"
proxyproto "github.com/pires/go-proxyproto"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type serverOptions struct {
CertFile string
KeyFile string
Provider string
Filters string
Kubeconfig string
Port int
}
func NewRootCmd() *cobra.Command {
o := &serverOptions{}
cmd := &cobra.Command{
Use: "bootstrap-server",
Short: "run bootstrap-server",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
if o.CertFile == "" {
return errors.New("must provide CertFile")
}
if o.KeyFile == "" {
return errors.New("must provide KeyFile")
}
s := &http.Server{
Addr: fmt.Sprintf(":%d", o.Port),
Handler: newBootstrapRouter(&bootstrapConfig{
Provider: o.Provider,
Filters: parseFilters(o.Filters),
Kubeconfig: o.Kubeconfig,
}),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
l, err := net.Listen("tcp", s.Addr)
if err != nil {
return err
}
return s.ServeTLS(&proxyproto.Listener{Listener: l}, o.CertFile, o.KeyFile)
},
}
cmd.Flags().StringVar(&o.Provider, "provider", "", "")
cmd.Flags().StringVar(&o.Filters, "filters", "", "")
cmd.Flags().StringVar(&o.CertFile, "cert-file", "", "server certificate")
cmd.Flags().StringVar(&o.KeyFile, "key-file", "", "server key")
cmd.Flags().StringVar(&o.Kubeconfig, "kubeconfig", "", "")
cmd.Flags().IntVar(&o.Port, "port", 8080, "")
return cmd
}
func parseFilters(s string) map[string]string {
filters := make(map[string]string)
pairs := strings.Split(s, ",")
for _, pair := range pairs {
parts := strings.SplitN(pair, "=", 2)
if len(parts) != 2 {
continue
}
filters[parts[0]] = parts[1]
}
return filters
}