-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathmain.go
172 lines (148 loc) · 5.42 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2017 The Kubernetes Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"crypto/elliptic"
"crypto/tls"
"net/http"
"time"
restfulspec "github.com/emicklei/go-restful-openapi/v2"
"github.com/emicklei/go-restful/v3"
"github.com/go-openapi/spec"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/klog/v2"
"k8s.io/dashboard/api/pkg/args"
"k8s.io/dashboard/api/pkg/environment"
"k8s.io/dashboard/api/pkg/handler"
"k8s.io/dashboard/api/pkg/integration"
integrationapi "k8s.io/dashboard/api/pkg/integration/api"
"k8s.io/dashboard/certificates"
"k8s.io/dashboard/certificates/ecdsa"
"k8s.io/dashboard/client"
)
func main() {
klog.InfoS("Starting Kubernetes Dashboard API", "version", environment.Version)
client.Init(
client.WithUserAgent(environment.UserAgent()),
client.WithKubeconfig(args.KubeconfigPath()),
client.WithMasterUrl(args.ApiServerHost()),
client.WithInsecureTLSSkipVerify(args.ApiServerSkipTLSVerify()),
)
if !args.IsProxyEnabled() {
ensureAPIServerConnectionOrDie()
} else {
klog.Info("Running in proxy mode. InClusterClient connections will be disabled.")
}
// Init integrations
integrationManager := integration.NewIntegrationManager()
if !args.IsProxyEnabled() {
configureMetricsProvider(integrationManager)
} else {
klog.Info("Skipping metrics configuration. Metrics not available in proxy mode.")
}
apiHandler, err := handler.CreateHTTPAPIHandler(integrationManager)
if err != nil {
handleFatalInitError(err)
}
if args.IsOpenAPIEnabled() {
klog.Info("Enabling OpenAPI endpoint on /apidocs.json")
configureOpenAPI(apiHandler)
}
certCreator := ecdsa.NewECDSACreator(args.KeyFile(), args.CertFile(), elliptic.P256())
certManager := certificates.NewCertManager(certCreator, args.DefaultCertDir(), args.AutogenerateCertificates())
certs, err := certManager.GetCertificates()
if err != nil {
handleFatalInitServingCertError(err)
}
http.Handle("/", apiHandler)
http.Handle("/api/sockjs/", handler.CreateAttachHandler("/api/sockjs"))
http.Handle("/metrics", promhttp.Handler())
if certs != nil {
serveTLS(certs)
} else {
serve()
}
select {}
}
func serve() {
klog.V(1).InfoS("Listening and serving on", "address", args.InsecureAddress())
go func() { klog.Fatal(http.ListenAndServe(args.InsecureAddress(), nil)) }()
}
func serveTLS(certificates []tls.Certificate) {
klog.V(1).InfoS("Listening and serving on", "address", args.Address())
server := &http.Server{
Addr: args.Address(),
Handler: http.DefaultServeMux,
TLSConfig: &tls.Config{
Certificates: certificates,
MinVersion: tls.VersionTLS12,
},
}
go func() { klog.Fatal(server.ListenAndServeTLS("", "")) }()
}
func ensureAPIServerConnectionOrDie() {
versionInfo, err := client.InClusterClient().Discovery().ServerVersion()
if err != nil {
handleFatalInitError(err)
}
klog.InfoS("Successful initial request to the apiserver", "version", versionInfo.String())
}
func configureMetricsProvider(integrationManager integration.Manager) {
switch metricsProvider := args.MetricsProvider(); metricsProvider {
case "sidecar":
integrationManager.Metric().ConfigureSidecar(args.SidecarHost()).
EnableWithRetry(integrationapi.SidecarIntegrationID, time.Duration(args.MetricClientHealthCheckPeriod()))
case "none":
klog.Info("Metrics provider disabled")
default:
klog.InfoS("Invalid metrics provider", "provider", metricsProvider)
klog.Info("Using default metrics provider", "provider", "sidecar")
integrationManager.Metric().ConfigureSidecar(args.SidecarHost()).
EnableWithRetry(integrationapi.SidecarIntegrationID, time.Duration(args.MetricClientHealthCheckPeriod()))
}
}
func configureOpenAPI(container *restful.Container) {
config := restfulspec.Config{
WebServices: container.RegisteredWebServices(),
APIPath: "/apidocs.json",
PostBuildSwaggerObjectHandler: enrichOpenAPIObject,
}
container.Add(restfulspec.NewOpenAPIService(config))
}
func enrichOpenAPIObject(swo *spec.Swagger) {
swo.Info = &spec.Info{
InfoProps: spec.InfoProps{
Title: "Kubernetes Dashboard API",
Version: environment.Version,
},
}
}
/**
* Handles fatal init error that prevents server from doing any work. Prints verbose error
* message and quits the server.
*/
func handleFatalInitError(err error) {
klog.Fatalf("Error while initializing connection to Kubernetes apiserver. "+
"This most likely means that the cluster is misconfigured (e.g., it has "+
"invalid apiserver certificates or service account's configuration) or the "+
"--apiserver-host param points to a server that does not exist. Reason: %s\n"+
"Refer to our FAQ and wiki pages for more information: "+
"https://github.com/kubernetes/dashboard/wiki/FAQ", err)
}
/**
* Handles fatal init errors encountered during service cert loading.
*/
func handleFatalInitServingCertError(err error) {
klog.Fatalf("Error while loading dashboard server certificates. Reason: %s", err)
}