Skip to content

Commit

Permalink
Control URI in config; close #104
Browse files Browse the repository at this point in the history
  • Loading branch information
louisroyer committed Nov 14, 2024
1 parent eb36eaa commit 6c9704c
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 71 deletions.
5 changes: 3 additions & 2 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
# pre-exit-hook:
# post-exit-hook:

http-address: "192.0.2.1"
http-port: "8080"
control:
uri: "http://192.0.2.1"
bind-addr: "192.0.2.1:8080"
controller-uri: "http://192.0.2.2:8080"
backbone-ip: "fd00::01"

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/lib/pq v1.10.9
github.com/nextmn/gopacket-gtp v0.0.7
github.com/nextmn/gopacket-srv6 v0.0.8
github.com/nextmn/json-api v0.0.12
github.com/nextmn/json-api v0.0.13
github.com/nextmn/logrus-formatter v0.0.1
github.com/nextmn/rfc9433 v0.0.2
github.com/sirupsen/logrus v1.9.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ github.com/nextmn/gopacket-gtp v0.0.7 h1:O2cuShLTlpVBEXyHn9OIi1Nd+j4QCB66RAwzKBe
github.com/nextmn/gopacket-gtp v0.0.7/go.mod h1:94jLjLU04IOVTKBXUP09MXZCgmlizqmflU2ion1ht6E=
github.com/nextmn/gopacket-srv6 v0.0.8 h1:oP4wuJ7dOiV/gWmX3zoFcdp2dKdSWLUaxH2fJ3TYAwA=
github.com/nextmn/gopacket-srv6 v0.0.8/go.mod h1:2Tyuo9zsG0bP2IhC4tVRgPRuyUqOgrvEEH9seJSZTlU=
github.com/nextmn/json-api v0.0.12 h1:QIg+wmCBhti5hzvh2mtQ6sJ3XayFrOusvsnuHOd9fdU=
github.com/nextmn/json-api v0.0.12/go.mod h1:CQXeNPj9MDGsEExtnqJFIGjLgZAKsmOoO2fy+mep7Ak=
github.com/nextmn/json-api v0.0.13 h1:k8Z0Oo9et5PvdCa4wUmJE9TAHJp1zTkoAmvy1LQcoyQ=
github.com/nextmn/json-api v0.0.13/go.mod h1:CQXeNPj9MDGsEExtnqJFIGjLgZAKsmOoO2fy+mep7Ak=
github.com/nextmn/logrus-formatter v0.0.1 h1:Bsf78jjiEESc+rV8xE6IyKj4frDPGMwXFNrLQzm6A1E=
github.com/nextmn/logrus-formatter v0.0.1/go.mod h1:vdSZ+sIcSna8vjbXkSFxsnsKHqRwaUEed4JCPcXoGyM=
github.com/nextmn/rfc9433 v0.0.2 h1:6FjMY+Qy8MNXQ0PPxezUsyXDxJiCbTp5j3OcXQgIQh8=
Expand Down
18 changes: 3 additions & 15 deletions internal/app/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,16 @@ func (s *Setup) AddTasks(ctx context.Context) {
// 0.1 pre-hooks
s.tasks.Register(tasks.NewMultiHook("hook.pre.init", preInitHook, "hook.post.exit", postExitHook))

httpPort := "80" // default http port
if s.config.HTTPPort != nil {
httpPort = *s.config.HTTPPort
}
httpURI := "http://"
if s.config.HTTPAddress.Is6() {
httpURI = httpURI + "[" + s.config.HTTPAddress.String() + "]:" + httpPort
} else {
httpURI = httpURI + s.config.HTTPAddress.String() + ":" + httpPort
}
httpAddr := fmt.Sprintf("[%s]:%s", s.config.HTTPAddress, httpPort)

// 0.2 database
s.tasks.Register(tasks.NewDBTask("database", s.registry))

// 0.3 http server

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

// 0.4 controller registry
if s.config.Locator != nil {
s.tasks.Register(tasks.NewControllerRegistryTask("ctrl.registry", s.config.ControllerURI, s.config.BackboneIP, *s.config.Locator, httpURI, s.registry))
s.tasks.Register(tasks.NewControllerRegistryTask("ctrl.registry", s.config.ControllerURI, s.config.BackboneIP, *s.config.Locator, s.config.Control.Uri, s.registry))
}

// 1. ifaces
Expand Down Expand Up @@ -153,7 +141,7 @@ func (s *Setup) AddTasks(ctx context.Context) {
// 4. ip rules
// 4.1 rule to rttable nextmn-srv6
if s.config.Locator != nil {
s.tasks.Register(tasks.NewTaskIP6Rule("iproute2.rule.nextmn-srv6", *s.config.Locator, constants.RT_TABLE_NEXTMN_IPV6))
s.tasks.Register(tasks.NewTaskIP6Rule("iproute2.rule.nextmn-srv6", s.config.Locator.Prefix, constants.RT_TABLE_NEXTMN_IPV6))
}
// 4.2 rule to rttable nextmn-gtp4
if s.config.GTP4HeadendPrefix != nil {
Expand Down
24 changes: 12 additions & 12 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
// SPDX-License-Identifier: MIT

package config

import (
"io/ioutil"
"net/netip"
"path/filepath"

"github.com/nextmn/json-api/jsonapi"
"gopkg.in/yaml.v3"
)

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

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

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

// headends
LinuxHeadendSetSourceAddress *string `yaml:"linux-headend-set-source-address,omitempty"`
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)
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)
Headends Headends `yaml:"headends"`
LinuxHeadendSetSourceAddress *netip.Addr `yaml:"linux-headend-set-source-address,omitempty"`
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)
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)
Headends Headends `yaml:"headends"`

// endpoints
Locator *string `yaml:"locator,omitempty"` // example of locator: fd00:51D5:0000:1::/64
Endpoints Endpoints `yaml:"endpoints"`
Logger *Logger `yaml:"logger,omitempty"`
Locator *jsonapi.Locator `yaml:"locator,omitempty"` // example of locator: fd00:51D5:0000:1::/64
Endpoints Endpoints `yaml:"endpoints"`
Logger *Logger `yaml:"logger,omitempty"`
}
15 changes: 15 additions & 0 deletions internal/config/control.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024 Louis Royer and the NextMN contributors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
// SPDX-License-Identifier: MIT

package config

import (
"github.com/nextmn/json-api/jsonapi"
)

type Control struct {
Uri jsonapi.ControlURI `yaml:"uri"` // may contain domain name instead of ip address
BindAddr string `yaml:"bind-addr"` // in the form `ip` or `ip:port` (with default port being 80)
}
10 changes: 5 additions & 5 deletions internal/ctrl/controller-registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
package ctrl

import (
"net/netip"
"github.com/nextmn/json-api/jsonapi"
)

type ControllerRegistry struct {
RemoteControlURI string // URI of the controller
LocalControlURI string // URI of the router, used to control it
Locator string
Backbone netip.Addr
RemoteControlURI jsonapi.ControlURI // URI of the controller
LocalControlURI jsonapi.ControlURI // URI of the router, used to control it
Locator jsonapi.Locator
Backbone jsonapi.BackboneIP
Resource string
}
5 changes: 3 additions & 2 deletions internal/iproute2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package iproute2

import (
"fmt"
"net/netip"
"os"
"os/exec"
)
Expand Down Expand Up @@ -46,6 +47,6 @@ func runIP6Tables(args ...string) error {
return nil
}

func IPSrSetSourceAddress(address string) error {
return runIP("sr", "tunsrc", "set", address)
func IPSrSetSourceAddress(address netip.Addr) error {
return runIP("sr", "tunsrc", "set", address.String())
}
21 changes: 11 additions & 10 deletions internal/tasks/controller-registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/netip"

app_api "github.com/nextmn/srv6/internal/app/api"
"github.com/nextmn/srv6/internal/ctrl"

"github.com/nextmn/json-api/jsonapi"
)

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

// Create a new ControllerRegistry
func NewControllerRegistryTask(name string, remoteControlURI string, backbone netip.Addr, locator string, localControlURI string, setup_registry app_api.Registry) *ControllerRegistryTask {
func NewControllerRegistryTask(name string, remoteControlURI jsonapi.ControlURI, backbone jsonapi.BackboneIP, locator jsonapi.Locator, localControlURI jsonapi.ControlURI, setup_registry app_api.Registry) *ControllerRegistryTask {
return &ControllerRegistryTask{
WithName: NewName(name),
WithState: NewState(),
Expand All @@ -49,19 +50,19 @@ func (t *ControllerRegistryTask) RunInit(ctx context.Context) error {
if t.SetupRegistry != nil {
t.SetupRegistry.RegisterControllerRegistry(t.ControllerRegistry)
} else {
return fmt.Errorf("could not register ControllerRegistry")
return fmt.Errorf("could not register controllerregistry")
}
data := map[string]string{
"locator": t.ControllerRegistry.Locator,
"backbone": t.ControllerRegistry.Backbone.String(),
"control": t.ControllerRegistry.LocalControlURI,
data := jsonapi.Router{
Locator: t.ControllerRegistry.Locator,
Backbone: t.ControllerRegistry.Backbone,
Control: t.ControllerRegistry.LocalControlURI,
}
json_data, err := json.Marshal(data)
reqBody, err := json.Marshal(data)
if err != nil {
return err
}
// TODO: retry on timeout failure (use a new ctx)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.ControllerRegistry.RemoteControlURI+"/routers", bytes.NewBuffer(json_data))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, t.ControllerRegistry.RemoteControlURI.JoinPath("routers").String(), bytes.NewBuffer(reqBody))
if err != nil {
return err
}
Expand Down Expand Up @@ -100,7 +101,7 @@ func (t *ControllerRegistryTask) RunExit() error {
return nil
}
// no context since Background Context is already Done
req, err := http.NewRequest(http.MethodDelete, t.ControllerRegistry.RemoteControlURI+t.ControllerRegistry.Resource, nil)
req, err := http.NewRequest(http.MethodDelete, t.ControllerRegistry.RemoteControlURI.JoinPath(t.ControllerRegistry.Resource).String(), nil)
if err != nil {
return err
}
Expand Down
15 changes: 8 additions & 7 deletions internal/tasks/iprule.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package tasks

import (
"context"
"net/netip"

"github.com/nextmn/srv6/internal/constants"
"github.com/nextmn/srv6/internal/iproute2"
Expand All @@ -15,13 +16,13 @@ import (
type TaskIPRule struct {
WithName
WithState
prefix string
prefix netip.Prefix
family4 bool
table iproute2.Table
}

// Create a new Task for IPRule
func NewTaskIP6Rule(name string, prefix string, table_name string) *TaskIPRule {
func NewTaskIP6Rule(name string, prefix netip.Prefix, table_name string) *TaskIPRule {
return &TaskIPRule{
WithName: NewName(name),
WithState: NewState(),
Expand All @@ -32,7 +33,7 @@ func NewTaskIP6Rule(name string, prefix string, table_name string) *TaskIPRule {
}

// Create a new Task for IPRule
func NewTaskIP4Rule(name string, prefix string, table_name string) *TaskIPRule {
func NewTaskIP4Rule(name string, prefix netip.Prefix, table_name string) *TaskIPRule {
return &TaskIPRule{
WithName: NewName(name),
WithState: NewState(),
Expand All @@ -45,11 +46,11 @@ func NewTaskIP4Rule(name string, prefix string, table_name string) *TaskIPRule {
// Setup ip rules
func (t *TaskIPRule) RunInit(ctx context.Context) error {
if t.family4 {
if err := t.table.AddRule4(t.prefix); err != nil {
if err := t.table.AddRule4(t.prefix.String()); err != nil {
return err
}
} else {
if err := t.table.AddRule6(t.prefix); err != nil {
if err := t.table.AddRule6(t.prefix.String()); err != nil {
return err
}
}
Expand All @@ -60,11 +61,11 @@ func (t *TaskIPRule) RunInit(ctx context.Context) error {
// Delete ip rules
func (t *TaskIPRule) RunExit() error {
if t.family4 {
if err := t.table.DelRule4(t.prefix); err != nil {
if err := t.table.DelRule4(t.prefix.String()); err != nil {
return err
}
} else {
if err := t.table.DelRule6(t.prefix); err != nil {
if err := t.table.DelRule6(t.prefix.String()); err != nil {
return err
}
}
Expand Down
7 changes: 4 additions & 3 deletions internal/tasks/linux-headend-set-source-address.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package tasks

import (
"context"
"net/netip"

"github.com/nextmn/srv6/internal/iproute2"
)
Expand All @@ -14,11 +15,11 @@ import (
type TaskLinuxHeadendSetSourceAddress struct {
WithName
WithState
address string
address netip.Addr
}

// Create a new TaskLinuxHeadendSetSourceAddress
func NewTaskLinuxHeadendSetSourceAddress(name string, address string) *TaskLinuxHeadendSetSourceAddress {
func NewTaskLinuxHeadendSetSourceAddress(name string, address netip.Addr) *TaskLinuxHeadendSetSourceAddress {
return &TaskLinuxHeadendSetSourceAddress{
WithName: NewName(name),
WithState: NewState(),
Expand All @@ -38,7 +39,7 @@ func (t *TaskLinuxHeadendSetSourceAddress) RunInit(ctx context.Context) error {
// Exit
func (t *TaskLinuxHeadendSetSourceAddress) RunExit() error {
// :: resets to default behavior
if err := iproute2.IPSrSetSourceAddress("::"); err != nil {
if err := iproute2.IPSrSetSourceAddress(netip.MustParseAddr("::")); err != nil {
return err
}
t.state = false
Expand Down
13 changes: 1 addition & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,7 @@ func main() {
if conf.Logger != nil {
logrus.SetLevel(conf.Logger.Level)
}
// TODO: use directly URI in config
httpPort := "80" // default http port
if conf.HTTPPort != nil {
httpPort = *conf.HTTPPort
}
httpURI := "http://"
if conf.HTTPAddress.Is6() {
httpURI = httpURI + "[" + conf.HTTPAddress.String() + "]:" + httpPort
} else {
httpURI = httpURI + conf.HTTPAddress.String() + ":" + httpPort
}
if err := healthcheck.NewHealthcheck(httpURI, "go-github-nextmn-srv6").Run(ctx.Context); err != nil {
if err := healthcheck.NewHealthcheck(*conf.Control.Uri.JoinPath("status"), "go-github-nextmn-srv6").Run(ctx.Context); err != nil {
os.Exit(1)
}
return nil
Expand Down

0 comments on commit 6c9704c

Please sign in to comment.