Skip to content

Commit

Permalink
Merge pull request #134 from daveshepherd/master
Browse files Browse the repository at this point in the history
Add uptime robot annotation to allow configuration of the monitor interval
  • Loading branch information
kahootali authored Jan 3, 2019
2 parents e94e6bd + fa65bed commit 1c521f1
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 17 deletions.
10 changes: 9 additions & 1 deletion docs/uptimerobot-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,12 @@ You will get a response similar to what is shown below
]
```

Copy values of `id` field of your alert contacts which you want to use for Ingress Monitor Controller and append `_0_0` to them and seperate them by `-`. You will now have a string similar to `12345_0_0-23564_0_0`. This is basically the value you will need to specify in Ingress Monitor Controller's ConfigMap as `alertContacts`.
Copy values of `id` field of your alert contacts which you want to use for Ingress Monitor Controller and append `_0_0` to them and seperate them by `-`. You will now have a string similar to `12345_0_0-23564_0_0`. This is basically the value you will need to specify in Ingress Monitor Controller's ConfigMap as `alertContacts`.

## Annotations

Additional uptime robot configurations can be added through a set of annotations to each ingress object, the current supported annotations are:

| Annotation | Description |
|:-----------------------------------------:|:-----------------------------------------:|
| uptimerobot.monitor.stakater.com/interval | The uptimerobot check interval in minutes |
10 changes: 5 additions & 5 deletions pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func TestUpdateIngressWithAnnotationDisabledShouldNotCreateMonitor(t *testing.T)

ingress := util.CreateIngressObject(ingressName, namespace, url)

ingress, err := controller.kubeClient.ExtensionsV1beta1().Ingresses(namespace).Create(ingress)
_, err := controller.kubeClient.ExtensionsV1beta1().Ingresses(namespace).Create(ingress)

if err != nil {
panic(err)
Expand Down Expand Up @@ -348,7 +348,7 @@ func TestUpdateIngressWithAnnotationEnabledShouldCreateMonitorAndDelete(t *testi

ingress := util.CreateIngressObject(ingressName, namespace, url)

ingress, err := controller.kubeClient.ExtensionsV1beta1().Ingresses(namespace).Create(ingress)
_, err := controller.kubeClient.ExtensionsV1beta1().Ingresses(namespace).Create(ingress)

if err != nil {
panic(err)
Expand Down Expand Up @@ -397,7 +397,7 @@ func TestUpdateIngressWithAnnotationFromEnabledToDisabledShouldDeleteMonitor(t *

ingress = addMonitorAnnotationToIngress(ingress, true)

ingress, err := controller.kubeClient.ExtensionsV1beta1().Ingresses(namespace).Create(ingress)
_, err := controller.kubeClient.ExtensionsV1beta1().Ingresses(namespace).Create(ingress)

if err != nil {
panic(err)
Expand Down Expand Up @@ -447,7 +447,7 @@ func TestUpdateIngressWithNewURLShouldUpdateMonitor(t *testing.T) {

ingress = addMonitorAnnotationToIngress(ingress, true)

ingress, err := controller.kubeClient.ExtensionsV1beta1().Ingresses(namespace).Create(ingress)
_, err := controller.kubeClient.ExtensionsV1beta1().Ingresses(namespace).Create(ingress)

if err != nil {
panic(err)
Expand Down Expand Up @@ -517,7 +517,7 @@ func TestUpdateIngressWithEnabledAnnotationShouldCreateMonitorAndDelete(t *testi

ingress = addMonitorAnnotationToIngress(ingress, false)

ingress, err := controller.kubeClient.ExtensionsV1beta1().Ingresses(namespace).Create(ingress)
_, err := controller.kubeClient.ExtensionsV1beta1().Ingresses(namespace).Create(ingress)

if err != nil {
panic(err)
Expand Down
5 changes: 5 additions & 0 deletions pkg/monitors/uptimerobot/uptime-mappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ func UptimeMonitorMonitorToBaseMonitorMapper(uptimeMonitor UptimeMonitorMonitor)
m.URL = uptimeMonitor.URL
m.ID = strconv.Itoa(uptimeMonitor.ID)

var annotations = map[string]string {
"uptimerobot.monitor.stakater.com/interval": strconv.Itoa(uptimeMonitor.Interval),
}
m.Annotations = annotations

return &m
}

Expand Down
17 changes: 12 additions & 5 deletions pkg/monitors/uptimerobot/uptime-mappers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,27 @@ import (
)

func TestUptimeMonitorMonitorToBaseMonitorMapper(t *testing.T) {
uptimeMonitorObject := UptimeMonitorMonitor{FriendlyName: "Test Monitor", ID: 124, URL: "https://stakater.com"}
uptimeMonitorObject := UptimeMonitorMonitor{FriendlyName: "Test Monitor", ID: 124, URL: "https://stakater.com", Interval: 900}

monitorObject := UptimeMonitorMonitorToBaseMonitorMapper(uptimeMonitorObject)

if monitorObject.ID != strconv.Itoa(uptimeMonitorObject.ID) || monitorObject.Name != uptimeMonitorObject.FriendlyName || monitorObject.URL != uptimeMonitorObject.URL {
if monitorObject.ID != strconv.Itoa(uptimeMonitorObject.ID) || monitorObject.Name != uptimeMonitorObject.FriendlyName || monitorObject.URL != uptimeMonitorObject.URL || "900" != monitorObject.Annotations["uptimerobot.monitor.stakater.com/interval"] {
t.Error("Mapper did not map the values correctly")
}
}

func TestUptimeMonitorMonitorsToBaseMonitorsMapper(t *testing.T) {
uptimeMonitorObject1 := UptimeMonitorMonitor{FriendlyName: "Test Monitor 1", ID: 124, URL: "https://stakater.com"}
uptimeMonitorObject2 := UptimeMonitorMonitor{FriendlyName: "Test Monitor 2", ID: 125, URL: "https://stackator.com"}
uptimeMonitorObject1 := UptimeMonitorMonitor{FriendlyName: "Test Monitor 1", ID: 124, URL: "https://stakater.com", Interval: 900}
uptimeMonitorObject2 := UptimeMonitorMonitor{FriendlyName: "Test Monitor 2", ID: 125, URL: "https://stackator.com", Interval: 600}

correctMonitors := []models.Monitor{models.Monitor{Name: "Test Monitor 1", ID: "124", URL: "https://stakater.com"}, models.Monitor{Name: "Test Monitor 2", ID: "125", URL: "https://stackator.com"}}
var annotations1 = map[string]string {
"uptimerobot.monitor.stakater.com/interval": "900",
}
var annotations2 = map[string]string {
"uptimerobot.monitor.stakater.com/interval": "600",
}

correctMonitors := []models.Monitor{models.Monitor{Name: "Test Monitor 1", ID: "124", URL: "https://stakater.com", Annotations: annotations1}, models.Monitor{Name: "Test Monitor 2", ID: "125", URL: "https://stackator.com", Annotations: annotations2}}

var uptimeMonitors []UptimeMonitorMonitor
uptimeMonitors = append(uptimeMonitors, uptimeMonitorObject1)
Expand Down
8 changes: 8 additions & 0 deletions pkg/monitors/uptimerobot/uptime-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ func (monitor *UpTimeMonitorService) Add(m models.Monitor) {

body := "api_key=" + monitor.apiKey + "&format=json&type=1&url=" + url.QueryEscape(m.URL) + "&friendly_name=" + url.QueryEscape(m.Name) + "&alert_contacts=" + monitor.alertContacts

if val, ok := m.Annotations["uptimerobot.monitor.stakater.com/interval"]; ok {
body += "&interval=" + val
}

response := client.PostUrlEncodedFormBody(body)

if response.StatusCode == 200 {
Expand All @@ -108,6 +112,10 @@ func (monitor *UpTimeMonitorService) Update(m models.Monitor) {

body := "api_key=" + monitor.apiKey + "&format=json&id=" + m.ID + "&friendly_name=" + m.Name + "&url=" + m.URL

if val, ok := m.Annotations["uptimerobot.monitor.stakater.com/interval"]; ok {
body += "&interval=" + val
}

response := client.PostUrlEncodedFormBody(body)

if response.StatusCode == 200 {
Expand Down
95 changes: 90 additions & 5 deletions pkg/monitors/uptimerobot/uptime-monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ func TestAddMonitorWithCorrectValues(t *testing.T) {
if err != nil {
t.Error("Error: " + err.Error())
}
if mRes.Name != m.Name || mRes.URL != m.URL {
t.Error("URL and name should be the same")
if mRes.Name != m.Name {
t.Error("The name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name)
}
if mRes.URL != m.URL {
t.Error("The URL is incorrect, expected: " + m.URL + ", but was: " + mRes.URL)
}
service.Remove(*mRes)
}
Expand All @@ -41,11 +44,90 @@ func TestUpdateMonitorWithCorrectValues(t *testing.T) {
if err != nil {
t.Error("Error: " + err.Error())
}
if mRes.Name != m.Name || mRes.URL != m.URL {
t.Error("URL and name should be the same")
if mRes.Name != m.Name {
t.Error("The initial name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name)
}
if mRes.URL != m.URL {
t.Error("The initial URL is incorrect, expected: " + m.URL + ", but was: " + mRes.URL)
}

mRes.URL = "https://facebook.com"

service.Update(*mRes)

mRes, err = service.GetByName("google-test")

if err != nil {
t.Error("Error: " + err.Error())
}
if mRes.URL != "https://facebook.com" {
t.Error("The URL should have been updated, expected: https://facebook.com, but was: " + mRes.URL)
}

service.Remove(*mRes)
}

func TestAddMonitorWithAnnotations(t *testing.T) {
config := config.GetControllerConfig()

service := UpTimeMonitorService{}
service.Setup(config.Providers[0])

var annotations = map[string]string {
"uptimerobot.monitor.stakater.com/interval": "600",
}

m := models.Monitor{Name: "google-test", URL: "https://google.com", Annotations: annotations}
service.Add(m)

mRes, err := service.GetByName("google-test")

if err != nil {
t.Error("Error: " + err.Error())
}
if mRes.Name != m.Name {
t.Error("The name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name)
}
if mRes.URL != m.URL {
t.Error("The URL is incorrect, expected: " + m.URL + ", but was: " + mRes.URL)
}
if "600" != mRes.Annotations["uptimerobot.monitor.stakater.com/interval"] {
t.Error("The interval is incorrect, expected: 600, but was: " + mRes.Annotations["uptimerobot.monitor.stakater.com/interval"])
}
service.Remove(*mRes)
}

func TestUpdateMonitorAnnotations(t *testing.T) {
config := config.GetControllerConfig()

service := UpTimeMonitorService{}
service.Setup(config.Providers[0])

var annotations = map[string]string {
"uptimerobot.monitor.stakater.com/interval": "600",
}

m := models.Monitor{Name: "google-test", URL: "https://google.com", Annotations: annotations}
service.Add(m)

mRes, err := service.GetByName("google-test")

if err != nil {
t.Error("Error: " + err.Error())
}
if mRes.Name != m.Name {
t.Error("The initial name is incorrect, expected: " + m.Name + ", but was: " + mRes.Name)
}
if mRes.URL != m.URL {
t.Error("The initial URL is incorrect, expected: " + m.URL + ", but was: " + mRes.URL)
}
if "600" != mRes.Annotations["uptimerobot.monitor.stakater.com/interval"] {
t.Error("The initial interval is incorrect: 600, but was: " + mRes.Annotations["uptimerobot.monitor.stakater.com/interval"])
}

mRes.URL = "https://facebook.com"
annotations["uptimerobot.monitor.stakater.com/interval"] = "900"
mRes.Annotations = annotations

service.Update(*mRes)

Expand All @@ -55,7 +137,10 @@ func TestUpdateMonitorWithCorrectValues(t *testing.T) {
t.Error("Error: " + err.Error())
}
if mRes.URL != "https://facebook.com" {
t.Error("URL and name should be the same")
t.Error("The updated URL is incorrect, expected: https://facebook.com, but was: " + mRes.URL)
}
if "900" != mRes.Annotations["uptimerobot.monitor.stakater.com/interval"] {
t.Error("The updated interval is incorrect, expected: 900, but was: " + mRes.Annotations["uptimerobot.monitor.stakater.com/interval"])
}

service.Remove(*mRes)
Expand Down
2 changes: 1 addition & 1 deletion pkg/monitors/uptimerobot/uptime-responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type UptimeMonitorMonitor struct {
URL string `json:"url"`
Type int `json:"type"`
SubType string `json:"sub_type"`
KeywordType string `json:"keyword_type"`
KeywordType int `json:"keyword_type"`
KeywordValue string `json:"keyword_value"`
HTTPUsername string `json:"http_username"`
HTTPPassword string `json:"http_password"`
Expand Down

0 comments on commit 1c521f1

Please sign in to comment.