-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
132 lines (102 loc) · 2.85 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"crypto/tls"
"encoding/json"
"fmt"
"net"
"os"
"os/signal"
"strings"
"syscall"
"k8s.io/cli-runtime/pkg/genericclioptions"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/klog/v2"
"k8s.io/kubectl/pkg/proxy"
)
// These get overridden at build time: -X main.Version=$VERSION
var (
Version = "0.0.1"
Commit = ""
)
type VersionData struct {
Version string `json:"gitVersion"`
Commit string `json:"gitCommit"`
}
func main() {
argsWithoutProg := os.Args[1:]
if len(argsWithoutProg) > 0 && argsWithoutProg[0] == "version" {
err := json.NewEncoder(os.Stdout).Encode(&VersionData{
Version: Version,
Commit: Commit,
})
if err != nil {
klog.Fatal("failed to marshal version data", err)
}
os.Exit(0)
}
kubeconfig := os.Getenv("KUBECONFIG")
kubeconfigContext := os.Getenv("KUBECONFIG_CONTEXT")
apiPrefix := os.Getenv("API_PREFIX")
proxyCert := os.Getenv("PROXY_CERT")
proxyKey := os.Getenv("PROXY_KEY")
if !strings.HasSuffix(apiPrefix, "/") {
apiPrefix += "/"
}
done := make(chan os.Signal, 2)
signal.Notify(done, os.Interrupt, syscall.SIGTERM)
fmt.Printf("~~ Lens K8s Proxy, '%s' ~~\n", Version)
fmt.Printf("kubeconfig: %s\n", kubeconfig)
fmt.Printf("api prefix: %s\n", apiPrefix)
config := genericclioptions.NewConfigFlags(false)
config.KubeConfig = &kubeconfig
if kubeconfigContext != "" {
config.Context = &kubeconfigContext
}
clientConfig, err := config.ToRESTConfig()
if err != nil {
klog.Fatal("failed to initialize kubeconfig", err)
}
server, err := proxy.NewServer("", apiPrefix, "", nil, clientConfig, 0, true)
if err != nil {
klog.Fatal("failed to initialize proxy", err)
}
l, err := getListener(proxyCert, proxyKey)
if err != nil {
klog.Fatal("failed to get listener", err)
}
fmt.Printf("starting to serve on %s\n", l.Addr().String())
go func() {
err := server.ServeOnListener(l)
if err != nil {
klog.Fatal(err)
}
}()
<-done
fmt.Println("shutting down ...")
l.Close()
os.Exit(0)
}
const proxyAddr = "127.0.0.1:0"
func getListener(proxyCert string, proxyKey string) (net.Listener, error) {
if proxyCert == "" || proxyKey == "" {
return net.Listen("tcp", proxyAddr)
}
cert, err := tls.X509KeyPair([]byte(proxyCert), []byte(proxyKey))
if err != nil {
klog.Fatal(err)
}
return tls.Listen("tcp", proxyAddr, &tls.Config{
Certificates: []tls.Certificate{cert},
MinVersion: tls.VersionTLS12, // Set the minimum version of TLS to 1.2
MaxVersion: tls.VersionTLS13, // Set the maximum version of TLS to 1.3
PreferServerCipherSuites: true,
CipherSuites: []uint16{
tls.TLS_AES_256_GCM_SHA384,
tls.TLS_CHACHA20_POLY1305_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
},
})
}