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 pathdeploymentconfig.go
262 lines (205 loc) · 9.76 KB
/
deploymentconfig.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
/*
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.
*/
package main
import (
"time"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/net/context"
/*"k8s.io/api/core/v1"*/
"k8s.io/client-go/rest"
"k8s.io/apimachinery/pkg/fields"
/*"k8s.io/client-go/kubernetes"*/
"k8s.io/client-go/tools/cache"
deploymentconfigv1meta "github.com/openshift/api/apps/v1"
deploymentconfigv1clientset "github.com/openshift/client-go/apps/clientset/versioned"
/*deploymentconfigv1client "github.com/openshift/client-go/deploymentconfig/clientset/versioned/typed/deploymentconfig/v1"*/
/*v1meta "k8s.io/apimachinery/pkg/apis/meta/v1"*/
/* "k8s.io/apimachinery/pkg/runtime"*/
"k8s.io/apimachinery/pkg/util/intstr"
/* for Dump of structs */
"github.com/davecgh/go-spew/spew"
)
var (
descDeploymentConfigLabelsName = "oapi_deploymentconfig_labels"
descDeploymentConfigLabelsHelp = "DeploymentConfig labels converted to Prometheus labels."
descDeploymentConfigLabelsDefaultLabels = []string{"namespace", "deploymentconfig"}
descDeploymentConfigCreated = prometheus.NewDesc(
"oapi_deploymentconfig_created",
"Unix creation timestamp of DeploymentConfig",
[]string{"namespace", "deploymentconfig"}, nil,
)
descDeploymentConfigStatusReplicas = prometheus.NewDesc(
"oapi_deploymentconfig_status_replicas",
"The number of replicas per DeploymentConfig.",
[]string{"namespace", "deploymentconfig"}, nil,
)
descDeploymentConfigStatusReplicasAvailable = prometheus.NewDesc(
"oapi_deploymentconfig_status_replicas_available",
"The number of available replicas per DeploymentConfig.",
[]string{"namespace", "deploymentconfig"}, nil,
)
descDeploymentConfigStatusReplicasUnavailable = prometheus.NewDesc(
"oapi_deploymentconfig_status_replicas_unavailable",
"The number of unavailable replicas per DeploymentConfig.",
[]string{"namespace", "deploymentconfig"}, nil,
)
descDeploymentConfigStatusReplicasUpdated = prometheus.NewDesc(
"oapi_deploymentconfig_status_replicas_updated",
"The number of updated replicas per DeploymentConfig.",
[]string{"namespace", "deploymentconfig"}, nil,
)
descDeploymentConfigStatusObservedGeneration = prometheus.NewDesc(
"oapi_deploymentconfig_status_observed_generation",
"The generation observed by the deployment replication controller.",
[]string{"namespace", "deploymentconfig"}, nil,
)
descDeploymentConfigSpecReplicas = prometheus.NewDesc(
"oapi_deploymentconfig_spec_replicas",
"Number of desired pods for a DeploymentConfig.",
[]string{"namespace", "deploymentconfig"}, nil,
)
descDeploymentConfigSpecPaused = prometheus.NewDesc(
"oapi_deploymentconfig_spec_paused",
"Whether the deployment config is paused and will not be processed by the replication controller.",
[]string{"namespace", "deploymentconfig"}, nil,
)
descDeploymentConfigStrategyRollingUpdateMaxUnavailable = prometheus.NewDesc(
"oapi_deploymentconfig_spec_strategy_rollingupdate_max_unavailable",
"Maximum number of unavailable replicas during a rolling update of a deployment config.",
[]string{"namespace", "deploymentconfig"}, nil,
)
descDeploymentStrategyRollingUpdateMaxSurge = prometheus.NewDesc(
"oapi_deploymentconfig_spec_strategy_rollingupdate_max_surge",
"Maximum number of replicas that can be scheduled above the desired number of replicas during a rolling update of a deployment config.",
[]string{"namespace", "deploymentconfig"}, nil,
)
descDeploymentConfigMetadataGeneration = prometheus.NewDesc(
"oapi_deploymentconfig_metadata_generation",
"Sequence number representing a specific generation of the desired state.",
[]string{"namespace", "deploymentconfig"}, nil,
)
descDeploymentConfigLabels = prometheus.NewDesc(
descDeploymentConfigLabelsName,
descDeploymentConfigLabelsHelp,
descDeploymentConfigLabelsDefaultLabels, nil,
)
)
type DeploymentConfigLister func() ([]deploymentconfigv1meta.DeploymentConfig, error)
func (l DeploymentConfigLister) List() ([]deploymentconfigv1meta.DeploymentConfig, error) {
return l()
}
func RegisterDeploymentConfigCollectorOApi(registry prometheus.Registerer, kubeConfig *rest.Config, namespace string) {
/* NOTE: appliedclusterresourcequata does not support watch and select by all namespaces*/
/* Note: OAPI only provides very specifiy clientsets */
deploymentconfigClient, err := deploymentconfigv1clientset.NewForConfig(kubeConfig)
if err != nil {
glog.Fatalf("Failed to access deploymentconfigs api: %v", err)
}
resyncPeriod, _ := time.ParseDuration("0h0m30s")
client := deploymentconfigClient.AppsV1().RESTClient()
rqlw := cache.NewListWatchFromClient(client, "deploymentconfigs", namespace, fields.Everything())
rqinf := cache.NewSharedInformer(rqlw, &deploymentconfigv1meta.DeploymentConfig{}, resyncPeriod)
deploymentConfigLister := DeploymentConfigLister(func() (deploymentconfigs []deploymentconfigv1meta.DeploymentConfig, err error) {
for _, dc := range rqinf.GetStore().List() {
deploymentconfigs = append(deploymentconfigs, *(dc.(*deploymentconfigv1meta.DeploymentConfig)))
}
return deploymentconfigs, nil
})
registry.MustRegister(&deploymentConfigCollector{store: deploymentConfigLister})
go rqinf.Run(context.Background().Done())
}
type deploymentConfigStore interface {
List() ([]deploymentconfigv1meta.DeploymentConfig, error)
}
// deploymentConfigCollector collects metrics about all resource deploymentconfigs in the cluster.
type deploymentConfigCollector struct {
store deploymentConfigStore
}
// Describe implements the prometheus.Collector interface.
func (dc *deploymentConfigCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- descDeploymentConfigCreated
ch <- descDeploymentConfigStatusReplicas
ch <- descDeploymentConfigStatusReplicasAvailable
ch <- descDeploymentConfigStatusReplicasUnavailable
ch <- descDeploymentConfigStatusReplicasUpdated
ch <- descDeploymentConfigStatusObservedGeneration
ch <- descDeploymentConfigSpecPaused
ch <- descDeploymentConfigStrategyRollingUpdateMaxUnavailable
ch <- descDeploymentStrategyRollingUpdateMaxSurge
ch <- descDeploymentConfigSpecReplicas
ch <- descDeploymentConfigMetadataGeneration
ch <- descDeploymentConfigLabels
}
// Collect implements the prometheus.Collector interface.
func (dc *deploymentConfigCollector) Collect(ch chan<- prometheus.Metric) {
/* collect metrics for execution times */
start := time.Now()
ds, err := dc.store.List()
if err != nil {
glog.Errorf("listing deployments failed: %s", err)
return
}
for _, d := range ds {
dc.collectDeploymentConfig(ch, d)
}
duration := time.Since(start)
ScrapeDurationHistogram.WithLabelValues("deploymentconfig").Observe(duration.Seconds())
ResourcesPerScrapeMetric.With(prometheus.Labels{"resource": "deploymentconfig"}).Observe(float64(len(ds)))
glog.Infof("collected %d deployments", len(ds))
}
func deploymentLabelsDesc(labelKeys []string) *prometheus.Desc {
return prometheus.NewDesc(
descDeploymentConfigLabelsName,
descDeploymentConfigLabelsHelp,
append(descDeploymentConfigLabelsDefaultLabels, labelKeys...),
nil,
)
}
func (dc *deploymentConfigCollector) collectDeploymentConfig(ch chan<- prometheus.Metric, d deploymentconfigv1meta.DeploymentConfig) {
addGauge := func(desc *prometheus.Desc, v float64, lv ...string) {
lv = append([]string{d.Namespace, d.Name}, lv...)
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, v, lv...)
}
labelKeys, labelValues := kubeLabelsToPrometheusLabels(d.Labels)
addGauge(deploymentLabelsDesc(labelKeys), 1, labelValues...)
if !d.CreationTimestamp.IsZero() {
addGauge(descDeploymentConfigCreated, float64(d.CreationTimestamp.Unix()))
}
addGauge(descDeploymentConfigStatusReplicas, float64(d.Status.Replicas))
addGauge(descDeploymentConfigStatusReplicasAvailable, float64(d.Status.AvailableReplicas))
addGauge(descDeploymentConfigStatusReplicasUnavailable, float64(d.Status.UnavailableReplicas))
addGauge(descDeploymentConfigStatusReplicasUpdated, float64(d.Status.UpdatedReplicas))
addGauge(descDeploymentConfigStatusObservedGeneration, float64(d.Status.ObservedGeneration))
addGauge(descDeploymentConfigSpecPaused, boolFloat64(d.Spec.Paused))
addGauge(descDeploymentConfigSpecReplicas, float64(d.Spec.Replicas))
addGauge(descDeploymentConfigMetadataGeneration, float64(d.ObjectMeta.Generation))
if (false) {
spew.Dump(d.Spec.Strategy)
}
if (d.Spec.Strategy.RollingParams != nil) {
dcStratParams := d.Spec.Strategy.RollingParams
maxUnavailable, err := intstr.GetValueFromIntOrPercent(dcStratParams.MaxUnavailable, int(d.Spec.Replicas), true)
if err != nil {
glog.Errorf("Error converting RollingUpdate MaxSurge to int: %s", err)
} else {
addGauge(descDeploymentConfigStrategyRollingUpdateMaxUnavailable, float64(maxUnavailable))
}
maxSurge, err := intstr.GetValueFromIntOrPercent(dcStratParams.MaxSurge, int(d.Spec.Replicas), true)
if err != nil {
glog.Errorf("Error converting RollingUpdate MaxSurge to int: %s", err)
} else {
addGauge(descDeploymentStrategyRollingUpdateMaxSurge, float64(maxSurge))
}
}
}