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:
parent
c26606787e
commit
450d0c801a
5 changed files with 86 additions and 7 deletions
|
|
@ -78,14 +78,30 @@ func TestRenderConfig_VendorData_ContainsSSHKey(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRenderConfig_NetworkConfig(t *testing.T) {
|
func TestRenderConfig_NetworkConfigMatchesEveryInterfaceByMAC(t *testing.T) {
|
||||||
cfg := newCfg()
|
cfg := newCfg()
|
||||||
out, err := RenderConfig("templates/network-config.tmpl", cfg)
|
cfg.NICs = []NIC{{MAC: "52:54:00:49:01:0c", RouteMetric: 100}, {MAC: "52:54:00:49:02:16", RouteMetric: 200}}
|
||||||
|
out, err := renderDocument(DocNetworkConfig, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("RenderConfig network-config : %v", err)
|
t.Fatalf("renderDocument network-config : %v", err)
|
||||||
}
|
}
|
||||||
if !strings.Contains(out, "dhcp4: true") {
|
want := `version: 2
|
||||||
t.Errorf("dhcp4 absent du network-config :\n%s", out)
|
ethernets:
|
||||||
|
eth0:
|
||||||
|
match:
|
||||||
|
macaddress: "52:54:00:49:01:0c"
|
||||||
|
dhcp4: true
|
||||||
|
dhcp4-overrides:
|
||||||
|
route-metric: 100
|
||||||
|
eth1:
|
||||||
|
match:
|
||||||
|
macaddress: "52:54:00:49:02:16"
|
||||||
|
dhcp4: true
|
||||||
|
dhcp4-overrides:
|
||||||
|
route-metric: 200
|
||||||
|
`
|
||||||
|
if out != want {
|
||||||
|
t.Errorf("network-config :\n%s\nattendu :\n%s", out, want)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,5 +27,11 @@ type NoCloudConfig struct {
|
||||||
Name string
|
Name string
|
||||||
Password string
|
Password string
|
||||||
SSHKEY string
|
SSHKEY string
|
||||||
|
NICs []NIC
|
||||||
Documents map[string]string
|
Documents map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type NIC struct {
|
||||||
|
MAC string
|
||||||
|
RouteMetric int
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,10 @@
|
||||||
version: 2
|
version: 2
|
||||||
ethernets:
|
ethernets:
|
||||||
eth0:
|
{{- range $i, $nic := .NICs }}
|
||||||
|
eth{{ $i }}:
|
||||||
|
match:
|
||||||
|
macaddress: "{{ $nic.MAC }}"
|
||||||
dhcp4: true
|
dhcp4: true
|
||||||
|
dhcp4-overrides:
|
||||||
|
route-metric: {{ $nic.RouteMetric }}
|
||||||
|
{{- end }}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
configuration "git.g3e.fr/syonad/two/internal/config/agent"
|
||||||
"git.g3e.fr/syonad/two/internal/dhcpbackend"
|
"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,
|
BindPort: d.metadataPort,
|
||||||
Password: d.password,
|
Password: d.password,
|
||||||
SSHKEY: d.sshkey,
|
SSHKEY: d.sshkey,
|
||||||
|
NICs: metadataNICs(d.nics),
|
||||||
Documents: d.documents,
|
Documents: d.documents,
|
||||||
}, cfg, false); err != nil {
|
}, cfg, false); err != nil {
|
||||||
return fmt.Errorf("start metadata: %w", err)
|
return fmt.Errorf("start metadata: %w", err)
|
||||||
|
|
@ -171,3 +173,16 @@ func copyFile(src, dst string) error {
|
||||||
}
|
}
|
||||||
return out.Sync()
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
36
internal/vm/create_test.go
Normal file
36
internal/vm/create_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue