Compare commits

...

23 commits

Author SHA1 Message Date
25aebcc123
f-21: vpc: change to be ok in the name
All checks were successful
Pre Release Workflow / build (dhcp, amd64, linux) (push) Successful in 1m29s
Pre Release Workflow / build (metacli, amd64, linux) (push) Successful in 1m31s
Pre Release Workflow / build (metadata, amd64, linux) (push) Successful in 1m33s
Pre Release Workflow / build (subnet, amd64, linux) (push) Successful in 1m31s
Pre Release Workflow / upload-scripts (run-dnsmasq-in-netns.sh) (push) Successful in 6s
Pre Release Workflow / prerelease (push) Successful in 13s
Pre Release Workflow / set-release-target (push) Successful in 1s
Pre Release Workflow / build (db, amd64, linux) (push) Successful in 1m31s
Pre Release Workflow / build (agent, amd64, linux) (push) Successful in 1m34s
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-21 21:52:03 +02:00
4470af0e9e
f-21: api: update api
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-21 21:43:17 +02:00
28afabbd63
f-21: code: add default interface
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-21 21:38:46 +02:00
8724570c81
f-21: code: swap from interface only to full conf
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-21 21:32:59 +02:00
a52118cddb
f-21: code: change from ip to interface type
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-21 21:28:20 +02:00
a3549fd822
f-21: code: add interface name in config
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-21 21:27:19 +02:00
374bb34695
f-21: code: add dispatch command for subnet
All checks were successful
Pre Release Workflow / set-release-target (push) Successful in 1s
Pre Release Workflow / build (db, amd64, linux) (push) Successful in 1m32s
Pre Release Workflow / build (metacli, amd64, linux) (push) Successful in 1m31s
Pre Release Workflow / build (metadata, amd64, linux) (push) Successful in 1m31s
Pre Release Workflow / build (agent, amd64, linux) (push) Successful in 1m35s
Pre Release Workflow / build (dhcp, amd64, linux) (push) Successful in 1m28s
Pre Release Workflow / build (subnet, amd64, linux) (push) Successful in 1m30s
Pre Release Workflow / upload-scripts (run-dnsmasq-in-netns.sh) (push) Successful in 5s
Pre Release Workflow / prerelease (push) Successful in 12s
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-19 21:02:54 +02:00
d028dbe57b
f-21: clean: delete vpc notions
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-19 20:49:50 +02:00
064b90f5e4
f-21: refactor: add dispatcher layer for MQTT migration
Introduce internal/dispatcher package with a Command interface and typed
commands (CreateVPC, DeleteVPC, CreateSubnet, DeleteSubnet). The API
handlers now call dispatcher.Dispatch() instead of enqueuing closures
directly, decoupling transport (HTTP today, MQTT tomorrow) from execution.

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-19 00:11:59 +02:00
40fa49d3a9
f-21: git: add claude to gitignore
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-19 00:04:46 +02:00
6dd21a465d
f-21: code: add vpc gestion in api
All checks were successful
Pre Release Workflow / set-release-target (push) Successful in 1s
Pre Release Workflow / build (agent, amd64, linux) (push) Successful in 1m33s
Pre Release Workflow / build (db, amd64, linux) (push) Successful in 1m30s
Pre Release Workflow / build (metacli, amd64, linux) (push) Successful in 1m30s
Pre Release Workflow / build (metadata, amd64, linux) (push) Successful in 1m31s
Pre Release Workflow / build (subnet, amd64, linux) (push) Successful in 1m31s
Pre Release Workflow / prerelease (push) Successful in 13s
Pre Release Workflow / build (dhcp, amd64, linux) (push) Successful in 1m28s
Pre Release Workflow / build (vpc, amd64, linux) (push) Successful in 1m30s
Pre Release Workflow / upload-scripts (run-dnsmasq-in-netns.sh) (push) Successful in 5s
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-17 23:26:04 +02:00
3955fb2112
f-21: code: add database in api
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-17 23:25:35 +02:00
b0c73b2952
f-21: doc: rename api doc
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-16 22:59:19 +02:00
75e02bd4f7
f-21: code: separate and create route fonction
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-16 22:59:11 +02:00
24536ed891
f-21: code: set db tu readwrite
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-16 22:59:10 +02:00
8637c33bd8
f-21: code: ajout d'un system de worker
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-16 22:59:10 +02:00
e1f7676093
f-21: go: add import lib
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-14 22:00:59 +02:00
544016cefe
f-21: agent: implement api first
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-14 22:00:55 +02:00
1bb3681b68
f-21: config: add config data
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-14 22:00:52 +02:00
94a14dbe60
f-21: code: add list prefix
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-14 22:00:48 +02:00
32669371d6
f-21: mon: add prometheus data
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-14 22:00:29 +02:00
9ad407755b
f-21: add first api file
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-14 22:00:16 +02:00
ca8f86a07f
f-21: api: add openapi file
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
2026-04-14 21:58:19 +02:00
26 changed files with 942 additions and 107 deletions

View file

@ -37,7 +37,6 @@ jobs:
- metadata - metadata
- metacli - metacli
- agent - agent
- vpc
- dhcp - dhcp
- subnet - subnet
uses: ./.forgejo/workflows/build.yml uses: ./.forgejo/workflows/build.yml

3
.gitignore vendored
View file

@ -15,6 +15,9 @@
# Output of the go coverage tool, specifically when used with LiteIDE # Output of the go coverage tool, specifically when used with LiteIDE
*.out *.out
# Output of the go coverage tool, specifically when used with LiteIDE
.claude
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/

303
api/agent.yaml Normal file
View file

@ -0,0 +1,303 @@
openapi: "3.1.0"
info:
title: Two API
version: "0.1.0"
description: REST API for managing VPCs and Subnets in the Two orchestrator.
servers:
- url: http://localhost:8080
description: Local development server
paths:
# ── VPC ────────────────────────────────────────────────────────────────────
/vpcs:
get:
summary: List all VPCs
operationId: listVPCs
responses:
"200":
description: List of VPCs
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/VPC"
"500":
$ref: "#/components/responses/InternalError"
post:
summary: Create a VPC
operationId: createVPC
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/VPCCreateRequest"
responses:
"202":
description: VPC creation accepted
content:
application/json:
schema:
$ref: "#/components/schemas/VPC"
"409":
description: VPC already exists
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"500":
$ref: "#/components/responses/InternalError"
/vpcs/{name}:
parameters:
- $ref: "#/components/parameters/ResourceName"
get:
summary: Get VPC status and info
operationId: getVPC
responses:
"200":
description: VPC found
content:
application/json:
schema:
$ref: "#/components/schemas/VPC"
"404":
$ref: "#/components/responses/NotFound"
"500":
$ref: "#/components/responses/InternalError"
delete:
summary: Delete a VPC
operationId: deleteVPC
responses:
"202":
description: VPC deletion accepted
content:
application/json:
schema:
$ref: "#/components/schemas/VPC"
"404":
$ref: "#/components/responses/NotFound"
"409":
description: VPC not in a deletable state
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"500":
$ref: "#/components/responses/InternalError"
# ── Subnet ─────────────────────────────────────────────────────────────────
/subnets:
get:
summary: List all subnets
operationId: listSubnets
responses:
"200":
description: List of subnets
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Subnet"
"500":
$ref: "#/components/responses/InternalError"
post:
summary: Create a subnet
operationId: createSubnet
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/SubnetCreateRequest"
responses:
"202":
description: Subnet creation accepted
content:
application/json:
schema:
$ref: "#/components/schemas/Subnet"
"400":
description: Missing required field or unknown iface_type
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"409":
description: Subnet already exists
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"422":
description: Parent VPC does not exist or is not ready
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"500":
$ref: "#/components/responses/InternalError"
/subnets/{name}:
parameters:
- $ref: "#/components/parameters/ResourceName"
get:
summary: Get subnet status and info
operationId: getSubnet
responses:
"200":
description: Subnet found
content:
application/json:
schema:
$ref: "#/components/schemas/Subnet"
"404":
$ref: "#/components/responses/NotFound"
"500":
$ref: "#/components/responses/InternalError"
delete:
summary: Delete a subnet
operationId: deleteSubnet
responses:
"202":
description: Subnet deletion accepted
content:
application/json:
schema:
$ref: "#/components/schemas/Subnet"
"404":
$ref: "#/components/responses/NotFound"
"409":
description: Subnet not in a deletable state
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
"500":
$ref: "#/components/responses/InternalError"
# ── Components ──────────────────────────────────────────────────────────────
components:
parameters:
ResourceName:
name: name
in: path
required: true
schema:
type: string
description: Resource name
schemas:
VPCCreateRequest:
type: object
required: [name]
properties:
name:
type: string
description: Unique name for the VPC, must follow the format vp-[id]
pattern: '^vp-.+'
example: vp-00001
VPC:
type: object
properties:
name:
type: string
example: vp-00001
state:
type: string
enum: [creating, created, deleting, deleted]
example: created
SubnetCreateRequest:
type: object
required: [name, vpc, vxlan_id, gateway_ip, cidr]
properties:
name:
type: string
description: Unique name for the subnet
example: sn-00001
vpc:
type: string
description: Parent VPC name
example: vpc1
vxlan_id:
type: integer
description: VXLAN VNI identifier
example: 100
iface_type:
type: string
description: Interface type key defined in the agent config (e.g. vms, internet, admin). Falls back to default_interface if omitted or unknown.
example: vms
gateway_ip:
type: string
format: ipv4
description: Gateway IP for the subnet
example: "10.10.10.1"
cidr:
type: string
description: Subnet CIDR block
example: "10.10.10.0/24"
Subnet:
type: object
properties:
name:
type: string
example: sn-00001
state:
type: string
enum: [creating, created, deleting, deleted]
example: created
vpc:
type: string
example: vpc1
vxlan_id:
type: integer
example: 100
local_iface:
type: string
description: Resolved interface name
example: br-000000
gateway_ip:
type: string
example: "10.10.10.1"
cidr:
type: string
example: "10.10.10.0/24"
Error:
type: object
properties:
error:
type: string
example: "resource not found"
responses:
NotFound:
description: Resource not found
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
InternalError:
description: Internal server error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"

View file

@ -1,17 +1,44 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"os" "log"
)
var ( agentapi "git.g3e.fr/syonad/two/internal/api/agent"
bin_name = os.Args[0] configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/internal/dispatcher"
agentmetrics "git.g3e.fr/syonad/two/internal/prometheus/agent"
"git.g3e.fr/syonad/two/pkg/db/kv"
promserver "git.g3e.fr/syonad/two/pkg/prometheus"
"git.g3e.fr/syonad/two/pkg/worker"
"github.com/prometheus/client_golang/prometheus"
) )
func main() { func main() {
confFile := flag.String("config", "/etc/two/agent.yml", "config file path")
flag.Parse()
fmt.Printf("%s: Start process\n", bin_name) cfg, err := configuration.LoadConfig(*confFile)
if err != nil {
log.Fatalf("failed to load config: %v", err)
}
os.Exit(0) db := kv.InitDB(kv.Config{Path: cfg.Database.Path}, false)
defer db.Close()
q := worker.New(cfg.Worker.BufferSize)
q.Start(cfg.Worker.Count)
registry := prometheus.NewRegistry()
registry.MustRegister(agentmetrics.NewAgentCollector(db))
apiAddr := fmt.Sprintf("%s:%d", cfg.Api.Address, cfg.Api.Port)
promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port)
d := dispatcher.New(q, db, cfg)
go agentapi.New(d, db).Start(apiAddr)
go promserver.Start(promAddr, registry)
select {}
} }

View file

@ -1,65 +0,0 @@
package main
import (
"flag"
"fmt"
"os"
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/internal/vpc"
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
)
var (
netns = flag.String("netns", "", "Network namespace à faire")
name = flag.String("name", "", "interface name")
action = flag.String("action", "", "Action a faire")
conf_file = flag.String("conf", "/etc/two/agent.yml", "configuration file")
)
var DB *badger.DB
func main() {
flag.Parse()
conf, err := configuration.LoadConfig(*conf_file)
if err != nil {
fmt.Println(err)
return
}
DB = kv.InitDB(kv.Config{
Path: conf.Database.Path,
}, false)
defer DB.Close()
switch *action {
case "create":
kv.AddInDB(DB, "vpc/"+*name+"/state", "creating")
if err := vpc.CreateVPC(DB, *name); err != nil {
fmt.Println(err)
}
case "delete":
kv.AddInDB(DB, "vpc/"+*name+"/state", "deleting")
if err := vpc.DeleteVPC(DB, *name); err != nil {
fmt.Println(err)
}
if state, err := kv.GetFromDB(DB, "vpc/"+*name+"/state"); err != nil {
fmt.Println(err)
os.Exit(1)
} else if state == "deleted" {
kv.DeleteInDB(DB, "vpc/"+*name)
}
case "check":
if state, err := kv.GetFromDB(DB, "vpc/"+*name+"/state"); err != nil {
os.Exit(1)
} else if state != "created" {
os.Exit(1)
}
default:
fmt.Printf("Available commande:\n - create\n - delete\n - check\n")
os.Exit(1)
}
os.Exit(0)
}

View file

@ -1,2 +1,9 @@
database: database:
path: "/var/lib/two/data/" path: "/var/lib/two/data/"
default_interface: br-000000
interfaces:
vms: br-000000
internet: br-000000
admin: br-000000

11
go.mod
View file

@ -5,6 +5,7 @@ go 1.24.0
toolchain go1.24.11 toolchain go1.24.11
require ( require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/coreos/go-systemd/v22 v22.6.0 // indirect github.com/coreos/go-systemd/v22 v22.6.0 // indirect
github.com/dgraph-io/badger/v4 v4.8.0 // indirect github.com/dgraph-io/badger/v4 v4.8.0 // indirect
@ -17,7 +18,12 @@ require (
github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/google/flatbuffers v25.2.10+incompatible // indirect github.com/google/flatbuffers v25.2.10+incompatible // indirect
github.com/klauspost/compress v1.18.0 // indirect github.com/klauspost/compress v1.18.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pelletier/go-toml/v2 v2.2.4 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/prometheus/client_golang v1.23.2 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/common v0.66.1 // indirect
github.com/prometheus/procfs v0.16.1 // indirect
github.com/sagikazarmark/locafero v0.11.0 // indirect github.com/sagikazarmark/locafero v0.11.0 // indirect
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
github.com/spf13/afero v1.15.0 // indirect github.com/spf13/afero v1.15.0 // indirect
@ -31,9 +37,10 @@ require (
go.opentelemetry.io/otel v1.37.0 // indirect go.opentelemetry.io/otel v1.37.0 // indirect
go.opentelemetry.io/otel/metric v1.37.0 // indirect go.opentelemetry.io/otel/metric v1.37.0 // indirect
go.opentelemetry.io/otel/trace v1.37.0 // indirect go.opentelemetry.io/otel/trace v1.37.0 // indirect
go.yaml.in/yaml/v2 v2.4.2 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/net v0.41.0 // indirect golang.org/x/net v0.43.0 // indirect
golang.org/x/sys v0.39.0 // indirect golang.org/x/sys v0.39.0 // indirect
golang.org/x/text v0.28.0 // indirect golang.org/x/text v0.28.0 // indirect
google.golang.org/protobuf v1.36.6 // indirect google.golang.org/protobuf v1.36.8 // indirect
) )

18
go.sum
View file

@ -1,3 +1,5 @@
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/coreos/go-systemd/v22 v22.6.0 h1:aGVa/v8B7hpb0TKl0MWoAavPDmHvobFe5R5zn0bCJWo= github.com/coreos/go-systemd/v22 v22.6.0 h1:aGVa/v8B7hpb0TKl0MWoAavPDmHvobFe5R5zn0bCJWo=
@ -23,8 +25,18 @@ github.com/google/flatbuffers v25.2.10+incompatible h1:F3vclr7C3HpB1k9mxCGRMXq6F
github.com/google/flatbuffers v25.2.10+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8= github.com/google/flatbuffers v25.2.10+incompatible/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv5+zNeJKJCa8=
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo= github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ= github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o=
github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg=
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs=
github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA=
github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg=
github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is=
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc= github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik= github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw= github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=
@ -51,10 +63,14 @@ go.opentelemetry.io/otel/metric v1.37.0 h1:mvwbQS5m0tbmqML4NqK+e3aDiO02vsf/Wgbsd
go.opentelemetry.io/otel/metric v1.37.0/go.mod h1:04wGrZurHYKOc+RKeye86GwKiTb9FKm1WHtO+4EVr2E= go.opentelemetry.io/otel/metric v1.37.0/go.mod h1:04wGrZurHYKOc+RKeye86GwKiTb9FKm1WHtO+4EVr2E=
go.opentelemetry.io/otel/trace v1.37.0 h1:HLdcFNbRQBE2imdSEgm/kwqmQj1Or1l/7bW6mxVK7z4= go.opentelemetry.io/otel/trace v1.37.0 h1:HLdcFNbRQBE2imdSEgm/kwqmQj1Or1l/7bW6mxVK7z4=
go.opentelemetry.io/otel/trace v1.37.0/go.mod h1:TlgrlQ+PtQO5XFerSPUYG0JSgGyryXewPGyayAWSBS0= go.opentelemetry.io/otel/trace v1.37.0/go.mod h1:TlgrlQ+PtQO5XFerSPUYG0JSgGyryXewPGyayAWSBS0=
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw= golang.org/x/net v0.41.0 h1:vBTly1HeNPEn3wtREYfy4GZ/NECgw2Cnl+nK6Nz3uvw=
golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA= golang.org/x/net v0.41.0/go.mod h1:B/K4NNqkfmg07DQYrbwvSluqCJOOXwUjeb/5lOisjbA=
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA= golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
@ -65,4 +81,6 @@ golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU= golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY= google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

View file

@ -0,0 +1,33 @@
package agentapi
type VPCCreateRequest struct {
Name string `json:"name"`
}
type VPC struct {
Name string `json:"name"`
State string `json:"state"`
}
type SubnetCreateRequest struct {
Name string `json:"name"`
VPC string `json:"vpc"`
VxlanID int `json:"vxlan_id"`
IfaceType string `json:"iface_type"`
GatewayIP string `json:"gateway_ip"`
CIDR string `json:"cidr"`
}
type Subnet struct {
Name string `json:"name"`
State string `json:"state"`
VPC string `json:"vpc"`
VxlanID int `json:"vxlan_id"`
LocalIface string `json:"local_iface"`
GatewayIP string `json:"gateway_ip"`
CIDR string `json:"cidr"`
}
type ErrorResponse struct {
Error string `json:"error"`
}

View file

@ -0,0 +1,35 @@
package agentapi
import (
"log"
"net/http"
"git.g3e.fr/syonad/two/internal/dispatcher"
"github.com/dgraph-io/badger/v4"
)
type Server struct {
dispatcher *dispatcher.Dispatcher
db *badger.DB
}
func New(d *dispatcher.Dispatcher, db *badger.DB) *Server {
return &Server{dispatcher: d, db: db}
}
func (s *Server) Start(address string) {
mux := http.NewServeMux()
mux.HandleFunc("/vpcs", s.VpcsHandler)
mux.HandleFunc("/vpcs/", s.VpcByNameHandler)
mux.HandleFunc("/subnets", s.SubnetsHandler)
mux.HandleFunc("/subnets/", s.SubnetByNameHandler)
log.Printf("API server listening on %s", address)
log.Fatal(http.ListenAndServe(address, logMiddleware(mux)))
}
func logMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s %s", r.RemoteAddr, r.Method, r.URL.Path)
next.ServeHTTP(w, r)
})
}

View file

@ -0,0 +1,40 @@
package agentapi
import (
"encoding/json"
"net/http"
"strings"
"git.g3e.fr/syonad/two/internal/dispatcher"
)
func (s *Server) SubnetByNameHandler(w http.ResponseWriter, r *http.Request) {
name := strings.TrimPrefix(r.URL.Path, "/subnets/")
if name == "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(ErrorResponse{Error: "resource not found"})
return
}
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case http.MethodGet:
s.getSubnet(w, r, name)
case http.MethodDelete:
s.deleteSubnet(w, r, name)
default:
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
}
}
func (s *Server) getSubnet(w http.ResponseWriter, r *http.Request, name string) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(Subnet{Name: name, State: "created"})
}
func (s *Server) deleteSubnet(w http.ResponseWriter, r *http.Request, name string) {
s.dispatcher.Dispatch(dispatcher.DeleteSubnetCommand{Name: name})
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(Subnet{Name: name, State: "deleting"})
}

View file

@ -0,0 +1,56 @@
package agentapi
import (
"encoding/json"
"net/http"
"git.g3e.fr/syonad/two/internal/dispatcher"
)
func (s *Server) SubnetsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case http.MethodGet:
s.listSubnets(w, r)
case http.MethodPost:
s.postSubnet(w, r)
default:
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
}
}
func (s *Server) listSubnets(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode([]Subnet{})
}
func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
var req SubnetCreateRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"})
return
}
if req.Name == "" || req.VPC == "" || req.IfaceType == "" || req.GatewayIP == "" || req.CIDR == "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, iface_type, gateway_ip and cidr are required"})
return
}
s.dispatcher.Dispatch(dispatcher.CreateSubnetCommand{
Name: req.Name,
VPC: req.VPC,
VxlanID: req.VxlanID,
IfaceType: req.IfaceType,
GatewayIP: req.GatewayIP,
CIDR: req.CIDR,
})
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(Subnet{
Name: req.Name,
State: "creating",
VPC: req.VPC,
VxlanID: req.VxlanID,
GatewayIP: req.GatewayIP,
CIDR: req.CIDR,
})
}

40
internal/api/agent/vpc.go Normal file
View file

@ -0,0 +1,40 @@
package agentapi
import (
"encoding/json"
"net/http"
"strings"
"git.g3e.fr/syonad/two/internal/dispatcher"
)
func (s *Server) VpcByNameHandler(w http.ResponseWriter, r *http.Request) {
name := strings.TrimPrefix(r.URL.Path, "/vpcs/")
if name == "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(ErrorResponse{Error: "resource not found"})
return
}
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case http.MethodGet:
s.getVpc(w, r, name)
case http.MethodDelete:
s.deleteVpc(w, r, name)
default:
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
}
}
func (s *Server) getVpc(w http.ResponseWriter, _ *http.Request, name string) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(VPC{Name: name, State: "created"})
}
func (s *Server) deleteVpc(w http.ResponseWriter, _ *http.Request, name string) {
s.dispatcher.Dispatch(dispatcher.DeleteVPCCommand{Name: name})
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(VPC{Name: name, State: "deleting"})
}

View file

@ -0,0 +1,42 @@
package agentapi
import (
"encoding/json"
"net/http"
"git.g3e.fr/syonad/two/internal/dispatcher"
)
func (s *Server) VpcsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.Method {
case http.MethodGet:
s.listVpcs(w, r)
case http.MethodPost:
s.postVpc(w, r)
default:
http.Error(w, `{"error":"method not allowed"}`, http.StatusMethodNotAllowed)
}
}
func (s *Server) listVpcs(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode([]VPC{})
}
func (s *Server) postVpc(w http.ResponseWriter, r *http.Request) {
var req VPCCreateRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"})
return
}
if req.Name == "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(ErrorResponse{Error: "name is required"})
return
}
s.dispatcher.Dispatch(dispatcher.CreateVPCCommand{Name: req.Name})
w.WriteHeader(http.StatusAccepted)
json.NewEncoder(w).Encode(VPC{Name: req.Name, State: "creating"})
}

View file

@ -8,6 +8,20 @@ type Config struct {
Database struct { Database struct {
Path string `mapstructure:"path"` Path string `mapstructure:"path"`
} `mapstructure:"database"` } `mapstructure:"database"`
Api struct {
Address string `mapstructure:"address"`
Port int `mapstructure:"port"`
} `mapstructure:"api"`
Prometheus struct {
Address string `mapstructure:"address"`
Port int `mapstructure:"port"`
} `mapstructure:"prometheus"`
Worker struct {
Count int `mapstructure:"count"`
BufferSize int `mapstructure:"buffer_size"`
} `mapstructure:"worker"`
DefaultInterface string `mapstructure:"default_interface"`
Interfaces map[string]string `mapstructure:"interfaces"`
} }
func LoadConfig(path string) (*Config, error) { func LoadConfig(path string) (*Config, error) {
@ -16,6 +30,13 @@ func LoadConfig(path string) (*Config, error) {
v.SetConfigType("yaml") v.SetConfigType("yaml")
v.SetDefault("database.path", "/var/lib/two/data/") v.SetDefault("database.path", "/var/lib/two/data/")
v.SetDefault("api.address", "")
v.SetDefault("api.port", 8080)
v.SetDefault("prometheus.address", "")
v.SetDefault("prometheus.port", 9090)
v.SetDefault("worker.count", 4)
v.SetDefault("worker.buffer_size", 100)
v.SetDefault("default_interface", "br-000000")
v.ReadInConfig() v.ReadInConfig()

View file

@ -0,0 +1,31 @@
package dispatcher
import (
"log"
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/pkg/worker"
"github.com/dgraph-io/badger/v4"
)
type Command interface {
Execute(db *badger.DB, cfg *configuration.Config) error
}
type Dispatcher struct {
queue *worker.Queue
db *badger.DB
cfg *configuration.Config
}
func New(queue *worker.Queue, db *badger.DB, cfg *configuration.Config) *Dispatcher {
return &Dispatcher{queue: queue, db: db, cfg: cfg}
}
func (d *Dispatcher) Dispatch(cmd Command) {
d.queue.Submit(func() {
if err := cmd.Execute(d.db, d.cfg); err != nil {
log.Printf("command error (%T): %v", cmd, err)
}
})
}

View file

@ -0,0 +1,54 @@
package dispatcher
import (
"fmt"
"os"
"strconv"
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/internal/subnet"
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
)
type CreateSubnetCommand struct {
Name string
VPC string
VxlanID int
IfaceType string
GatewayIP string
CIDR string
}
func (c CreateSubnetCommand) Execute(db *badger.DB, cfg *configuration.Config) error {
localIface, ok := cfg.Interfaces[c.IfaceType]
if !ok {
localIface = cfg.DefaultInterface
}
kv.AddInDB(db, "subnet/"+c.Name+"/state", "creating")
kv.AddInDB(db, "subnet/"+c.Name+"/vpc", c.VPC)
kv.AddInDB(db, "subnet/"+c.Name+"/vxlan_id", strconv.Itoa(c.VxlanID))
kv.AddInDB(db, "subnet/"+c.Name+"/local_iface", localIface)
kv.AddInDB(db, "subnet/"+c.Name+"/gateway_ip", c.GatewayIP)
kv.AddInDB(db, "subnet/"+c.Name+"/cidr", c.CIDR)
return subnet.CreateSubnet(db, c.Name)
}
type DeleteSubnetCommand struct {
Name string
}
func (c DeleteSubnetCommand) Execute(db *badger.DB, _ *configuration.Config) error {
kv.AddInDB(db, "subnet/"+c.Name+"/state", "deleting")
if err := subnet.DeleteSubnet(db, c.Name); err != nil {
fmt.Println(err)
os.Exit(1)
}
if state, err := kv.GetFromDB(db, "subnet/"+c.Name+"/state"); err != nil {
fmt.Println(err)
os.Exit(1)
} else if state == "deleted" {
kv.DeleteInDB(db, "subnet/"+c.Name)
}
return nil
}

View file

@ -0,0 +1,36 @@
package dispatcher
import (
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/internal/vpc"
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
)
type CreateVPCCommand struct {
Name string
}
func (c CreateVPCCommand) Execute(db *badger.DB, _ *configuration.Config) error {
kv.AddInDB(db, "vpc/"+c.Name+"/state", "creating")
return vpc.CreateVPC(db, c.Name)
}
type DeleteVPCCommand struct {
Name string
}
func (c DeleteVPCCommand) Execute(db *badger.DB, _ *configuration.Config) error {
kv.AddInDB(db, "vpc/"+c.Name+"/state", "deleting")
if err := vpc.DeleteVPC(db, c.Name); err != nil {
return err
}
state, err := kv.GetFromDB(db, "vpc/"+c.Name+"/state")
if err != nil {
return err
}
if state == "deleted" {
kv.DeleteInDB(db, "vpc/"+c.Name)
}
return nil
}

View file

@ -1,20 +1,22 @@
package netif package netif
import ( import (
"net"
"github.com/vishvananda/netlink" "github.com/vishvananda/netlink"
) )
func CreateVxlan(name string, vxlanID int, localIP net.IP) error { func CreateVxlan(name string, vxlanID int, localIface string) error {
link, err := netlink.LinkByName(localIface)
if err != nil {
return err
}
vxlan := &netlink.Vxlan{ vxlan := &netlink.Vxlan{
LinkAttrs: netlink.LinkAttrs{ LinkAttrs: netlink.LinkAttrs{
Name: name, Name: name,
}, },
VxlanId: vxlanID, VxlanId: vxlanID,
Port: 4789, Port: 4789,
SrcAddr: localIP, VtepDevIndex: link.Attrs().Index,
Learning: false, Learning: false,
} }
return netlink.LinkAdd(vxlan) return netlink.LinkAdd(vxlan)
} }

View file

@ -0,0 +1,67 @@
package agentmetrics
import (
"strings"
"git.g3e.fr/syonad/two/pkg/db/kv"
"github.com/dgraph-io/badger/v4"
"github.com/prometheus/client_golang/prometheus"
)
var allStates = []string{"creating", "created", "deleting", "deleted"}
// AgentCollector implements prometheus.Collector and exposes agent metrics
// by querying the BadgerDB on each scrape.
type AgentCollector struct {
db *badger.DB
vpcsTotal *prometheus.Desc
subnetsTotal *prometheus.Desc
}
func NewAgentCollector(db *badger.DB) *AgentCollector {
return &AgentCollector{
db: db,
vpcsTotal: prometheus.NewDesc(
"syonad_vpcs_total",
"Number of VPCs by state.",
[]string{"state"}, nil,
),
subnetsTotal: prometheus.NewDesc(
"syonad_subnets_total",
"Number of subnets by state.",
[]string{"state"}, nil,
),
}
}
func (c *AgentCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.vpcsTotal
ch <- c.subnetsTotal
}
func (c *AgentCollector) Collect(ch chan<- prometheus.Metric) {
c.collectStates(ch, "vpc/", c.vpcsTotal)
c.collectStates(ch, "subnet/", c.subnetsTotal)
}
// collectStates counts resources under the given DB prefix by their state value
// and emits one gauge per state label.
func (c *AgentCollector) collectStates(ch chan<- prometheus.Metric, prefix string, desc *prometheus.Desc) {
counts := make(map[string]float64, len(allStates))
for _, s := range allStates {
counts[s] = 0
}
items, err := kv.ListByPrefix(c.db, prefix)
if err == nil {
for key, val := range items {
if strings.HasSuffix(key, "/state") {
counts[val]++
}
}
}
for _, state := range allStates {
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, counts[state], state)
}
}

View file

@ -40,13 +40,9 @@ func CreateSubnet(db *badger.DB, subnetName string) error {
return fmt.Errorf("parse vxlan_id: %w", err) return fmt.Errorf("parse vxlan_id: %w", err)
} }
localIPStr, err := kv.GetFromDB(db, "subnet/"+subnetName+"/local_ip") localIface, err := kv.GetFromDB(db, "subnet/"+subnetName+"/local_iface")
if err != nil { if err != nil {
return fmt.Errorf("get local_ip: %w", err) return fmt.Errorf("get local_iface: %w", err)
}
localIP := net.ParseIP(localIPStr)
if localIP == nil {
return fmt.Errorf("invalid local_ip: %s", localIPStr)
} }
gatewayIPStr, err := kv.GetFromDB(db, "subnet/"+subnetName+"/gateway_ip") gatewayIPStr, err := kv.GetFromDB(db, "subnet/"+subnetName+"/gateway_ip")
@ -90,7 +86,7 @@ func CreateSubnet(db *badger.DB, subnetName string) error {
} }
// vxlan // vxlan
if err := netif.CreateVxlan(vxlanIface, vxlanID, localIP); err != nil { if err := netif.CreateVxlan(vxlanIface, vxlanID, localIface); err != nil {
return fmt.Errorf("create vxlan: %w", err) return fmt.Errorf("create vxlan: %w", err)
} }

View file

@ -1,6 +1,8 @@
package vpc package vpc
import ( import (
"strings"
"git.g3e.fr/syonad/two/internal/netif" "git.g3e.fr/syonad/two/internal/netif"
"git.g3e.fr/syonad/two/internal/netns" "git.g3e.fr/syonad/two/internal/netns"
"git.g3e.fr/syonad/two/pkg/db/kv" "git.g3e.fr/syonad/two/pkg/db/kv"
@ -9,49 +11,40 @@ import (
) )
func CreateVPC(db *badger.DB, name string) error { func CreateVPC(db *badger.DB, name string) error {
// missing
// search data in db
// change state in db
// create netns
if state, err := kv.GetFromDB(db, "vpc/"+name+"/state"); err != nil { if state, err := kv.GetFromDB(db, "vpc/"+name+"/state"); err != nil {
return err return err
} else if state == "creating" { } else if state == "creating" {
vpcID := strings.SplitN(name, "-", 2)[1]
if err := netns.Create(name); err != nil { if err := netns.Create(name); err != nil {
return err return err
} }
// create veth public for this netns if err := netif.CreateVethToNetns("vp-"+vpcID+"-e", "vp-"+vpcID+"-i", "/var/run/netns/"+name, 9000); err != nil {
if err := netif.CreateVethToNetns("vp-"+name+"-e", "vp-public-i", "/var/run/netns/"+name, 9000); err != nil {
return err return err
} }
// create public bridge in netns
if err := netns.Call(name, func() error { if err := netns.Call(name, func() error {
return netif.CreateBridge("br-public", 1500) return netif.CreateBridge("br-public", 1500)
}); err != nil { }); err != nil {
return err return err
} }
// set veth to ext public bridge if err := netif.BridgeSetMaster("vp-"+vpcID+"-e", "br-public"); err != nil {
if err := netif.BridgeSetMaster("vp-"+name+"-e", "br-public"); err != nil {
return err return err
} }
// set veth to int public bridge
if err := netns.Call(name, func() error { if err := netns.Call(name, func() error {
return netif.BridgeSetMaster("vp-public-i", "br-public") return netif.BridgeSetMaster("vp-"+vpcID+"-i", "br-public")
}); err != nil { }); err != nil {
return err return err
} }
// set set ext veth up if err := netif.LinkSetUp("vp-" + vpcID + "-e"); err != nil {
if err := netif.LinkSetUp("vp-" + name + "-e"); err != nil {
return err return err
} }
// set set int veth up
if err := netns.Call(name, func() error { if err := netns.Call(name, func() error {
return netif.LinkSetUp("vp-public-i") return netif.LinkSetUp("vp-" + vpcID + "-i")
}); err != nil { }); err != nil {
return err return err
} }

View file

@ -1,6 +1,8 @@
package vpc package vpc
import ( import (
"strings"
"git.g3e.fr/syonad/two/internal/netif" "git.g3e.fr/syonad/two/internal/netif"
"git.g3e.fr/syonad/two/internal/netns" "git.g3e.fr/syonad/two/internal/netns"
"git.g3e.fr/syonad/two/pkg/db/kv" "git.g3e.fr/syonad/two/pkg/db/kv"
@ -12,7 +14,9 @@ func DeleteVPC(db *badger.DB, name string) error {
if state, err := kv.GetFromDB(db, "vpc/"+name+"/state"); err != nil { if state, err := kv.GetFromDB(db, "vpc/"+name+"/state"); err != nil {
return err return err
} else if state == "deleting" { } else if state == "deleting" {
if err := netif.DeleteLink("vp-" + name + "-e"); err != nil { vpcID := strings.SplitN(name, "-", 2)[1]
if err := netif.DeleteLink("vp-" + vpcID + "-e"); err != nil {
return err return err
} }

33
pkg/db/kv/listByPrefix.go Normal file
View file

@ -0,0 +1,33 @@
package kv
import (
"github.com/dgraph-io/badger/v4"
)
// ListByPrefix returns all key-value pairs whose key starts with prefix.
func ListByPrefix(db *badger.DB, prefix string) (map[string]string, error) {
result := make(map[string]string)
p := []byte(prefix)
err := db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
it := txn.NewIterator(opts)
defer it.Close()
for it.Seek(p); it.ValidForPrefix(p); it.Next() {
item := it.Item()
key := string(item.Key())
val, err := item.ValueCopy(nil)
if err != nil {
return err
}
result[key] = string(val)
}
return nil
})
return result, err
}

20
pkg/prometheus/server.go Normal file
View file

@ -0,0 +1,20 @@
package promserver
import (
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Start launches the Prometheus metrics HTTP server on the given address.
// The provided registry is used to expose metrics at /metrics.
func Start(address string, registry *prometheus.Registry) {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{
EnableOpenMetrics: true,
}))
log.Printf("Prometheus server listening on %s", address)
log.Fatal(http.ListenAndServe(address, mux))
}

33
pkg/worker/queue.go Normal file
View file

@ -0,0 +1,33 @@
package worker
import "log"
// Task is a function to be executed asynchronously by a worker.
type Task func()
// Queue is a FIFO channel-backed task queue consumed by worker goroutines.
type Queue struct {
tasks chan Task
}
// New creates a Queue with the given channel buffer size.
func New(bufferSize int) *Queue {
return &Queue{tasks: make(chan Task, bufferSize)}
}
// Submit enqueues a task. Blocks if the queue is full.
func (q *Queue) Submit(t Task) {
q.tasks <- t
}
// Start launches n worker goroutines that consume and execute tasks.
func (q *Queue) Start(n int) {
log.Printf("worker: starting %d workers", n)
for i := range n {
go func(id int) {
for task := range q.tasks {
task()
}
}(i)
}
}