-
Notifications
You must be signed in to change notification settings - Fork 456
/
prometheus.go
193 lines (175 loc) · 6 KB
/
prometheus.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
// Copyright (C) 2022, MinIO, Inc.
//
// This code is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License, version 3,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License, version 3,
// along with this program. If not, see <http://www.gnu.org/licenses/>
package controller
import (
"context"
"errors"
promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"
miniov2 "github.com/minio/operator/pkg/apis/minio.min.io/v2"
"github.com/minio/operator/pkg/resources/configmaps"
)
// MinIOPrometheusMetrics holds metrics pulled from prometheus
type MinIOPrometheusMetrics struct {
UsableCapacity int64
Usage int64
}
func (c *Controller) getPrometheus(ctx context.Context) (*promv1.Prometheus, error) {
ns := miniov2.GetPrometheusNamespace()
promName := miniov2.GetPrometheusName()
var p *promv1.Prometheus
var err error
if promName != "" {
p, err = c.promClient.MonitoringV1().Prometheuses(ns).Get(ctx, promName, metav1.GetOptions{})
if err != nil {
return nil, err
}
} else {
pList, err := c.promClient.MonitoringV1().Prometheuses(ns).List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
if len(pList.Items) == 0 {
return nil, errors.New("No prometheus found on namespace " + ns)
}
if len(pList.Items) > 1 {
return nil, errors.New("More than 1 prometheus found on namespace " + ns + ". PROMETHEUS_NAME not specified.")
}
p = pList.Items[0]
}
return p, nil
}
func (c *Controller) checkAndCreatePrometheusAddlConfig(ctx context.Context, tenant *miniov2.Tenant, accessKey, secretKey string) error {
ns := miniov2.GetPrometheusNamespace()
p, err := c.getPrometheus(ctx)
if err != nil {
return err
}
// If the additional scrape config is set to something else, we will error out
if p.Spec.AdditionalScrapeConfigs != nil && p.Spec.AdditionalScrapeConfigs.Name != miniov2.PrometheusAddlScrapeConfigSecret {
return errors.New(p.Spec.AdditionalScrapeConfigs.Name + " is alreay set as additional scrape config in prometheus")
}
secret, err := c.kubeClientSet.CoreV1().Secrets(ns).Get(ctx, miniov2.PrometheusAddlScrapeConfigSecret, metav1.GetOptions{})
if err != nil && !k8serrors.IsNotFound(err) {
return err
}
promCfg := configmaps.GetPrometheusConfig(tenant, accessKey, secretKey)
// If the secret is not found, create the secret
if k8serrors.IsNotFound(err) {
klog.Infof("Adding MinIO tenant Prometheus scrape config")
scrapeCfgYaml, err := yaml.Marshal(&promCfg.ScrapeConfigs)
if err != nil {
return err
}
secret = &corev1.Secret{
Type: "Opaque",
ObjectMeta: metav1.ObjectMeta{
Name: miniov2.PrometheusAddlScrapeConfigSecret,
Namespace: ns,
},
Data: map[string][]byte{
miniov2.PrometheusAddlScrapeConfigKey: scrapeCfgYaml,
},
}
_, err = c.kubeClientSet.CoreV1().Secrets(ns).Create(ctx, secret, metav1.CreateOptions{})
if err != nil {
return err
}
} else {
var scrapeConfigs []configmaps.ScrapeConfig
err := yaml.Unmarshal(secret.Data[miniov2.PrometheusAddlScrapeConfigKey], &scrapeConfigs)
if err != nil {
return err
}
// Check if the scrape config is already present
hasScrapeConfig := false
for _, sc := range scrapeConfigs {
if sc.JobName == tenant.PrometheusOperatorAddlConfigJobName() {
hasScrapeConfig = true
break
}
}
if !hasScrapeConfig {
klog.Infof("Adding MinIO tenant Prometheus scrape config")
scrapeConfigs = append(scrapeConfigs, promCfg.ScrapeConfigs...)
scrapeCfgYaml, err := yaml.Marshal(scrapeConfigs)
if err != nil {
return err
}
secret.Data[miniov2.PrometheusAddlScrapeConfigKey] = scrapeCfgYaml
_, err = c.kubeClientSet.CoreV1().Secrets(ns).Update(ctx, secret, metav1.UpdateOptions{})
if err != nil {
return err
}
}
}
// Update prometheus if it's not done alreay
if p.Spec.AdditionalScrapeConfigs == nil {
p.Spec.AdditionalScrapeConfigs = &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{Name: miniov2.PrometheusAddlScrapeConfigSecret},
Key: miniov2.PrometheusAddlScrapeConfigKey,
}
_, err = c.promClient.MonitoringV1().Prometheuses(ns).Update(ctx, p, metav1.UpdateOptions{})
if err != nil {
return err
}
}
return nil
}
func (c *Controller) deletePrometheusAddlConfig(ctx context.Context, tenant *miniov2.Tenant) error {
ns := miniov2.GetPrometheusNamespace()
secret, err := c.kubeClientSet.CoreV1().Secrets(ns).Get(ctx, miniov2.PrometheusAddlScrapeConfigSecret, metav1.GetOptions{})
// If the secret is not found, return from here
if k8serrors.IsNotFound(err) {
return nil
}
if err != nil {
return err
}
var scrapeConfigs []configmaps.ScrapeConfig
err = yaml.Unmarshal(secret.Data[miniov2.PrometheusAddlScrapeConfigKey], &scrapeConfigs)
if err != nil {
return err
}
// Check if the scrape config is present
hasScrapeConfig := false
scIndex := -1
for i, sc := range scrapeConfigs {
if sc.JobName == tenant.PrometheusOperatorAddlConfigJobName() {
hasScrapeConfig = true
scIndex = i
break
}
}
if hasScrapeConfig {
klog.Infof("Deleting MinIO tenant Prometheus scrape config")
// Delete the config
newScrapeConfigs := append(scrapeConfigs[:scIndex], scrapeConfigs[scIndex+1:]...)
// Update the secret
scrapeCfgYaml, err := yaml.Marshal(newScrapeConfigs)
if err != nil {
return err
}
secret.Data[miniov2.PrometheusAddlScrapeConfigKey] = scrapeCfgYaml
_, err = c.kubeClientSet.CoreV1().Secrets(ns).Update(ctx, secret, metav1.UpdateOptions{})
if err != nil {
return err
}
}
return nil
}