From 7a3f1d0809abcc6286e3a3a3fa4483ba9e52aeee Mon Sep 17 00:00:00 2001 From: Matthijs Wolters Date: Wed, 4 Dec 2024 10:24:17 +0100 Subject: [PATCH] Statuscake: Implement Equals function using TestTags (#610) * 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. * Remove already merged content --- pkg/monitors/statuscake/statuscake-mappers.go | 11 +++++++++++ pkg/monitors/statuscake/statuscake-monitor.go | 15 ++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/pkg/monitors/statuscake/statuscake-mappers.go b/pkg/monitors/statuscake/statuscake-mappers.go index a5b8715a..5eed6537 100644 --- a/pkg/monitors/statuscake/statuscake-mappers.go +++ b/pkg/monitors/statuscake/statuscake-mappers.go @@ -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" ) @@ -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 } @@ -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 } diff --git a/pkg/monitors/statuscake/statuscake-monitor.go b/pkg/monitors/statuscake/statuscake-monitor.go index b4cd58c9..3cdcd35f 100644 --- a/pkg/monitors/statuscake/statuscake-monitor.go +++ b/pkg/monitors/statuscake/statuscake-monitor.go @@ -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 @@ -219,7 +228,7 @@ 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 {