-
Notifications
You must be signed in to change notification settings - Fork 24
/
cluster.go
86 lines (73 loc) · 1.82 KB
/
cluster.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
package icinga
import (
api "github.com/appscode/searchlight/apis/monitoring/v1alpha1"
)
type ClusterHost struct {
commonHost
}
func NewClusterHost(IcingaClient *Client, verbosity string) *ClusterHost {
return &ClusterHost{
commonHost: commonHost{
IcingaClient: IcingaClient,
verbosity: verbosity,
},
}
}
func (h *ClusterHost) getHost(namespace string) IcingaHost {
return IcingaHost{
Type: TypeCluster,
AlertNamespace: namespace,
IP: "127.0.0.1",
}
}
func (h *ClusterHost) Apply(alert *api.ClusterAlert) error {
alertSpec := alert.Spec
kh := h.getHost(alert.Namespace)
if err := h.reconcileIcingaHost(kh); err != nil {
return err
}
has, err := h.checkIcingaService(alert.Name, kh)
if err != nil {
return err
}
if alertSpec.Paused {
if has {
if err := h.deleteIcingaService(alert.Name, kh); err != nil {
return err
}
}
return nil
}
attrs := make(map[string]interface{})
if alertSpec.CheckInterval.Seconds() > 0 {
attrs["check_interval"] = alertSpec.CheckInterval.Seconds()
}
cmd, _ := api.ClusterCommands.Get(alertSpec.Check)
commandVars := cmd.Vars.Fields
for key, val := range alertSpec.Vars {
if _, found := commandVars[key]; found {
attrs[IVar(key)] = val
}
}
if !has {
attrs["check_command"] = alertSpec.Check
if err := h.createIcingaService(alert.Name, kh, attrs); err != nil {
return err
}
} else {
if err := h.updateIcingaService(alert.Name, kh, attrs); err != nil {
return err
}
}
return h.reconcileIcingaNotification(alert, kh)
}
func (h *ClusterHost) Delete(namespace, name string) error {
kh := h.getHost(namespace)
if err := h.deleteIcingaService(name, kh); err != nil {
return err
}
return h.deleteIcingaHost(kh)
}
func (h *ClusterHost) DeleteChecks(cmd string) error {
return h.deleteIcingaServiceForCheckCommand(cmd)
}