Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update config scheme #6

Merged
merged 2 commits into from
Jun 1, 2024
Merged
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
27 changes: 16 additions & 11 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,29 @@ 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 {
Name string `json:"name"`
CheckOperator string `json:"check_operator"`
CheckInterval Duration `json:"check_interval"`
Checks []Check `json:"checks"`
Peers []Peer `json:"peers"`
}

type Metrics struct {
Expand All @@ -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) {
Expand All @@ -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{}
Expand Down
37 changes: 18 additions & 19 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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{
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
41 changes: 19 additions & 22 deletions config/testdata/sample.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
]
}
]
}
],
Expand Down
27 changes: 12 additions & 15 deletions config/testdata/sample.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Loading