Skip to content

Commit 6c9704c

Browse files
committed
Control URI in config; close #104
1 parent eb36eaa commit 6c9704c

File tree

12 files changed

+68
-71
lines changed

12 files changed

+68
-71
lines changed

config/config.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
# pre-exit-hook:
55
# post-exit-hook:
66

7-
http-address: "192.0.2.1"
8-
http-port: "8080"
7+
control:
8+
uri: "http://192.0.2.1"
9+
bind-addr: "192.0.2.1:8080"
910
controller-uri: "http://192.0.2.2:8080"
1011
backbone-ip: "fd00::01"
1112

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/lib/pq v1.10.9
1111
github.com/nextmn/gopacket-gtp v0.0.7
1212
github.com/nextmn/gopacket-srv6 v0.0.8
13-
github.com/nextmn/json-api v0.0.12
13+
github.com/nextmn/json-api v0.0.13
1414
github.com/nextmn/logrus-formatter v0.0.1
1515
github.com/nextmn/rfc9433 v0.0.2
1616
github.com/sirupsen/logrus v1.9.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ github.com/nextmn/gopacket-gtp v0.0.7 h1:O2cuShLTlpVBEXyHn9OIi1Nd+j4QCB66RAwzKBe
5858
github.com/nextmn/gopacket-gtp v0.0.7/go.mod h1:94jLjLU04IOVTKBXUP09MXZCgmlizqmflU2ion1ht6E=
5959
github.com/nextmn/gopacket-srv6 v0.0.8 h1:oP4wuJ7dOiV/gWmX3zoFcdp2dKdSWLUaxH2fJ3TYAwA=
6060
github.com/nextmn/gopacket-srv6 v0.0.8/go.mod h1:2Tyuo9zsG0bP2IhC4tVRgPRuyUqOgrvEEH9seJSZTlU=
61-
github.com/nextmn/json-api v0.0.12 h1:QIg+wmCBhti5hzvh2mtQ6sJ3XayFrOusvsnuHOd9fdU=
62-
github.com/nextmn/json-api v0.0.12/go.mod h1:CQXeNPj9MDGsEExtnqJFIGjLgZAKsmOoO2fy+mep7Ak=
61+
github.com/nextmn/json-api v0.0.13 h1:k8Z0Oo9et5PvdCa4wUmJE9TAHJp1zTkoAmvy1LQcoyQ=
62+
github.com/nextmn/json-api v0.0.13/go.mod h1:CQXeNPj9MDGsEExtnqJFIGjLgZAKsmOoO2fy+mep7Ak=
6363
github.com/nextmn/logrus-formatter v0.0.1 h1:Bsf78jjiEESc+rV8xE6IyKj4frDPGMwXFNrLQzm6A1E=
6464
github.com/nextmn/logrus-formatter v0.0.1/go.mod h1:vdSZ+sIcSna8vjbXkSFxsnsKHqRwaUEed4JCPcXoGyM=
6565
github.com/nextmn/rfc9433 v0.0.2 h1:6FjMY+Qy8MNXQ0PPxezUsyXDxJiCbTp5j3OcXQgIQh8=

internal/app/setup.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,28 +43,16 @@ func (s *Setup) AddTasks(ctx context.Context) {
4343
// 0.1 pre-hooks
4444
s.tasks.Register(tasks.NewMultiHook("hook.pre.init", preInitHook, "hook.post.exit", postExitHook))
4545

46-
httpPort := "80" // default http port
47-
if s.config.HTTPPort != nil {
48-
httpPort = *s.config.HTTPPort
49-
}
50-
httpURI := "http://"
51-
if s.config.HTTPAddress.Is6() {
52-
httpURI = httpURI + "[" + s.config.HTTPAddress.String() + "]:" + httpPort
53-
} else {
54-
httpURI = httpURI + s.config.HTTPAddress.String() + ":" + httpPort
55-
}
56-
httpAddr := fmt.Sprintf("[%s]:%s", s.config.HTTPAddress, httpPort)
57-
5846
// 0.2 database
5947
s.tasks.Register(tasks.NewDBTask("database", s.registry))
6048

6149
// 0.3 http server
6250

63-
s.tasks.Register(tasks.NewHttpServerTask("ctrl.rest-api", httpAddr, s.registry))
51+
s.tasks.Register(tasks.NewHttpServerTask("ctrl.rest-api", s.config.Control.BindAddr, s.registry))
6452

6553
// 0.4 controller registry
6654
if s.config.Locator != nil {
67-
s.tasks.Register(tasks.NewControllerRegistryTask("ctrl.registry", s.config.ControllerURI, s.config.BackboneIP, *s.config.Locator, httpURI, s.registry))
55+
s.tasks.Register(tasks.NewControllerRegistryTask("ctrl.registry", s.config.ControllerURI, s.config.BackboneIP, *s.config.Locator, s.config.Control.Uri, s.registry))
6856
}
6957

7058
// 1. ifaces
@@ -153,7 +141,7 @@ func (s *Setup) AddTasks(ctx context.Context) {
153141
// 4. ip rules
154142
// 4.1 rule to rttable nextmn-srv6
155143
if s.config.Locator != nil {
156-
s.tasks.Register(tasks.NewTaskIP6Rule("iproute2.rule.nextmn-srv6", *s.config.Locator, constants.RT_TABLE_NEXTMN_IPV6))
144+
s.tasks.Register(tasks.NewTaskIP6Rule("iproute2.rule.nextmn-srv6", s.config.Locator.Prefix, constants.RT_TABLE_NEXTMN_IPV6))
157145
}
158146
// 4.2 rule to rttable nextmn-gtp4
159147
if s.config.GTP4HeadendPrefix != nil {

internal/config/config.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
// Use of this source code is governed by a MIT-style license that can be
33
// found in the LICENSE file.
44
// SPDX-License-Identifier: MIT
5+
56
package config
67

78
import (
89
"io/ioutil"
910
"net/netip"
1011
"path/filepath"
1112

13+
"github.com/nextmn/json-api/jsonapi"
1214
"gopkg.in/yaml.v3"
1315
)
1416

@@ -32,23 +34,21 @@ type SRv6Config struct {
3234
Hooks *Hooks `yaml:"hooks"`
3335

3436
// interface with controller
35-
HTTPAddress netip.Addr `yaml:"http-address"`
36-
HTTPPort *string `yaml:"http-port,omitempty"` // default: 80
37+
Control Control `yaml:"control"`
3738
// TODO: use a better type for this information
38-
ControllerURI string `yaml:"controller-uri"` // example: http://192.0.2.2/8080
39+
ControllerURI jsonapi.ControlURI `yaml:"controller-uri"` // example: http://192.0.2.2/8080
3940

4041
// Backbone IPv6 address
41-
// TODO: use a better type for this information
42-
BackboneIP netip.Addr `yaml:"backbone-ip"`
42+
BackboneIP jsonapi.BackboneIP `yaml:"backbone-ip"`
4343

4444
// headends
45-
LinuxHeadendSetSourceAddress *string `yaml:"linux-headend-set-source-address,omitempty"`
46-
GTP4HeadendPrefix *string `yaml:"gtp4-headend-prefix,omitempty"` // example of prefix: 10.0.0.1/32 (if you use a single GTP4 headend) or 10.0.1.0/24 (with more headends)
47-
IPV4HeadendPrefix *string `yaml:"ipv4-headend-prefix,omitempty"` // example of prefix: 10.0.0.1/32 (if you use a single IPV4 headend) or 10.0.1.0/24 (with more headends)
48-
Headends Headends `yaml:"headends"`
45+
LinuxHeadendSetSourceAddress *netip.Addr `yaml:"linux-headend-set-source-address,omitempty"`
46+
GTP4HeadendPrefix *netip.Prefix `yaml:"gtp4-headend-prefix,omitempty"` // example of prefix: 10.0.0.1/32 (if you use a single GTP4 headend) or 10.0.1.0/24 (with more headends)
47+
IPV4HeadendPrefix *netip.Prefix `yaml:"ipv4-headend-prefix,omitempty"` // example of prefix: 10.0.0.1/32 (if you use a single IPV4 headend) or 10.0.1.0/24 (with more headends)
48+
Headends Headends `yaml:"headends"`
4949

5050
// endpoints
51-
Locator *string `yaml:"locator,omitempty"` // example of locator: fd00:51D5:0000:1::/64
52-
Endpoints Endpoints `yaml:"endpoints"`
53-
Logger *Logger `yaml:"logger,omitempty"`
51+
Locator *jsonapi.Locator `yaml:"locator,omitempty"` // example of locator: fd00:51D5:0000:1::/64
52+
Endpoints Endpoints `yaml:"endpoints"`
53+
Logger *Logger `yaml:"logger,omitempty"`
5454
}

internal/config/control.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2024 Louis Royer and the NextMN contributors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style license that can be
3+
// found in the LICENSE file.
4+
// SPDX-License-Identifier: MIT
5+
6+
package config
7+
8+
import (
9+
"github.com/nextmn/json-api/jsonapi"
10+
)
11+
12+
type Control struct {
13+
Uri jsonapi.ControlURI `yaml:"uri"` // may contain domain name instead of ip address
14+
BindAddr string `yaml:"bind-addr"` // in the form `ip` or `ip:port` (with default port being 80)
15+
}

internal/ctrl/controller-registry.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
package ctrl
66

77
import (
8-
"net/netip"
8+
"github.com/nextmn/json-api/jsonapi"
99
)
1010

1111
type ControllerRegistry struct {
12-
RemoteControlURI string // URI of the controller
13-
LocalControlURI string // URI of the router, used to control it
14-
Locator string
15-
Backbone netip.Addr
12+
RemoteControlURI jsonapi.ControlURI // URI of the controller
13+
LocalControlURI jsonapi.ControlURI // URI of the router, used to control it
14+
Locator jsonapi.Locator
15+
Backbone jsonapi.BackboneIP
1616
Resource string
1717
}

internal/iproute2/utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package iproute2
66

77
import (
88
"fmt"
9+
"net/netip"
910
"os"
1011
"os/exec"
1112
)
@@ -46,6 +47,6 @@ func runIP6Tables(args ...string) error {
4647
return nil
4748
}
4849

49-
func IPSrSetSourceAddress(address string) error {
50-
return runIP("sr", "tunsrc", "set", address)
50+
func IPSrSetSourceAddress(address netip.Addr) error {
51+
return runIP("sr", "tunsrc", "set", address.String())
5152
}

internal/tasks/controller-registry.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010
"encoding/json"
1111
"fmt"
1212
"net/http"
13-
"net/netip"
1413

1514
app_api "github.com/nextmn/srv6/internal/app/api"
1615
"github.com/nextmn/srv6/internal/ctrl"
16+
17+
"github.com/nextmn/json-api/jsonapi"
1718
)
1819

1920
const UserAgent = "go-github-nextmn-srv6"
@@ -28,7 +29,7 @@ type ControllerRegistryTask struct {
2829
}
2930

3031
// Create a new ControllerRegistry
31-
func NewControllerRegistryTask(name string, remoteControlURI string, backbone netip.Addr, locator string, localControlURI string, setup_registry app_api.Registry) *ControllerRegistryTask {
32+
func NewControllerRegistryTask(name string, remoteControlURI jsonapi.ControlURI, backbone jsonapi.BackboneIP, locator jsonapi.Locator, localControlURI jsonapi.ControlURI, setup_registry app_api.Registry) *ControllerRegistryTask {
3233
return &ControllerRegistryTask{
3334
WithName: NewName(name),
3435
WithState: NewState(),
@@ -49,19 +50,19 @@ func (t *ControllerRegistryTask) RunInit(ctx context.Context) error {
4950
if t.SetupRegistry != nil {
5051
t.SetupRegistry.RegisterControllerRegistry(t.ControllerRegistry)
5152
} else {
52-
return fmt.Errorf("could not register ControllerRegistry")
53+
return fmt.Errorf("could not register controllerregistry")
5354
}
54-
data := map[string]string{
55-
"locator": t.ControllerRegistry.Locator,
56-
"backbone": t.ControllerRegistry.Backbone.String(),
57-
"control": t.ControllerRegistry.LocalControlURI,
55+
data := jsonapi.Router{
56+
Locator: t.ControllerRegistry.Locator,
57+
Backbone: t.ControllerRegistry.Backbone,
58+
Control: t.ControllerRegistry.LocalControlURI,
5859
}
59-
json_data, err := json.Marshal(data)
60+
reqBody, err := json.Marshal(data)
6061
if err != nil {
6162
return err
6263
}
6364
// TODO: retry on timeout failure (use a new ctx)
64-
req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.ControllerRegistry.RemoteControlURI+"/routers", bytes.NewBuffer(json_data))
65+
req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.ControllerRegistry.RemoteControlURI.JoinPath("routers").String(), bytes.NewBuffer(reqBody))
6566
if err != nil {
6667
return err
6768
}
@@ -100,7 +101,7 @@ func (t *ControllerRegistryTask) RunExit() error {
100101
return nil
101102
}
102103
// no context since Background Context is already Done
103-
req, err := http.NewRequest(http.MethodDelete, t.ControllerRegistry.RemoteControlURI+t.ControllerRegistry.Resource, nil)
104+
req, err := http.NewRequest(http.MethodDelete, t.ControllerRegistry.RemoteControlURI.JoinPath(t.ControllerRegistry.Resource).String(), nil)
104105
if err != nil {
105106
return err
106107
}

internal/tasks/iprule.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package tasks
66

77
import (
88
"context"
9+
"net/netip"
910

1011
"github.com/nextmn/srv6/internal/constants"
1112
"github.com/nextmn/srv6/internal/iproute2"
@@ -15,13 +16,13 @@ import (
1516
type TaskIPRule struct {
1617
WithName
1718
WithState
18-
prefix string
19+
prefix netip.Prefix
1920
family4 bool
2021
table iproute2.Table
2122
}
2223

2324
// Create a new Task for IPRule
24-
func NewTaskIP6Rule(name string, prefix string, table_name string) *TaskIPRule {
25+
func NewTaskIP6Rule(name string, prefix netip.Prefix, table_name string) *TaskIPRule {
2526
return &TaskIPRule{
2627
WithName: NewName(name),
2728
WithState: NewState(),
@@ -32,7 +33,7 @@ func NewTaskIP6Rule(name string, prefix string, table_name string) *TaskIPRule {
3233
}
3334

3435
// Create a new Task for IPRule
35-
func NewTaskIP4Rule(name string, prefix string, table_name string) *TaskIPRule {
36+
func NewTaskIP4Rule(name string, prefix netip.Prefix, table_name string) *TaskIPRule {
3637
return &TaskIPRule{
3738
WithName: NewName(name),
3839
WithState: NewState(),
@@ -45,11 +46,11 @@ func NewTaskIP4Rule(name string, prefix string, table_name string) *TaskIPRule {
4546
// Setup ip rules
4647
func (t *TaskIPRule) RunInit(ctx context.Context) error {
4748
if t.family4 {
48-
if err := t.table.AddRule4(t.prefix); err != nil {
49+
if err := t.table.AddRule4(t.prefix.String()); err != nil {
4950
return err
5051
}
5152
} else {
52-
if err := t.table.AddRule6(t.prefix); err != nil {
53+
if err := t.table.AddRule6(t.prefix.String()); err != nil {
5354
return err
5455
}
5556
}
@@ -60,11 +61,11 @@ func (t *TaskIPRule) RunInit(ctx context.Context) error {
6061
// Delete ip rules
6162
func (t *TaskIPRule) RunExit() error {
6263
if t.family4 {
63-
if err := t.table.DelRule4(t.prefix); err != nil {
64+
if err := t.table.DelRule4(t.prefix.String()); err != nil {
6465
return err
6566
}
6667
} else {
67-
if err := t.table.DelRule6(t.prefix); err != nil {
68+
if err := t.table.DelRule6(t.prefix.String()); err != nil {
6869
return err
6970
}
7071
}

0 commit comments

Comments
 (0)