/
kubernetes.go
210 lines (182 loc) 路 7.61 KB
/
kubernetes.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
package k8sprometheus
import (
"context"
"time"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
monitoringclientset "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
monitoringclientsetfake "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/fake"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"github.com/slok/sloth/internal/log"
slothv1 "github.com/slok/sloth/pkg/kubernetes/api/sloth/v1"
slothclientset "github.com/slok/sloth/pkg/kubernetes/gen/clientset/versioned"
slothclientsetfake "github.com/slok/sloth/pkg/kubernetes/gen/clientset/versioned/fake"
)
type KubernetesService struct {
slothCli slothclientset.Interface
monitoringCli monitoringclientset.Interface
logger log.Logger
}
// NewKubernetesService returns a new Kubernetes Service.
func NewKubernetesService(slothCli slothclientset.Interface, monitoringCli monitoringclientset.Interface, logger log.Logger) KubernetesService {
return KubernetesService{
slothCli: slothCli,
monitoringCli: monitoringCli,
logger: logger.WithValues(log.Kv{"service": "k8sprometheus.Service"}),
}
}
func (k KubernetesService) ListPrometheusServiceLevels(ctx context.Context, ns string, opts metav1.ListOptions) (*slothv1.PrometheusServiceLevelList, error) {
return k.slothCli.SlothV1().PrometheusServiceLevels(ns).List(ctx, opts)
}
func (k KubernetesService) WatchPrometheusServiceLevels(ctx context.Context, ns string, opts metav1.ListOptions) (watch.Interface, error) {
return k.slothCli.SlothV1().PrometheusServiceLevels(ns).Watch(ctx, opts)
}
func (k KubernetesService) EnsurePrometheusRule(ctx context.Context, pr *monitoringv1.PrometheusRule) error {
logger := k.logger.WithCtxValues(ctx)
pr = pr.DeepCopy()
stored, err := k.monitoringCli.MonitoringV1().PrometheusRules(pr.Namespace).Get(ctx, pr.Name, metav1.GetOptions{})
if err != nil {
if !kubeerrors.IsNotFound(err) {
return err
}
_, err = k.monitoringCli.MonitoringV1().PrometheusRules(pr.Namespace).Create(ctx, pr, metav1.CreateOptions{})
if err != nil {
return err
}
logger.Debugf("monitoringv1.PrometheusRule has been created")
return nil
}
// Force overwrite.
pr.ObjectMeta.ResourceVersion = stored.ResourceVersion
_, err = k.monitoringCli.MonitoringV1().PrometheusRules(pr.Namespace).Update(ctx, pr, metav1.UpdateOptions{})
if err != nil {
return err
}
logger.Debugf("monitoringv1.PrometheusRule has been overwritten")
return nil
}
// EnsurePrometheusServiceLevelStatus updates the status of a PrometheusServiceLeve, be aware that updating
// an status will trigger a watch update event on a controller.
// In case of no error we will update "last correct Prometheus operation rules generated" TS so we can be in
// a infinite loop of handling, the handler should break this loop somehow (e.g: if ok and last generated < 5m, ignore).
func (k KubernetesService) EnsurePrometheusServiceLevelStatus(ctx context.Context, slo *slothv1.PrometheusServiceLevel, err error) error {
slo = slo.DeepCopy()
slo.Status.PromOpRulesGenerated = false
slo.Status.PromOpRulesGeneratedSLOs = 0
slo.Status.ProcessedSLOs = len(slo.Spec.SLOs)
slo.Status.ObservedGeneration = slo.Generation
if err == nil {
slo.Status.PromOpRulesGenerated = true
slo.Status.PromOpRulesGeneratedSLOs = len(slo.Spec.SLOs)
slo.Status.LastPromOpRulesSuccessfulGenerated = &metav1.Time{Time: time.Now().UTC()}
}
_, err = k.slothCli.SlothV1().PrometheusServiceLevels(slo.Namespace).UpdateStatus(ctx, slo, metav1.UpdateOptions{})
return err
}
type DryRunKubernetesService struct {
svc KubernetesService
logger log.Logger
}
// NewKubernetesServiceDryRun returns a new Kubernetes Service that will dry-run that will only do real ReadOnly operations.
func NewKubernetesServiceDryRun(svc KubernetesService, logger log.Logger) DryRunKubernetesService {
return DryRunKubernetesService{
svc: svc,
logger: logger.WithValues(log.Kv{"service": "k8sprometheus.DryRunService"}),
}
}
func (d DryRunKubernetesService) ListPrometheusServiceLevels(ctx context.Context, ns string, opts metav1.ListOptions) (*slothv1.PrometheusServiceLevelList, error) {
return d.svc.ListPrometheusServiceLevels(ctx, ns, opts)
}
func (d DryRunKubernetesService) WatchPrometheusServiceLevels(ctx context.Context, ns string, opts metav1.ListOptions) (watch.Interface, error) {
return d.svc.WatchPrometheusServiceLevels(ctx, ns, opts)
}
func (d DryRunKubernetesService) EnsurePrometheusRule(ctx context.Context, pr *monitoringv1.PrometheusRule) error {
d.logger.Infof("Dry run EnsurePrometheusRule")
return nil
}
func (d DryRunKubernetesService) EnsurePrometheusServiceLevelStatus(ctx context.Context, slo *slothv1.PrometheusServiceLevel, err error) error {
d.logger.Infof("Dry run EnsurePrometheusServiceLevelStatus")
return nil
}
type FakeKubernetesService struct {
ksvc KubernetesService
}
// NewKubernetesServiceFake returns a new Kubernetes Service that will fake Kubernetes operations
// using fake clients.
func NewKubernetesServiceFake(logger log.Logger) FakeKubernetesService {
return FakeKubernetesService{
ksvc: NewKubernetesService(
slothclientsetfake.NewSimpleClientset(prometheusServiceLevelFakes...),
monitoringclientsetfake.NewSimpleClientset(),
logger),
}
}
func (f FakeKubernetesService) ListPrometheusServiceLevels(ctx context.Context, ns string, opts metav1.ListOptions) (*slothv1.PrometheusServiceLevelList, error) {
return f.ksvc.ListPrometheusServiceLevels(ctx, ns, opts)
}
func (f FakeKubernetesService) WatchPrometheusServiceLevels(ctx context.Context, ns string, opts metav1.ListOptions) (watch.Interface, error) {
return f.ksvc.WatchPrometheusServiceLevels(ctx, ns, opts)
}
func (f FakeKubernetesService) EnsurePrometheusRule(ctx context.Context, pr *monitoringv1.PrometheusRule) error {
return f.ksvc.EnsurePrometheusRule(ctx, pr)
}
func (f FakeKubernetesService) EnsurePrometheusServiceLevelStatus(ctx context.Context, slo *slothv1.PrometheusServiceLevel, err error) error {
return f.ksvc.EnsurePrometheusServiceLevelStatus(ctx, slo, err)
}
var prometheusServiceLevelFakes = []runtime.Object{
&slothv1.PrometheusServiceLevel{
ObjectMeta: metav1.ObjectMeta{
Name: "fake01",
Labels: map[string]string{
"prometheus": "default",
},
},
Spec: slothv1.PrometheusServiceLevelSpec{
Service: "svc01",
Labels: map[string]string{
"globalk1": "globalv1",
},
SLOs: []slothv1.SLO{
{
Name: "slo01",
Objective: 99.9,
Labels: map[string]string{
"slo01k1": "slo01v1",
},
SLI: slothv1.SLI{Events: &slothv1.SLIEvents{
ErrorQuery: `sum(rate(http_request_duration_seconds_count{job="myservice",code=~"(5..|429)"}[{{.window}}]))`,
TotalQuery: `sum(rate(http_request_duration_seconds_count{job="myservice"}[{{.window}}]))`,
}},
Alerting: slothv1.Alerting{
Name: "myServiceAlert",
Labels: map[string]string{
"alert01k1": "alert01v1",
},
Annotations: map[string]string{
"alert02k1": "alert02v1",
},
PageAlert: slothv1.Alert{},
TicketAlert: slothv1.Alert{},
},
},
{
Name: "slo02",
Objective: 99.99,
SLI: slothv1.SLI{Raw: &slothv1.SLIRaw{
ErrorRatioQuery: `
sum(rate(http_request_duration_seconds_count{job="myservice2",code=~"(5..|429)"}[{{.window}}]))
/
sum(rate(http_request_duration_seconds_count{job="myservice2"}[{{.window}}]))
`,
}},
Alerting: slothv1.Alerting{
PageAlert: slothv1.Alert{Disable: true},
TicketAlert: slothv1.Alert{Disable: true},
},
},
},
},
},
}