This repository has been archived by the owner on May 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappliedclusterresourcequotas.go
380 lines (311 loc) · 11.9 KB
/
appliedclusterresourcequotas.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
Copyright 2016 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.
*/
/*
oc new-project my-project
oc annotate namespace my-project clusterquota=test
oc create clusterquota crq-test \
--project-annotation-selector clusterquota=test \
--hard pods=10 \
--hard secrets=20\
--hard memory=1Gi\
--hard "limits.memory"=2Gi\
--hard cpu=1G
oc create clusterquota crq-storage-label \
--project-label-selector quotalabel=test \
--hard "requests.storage"=10G
oc create quota rq-test \
--hard pods=10 \
--hard secrets=20\
--hard memory=1Gi\
--hard "limits.memory"=2Gi\
--hard cpu=1G
apiVersion: v1
items:
- apiVersion: quota.openshift.io/v1
kind: AppliedClusterResourceQuota
metadata:
creationTimestamp: 2019-06-08T08:52:37Z
name: crq-test
namespace: my-project
resourceVersion: "18064"
selfLink: /apis/quota.openshift.io/v1/namespaces/my-project/appliedclusterresourcequotas/crq-test
uid: c41318ca-89ca-11e9-824d-080027df2b71
spec:
quota:
hard:
cpu: 1G
limits.memory: 2Gi
memory: 1Gi
pods: "10"
secrets: "20"
selector:
annotations:
clusterquota: test
labels: null
status:
namespaces:
- namespace: my-project
status:
hard:
cpu: 1G
limits.memory: 2Gi
memory: 1Gi
pods: "10"
secrets: "20"
used:
cpu: "0"
limits.memory: "0"
memory: "0"
pods: "0"
secrets: "9"
total:
hard:
cpu: 1G
limits.memory: 2Gi
memory: 1Gi
pods: "10"
secrets: "20"
used:
cpu: "0"
limits.memory: "0"
memory: "0"
pods: "0"
secrets: "9"
kind: List
metadata:
resourceVersion: ""
selfLink: ""
[root@demo test]# oc create clusterquota crq-test --project-annotation-selector clusterquota=test --hard pods=10 --hard secrets=20 --hard memory=1Gi --hard "limits.memory"=2Gi --hard cpu=1G^C
[root@demo test]# oc create quota rq-test \
> --hard pods=10 \
> --hard secrets=20\
> --hard memory=1Gi\
> --hard "limits.memory"=2Gi\
> --hard cpu=1G
resourcequota/rq-test created
[root@demo test]# oc --loglevel=7 get resourcequota -o yaml
apiVersion: v1
items:
- apiVersion: v1
kind: ResourceQuota
metadata:
creationTimestamp: 2019-06-08T09:10:22Z
name: rq-test
namespace: my-project
resourceVersion: "19565"
selfLink: /api/v1/namespaces/my-project/resourcequotas/rq-test
uid: 3edd6af7-89cd-11e9-824d-080027df2b71
spec:
hard:
cpu: 1G
status:
hard:
cpu: 1G
used:
cpu: "0"
kind: List
metadata:
resourceVersion: ""
selfLink: ""
*/
package main
import (
"time"
"strings"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/rest"
kubeclientset "k8s.io/client-go/kubernetes"
quotav1meta "github.com/openshift/api/quota/v1"
quotav1clientset "github.com/openshift/client-go/quota/clientset/versioned"
v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"
/* for Dump of structs */
"github.com/davecgh/go-spew/spew"
)
var (
descAppliedClusterResourceQuotaCreated = prometheus.NewDesc(
"oapi_appliedclusterresourcequota_created",
"Unix creation timestamp of clusterresourcequota",
[]string{"clusterresourcequota", "namespace"}, nil,
)
descAppliedClusterResourceQuotaSelector = prometheus.NewDesc(
"oapi_appliedclusterresourcequota_selector",
"Selector of clusterresourcequota to determine the effected namespaces",
[]string{"clusterresourcequota","type","key","value"}, nil,
)
descAppliedClusterResourceQuota = prometheus.NewDesc(
"oapi_appliedclusterresourcequota",
"Information about resource requests and limits of appliedclusterresourcequota.",
[]string{
"clusterresourcequota",
"namespace",
"resource",
"type",
}, nil,
)
)
/*
type AppliedClusterResourceQuotaLister func() (quotav1meta.AppliedClusterResourceQuotaList, error)
func (l AppliedClusterResourceQuotaLister) List() (quotav1meta.AppliedClusterResourceQuotaList, error) {
return l()
}
func RegisterAppliedClusterResourceQuotaCollector(registry prometheus.Registerer, kubeClient quotav1clientset.Interface, namespace string) {
glog.Infof("collect appliedclusterresourcequota on demand")
_, err := kubeClient.QuotaV1().AppliedClusterResourceQuotas("dummyns").List(v1meta.ListOptions{})
if err != nil {
glog.Fatalf("Failed to access quotas api: %v", err)
}
resourceQuotaLister := AppliedClusterResourceQuotaLister(func() (quotas quotav1meta.AppliedClusterResourceQuotaList, err error) {
return (quotav1meta.AppliedClusterResourceQuotaList{}), nil
})
registry.MustRegister(&resourceQuotaCollector{store: resourceQuotaLister})
}
*/
func RegisterAppliedClusterResourceQuotaCollectorOApi(registry prometheus.Registerer, kubeConfig *rest.Config, namespace string) {
/* NOTE: appliedclusterresourcequata does not support watch and select by all namespaces*/
/* for retrieving the current namespace list */
kubeClient, err := kubeclientset.NewForConfig(kubeConfig)
if err != nil {
glog.Fatalf("Failed to access kube api: %v", err)
}
/* Note: OAPI only provides very specifiy clientsets */
quotaClient, err := quotav1clientset.NewForConfig(kubeConfig)
if err != nil {
glog.Fatalf("Failed to access quotas oapi: %v", err)
}
glog.Infof("collect appliedclusterresourcequotas on demand")
if (namespace == v1meta.NamespaceAll) {
glog.Infof("using appliedclusterresourcequotas for all namespace may be an performance issue. It is recommended to use clusterresourcequotas instead.")
}
m := make(map[string]int)
registry.MustRegister(&resourceQuotaCollector{quotaclientset: quotaClient, kubeclientset: kubeClient, namespace: namespace, m: m})
}
type resourceQuotaCollector struct {
namespace string
quotaclientset quotav1clientset.Interface
kubeclientset kubeclientset.Interface
m map[string]int
}
// Describe implements the prometheus.Collector interface.
func (rqc *resourceQuotaCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- descAppliedClusterResourceQuotaCreated
ch <- descAppliedClusterResourceQuotaSelector
ch <- descAppliedClusterResourceQuota
}
// Collect implements the prometheus.Collector interface.
func (rqc *resourceQuotaCollector) Collect(ch chan<- prometheus.Metric) {
/* NOTE: appliedclusterresourcequata does not support watch! */
quotaClient := rqc.quotaclientset
kubeClient := rqc.kubeclientset
//m := make(map[string]int)
rqc.m = make(map[string]int)
if rqc.namespace == v1meta.NamespaceAll {
/* collect metrics for execution times */
start := time.Now()
namespaceList, err := kubeClient.CoreV1().Namespaces().List(v1meta.ListOptions{})
if err != nil {
glog.Fatalf("Failed to list namespaces: %v", err)
}
duration := time.Since(start)
ScrapeDurationHistogram.WithLabelValues("appliedclusterresourcequotas ns list").Observe(duration.Seconds())
ResourcesPerScrapeMetric.With(prometheus.Labels{"resource": "appliedclusterresourcequotas ns list"}).Observe(float64(len(namespaceList.Items)))
start = time.Now()
var ns_count int
ns_count = 0
for _, ns := range namespaceList.Items {
resourceQuota, err := quotaClient.QuotaV1().AppliedClusterResourceQuotas(ns.Name).List(v1meta.ListOptions{})
if err != nil {
glog.Fatalf("Failed to read quotas: %v", err)
}
for _, rq := range resourceQuota.Items {
rqc.collectAppliedClusterResourceQuota(ch, rq)
}
ns_count = ns_count + len(resourceQuota.Items)
}
duration = time.Since(start)
ScrapeDurationHistogram.WithLabelValues("appliedclusterresourcequotas").Observe(duration.Seconds())
ResourcesPerScrapeMetric.With(prometheus.Labels{"resource": "appliedclusterresourcequotas"}).Observe(float64(ns_count))
glog.Infof("collected %d appliedclusterresourcequotas for %s", ns_count , "all namespaces")
} else {
/* collect metrics for execution times */
start := time.Now()
resourceQuota, err := quotaClient.QuotaV1().AppliedClusterResourceQuotas(rqc.namespace).List(v1meta.ListOptions{})
if err != nil {
glog.Fatalf("Failed to read quotas: %v", err)
}
for _, rq := range resourceQuota.Items {
rqc.collectAppliedClusterResourceQuota(ch, rq)
}
duration := time.Since(start)
ScrapeDurationHistogram.WithLabelValues("appliedclusterresourcequotas").Observe(duration.Seconds())
ResourcesPerScrapeMetric.With(prometheus.Labels{"resource": "appliedclusterresourcequotas"}).Observe(float64(len(resourceQuota.Items)))
glog.Infof("collected %d appliedclusterresourcequotas for %s", len(resourceQuota.Items), rqc.namespace)
}
}
func (rqc *resourceQuotaCollector) collectAppliedClusterResourceQuota(ch chan<- prometheus.Metric, rql quotav1meta.AppliedClusterResourceQuota) {
//glog.Infof("m before %s", rpc.m)
for _, rq := range rql.Status.Namespaces {
if (rqc.namespace == rq.Namespace || rqc.namespace == v1meta.NamespaceAll) {
// only include metrics from selected namespaces:
_, ok := rqc.m[strings.Join([]string{rql.Name, rq.Namespace},"/")]
if !(ok) {
rqc.m[strings.Join([]string{rql.Name, rq.Namespace},"/")] = 1
addGauge := func(desc *prometheus.Desc, v float64, lv ...string) {
lv = append([]string{rql.Name, rq.Namespace}, lv...)
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, lv...)
}
if !rql.CreationTimestamp.IsZero() {
addGauge(descAppliedClusterResourceQuotaCreated, float64(rql.CreationTimestamp.Unix()))
}
for res, qty := range rq.Status.Hard {
addGauge(descAppliedClusterResourceQuota, float64(qty.MilliValue())/1000, string(res), "hard")
}
for res, qty := range rq.Status.Used {
addGauge(descAppliedClusterResourceQuota, float64(qty.MilliValue())/1000, string(res), "used")
}
}
}
}
_, ok := rqc.m[strings.Join([]string{rql.Name, ""},"/")]
if !(ok) {
rqc.m[strings.Join([]string{rql.Name, ""},"/")] = 1
rqTotal := rql.Status.Total
addGauge := func(desc *prometheus.Desc, v float64, lv ...string) {
lv = append([]string{rql.Name, ""}, lv...)
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, lv...)
}
if !rql.CreationTimestamp.IsZero() {
addGauge(descAppliedClusterResourceQuotaCreated, float64(rql.CreationTimestamp.Unix()))
}
for res, qty := range rqTotal.Hard {
addGauge(descAppliedClusterResourceQuota, float64(qty.MilliValue())/1000, string(res), "hard")
}
for res, qty := range rqTotal.Used {
addGauge(descAppliedClusterResourceQuota, float64(qty.MilliValue())/1000, string(res), "used")
}
sel := rql.Spec.Selector
if (false) {spew.Dump(sel)}
for key, value := range sel.AnnotationSelector {
lv := append([]string{rql.Name, "annotation", key, value})
ch <- prometheus.MustNewConstMetric(descAppliedClusterResourceQuotaSelector, prometheus.GaugeValue, 1, lv...)
}
if (sel.LabelSelector != nil) {
labelMap := (make(map[string]string))
v1meta.Convert_v1_LabelSelector_To_Map_string_To_string(sel.LabelSelector,&labelMap,nil)
for key, value := range labelMap {
lv := append([]string{rql.Name, "label", key, value})
ch <- prometheus.MustNewConstMetric(descAppliedClusterResourceQuotaSelector, prometheus.GaugeValue, 1, lv...)
}
}
}
}