/
pingdom.go
137 lines (124 loc) · 4.25 KB
/
pingdom.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
package controller
import (
"log"
"os"
"strconv"
"strings"
"github.com/russellcardullo/go-pingdom/pingdom"
extensions "k8s.io/api/extensions/v1beta1"
utils "pingdom_controller/general"
)
// PingdomEngine struct
type PingdomEngine struct {
addIngress chan *extensions.Ingress
updateIngress chan *extensions.Ingress
deleteIngress chan string
}
// NewPingdomEngine is running a channel for each event
func NewPingdomEngine() *PingdomEngine {
return &PingdomEngine{
addIngress: make(chan *extensions.Ingress),
updateIngress: make(chan *extensions.Ingress),
deleteIngress: make(chan string),
}
}
var client, _ = pingdom.NewClientWithConfig(pingdom.ClientConfig{
APIToken: os.Getenv("PINGDOM_TOKEN"),
})
// Run will start PingdomEngine's channels
func (p *PingdomEngine) Run() {
for {
select {
case ing := <-p.addIngress:
createNewCheck(ing)
case ing := <-p.updateIngress:
updateCheck(ing)
case ingName := <-p.deleteIngress:
deleteCheck(ingName)
}
}
}
func createPingdomCheckType(ing *extensions.Ingress) pingdom.HttpCheck {
check := pingdom.HttpCheck{
Name: ing.Name,
Hostname: ing.Spec.Rules[0].Host,
Resolution: utils.StringToInt(utils.GetAnnotationValue(ing.Annotations, "pingdom.controller.yad2/resolution")),
Paused: utils.StringToBool(utils.GetAnnotationValue(ing.Annotations, "pingdom.controller.yad2/paused")),
Encryption: utils.StringToBool(utils.GetAnnotationValue(ing.Annotations, "pingdom.controller.yad2/encryption")),
Url: utils.GetAnnotationValue(ing.Annotations, "pingdom.controller.yad2/custom-path"),
Port: utils.StringToInt(utils.GetAnnotationValue(ing.Annotations, "pingdom.controller.yad2/port")),
IntegrationIds: extractAndBuildArrayOfIntegers(ing, "pingdom.controller.yad2/integrationids"),
Tags: utils.GetAnnotationValue(ing.Annotations, "pingdom.controller.yad2/port"),
ProbeFilters: utils.GetAnnotationValue(ing.Annotations, "pingdom.controller.yad2/probe-filters"),
TeamIds: extractAndBuildArrayOfIntegers(ing, "pingdom.controller.yad2/teamids"),
VerifyCertificate: utils.GetBoolPointer(utils.StringToBool(utils.GetAnnotationValue(ing.Annotations, "pingdom.controller.yad2/verify-certificate"))),
}
return check
}
func createNewCheck(ing *extensions.Ingress) {
if checkID := getCheckID(ing.Name); checkID != "" {
log.Printf("\nCheck with the name %s is already exist. Starting update operation\n", ing.Name)
updateCheck(ing)
return
}
pct := createPingdomCheckType(ing)
// Create a new http check
check, err := client.Checks.Create(&pct)
if err != nil {
log.Println("Error", err)
}
log.Println("Created check:", check) // {ID, Name}
}
func extractAndBuildArrayOfIntegers(ing *extensions.Ingress, annotate string) []int {
userInput := utils.GetAnnotationValue(ing.Annotations, annotate)
var userInputAsArray = []int{}
if userInput == "" {
log.Printf("No %s input found in annotations list", annotate)
} else {
userInputSplit := strings.Split(userInput, ",")
for _, input := range userInputSplit {
res, err := strconv.Atoi(input)
if err != nil {
log.Fatal("Cannot convert to int in Integrationids")
break
}
userInputAsArray = append(userInputAsArray, res)
}
}
return userInputAsArray
}
func updateCheck(ing *extensions.Ingress) {
pct := createPingdomCheckType(ing)
if checkID := getCheckID(pct.Name); checkID != "" {
msg, err := client.Checks.Update(utils.StringToInt(checkID), &pct)
if err != nil {
log.Printf("\nError while trying to update %s check", ing.Name)
} else {
log.Printf(msg.Message)
}
} else {
log.Printf("\nCannot find %s check, update failed. Starting create operation\n", ing.Name)
createNewCheck(ing)
}
}
func deleteCheck(ingName string) {
if checkID := getCheckID(ingName); checkID != "" {
msg, err := client.Checks.Delete(utils.StringToInt(checkID))
if err != nil {
log.Printf("\nError while trying to delete %s check", ingName)
} else {
log.Printf(msg.Message)
}
} else {
log.Printf("\nCannot find %s check, delete failed\n", ingName)
}
}
func getCheckID(checkName string) string {
checks, _ := client.Checks.List()
for _, check := range checks {
if check.Name == checkName {
return strconv.Itoa(check.ID)
}
}
return ""
}