Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions birdwatcher/bird_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestWriteBirdConfig(t *testing.T) {
defer os.Remove(tmpFile.Name())

prefixes := make(PrefixCollection)
prefixes["match_route"] = NewPrefixSet("match_route")
prefixes["match_route"] = NewPrefixSet("match_route", true)

// write bird config with empty prefix list
err = writeBirdConfig(tmpFile.Name(), prefixes, false)
Expand All @@ -47,7 +47,7 @@ func TestWriteBirdConfig(t *testing.T) {
defer os.Remove(tmpFile.Name())

prefixes := make(PrefixCollection)
prefixes["match_route"] = NewPrefixSet("match_route")
prefixes["match_route"] = NewPrefixSet("match_route", true)

for _, pref := range []string{"1.2.3.4/32", "2.3.4.5/26", "3.4.5.6/24", "4.5.6.7/21"} {
_, prf, _ := net.ParseCIDR(pref)
Expand All @@ -68,6 +68,36 @@ func TestWriteBirdConfig(t *testing.T) {
assert.Equal(t, string(fixture), string(data))
})

t.Run("one prefixset, do not filter", func(t *testing.T) {
t.Parallel()

// open tempfile
tmpFile, err := os.CreateTemp(t.TempDir(), "bird_test")
require.NoError(t, err)
defer os.Remove(tmpFile.Name())

prefixes := make(PrefixCollection)
prefixes["match_route"] = NewPrefixSet("match_route", /* enablePrefixFilter */false )

for _, pref := range []string{"1.2.3.4/32", "2.3.4.5/26", "3.4.5.6/24", "4.5.6.7/21"} {
_, prf, _ := net.ParseCIDR(pref)
prefixes["match_route"].Add(*prf)
}

// write bird config to it
err = writeBirdConfig(tmpFile.Name(), prefixes, false)
require.NoError(t, err)

// read data from temp file and compare it to file fixture
data, err := os.ReadFile(tmpFile.Name())
require.NoError(t, err)

fixture, err := os.ReadFile("testdata/bird/config_return_true")
require.NoError(t, err)

assert.Equal(t, string(fixture), string(data))
})

t.Run("one prefix, compat", func(t *testing.T) {
t.Parallel()

Expand All @@ -78,7 +108,7 @@ func TestWriteBirdConfig(t *testing.T) {

prefixes := make(PrefixCollection)

prefixes["other_function"] = NewPrefixSet("other_function")
prefixes["other_function"] = NewPrefixSet("other_function", true)
for _, pref := range []string{"5.6.7.8/32", "6.7.8.9/26", "7.8.9.10/24"} {
_, prf, _ := net.ParseCIDR(pref)
prefixes["other_function"].Add(*prf)
Expand Down
3 changes: 3 additions & 0 deletions birdwatcher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func ReadConfig(conf *Config, configFile string) error {
allPrefixes[ipn.String()] = true
}

// TODO: How to make it true by default, if toml.DecodeFile set boolean to false by default ?
s.enablePrefixFilter = true

// map name to each search
conf.Services[name] = s
}
Expand Down
1 change: 1 addition & 0 deletions birdwatcher/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"regexp"
"testing"
"time"
"fmt"

"github.com/stretchr/testify/assert"
)
Expand Down
8 changes: 4 additions & 4 deletions birdwatcher/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,28 +216,28 @@ func (h *HealthCheck) applyConfig(config Config, prefixes PrefixCollection) erro
}

func (h *HealthCheck) addPrefix(svc *ServiceCheck, prefix net.IPNet) {
h.ensurePrefixSet(svc.FunctionName)
h.ensurePrefixSet(svc.FunctionName, svc.enablePrefixFilter)

h.prefixes[svc.FunctionName].Add(prefix)
prefixStateMetric.WithLabelValues(svc.Name(), prefix.String()).Set(1.0)
}

func (h *HealthCheck) removePrefix(svc *ServiceCheck, prefix net.IPNet) {
h.ensurePrefixSet(svc.FunctionName)
h.ensurePrefixSet(svc.FunctionName, svc.enablePrefixFilter)

h.prefixes[svc.FunctionName].Remove(prefix)
prefixStateMetric.WithLabelValues(svc.Name(), prefix.String()).Set(0.0)
}

func (h *HealthCheck) ensurePrefixSet(functionName string) {
func (h *HealthCheck) ensurePrefixSet(functionName string, enablePrefixFilter bool) {
// make sure the top level map is prepared
if h.prefixes == nil {
h.prefixes = make(PrefixCollection)
}

// make sure a mapping for this function name exists
if _, found := h.prefixes[functionName]; !found {
h.prefixes[functionName] = NewPrefixSet(functionName)
h.prefixes[functionName] = NewPrefixSet(functionName, enablePrefixFilter)
}
}

Expand Down
14 changes: 10 additions & 4 deletions birdwatcher/prefixset.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,26 @@ type PrefixCollection map[string]*PrefixSet

// PrefixSet represents a list of prefixes alongside a function name
type PrefixSet struct {
prefixes []net.IPNet
functionName string
prefixes []net.IPNet
enablePrefixFilter bool
functionName string
}

// NewPrefixSet returns a new prefixset with given function name
func NewPrefixSet(functionName string) *PrefixSet {
return &PrefixSet{functionName: functionName}
func NewPrefixSet(functionName string, enablePrefixFilter bool) *PrefixSet {
return &PrefixSet{functionName: functionName, prefixes: make([]net.IPNet, 0), enablePrefixFilter: enablePrefixFilter}
}

// FunctionName returns the function name
func (p PrefixSet) FunctionName() string {
return p.functionName
}

// EnablePrefixFilter returns if we should filter `net` with the prefixes, or just return true/false
func (p PrefixSet) EnablePrefixFilter() bool {
return p.enablePrefixFilter
}

// Prefixes returns the prefixes
func (p PrefixSet) Prefixes() []net.IPNet {
return p.prefixes
Expand Down
4 changes: 2 additions & 2 deletions birdwatcher/prefixset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func TestPrefixSet_Add(t *testing.T) {
t.Parallel()

p := NewPrefixSet("foobar")
p := NewPrefixSet("foobar", true)
// should be empty
assert.Empty(t, p.prefixes)

Expand Down Expand Up @@ -44,7 +44,7 @@ func TestPrefixSet_Add(t *testing.T) {
func TestPrefixSet_Remove(t *testing.T) {
t.Parallel()

p := NewPrefixSet("foobar")
p := NewPrefixSet("foobar", true)

// add some prefixes
prefixes := make([]net.IPNet, 4)
Expand Down
2 changes: 1 addition & 1 deletion birdwatcher/servicecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type ServiceCheck struct {
//nolint:revive // these prefixes are converted into net.IPNet
prefixes []net.IPNet
state ServiceState
disablePrefixCheck bool
enablePrefixFilter bool
stopped chan any
}

Expand Down
1 change: 0 additions & 1 deletion birdwatcher/servicecheck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func TestServiceCheckPushChannel(t *testing.T) {

buf := make(chan *Action)
sc := ServiceCheck{
disablePrefixCheck: true,
name: "test",
Command: "/usr/bin/true",
Fail: 3,
Expand Down
13 changes: 11 additions & 2 deletions birdwatcher/templates/functions.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@
{{- range .Collections }}
function {{.FunctionName}}(){{- if not $.CompatBird213 }} -> bool{{- end }}
{
{{- if .EnablePrefixFilter }}
{{- with .Prefixes}}
return net ~ [
{{- range prefixPad . }}
{{.}}
{{- end }}
{{- end }}{{/* end of range prefixPad */}}
];
{{- else }}
return false;
{{- end }}
}
{{- else }}{{/* else of if .enablePrefixFilter */}}
{{- with .Prefixes}}
return true;
{{- else }}
return false;
{{- end }}

{{- end }}{{/* end of if .enablePrefixFilter */}}
}
{{- end }}{{/* end of range .Collections */}}
5 changes: 5 additions & 0 deletions birdwatcher/testdata/bird/config_return_true
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# DO NOT EDIT MANUALLY
function match_route() -> bool
{
return true;
}