Skip to content

Commit

Permalink
fix: enrich targets now accepts any scheme (#149)
Browse files Browse the repository at this point in the history
* fix: enrich targets now accepts any scheme

Signed-off-by: Niklas Treml <[email protected]>

* chore: remove debug logs

Signed-off-by: Niklas Treml <[email protected]>

* chore: add test for new validation logic

Signed-off-by: Niklas Treml <[email protected]>

---------

Signed-off-by: Niklas Treml <[email protected]>
  • Loading branch information
niklastreml authored Jun 7, 2024
1 parent d55eefc commit 4d047e4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 11 deletions.
30 changes: 20 additions & 10 deletions pkg/sparrow/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package sparrow
import (
"context"
"fmt"
"net/url"
"slices"
"strings"
"sync"
Expand Down Expand Up @@ -114,7 +115,7 @@ func (s *Sparrow) Run(ctx context.Context) error {
for {
select {
case cfg := <-s.cRuntime:
cfg = s.enrichTargets(cfg)
cfg = s.enrichTargets(ctx, cfg)
s.controller.Reconcile(ctx, cfg)
case <-ctx.Done():
s.shutdown(ctx)
Expand All @@ -131,24 +132,33 @@ func (s *Sparrow) Run(ctx context.Context) error {

// enrichTargets updates the targets of the sparrow's checks with the
// global targets. Per default, the two target lists are merged.
func (s *Sparrow) enrichTargets(cfg runtime.Config) runtime.Config {
func (s *Sparrow) enrichTargets(ctx context.Context, cfg runtime.Config) runtime.Config {
l := logger.FromContext(ctx)
if cfg.Empty() || s.tarMan == nil {
return cfg
}

for _, gt := range s.tarMan.GetTargets() {
if gt.Url == fmt.Sprintf("https://%s", s.config.SparrowName) {
u, err := url.Parse(gt.Url)
if err != nil {
l.Error("Failed to parse global target URL", "error", err, "url", gt.Url)
continue
}
if cfg.HasHealthCheck() && !slices.Contains(cfg.Health.Targets, gt.Url) {
cfg.Health.Targets = append(cfg.Health.Targets, gt.Url)

// split off hostWithoutPort because it could contain a port
hostWithoutPort := strings.Split(u.Host, ":")[0]
if hostWithoutPort == s.config.SparrowName {
continue
}

if cfg.HasHealthCheck() && !slices.Contains(cfg.Health.Targets, u.String()) {
cfg.Health.Targets = append(cfg.Health.Targets, u.String())
}
if cfg.HasLatencyCheck() && !slices.Contains(cfg.Latency.Targets, gt.Url) {
cfg.Latency.Targets = append(cfg.Latency.Targets, gt.Url)
if cfg.HasLatencyCheck() && !slices.Contains(cfg.Latency.Targets, u.String()) {
cfg.Latency.Targets = append(cfg.Latency.Targets, u.String())
}
if cfg.HasDNSCheck() && !slices.Contains(cfg.Dns.Targets, gt.Url) {
t, _ := strings.CutPrefix(gt.Url, "https://")
cfg.Dns.Targets = append(cfg.Dns.Targets, t)
if cfg.HasDNSCheck() && !slices.Contains(cfg.Dns.Targets, hostWithoutPort) {
cfg.Dns.Targets = append(cfg.Dns.Targets, hostWithoutPort)
}
}

Expand Down
25 changes: 24 additions & 1 deletion pkg/sparrow/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func TestSparrow_Run_ContextCancel(t *testing.T) {
// TestSparrow_enrichTargets tests that the enrichTargets method
// updates the targets of the configured checks.
func TestSparrow_enrichTargets(t *testing.T) {
t.Parallel()
now := time.Now()
testTarget := "https://localhost.de"
gt := []checks.GlobalTarget{
Expand Down Expand Up @@ -237,6 +238,28 @@ func TestSparrow_enrichTargets(t *testing.T) {
},
},
},
{
name: "global targets contains http and https - dns validation still works does not fail and splits off scheme",
config: runtime.Config{
Dns: &dns.Config{
Targets: []string{},
},
},
globalTargets: []checks.GlobalTarget{
{
Url: "http://az1.sparrow.com",
LastSeen: now,
},
{
Url: "https://az2.sparrow.com",
},
},
expected: runtime.Config{
Dns: &dns.Config{
Targets: []string{"az1.sparrow.com", "az2.sparrow.com"},
},
},
},
}

for _, tt := range tests {
Expand All @@ -249,7 +272,7 @@ func TestSparrow_enrichTargets(t *testing.T) {
SparrowName: "sparrow.com",
},
}
got := s.enrichTargets(tt.config)
got := s.enrichTargets(context.Background(), tt.config)
assert.Equal(t, tt.expected, got)
})
}
Expand Down

0 comments on commit 4d047e4

Please sign in to comment.