f-73: metadata: network-config par VM, une entrée ethN par NIC visée par sa MAC, métrique croissante des NIC secondaires #73

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-10-20 00:36:14 +02:00
commit 450d0c801a
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
5 changed files with 86 additions and 7 deletions

View file

@ -6,6 +6,7 @@ import (
"net"
"os"
"path/filepath"
"strings"
configuration "git.g3e.fr/syonad/two/internal/config/agent"
"git.g3e.fr/syonad/two/internal/dhcpbackend"
@ -71,6 +72,7 @@ func StartVM(db *badger.DB, name string, cfg *configuration.Config) error {
BindPort: d.metadataPort,
Password: d.password,
SSHKEY: d.sshkey,
NICs: metadataNICs(d.nics),
Documents: d.documents,
}, cfg, false); err != nil {
return fmt.Errorf("start metadata: %w", err)
@ -171,3 +173,16 @@ func copyFile(src, dst string) error {
}
return out.Sync()
}
func metadataNICs(nics []nicData) []metadata.NIC {
out := make([]metadata.NIC, len(nics))
next := 200
for i, n := range nics {
out[i] = metadata.NIC{MAC: strings.ToLower(n.mac), RouteMetric: 100}
if !n.primary {
out[i].RouteMetric = next
next += 100
}
}
return out
}

View file

@ -0,0 +1,36 @@
package vm
import (
"reflect"
"testing"
"git.g3e.fr/syonad/two/internal/metadata"
)
func TestMetadataNICs_PrimaryKeepsTheLowestMetricWhateverItsPosition(t *testing.T) {
nics := []nicData{
{index: 0, mac: "52:54:00:49:01:0C"},
{index: 1, mac: "52:54:00:49:02:16", primary: true},
{index: 2, mac: "52:54:00:49:03:20"},
}
got := metadataNICs(nics)
want := []metadata.NIC{
{MAC: "52:54:00:49:01:0c", RouteMetric: 200},
{MAC: "52:54:00:49:02:16", RouteMetric: 100},
{MAC: "52:54:00:49:03:20", RouteMetric: 300},
}
if !reflect.DeepEqual(got, want) {
t.Errorf("metadataNICs = %+v, attendu %+v", got, want)
}
}
func TestMetadataNICs_SingleInterface(t *testing.T) {
got := metadataNICs([]nicData{{mac: "52:54:00:00:00:05", primary: true}})
want := []metadata.NIC{{MAC: "52:54:00:00:00:05", RouteMetric: 100}}
if !reflect.DeepEqual(got, want) {
t.Errorf("metadataNICs = %+v, attendu %+v", got, want)
}
}