Skip to content

Commit

Permalink
Implement Equals function using TestTags (#3)
Browse files Browse the repository at this point in the history
* WIP add config option

* Update controller-gen version

Fixes the broken v0.9 of controller-gen because it causes a segfault
when running `make install`

* Update formatting for base CRDS

* Working read secret

* Fixed read secret

* Use new secret function not client function

* Revert random formatting changes

* More stupid formatting fixes

* Final formatting fixes

I don't like formatting :(

* Small fixes and docs update

* Update example

* Update CRDs

* Add raw_post_data var to statuscake

* Add user_agent var to statuscake

* Update formatting for user_agent

* Fix tabs in statuscake-monitor.go

* Update base crds

* Docs and example update

* Update CRDs

* Implement Equals function using TestTags

It is mentioned in a comment in the code itself but because of the
discrepency between the fields in the EndpointMonitor CR and the
Statuscake API it is not immediately clear how to compare an old monitor
with an updated monitor. The way I have elected to check this is to use
the TestTags field to include some kind of identifier that should be
updated on any change. So if the tags have changed then the monitor
should be updated.
  • Loading branch information
matthijswolters-rl authored Oct 24, 2024
1 parent b97444a commit 70ce314
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
11 changes: 11 additions & 0 deletions pkg/monitors/statuscake/statuscake-mappers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package statuscake

import (
"strings"

statuscake "github.com/StatusCakeDev/statuscake-go"
endpointmonitorv1alpha1 "github.com/stakater/IngressMonitorController/v2/api/v1alpha1"
"github.com/stakater/IngressMonitorController/v2/pkg/models"
)

Expand All @@ -11,6 +14,10 @@ func StatusCakeMonitorMonitorToBaseMonitorMapper(statuscakeData StatusCakeMonito
m.Name = statuscakeData.WebsiteName
m.URL = statuscakeData.WebsiteURL
m.ID = statuscakeData.TestID

var providerConfig endpointmonitorv1alpha1.StatusCakeConfig
providerConfig.TestTags = strings.Join(statuscakeData.Tags, ",")
m.Config = &providerConfig
return &m
}

Expand All @@ -20,6 +27,10 @@ func StatusCakeApiResponseDataToBaseMonitorMapper(statuscakeData statuscake.Upti
m.Name = statuscakeData.Data.Name
m.URL = statuscakeData.Data.WebsiteURL
m.ID = statuscakeData.Data.ID

var providerConfig endpointmonitorv1alpha1.StatusCakeConfig
providerConfig.TestTags = strings.Join(statuscakeData.Data.Tags, ",")
m.Config = &providerConfig
return &m
}

Expand Down
21 changes: 15 additions & 6 deletions pkg/monitors/statuscake/statuscake-monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,17 @@ type StatusCakeMonitorService struct {
}

func (monitor *StatusCakeMonitorService) Equal(oldMonitor models.Monitor, newMonitor models.Monitor) bool {
// TODO: Retrieve oldMonitor config and compare it here
return false
// Since there is a discrepency between the fields in the endpointmonitor CR and the statuscake API
// use the tags to define a last updated by tags. This ensures we are not ratelimited by statuscake.
oldConf := oldMonitor.Config.(*endpointmonitorv1alpha1.StatusCakeConfig)
newConf := newMonitor.Config.(*endpointmonitorv1alpha1.StatusCakeConfig)
if oldConf.TestTags != newConf.TestTags {
msg := "Found a difference between the old TestTags and new TestTags. Updating the UptimeCheck..."
log.Info(msg, "Old Tags", oldConf.TestTags, "New Tags", newConf.TestTags)
return false
} else {
return true
}
}

// buildUpsertForm function is used to create the form needed to Add or update a monitor
Expand Down Expand Up @@ -219,15 +228,15 @@ func buildUpsertForm(m models.Monitor, cgroup string) url.Values {
if providerConfig != nil && providerConfig.Confirmation > 0 {
f.Add("confirmation", strconv.Itoa(providerConfig.Confirmation))
}
if providerConfig != nil {
if providerConfig != nil && len(providerConfig.FindString) > 0 {
f.Add("find_string", providerConfig.FindString)
}
if providerConfig != nil && len(providerConfig.RawPostData) > 0 {
f.Add("post_raw", providerConfig.RawPostData)
}
if providerConfig != nil && len(providerConfig.UserAgent) > 0 {
f.Add("user_agent", providerConfig.UserAgent)
}
if providerConfig != nil && len(providerConfig.UserAgent) > 0 {
f.Add("user_agent", providerConfig.UserAgent)
}
return f
}

Expand Down

0 comments on commit 70ce314

Please sign in to comment.