-
Notifications
You must be signed in to change notification settings - Fork 2k
/
builder.go
264 lines (214 loc) · 10.4 KB
/
builder.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
/*
Copyright 2018 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 store
import (
"sort"
"strings"
"k8s.io/klog"
"golang.org/x/net/context"
appsv1 "k8s.io/api/apps/v1"
autoscaling "k8s.io/api/autoscaling/v2beta1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
certv1beta1 "k8s.io/api/certificates/v1beta1"
v1 "k8s.io/api/core/v1"
extensions "k8s.io/api/extensions/v1beta1"
policy "k8s.io/api/policy/v1beta1"
storagev1 "k8s.io/api/storage/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/kube-state-metrics/pkg/metric"
metricsstore "k8s.io/kube-state-metrics/pkg/metrics_store"
"k8s.io/kube-state-metrics/pkg/options"
)
type whiteBlackLister interface {
IsIncluded(string) bool
IsExcluded(string) bool
}
// Builder helps to build store. It follows the builder pattern
// (https://en.wikipedia.org/wiki/Builder_pattern).
type Builder struct {
kubeClient clientset.Interface
namespaces options.NamespaceList
ctx context.Context
enabledResources []string
whiteBlackList whiteBlackLister
}
// NewBuilder returns a new builder.
func NewBuilder(
ctx context.Context,
) *Builder {
return &Builder{
ctx: ctx,
}
}
// WithEnabledResources sets the enabledResources property of a Builder.
func (b *Builder) WithEnabledResources(c []string) {
var copy []string
copy = append(copy, c...)
sort.Strings(copy)
b.enabledResources = copy
}
// WithNamespaces sets the namespaces property of a Builder.
func (b *Builder) WithNamespaces(n options.NamespaceList) {
b.namespaces = n
}
// WithKubeClient sets the kubeClient property of a Builder.
func (b *Builder) WithKubeClient(c clientset.Interface) {
b.kubeClient = c
}
// WithWhiteBlackList configures the white or blacklisted metric to be exposed
// by the store build by the Builder.
func (b *Builder) WithWhiteBlackList(l whiteBlackLister) {
b.whiteBlackList = l
}
// Build initializes and registers all enabled stores.
func (b *Builder) Build() []*metricsstore.MetricsStore {
if b.whiteBlackList == nil {
panic("whiteBlackList should not be nil")
}
stores := []*metricsstore.MetricsStore{}
activeStoreNames := []string{}
for _, c := range b.enabledResources {
constructor, ok := availableStores[c]
if ok {
store := constructor(b)
activeStoreNames = append(activeStoreNames, c)
stores = append(stores, store)
}
}
klog.Infof("Active collectors: %s", strings.Join(activeStoreNames, ","))
return stores
}
var availableStores = map[string]func(f *Builder) *metricsstore.MetricsStore{
"certificatesigningrequests": func(b *Builder) *metricsstore.MetricsStore { return b.buildCsrStore() },
"configmaps": func(b *Builder) *metricsstore.MetricsStore { return b.buildConfigMapStore() },
"cronjobs": func(b *Builder) *metricsstore.MetricsStore { return b.buildCronJobStore() },
"daemonsets": func(b *Builder) *metricsstore.MetricsStore { return b.buildDaemonSetStore() },
"deployments": func(b *Builder) *metricsstore.MetricsStore { return b.buildDeploymentStore() },
"endpoints": func(b *Builder) *metricsstore.MetricsStore { return b.buildEndpointsStore() },
"horizontalpodautoscalers": func(b *Builder) *metricsstore.MetricsStore { return b.buildHPAStore() },
"ingresses": func(b *Builder) *metricsstore.MetricsStore { return b.buildIngressStore() },
"jobs": func(b *Builder) *metricsstore.MetricsStore { return b.buildJobStore() },
"limitranges": func(b *Builder) *metricsstore.MetricsStore { return b.buildLimitRangeStore() },
"namespaces": func(b *Builder) *metricsstore.MetricsStore { return b.buildNamespaceStore() },
"nodes": func(b *Builder) *metricsstore.MetricsStore { return b.buildNodeStore() },
"persistentvolumeclaims": func(b *Builder) *metricsstore.MetricsStore { return b.buildPersistentVolumeClaimStore() },
"persistentvolumes": func(b *Builder) *metricsstore.MetricsStore { return b.buildPersistentVolumeStore() },
"poddisruptionbudgets": func(b *Builder) *metricsstore.MetricsStore { return b.buildPodDisruptionBudgetStore() },
"pods": func(b *Builder) *metricsstore.MetricsStore { return b.buildPodStore() },
"replicasets": func(b *Builder) *metricsstore.MetricsStore { return b.buildReplicaSetStore() },
"replicationcontrollers": func(b *Builder) *metricsstore.MetricsStore { return b.buildReplicationControllerStore() },
"resourcequotas": func(b *Builder) *metricsstore.MetricsStore { return b.buildResourceQuotaStore() },
"secrets": func(b *Builder) *metricsstore.MetricsStore { return b.buildSecretStore() },
"services": func(b *Builder) *metricsstore.MetricsStore { return b.buildServiceStore() },
"statefulsets": func(b *Builder) *metricsstore.MetricsStore { return b.buildStatefulSetStore() },
"storageclasses": func(b *Builder) *metricsstore.MetricsStore { return b.buildStorageClassStore() },
}
func (b *Builder) buildConfigMapStore() *metricsstore.MetricsStore {
return b.buildStore(configMapMetricFamilies, &v1.ConfigMap{}, createConfigMapListWatch)
}
func (b *Builder) buildCronJobStore() *metricsstore.MetricsStore {
return b.buildStore(cronJobMetricFamilies, &batchv1beta1.CronJob{}, createCronJobListWatch)
}
func (b *Builder) buildDaemonSetStore() *metricsstore.MetricsStore {
return b.buildStore(daemonSetMetricFamilies, &appsv1.DaemonSet{}, createDaemonSetListWatch)
}
func (b *Builder) buildDeploymentStore() *metricsstore.MetricsStore {
return b.buildStore(deploymentMetricFamilies, &appsv1.Deployment{}, createDeploymentListWatch)
}
func (b *Builder) buildEndpointsStore() *metricsstore.MetricsStore {
return b.buildStore(endpointMetricFamilies, &v1.Endpoints{}, createEndpointsListWatch)
}
func (b *Builder) buildHPAStore() *metricsstore.MetricsStore {
return b.buildStore(hpaMetricFamilies, &autoscaling.HorizontalPodAutoscaler{}, createHPAListWatch)
}
func (b *Builder) buildIngressStore() *metricsstore.MetricsStore {
return b.buildStore(ingressMetricFamilies, &extensions.Ingress{}, createIngressListWatch)
}
func (b *Builder) buildJobStore() *metricsstore.MetricsStore {
return b.buildStore(jobMetricFamilies, &batchv1.Job{}, createJobListWatch)
}
func (b *Builder) buildLimitRangeStore() *metricsstore.MetricsStore {
return b.buildStore(limitRangeMetricFamilies, &v1.LimitRange{}, createLimitRangeListWatch)
}
func (b *Builder) buildNamespaceStore() *metricsstore.MetricsStore {
return b.buildStore(namespaceMetricFamilies, &v1.Namespace{}, createNamespaceListWatch)
}
func (b *Builder) buildNodeStore() *metricsstore.MetricsStore {
return b.buildStore(nodeMetricFamilies, &v1.Node{}, createNodeListWatch)
}
func (b *Builder) buildPersistentVolumeClaimStore() *metricsstore.MetricsStore {
return b.buildStore(persistentVolumeClaimMetricFamilies, &v1.PersistentVolumeClaim{}, createPersistentVolumeClaimListWatch)
}
func (b *Builder) buildPersistentVolumeStore() *metricsstore.MetricsStore {
return b.buildStore(persistentVolumeMetricFamilies, &v1.PersistentVolume{}, createPersistentVolumeListWatch)
}
func (b *Builder) buildPodDisruptionBudgetStore() *metricsstore.MetricsStore {
return b.buildStore(podDisruptionBudgetMetricFamilies, &policy.PodDisruptionBudget{}, createPodDisruptionBudgetListWatch)
}
func (b *Builder) buildReplicaSetStore() *metricsstore.MetricsStore {
return b.buildStore(replicaSetMetricFamilies, &extensions.ReplicaSet{}, createReplicaSetListWatch)
}
func (b *Builder) buildReplicationControllerStore() *metricsstore.MetricsStore {
return b.buildStore(replicationControllerMetricFamilies, &v1.ReplicationController{}, createReplicationControllerListWatch)
}
func (b *Builder) buildResourceQuotaStore() *metricsstore.MetricsStore {
return b.buildStore(resourceQuotaMetricFamilies, &v1.ResourceQuota{}, createResourceQuotaListWatch)
}
func (b *Builder) buildSecretStore() *metricsstore.MetricsStore {
return b.buildStore(secretMetricFamilies, &v1.Secret{}, createSecretListWatch)
}
func (b *Builder) buildServiceStore() *metricsstore.MetricsStore {
return b.buildStore(serviceMetricFamilies, &v1.Service{}, createServiceListWatch)
}
func (b *Builder) buildStatefulSetStore() *metricsstore.MetricsStore {
return b.buildStore(statefulSetMetricFamilies, &appsv1.StatefulSet{}, createStatefulSetListWatch)
}
func (b *Builder) buildStorageClassStore() *metricsstore.MetricsStore {
return b.buildStore(storageClassMetricFamilies, &storagev1.StorageClass{}, createStorageClassListWatch)
}
func (b *Builder) buildPodStore() *metricsstore.MetricsStore {
return b.buildStore(podMetricFamilies, &v1.Pod{}, createPodListWatch)
}
func (b *Builder) buildCsrStore() *metricsstore.MetricsStore {
return b.buildStore(csrMetricFamilies, &certv1beta1.CertificateSigningRequest{}, createCSRListWatch)
}
func (b *Builder) buildStore(
metricFamilies []metric.FamilyGenerator,
expectedType interface{},
listWatchFunc func(kubeClient clientset.Interface, ns string) cache.ListerWatcher,
) *metricsstore.MetricsStore {
filteredMetricFamilies := metric.FilterMetricFamilies(b.whiteBlackList, metricFamilies)
composedMetricGenFuncs := metric.ComposeMetricGenFuncs(filteredMetricFamilies)
familyHeaders := metric.ExtractMetricFamilyHeaders(filteredMetricFamilies)
store := metricsstore.NewMetricsStore(
familyHeaders,
composedMetricGenFuncs,
)
b.reflectorPerNamespace(expectedType, store, listWatchFunc)
return store
}
// reflectorPerNamespace creates a Kubernetes client-go reflector with the given
// listWatchFunc for each given namespace and registers it with the given store.
func (b *Builder) reflectorPerNamespace(
expectedType interface{},
store cache.Store,
listWatchFunc func(kubeClient clientset.Interface, ns string) cache.ListerWatcher,
) {
for _, ns := range b.namespaces {
lw := listWatchFunc(b.kubeClient, ns)
reflector := cache.NewReflector(lw, expectedType, store, 0)
go reflector.Run(b.ctx.Done())
}
}