f-21: code: change from ip to interface type
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
066b90dff4
commit
9ec6e64327
8 changed files with 41 additions and 40 deletions
|
|
@ -36,7 +36,7 @@ func main() {
|
||||||
apiAddr := fmt.Sprintf("%s:%d", cfg.Api.Address, cfg.Api.Port)
|
apiAddr := fmt.Sprintf("%s:%d", cfg.Api.Address, cfg.Api.Port)
|
||||||
promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port)
|
promAddr := fmt.Sprintf("%s:%d", cfg.Prometheus.Address, cfg.Prometheus.Port)
|
||||||
|
|
||||||
d := dispatcher.New(q, db)
|
d := dispatcher.New(q, db, cfg.Interfaces)
|
||||||
go agentapi.New(d, db).Start(apiAddr)
|
go agentapi.New(d, db).Start(apiAddr)
|
||||||
go promserver.Start(promAddr, registry)
|
go promserver.Start(promAddr, registry)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ type SubnetCreateRequest struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
VPC string `json:"vpc"`
|
VPC string `json:"vpc"`
|
||||||
VxlanID int `json:"vxlan_id"`
|
VxlanID int `json:"vxlan_id"`
|
||||||
LocalIP string `json:"local_ip"`
|
IfaceType string `json:"iface_type"`
|
||||||
GatewayIP string `json:"gateway_ip"`
|
GatewayIP string `json:"gateway_ip"`
|
||||||
CIDR string `json:"cidr"`
|
CIDR string `json:"cidr"`
|
||||||
}
|
}
|
||||||
|
|
@ -23,7 +23,7 @@ type Subnet struct {
|
||||||
State string `json:"state"`
|
State string `json:"state"`
|
||||||
VPC string `json:"vpc"`
|
VPC string `json:"vpc"`
|
||||||
VxlanID int `json:"vxlan_id"`
|
VxlanID int `json:"vxlan_id"`
|
||||||
LocalIP string `json:"local_ip"`
|
LocalIface string `json:"local_iface"`
|
||||||
GatewayIP string `json:"gateway_ip"`
|
GatewayIP string `json:"gateway_ip"`
|
||||||
CIDR string `json:"cidr"`
|
CIDR string `json:"cidr"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,16 +31,16 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
|
||||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"})
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "invalid request body"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.Name == "" || req.VPC == "" || req.LocalIP == "" || req.GatewayIP == "" || req.CIDR == "" {
|
if req.Name == "" || req.VPC == "" || req.IfaceType == "" || req.GatewayIP == "" || req.CIDR == "" {
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, local_ip, gateway_ip and cidr are required"})
|
json.NewEncoder(w).Encode(ErrorResponse{Error: "name, vpc, iface_type, gateway_ip and cidr are required"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.dispatcher.Dispatch(dispatcher.CreateSubnetCommand{
|
s.dispatcher.Dispatch(dispatcher.CreateSubnetCommand{
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
VPC: req.VPC,
|
VPC: req.VPC,
|
||||||
VxlanID: req.VxlanID,
|
VxlanID: req.VxlanID,
|
||||||
LocalIP: req.LocalIP,
|
IfaceType: req.IfaceType,
|
||||||
GatewayIP: req.GatewayIP,
|
GatewayIP: req.GatewayIP,
|
||||||
CIDR: req.CIDR,
|
CIDR: req.CIDR,
|
||||||
})
|
})
|
||||||
|
|
@ -50,9 +50,7 @@ func (s *Server) postSubnet(w http.ResponseWriter, r *http.Request) {
|
||||||
State: "creating",
|
State: "creating",
|
||||||
VPC: req.VPC,
|
VPC: req.VPC,
|
||||||
VxlanID: req.VxlanID,
|
VxlanID: req.VxlanID,
|
||||||
LocalIP: req.LocalIP,
|
|
||||||
GatewayIP: req.GatewayIP,
|
GatewayIP: req.GatewayIP,
|
||||||
CIDR: req.CIDR,
|
CIDR: req.CIDR,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,21 +8,22 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Command interface {
|
type Command interface {
|
||||||
Execute(db *badger.DB) error
|
Execute(db *badger.DB, interfaces map[string]string) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type Dispatcher struct {
|
type Dispatcher struct {
|
||||||
queue *worker.Queue
|
queue *worker.Queue
|
||||||
db *badger.DB
|
db *badger.DB
|
||||||
|
interfaces map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(queue *worker.Queue, db *badger.DB) *Dispatcher {
|
func New(queue *worker.Queue, db *badger.DB, interfaces map[string]string) *Dispatcher {
|
||||||
return &Dispatcher{queue: queue, db: db}
|
return &Dispatcher{queue: queue, db: db, interfaces: interfaces}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Dispatcher) Dispatch(cmd Command) {
|
func (d *Dispatcher) Dispatch(cmd Command) {
|
||||||
d.queue.Submit(func() {
|
d.queue.Submit(func() {
|
||||||
if err := cmd.Execute(d.db); err != nil {
|
if err := cmd.Execute(d.db, d.interfaces); err != nil {
|
||||||
log.Printf("command error (%T): %v", cmd, err)
|
log.Printf("command error (%T): %v", cmd, err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -14,16 +14,20 @@ type CreateSubnetCommand struct {
|
||||||
Name string
|
Name string
|
||||||
VPC string
|
VPC string
|
||||||
VxlanID int
|
VxlanID int
|
||||||
LocalIP string
|
IfaceType string
|
||||||
GatewayIP string
|
GatewayIP string
|
||||||
CIDR string
|
CIDR string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c CreateSubnetCommand) Execute(db *badger.DB) error {
|
func (c CreateSubnetCommand) Execute(db *badger.DB, interfaces map[string]string) error {
|
||||||
|
localIface, ok := interfaces[c.IfaceType]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("unknown iface_type %q: not found in config", c.IfaceType)
|
||||||
|
}
|
||||||
kv.AddInDB(db, "subnet/"+c.Name+"/state", "creating")
|
kv.AddInDB(db, "subnet/"+c.Name+"/state", "creating")
|
||||||
kv.AddInDB(db, "subnet/"+c.Name+"/vpc", c.VPC)
|
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+"/vxlan_id", strconv.Itoa(c.VxlanID))
|
||||||
kv.AddInDB(db, "subnet/"+c.Name+"/local_ip", c.LocalIP)
|
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+"/gateway_ip", c.GatewayIP)
|
||||||
kv.AddInDB(db, "subnet/"+c.Name+"/cidr", c.CIDR)
|
kv.AddInDB(db, "subnet/"+c.Name+"/cidr", c.CIDR)
|
||||||
return subnet.CreateSubnet(db, c.Name)
|
return subnet.CreateSubnet(db, c.Name)
|
||||||
|
|
@ -33,7 +37,7 @@ type DeleteSubnetCommand struct {
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c DeleteSubnetCommand) Execute(db *badger.DB) error {
|
func (c DeleteSubnetCommand) Execute(db *badger.DB, _ map[string]string) error {
|
||||||
kv.AddInDB(db, "subnet/"+c.Name+"/state", "deleting")
|
kv.AddInDB(db, "subnet/"+c.Name+"/state", "deleting")
|
||||||
if err := subnet.DeleteSubnet(db, c.Name); err != nil {
|
if err := subnet.DeleteSubnet(db, c.Name); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ type CreateVPCCommand struct {
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c CreateVPCCommand) Execute(db *badger.DB) error {
|
func (c CreateVPCCommand) Execute(db *badger.DB, _ map[string]string) error {
|
||||||
kv.AddInDB(db, "vpc/"+c.Name+"/state", "creating")
|
kv.AddInDB(db, "vpc/"+c.Name+"/state", "creating")
|
||||||
return vpc.CreateVPC(db, c.Name)
|
return vpc.CreateVPC(db, c.Name)
|
||||||
}
|
}
|
||||||
|
|
@ -19,7 +19,7 @@ type DeleteVPCCommand struct {
|
||||||
Name string
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c DeleteVPCCommand) Execute(db *badger.DB) error {
|
func (c DeleteVPCCommand) Execute(db *badger.DB, _ map[string]string) error {
|
||||||
kv.AddInDB(db, "vpc/"+c.Name+"/state", "deleting")
|
kv.AddInDB(db, "vpc/"+c.Name+"/state", "deleting")
|
||||||
if err := vpc.DeleteVPC(db, c.Name); err != nil {
|
if err := vpc.DeleteVPC(db, c.Name); err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,21 @@
|
||||||
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)
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue