From e4781618866a361640b8034498fd0984c70c1767 Mon Sep 17 00:00:00 2001 From: Jelmer Snoeck Date: Sun, 2 Sep 2018 20:56:47 +0200 Subject: [PATCH 1/4] Always post StatusCodes. The StatusCake Client expects us to always set the StatusCodes when we perform an update. This makes sure we do. This is the default list copied from StatusCake. --- internal/provider/statuscake/statuscake.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/internal/provider/statuscake/statuscake.go b/internal/provider/statuscake/statuscake.go index e7cb8fd..4c263f2 100644 --- a/internal/provider/statuscake/statuscake.go +++ b/internal/provider/statuscake/statuscake.go @@ -14,6 +14,10 @@ import ( "github.com/DreamItGetIT/statuscake" ) +// The StatusCake Client we're using expects us to set this. +// XXX remove this once we move to our own internal client. +const statusCodes = "204,205,206,303,400,401,403,404,405,406,408,410,413,444,429,494,495,496,499,500,501,502,503,504,505,506,507,508,509,510,511,521,522,523,524,520,598,599" + // Register registers the provider with a certain factory using the FactoryFunc. func Register(fact provider.FactoryInterface) { fact.Register("StatusCake", FactoryFunc) @@ -146,6 +150,7 @@ func (c *Client) translateSpec(spec v1alpha1.MonitorTemplateSpec) (*statuscake.T WebsiteName: spec.Name, TestType: spec.Type, ContactGroup: c.groups, + StatusCodes: statusCodes, } if spec.Timeout != nil { From fac2dfb293015754de7e338ccf758b618bdae51a Mon Sep 17 00:00:00 2001 From: Jelmer Snoeck Date: Sun, 2 Sep 2018 21:01:03 +0200 Subject: [PATCH 2/4] Add fix for StatusCake TestID. We were seeing issues where on data changes, the Operator would create a new monitor. This was caused because StatusCake returns no ID when there are changes to the object. This translates into a 0 within the client. We used to return this 0 id, which then lead to a non existing error, causing us to create a new monitor with StatusCake. --- internal/provider/statuscake/statuscake.go | 6 +++ .../provider/statuscake/statuscake_test.go | 43 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/internal/provider/statuscake/statuscake.go b/internal/provider/statuscake/statuscake.go index 4c263f2..bb29dc9 100644 --- a/internal/provider/statuscake/statuscake.go +++ b/internal/provider/statuscake/statuscake.go @@ -140,6 +140,12 @@ func (c *Client) Update(id string, spec v1alpha1.MonitorTemplateSpec) (string, e return id, err } + // The StatusCake API returns no ID if there is an update to the item. This + // means we need to check for this and actually avoid returning a "0" id. + if sct.TestID == 0 { + return id, nil + } + return strconv.Itoa(sct.TestID), nil } diff --git a/internal/provider/statuscake/statuscake_test.go b/internal/provider/statuscake/statuscake_test.go index 96448ca..e7e6783 100644 --- a/internal/provider/statuscake/statuscake_test.go +++ b/internal/provider/statuscake/statuscake_test.go @@ -219,8 +219,11 @@ func TestTranslateSpec(t *testing.T) { return } - if !reflect.DeepEqual(translation, tc.expected) { - t.Errorf("Expected translation to equal \n%#v\ngot\n%#v", tc.expected, translation) + exp := tc.expected + exp.StatusCodes = statusCodes + + if !reflect.DeepEqual(translation, exp) { + t.Errorf("Expected translation to equal \n%#v\ngot\n%#v", exp, translation) } }) } @@ -451,6 +454,42 @@ func TestClient_Update(t *testing.T) { t.Errorf("Expected 1 udpate call, got %d", fc.updateCount) } }) + + t.Run("with changed fields", func(t *testing.T) { + defer fc.flush() + + tpl := v1alpha1.MonitorTemplateSpec{ + Type: "HTTP", + HTTP: &v1alpha1.HTTPTemplate{ + CustomHeader: "Test-Header", + UserAgent: "(Test User Agent)", URL: "http://fully-qualified-url.com", + FollowRedirects: true, + }, + } + + fc.updateFunc = func(sct *statuscake.Test) (*statuscake.Test, error) { + if sct.TestID != 12345 { + t.Errorf("Expected TestID to be `12345`, got `%d`", sct.TestID) + } + + // StatusCake sets the ID to 0 if there's changes. + sct.TestID = 0 + return sct, nil + } + + id, err := cl.Update("12345", tpl) + if err != nil { + t.Errorf("Expected no error, got %s", err) + } + + if fc.updateCount != 1 { + t.Errorf("Expected 1 udpate call, got %d", fc.updateCount) + } + + if id != "12345" { + t.Errorf("Expected ID to be `12345`, got `%s`", id) + } + }) } type fakeClient struct { From b9a313bfa47bbab180b3d562325ac0a690458f27 Mon Sep 17 00:00:00 2001 From: Jelmer Snoeck Date: Sun, 2 Sep 2018 21:02:15 +0200 Subject: [PATCH 3/4] Add explanation for Status.ID update. --- internal/ingressmonitor/operator.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/ingressmonitor/operator.go b/internal/ingressmonitor/operator.go index 0630f61..000391d 100644 --- a/internal/ingressmonitor/operator.go +++ b/internal/ingressmonitor/operator.go @@ -167,6 +167,9 @@ func (o *Operator) handleIngressMonitor(obj *v1alpha1.IngressMonitor) error { return err } + // The ID has changed, update the status. This could happen when the test + // has been removed from the provider. The operator ensures that the test + // will be present, and thus create a new one. if obj.Status.ID != id { obj.Status.ID = id _, err = o.imClient.Ingressmonitor().IngressMonitors(obj.Namespace).Update(obj) From ef1ff53c02bfdc6da6387fa75005f9d70ce531aa Mon Sep 17 00:00:00 2001 From: Jelmer Snoeck Date: Sun, 2 Sep 2018 21:03:25 +0200 Subject: [PATCH 4/4] Prepare for v0.1.1. --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b301640..6dab2d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## v0.1.1 - 2018-09-02 + +### Fixed + +- Always post StatusCodes to StatusCake, ensuring we error on faulty codes. +- Handle monitor data changes with StatusCake, where an update caused the ID to reset to 0 and create a new monitor. + ## v0.1.0 - 2018-09-01 ### Added