forked from kubernetes/kube-state-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
242 lines (204 loc) · 7.69 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
Copyright 2015 The Kubernetes Authors All rights reserved.
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 (
"fmt"
"log"
"net"
"net/http"
"net/http/pprof"
"os"
"strconv"
"strings"
"github.com/golang/glog"
"github.com/openshift/origin/pkg/util/proc"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/client-go/informers"
clientset "k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/clientcmd"
kcollectors "k8s.io/kube-state-metrics/pkg/collectors"
"k8s.io/kube-state-metrics/pkg/metrics"
"k8s.io/kube-state-metrics/pkg/options"
"k8s.io/kube-state-metrics/pkg/version"
)
const (
metricsPath = "/metrics"
healthzPath = "/healthz"
)
// promLogger implements promhttp.Logger
type promLogger struct{}
func (pl promLogger) Println(v ...interface{}) {
glog.Error(v)
}
func main() {
opts := options.NewOptions()
opts.AddFlags()
err := opts.Parse()
if err != nil {
glog.Fatalf("Error: %s", err)
}
if opts.Version {
fmt.Printf("%#v\n", version.GetVersion())
os.Exit(0)
}
if opts.Help {
opts.Usage()
os.Exit(0)
}
var collectors options.CollectorSet
if len(opts.Collectors) == 0 {
glog.Info("Using default collectors")
collectors = options.DefaultCollectors
} else {
collectors = opts.Collectors
}
var namespaces options.NamespaceList
if len(opts.Namespaces) == 0 {
namespaces = options.DefaultNamespaces
} else {
namespaces = opts.Namespaces
}
if namespaces.IsAllNamespaces() {
glog.Info("Using all namespace")
} else {
glog.Infof("Using %s namespaces", namespaces)
}
if opts.MetricWhitelist.IsEmpty() && opts.MetricBlacklist.IsEmpty() {
glog.Info("No metric whitelist or blacklist set. No filtering of metrics will be done.")
}
if !opts.MetricWhitelist.IsEmpty() && !opts.MetricBlacklist.IsEmpty() {
glog.Fatal("Whitelist and blacklist are both set. They are mutually exclusive, only one of them can be set.")
}
if !opts.MetricWhitelist.IsEmpty() {
glog.Infof("A metric whitelist has been configured. Only the following metrics will be exposed: %s.", opts.MetricWhitelist.String())
}
if !opts.MetricBlacklist.IsEmpty() {
glog.Infof("A metric blacklist has been configured. The following metrics will not be exposed: %s.", opts.MetricBlacklist.String())
}
proc.StartReaper()
kubeClient, err := createKubeClient(opts.Apiserver, opts.Kubeconfig)
if err != nil {
glog.Fatalf("Failed to create client: %v", err)
}
ksmMetricsRegistry := prometheus.NewRegistry()
ksmMetricsRegistry.Register(kcollectors.ResourcesPerScrapeMetric)
ksmMetricsRegistry.Register(kcollectors.ScrapeErrorTotalMetric)
ksmMetricsRegistry.Register(prometheus.NewProcessCollector(os.Getpid(), ""))
ksmMetricsRegistry.Register(prometheus.NewGoCollector())
go telemetryServer(ksmMetricsRegistry, opts.TelemetryHost, opts.TelemetryPort)
registry := prometheus.NewRegistry()
registerCollectors(registry, kubeClient, collectors, namespaces, opts)
metricsServer(metrics.FilteredGatherer(registry, opts.MetricWhitelist, opts.MetricBlacklist), opts.Host, opts.Port)
}
func createKubeClient(apiserver string, kubeconfig string) (clientset.Interface, error) {
config, err := clientcmd.BuildConfigFromFlags(apiserver, kubeconfig)
if err != nil {
return nil, err
}
config.UserAgent = version.GetVersion().String()
config.AcceptContentTypes = "application/vnd.kubernetes.protobuf,application/json"
config.ContentType = "application/vnd.kubernetes.protobuf"
kubeClient, err := clientset.NewForConfig(config)
if err != nil {
return nil, err
}
// Informers don't seem to do a good job logging error messages when it
// can't reach the server, making debugging hard. This makes it easier to
// figure out if apiserver is configured incorrectly.
glog.Infof("Testing communication with server")
v, err := kubeClient.Discovery().ServerVersion()
if err != nil {
return nil, fmt.Errorf("ERROR communicating with apiserver: %v", err)
}
glog.Infof("Running with Kubernetes cluster version: v%s.%s. git version: %s. git tree state: %s. commit: %s. platform: %s",
v.Major, v.Minor, v.GitVersion, v.GitTreeState, v.GitCommit, v.Platform)
glog.Infof("Communication with server successful")
return kubeClient, nil
}
func telemetryServer(registry prometheus.Gatherer, host string, port int) {
// Address to listen on for web interface and telemetry
listenAddress := net.JoinHostPort(host, strconv.Itoa(port))
glog.Infof("Starting kube-state-metrics self metrics server: %s", listenAddress)
mux := http.NewServeMux()
// Add metricsPath
mux.Handle(metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorLog: promLogger{}}))
// Add index
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Kube-State-Metrics Metrics Server</title></head>
<body>
<h1>Kube-State-Metrics Metrics</h1>
<ul>
<li><a href='` + metricsPath + `'>metrics</a></li>
</ul>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(listenAddress, mux))
}
func metricsServer(registry prometheus.Gatherer, host string, port int) {
// Address to listen on for web interface and telemetry
listenAddress := net.JoinHostPort(host, strconv.Itoa(port))
glog.Infof("Starting metrics server: %s", listenAddress)
mux := http.NewServeMux()
mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
// Add metricsPath
mux.Handle(metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorLog: promLogger{}}))
// Add healthzPath
mux.HandleFunc(healthzPath, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("ok"))
})
// Add index
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Kube Metrics Server</title></head>
<body>
<h1>Kube Metrics</h1>
<ul>
<li><a href='` + metricsPath + `'>metrics</a></li>
<li><a href='` + healthzPath + `'>healthz</a></li>
</ul>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(listenAddress, mux))
}
// registerCollectors creates and starts informers and initializes and
// registers metrics for collection.
func registerCollectors(registry prometheus.Registerer, kubeClient clientset.Interface, enabledCollectors options.CollectorSet, namespaces options.NamespaceList, opts *options.Options) {
informerFactories := []informers.SharedInformerFactory{}
for _, ns := range namespaces {
informerFactories = append(
informerFactories,
informers.NewSharedInformerFactoryWithOptions(
kubeClient, 0, informers.WithNamespace(ns),
),
)
}
activeCollectors := []string{}
for c := range enabledCollectors {
f, ok := kcollectors.AvailableCollectors[c]
if ok {
f(registry, informerFactories, opts)
activeCollectors = append(activeCollectors, c)
}
}
glog.Infof("Active collectors: %s", strings.Join(activeCollectors, ","))
}