forked from kubernetes/kube-state-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
280 lines (245 loc) · 8.62 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
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 (
"flag"
"fmt"
"log"
"net/http"
"os"
"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"
"github.com/spf13/pflag"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/kube-state-metrics/collectors"
)
const (
metricsPath = "/metrics"
healthzPath = "/healthz"
)
var (
defaultCollectors = collectorSet{
"daemonsets": struct{}{},
"deployments": struct{}{},
"limitranges": struct{}{},
"nodes": struct{}{},
"pods": struct{}{},
"replicasets": struct{}{},
"replicationcontrollers": struct{}{},
"resourcequotas": struct{}{},
"services": struct{}{},
"jobs": struct{}{},
"cronjobs": struct{}{},
"statefulsets": struct{}{},
"persistentvolumeclaims": struct{}{},
}
availableCollectors = map[string]func(registry prometheus.Registerer, kubeClient clientset.Interface){
"cronjobs": collectors.RegisterCronJobCollector,
"daemonsets": collectors.RegisterDaemonSetCollector,
"deployments": collectors.RegisterDeploymentCollector,
"jobs": collectors.RegisterJobCollector,
"limitranges": collectors.RegisterLimitRangeCollector,
"nodes": collectors.RegisterNodeCollector,
"pods": collectors.RegisterPodCollector,
"replicasets": collectors.RegisterReplicaSetCollector,
"replicationcontrollers": collectors.RegisterReplicationControllerCollector,
"resourcequotas": collectors.RegisterResourceQuotaCollector,
"services": collectors.RegisterServiceCollector,
"statefulsets": collectors.RegisterStatefulSetCollector,
"persistentvolumeclaims": collectors.RegisterPersistentVolumeClaimCollector,
}
)
type collectorSet map[string]struct{}
func (c *collectorSet) String() string {
s := *c
return strings.Join(s.asSlice(), ",")
}
func (c *collectorSet) Set(value string) error {
s := *c
cols := strings.Split(value, ",")
for _, col := range cols {
_, ok := availableCollectors[col]
if !ok {
glog.Fatalf("Collector \"%s\" does not exist", col)
}
s[col] = struct{}{}
}
return nil
}
func (c collectorSet) asSlice() []string {
cols := []string{}
for col, _ := range c {
cols = append(cols, col)
}
return cols
}
func (c collectorSet) isEmpty() bool {
return len(c.asSlice()) == 0
}
func (c *collectorSet) Type() string {
return "map[string]struct{}"
}
type options struct {
inCluster bool
apiserver string
kubeconfig string
help bool
port int
collectors collectorSet
}
func main() {
// configure glog
flag.CommandLine.Parse([]string{})
flag.Lookup("logtostderr").Value.Set("true")
options := &options{collectors: make(collectorSet)}
flags := pflag.NewFlagSet("", pflag.ExitOnError)
flags.BoolVar(&options.inCluster, "in-cluster", true, `If true, use the built in kubernetes cluster for creating the client`)
flags.StringVar(&options.apiserver, "apiserver", "", `The URL of the apiserver to use as a master`)
flags.StringVar(&options.kubeconfig, "kubeconfig", "", "Absolute path to the kubeconfig file")
flags.BoolVarP(&options.help, "help", "h", false, "Print help text")
flags.IntVar(&options.port, "port", 80, `Port to expose metrics on.`)
flags.Var(&options.collectors, "collectors", "Collectors to be enabled")
flags.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flags.PrintDefaults()
}
err := flags.Parse(os.Args)
if err != nil {
glog.Fatalf("Error: %s", err)
}
if options.help {
flags.Usage()
os.Exit(0)
}
var collectors collectorSet
if len(options.collectors) == 0 {
glog.Infof("Using default collectors")
collectors = defaultCollectors
} else {
collectors = options.collectors
}
if isNotExists(options.kubeconfig) && !(options.inCluster) {
glog.Fatalf("kubeconfig invalid and --in-cluster is false; kubeconfig must be set to a valid file(kubeconfig default file name: $HOME/.kube/config)")
}
if options.apiserver != "" {
glog.Infof("apiserver set to: %v", options.apiserver)
}
proc.StartReaper()
kubeClient, err := createKubeClient(options.inCluster, options.apiserver, options.kubeconfig)
if err != nil {
glog.Fatalf("Failed to create client: %v", err)
}
registry := prometheus.NewRegistry()
registerCollectors(registry, kubeClient, collectors)
metricsServer(registry, options.port)
}
func isNotExists(file string) bool {
if file == "" {
file = clientcmd.NewDefaultClientConfigLoadingRules().GetDefaultFilename()
}
_, err := os.Stat(file)
return os.IsNotExist(err)
}
func createKubeClient(inCluster bool, apiserver string, kubeconfig string) (kubeClient clientset.Interface, err error) {
if inCluster {
config, err := rest.InClusterConfig()
if err != nil {
return nil, err
}
// Allow overriding of apiserver even if using inClusterConfig
// (necessary if kube-proxy isn't properly set up).
if apiserver != "" {
config.Host = apiserver
}
tokenPresent := false
if len(config.BearerToken) > 0 {
tokenPresent = true
}
glog.Infof("service account token present: %v", tokenPresent)
glog.Infof("service host: %s", config.Host)
if kubeClient, err = clientset.NewForConfig(config); err != nil {
return nil, err
}
} else {
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
// if you want to change the loading rules (which files in which order), you can do so here
loadingRules.ExplicitPath = kubeconfig
configOverrides := &clientcmd.ConfigOverrides{}
// if you want to change override values or bind them to flags, there are methods to help you
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
config, err := kubeConfig.ClientConfig()
//config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
//config, err := clientcmd.DefaultClientConfig.ClientConfig()
if err != nil {
return nil, err
}
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")
_, err = kubeClient.Discovery().ServerVersion()
if err != nil {
return nil, fmt.Errorf("ERROR communicating with apiserver: %v", err)
}
glog.Infof("Communication with server successful")
return kubeClient, nil
}
func metricsServer(registry prometheus.Gatherer, port int) {
// Address to listen on for web interface and telemetry
listenAddress := fmt.Sprintf(":%d", port)
glog.Infof("Starting metrics server: %s", listenAddress)
// Add metricsPath
http.Handle(metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
// Add healthzPath
http.HandleFunc(healthzPath, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("ok"))
})
// Add index
http.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, nil))
}
// registerCollectors creates and starts informers and initializes and
// registers metrics for collection.
func registerCollectors(registry prometheus.Registerer, kubeClient clientset.Interface, enabledCollectors collectorSet) {
activeCollectors := []string{}
for c, _ := range enabledCollectors {
f, ok := availableCollectors[c]
if ok {
f(registry, kubeClient)
activeCollectors = append(activeCollectors, c)
}
}
glog.Infof("Active collectors: %s", strings.Join(activeCollectors, ","))
}