From b919a87dcdecb9449143c34e06c39a5bae209e38 Mon Sep 17 00:00:00 2001 From: Igor Shishkin Date: Sat, 1 Jun 2024 19:41:53 +0300 Subject: [PATCH] Update config scheme Signed-off-by: Igor Shishkin --- config/config.go | 27 ++++++++++++++---------- config/config_test.go | 37 ++++++++++++++++----------------- config/testdata/sample.json | 41 +++++++++++++++++-------------------- config/testdata/sample.yaml | 27 +++++++++++------------- 4 files changed, 65 insertions(+), 67 deletions(-) diff --git a/config/config.go b/config/config.go index 07bb037..288fe9a 100644 --- a/config/config.go +++ b/config/config.go @@ -10,18 +10,22 @@ import ( yaml "gopkg.in/yaml.v3" ) +type Announcer struct { + LocalAddress string `json:"local_address"` + LocalAS uint32 `json:"local_as"` + Routes []string `json:"routes"` + Peers []Peer `json:"peers"` +} + type Check struct { Kind string `json:"kind"` Spec json.RawMessage `json:"spec"` } type Peer struct { - Name string `json:"name"` - RemoteAddress string `json:"remote_address"` - RemoteAS uint64 `json:"remote_as"` - LocalAddress string `json:"local_address"` - LocalAS uint64 `json:"local_as"` - Routes []string `json:"routes"` + Name string `json:"name"` + RemoteAddress string `json:"remote_address"` + RemoteAS uint32 `json:"remote_as"` } type Service struct { @@ -29,7 +33,6 @@ type Service struct { CheckOperator string `json:"check_operator"` CheckInterval Duration `json:"check_interval"` Checks []Check `json:"checks"` - Peers []Peer `json:"peers"` } type Metrics struct { @@ -38,8 +41,9 @@ type Metrics struct { } type Config struct { - Services []Service `json:"services"` - Metrics Metrics `json:"metrics"` + Announcer Announcer `json:"announcer"` + Services []Service `json:"services"` + Metrics Metrics `json:"metrics"` } func NewFromFile(filename string) (*Config, error) { @@ -55,8 +59,9 @@ func NewFromFile(filename string) (*Config, error) { switch ext { case ".yml", ".yaml": type intermediate struct { - Services []any `yaml:"services"` - Metrics map[string]any `yaml:"metrics"` + Announcer map[string]any `yaml:"announcer"` + Services []any `yaml:"services"` + Metrics map[string]any `yaml:"metrics"` } d := intermediate{} diff --git a/config/config_test.go b/config/config_test.go index 3799e99..c9ca670 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -18,6 +18,23 @@ func TestConfig(t *testing.T) { } sampleConfig := Config{ + Announcer: Announcer{ + LocalAddress: "10.0.0.1", + LocalAS: 65999, + Routes: []string{"10.0.0.128/32"}, + Peers: []Peer{ + { + Name: "some_router_1", + RemoteAddress: "10.0.0.252", + RemoteAS: 65000, + }, + { + Name: "some_router_2", + RemoteAddress: "10.0.0.253", + RemoteAS: 65000, + }, + }, + }, Services: []Service{ { Name: "http", @@ -37,24 +54,6 @@ func TestConfig(t *testing.T) { Spec: json.RawMessage(`{"interface":"dummy0","ipv4":"10.0.0.128"}`), }, }, - Peers: []Peer{ - { - Name: "some_router_1", - RemoteAddress: "10.0.0.252", - RemoteAS: 65000, - LocalAddress: "10.0.0.1", - LocalAS: 65999, - Routes: []string{"10.0.0.128/32"}, - }, - { - Name: "some_router_2", - RemoteAddress: "10.0.0.253", - RemoteAS: 65000, - LocalAddress: "10.0.0.1", - LocalAS: 65999, - Routes: []string{"10.0.0.128/32"}, - }, - }, }, }, Metrics: Metrics{ @@ -88,6 +87,7 @@ func TestConfig(t *testing.T) { cfg, err := NewFromFile(tc.samplePath) if tc.expError == nil { r.NoError(err) + r.Equal(tc.expOut.Announcer, cfg.Announcer) for i := range tc.expOut.Services { r.Equalf(tc.expOut.Services[i].Name, cfg.Services[i].Name, "svc#%d", i) r.Equalf(tc.expOut.Services[i].CheckInterval, cfg.Services[i].CheckInterval, "svc#%d", i) @@ -96,7 +96,6 @@ func TestConfig(t *testing.T) { r.Equalf(tc.expOut.Services[i].Checks[j].Kind, cfg.Services[i].Checks[j].Kind, "svc#%d check#%d", i, j) r.JSONEqf(string(tc.expOut.Services[i].Checks[j].Spec), string(cfg.Services[i].Checks[j].Spec), "svc#%d check#%d", i, j) } - r.Equalf(tc.expOut.Services[i].Peers, cfg.Services[i].Peers, "svc#%d", i) } r.Equal(tc.expOut.Metrics, cfg.Metrics) } else { diff --git a/config/testdata/sample.json b/config/testdata/sample.json index 27f8ef6..1e28218 100644 --- a/config/testdata/sample.json +++ b/config/testdata/sample.json @@ -1,4 +1,23 @@ { + "announcer": { + "local_address": "10.0.0.1", + "local_as": 65999, + "routes": [ + "10.0.0.128/32" + ], + "peers": [ + { + "name": "some_router_1", + "remote_address": "10.0.0.252", + "remote_as": 65000 + }, + { + "name": "some_router_2", + "remote_address": "10.0.0.253", + "remote_as": 65000 + } + ] + }, "services": [ { "name": "http", @@ -31,28 +50,6 @@ "ipv4": "10.0.0.128" } } - ], - "peers": [ - { - "name": "some_router_1", - "remote_address": "10.0.0.252", - "remote_as": 65000, - "local_address": "10.0.0.1", - "local_as": 65999, - "routes": [ - "10.0.0.128/32" - ] - }, - { - "name": "some_router_2", - "remote_address": "10.0.0.253", - "remote_as": 65000, - "local_address": "10.0.0.1", - "local_as": 65999, - "routes": [ - "10.0.0.128/32" - ] - } ] } ], diff --git a/config/testdata/sample.yaml b/config/testdata/sample.yaml index d6f20ee..d9f484f 100644 --- a/config/testdata/sample.yaml +++ b/config/testdata/sample.yaml @@ -1,4 +1,16 @@ --- +announcer: + local_address: 10.0.0.1 + local_as: 65999 + routes: + - 10.0.0.128/32 + peers: + - name: some_router_1 + remote_address: 10.0.0.252 + remote_as: 65000 + - name: some_router_2 + remote_address: 10.0.0.253 + remote_as: 65000 services: - name: http check_operator: and @@ -21,21 +33,6 @@ services: spec: interface: dummy0 ipv4: 10.0.0.128 - peers: - - name: some_router_1 - remote_address: 10.0.0.252 - remote_as: 65000 - local_address: 10.0.0.1 - local_as: 65999 - routes: - - 10.0.0.128/32 - - name: some_router_2 - remote_address: 10.0.0.253 - remote_as: 65000 - local_address: 10.0.0.1 - local_as: 65999 - routes: - - 10.0.0.128/32 metrics: enabled: true address: 127.0.0.1:9090