This repository was archived by the owner on Jul 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathservicemonitor.go
More file actions
112 lines (97 loc) · 3.82 KB
/
servicemonitor.go
File metadata and controls
112 lines (97 loc) · 3.82 KB
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
// Copyright 2019 Gravitational Inc.
//
// 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 rigging
import (
"context"
"github.com/gravitational/trace"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
monitoring "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// NewServiceMonitorControl returns new instance of ServiceMonitor updater
func NewServiceMonitorControl(config ServiceMonitorConfig) (*ServiceMonitorControl, error) {
err := config.CheckAndSetDefaults()
if err != nil {
return nil, trace.Wrap(err)
}
return &ServiceMonitorControl{
ServiceMonitorConfig: config,
FieldLogger: log.WithFields(log.Fields{
"serviceMonitor": formatMeta(config.ObjectMeta),
}),
}, nil
}
// ServiceMonitorConfig is a ServiceMonitor control configuration
type ServiceMonitorConfig struct {
// ServiceMonitor is the parsed ServiceMonitor resource
*monitoringv1.ServiceMonitor
// Client is monitoring API client
Client *monitoring.Clientset
}
// CheckAndSetDefaults validates the config
func (c *ServiceMonitorConfig) CheckAndSetDefaults() error {
if c.ServiceMonitor == nil {
return trace.BadParameter("missing parameter ServiceMonitor")
}
if c.Client == nil {
return trace.BadParameter("missing parameter Client")
}
updateTypeMetaServiceMonitor(c.ServiceMonitor)
return nil
}
// ServiceMonitorControl a controller for ServiceMonitor resources
type ServiceMonitorControl struct {
ServiceMonitorConfig
log.FieldLogger
}
func (c *ServiceMonitorControl) Delete(ctx context.Context, cascade bool) error {
c.Infof("delete %v", formatMeta(c.ServiceMonitor.ObjectMeta))
err := c.Client.MonitoringV1().ServiceMonitors(c.ServiceMonitor.Namespace).Delete(ctx, c.ServiceMonitor.Name, metav1.DeleteOptions{})
return ConvertError(err)
}
func (c *ServiceMonitorControl) Upsert(ctx context.Context) error {
c.Infof("upsert %v", formatMeta(c.ServiceMonitor.ObjectMeta))
serviceMonitorsClient := c.Client.MonitoringV1().ServiceMonitors(c.ServiceMonitor.Namespace)
c.ServiceMonitor.UID = ""
c.ServiceMonitor.SelfLink = ""
c.ServiceMonitor.ResourceVersion = ""
currentServiceMonitor, err := serviceMonitorsClient.Get(ctx, c.ServiceMonitor.Name, metav1.GetOptions{})
err = ConvertError(err)
if err != nil {
if !trace.IsNotFound(err) {
return trace.Wrap(err)
}
_, err = serviceMonitorsClient.Create(ctx, c.ServiceMonitor, metav1.CreateOptions{})
return ConvertError(err)
}
if checkCustomerManagedResource(currentServiceMonitor.Annotations) {
c.WithField("servicemonitor", formatMeta(c.ObjectMeta)).Info("Skipping update since object is customer managed.")
return nil
}
c.ServiceMonitor.ResourceVersion = currentServiceMonitor.ResourceVersion
_, err = serviceMonitorsClient.Update(ctx, c.ServiceMonitor, metav1.UpdateOptions{})
return ConvertError(err)
}
func (c *ServiceMonitorControl) Status(ctx context.Context) error {
client := c.Client.MonitoringV1().ServiceMonitors(c.ServiceMonitor.Namespace)
_, err := client.Get(ctx, c.ServiceMonitor.Name, metav1.GetOptions{})
return ConvertError(err)
}
func updateTypeMetaServiceMonitor(monitor *monitoringv1.ServiceMonitor) {
monitor.Kind = KindServiceMonitor
if monitor.APIVersion == "" {
monitor.APIVersion = monitoringv1.SchemeGroupVersion.String()
}
}