mirror of
https://github.com/rancher/os.git
synced 2025-09-16 06:59:12 +00:00
Refactor the cloud-init metadata to return a netconf.NetworkConfig
Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
@@ -102,10 +102,11 @@ func (cd *ConfigDrive) FetchMetadata() (metadata datasource.Metadata, err error)
|
||||
|
||||
metadata.SSHPublicKeys = m.SSHAuthorizedKeyMap
|
||||
metadata.Hostname = m.Hostname
|
||||
if m.NetworkConfig.ContentPath != "" {
|
||||
metadata.NetworkConfig, err = cd.tryReadFile(path.Join(cd.openstackRoot(), m.NetworkConfig.ContentPath))
|
||||
}
|
||||
|
||||
// TODO: I don't think we've used this for anything
|
||||
/* if m.NetworkConfig.ContentPath != "" {
|
||||
metadata.NetworkConfig, err = cd.tryReadFile(path.Join(cd.openstackRoot(), m.NetworkConfig.ContentPath))
|
||||
}
|
||||
*/
|
||||
return
|
||||
}
|
||||
|
||||
|
@@ -48,8 +48,7 @@ func TestFetchMetadata(t *testing.T) {
|
||||
test.File{Path: "/media/configdrive/openstack/config_file.json", Contents: "make it work"},
|
||||
),
|
||||
metadata: datasource.Metadata{
|
||||
Hostname: "host",
|
||||
NetworkConfig: []byte("make it work"),
|
||||
Hostname: "host",
|
||||
SSHPublicKeys: map[string]string{
|
||||
"1": "key1",
|
||||
"2": "key2",
|
||||
|
@@ -16,6 +16,8 @@ package datasource
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/rancher/os/netconf"
|
||||
)
|
||||
|
||||
type Datasource interface {
|
||||
@@ -31,11 +33,17 @@ type Datasource interface {
|
||||
}
|
||||
|
||||
type Metadata struct {
|
||||
PublicIPv4 net.IP
|
||||
PublicIPv6 net.IP
|
||||
PrivateIPv4 net.IP
|
||||
PrivateIPv6 net.IP
|
||||
// TODO: move to netconf/types.go ?
|
||||
// see https://ahmetalpbalkan.com/blog/comparison-of-instance-metadata-services/
|
||||
Hostname string
|
||||
SSHPublicKeys map[string]string
|
||||
NetworkConfig interface{}
|
||||
NetworkConfig netconf.NetworkConfig
|
||||
|
||||
// probably unused, but its in the initialize.env code
|
||||
// TODO: work out if there's any reason to keep it.
|
||||
// Lets see if anyone notices when its not set.
|
||||
PublicIPv4 net.IP
|
||||
PublicIPv6 net.IP
|
||||
PrivateIPv4 net.IP
|
||||
PrivateIPv6 net.IP
|
||||
}
|
||||
|
@@ -16,9 +16,13 @@ package digitalocean
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/rancher/os/netconf"
|
||||
|
||||
"net"
|
||||
|
||||
"github.com/rancher/os/config/cloudinit/datasource"
|
||||
"github.com/rancher/os/config/cloudinit/datasource/metadata"
|
||||
)
|
||||
@@ -99,12 +103,70 @@ func (ms *MetadataService) FetchMetadata() (metadata datasource.Metadata, err er
|
||||
metadata.PrivateIPv6 = net.ParseIP(m.Interfaces.Private[0].IPv6.IPAddress)
|
||||
}
|
||||
}
|
||||
|
||||
metadata.NetworkConfig.Interfaces = make(map[string]netconf.InterfaceConfig)
|
||||
|
||||
ethNumber := 0
|
||||
|
||||
for _, eth := range m.Interfaces.Public {
|
||||
network := netconf.InterfaceConfig{}
|
||||
|
||||
if eth.IPv4 != nil {
|
||||
network.Gateway = eth.IPv4.Gateway
|
||||
|
||||
network.Addresses = append(network.Addresses, fmt.Sprintf("%s/%s", eth.IPv4.IPAddress, eth.IPv4.Netmask))
|
||||
if metadata.PublicIPv4 == nil {
|
||||
metadata.PublicIPv4 = net.ParseIP(eth.IPv4.IPAddress)
|
||||
}
|
||||
}
|
||||
if eth.AnchorIPv4 != nil {
|
||||
network.Addresses = append(network.Addresses, fmt.Sprintf("%s/%s", eth.AnchorIPv4.IPAddress, eth.AnchorIPv4.Netmask))
|
||||
|
||||
}
|
||||
if eth.IPv6 != nil {
|
||||
network.Addresses = append(network.Addresses, eth.IPv6.IPAddress)
|
||||
network.GatewayIpv6 = eth.IPv6.Gateway
|
||||
if metadata.PublicIPv6 == nil {
|
||||
metadata.PublicIPv6 = net.ParseIP(eth.IPv6.IPAddress)
|
||||
}
|
||||
}
|
||||
metadata.NetworkConfig.Interfaces[fmt.Sprintf("eth%d", ethNumber)] = network
|
||||
ethNumber = ethNumber + 1
|
||||
}
|
||||
|
||||
for _, eth := range m.Interfaces.Private {
|
||||
network := netconf.InterfaceConfig{}
|
||||
if eth.IPv4 != nil {
|
||||
network.Gateway = eth.IPv4.Gateway
|
||||
|
||||
network.Addresses = append(network.Addresses, fmt.Sprintf("%s/%s", eth.IPv4.IPAddress, eth.IPv4.Netmask))
|
||||
if metadata.PrivateIPv4 == nil {
|
||||
metadata.PrivateIPv4 = net.ParseIP(eth.IPv6.IPAddress)
|
||||
}
|
||||
}
|
||||
if eth.AnchorIPv4 != nil {
|
||||
network.Addresses = append(network.Addresses, fmt.Sprintf("%s/%s", eth.AnchorIPv4.IPAddress, eth.AnchorIPv4.Netmask))
|
||||
|
||||
}
|
||||
if eth.IPv6 != nil {
|
||||
network.Address = eth.IPv6.IPAddress
|
||||
network.GatewayIpv6 = eth.IPv6.Gateway
|
||||
if metadata.PrivateIPv6 == nil {
|
||||
metadata.PrivateIPv6 = net.ParseIP(eth.IPv6.IPAddress)
|
||||
}
|
||||
}
|
||||
metadata.NetworkConfig.Interfaces[fmt.Sprintf("eth%d", ethNumber)] = network
|
||||
ethNumber = ethNumber + 1
|
||||
}
|
||||
|
||||
metadata.NetworkConfig.DNS.Nameservers = m.DNS.Nameservers
|
||||
|
||||
metadata.Hostname = m.Hostname
|
||||
metadata.SSHPublicKeys = map[string]string{}
|
||||
for i, key := range m.PublicKeys {
|
||||
metadata.SSHPublicKeys[strconv.Itoa(i)] = key
|
||||
}
|
||||
metadata.NetworkConfig = m
|
||||
// metadata.NetworkConfig = m
|
||||
|
||||
return
|
||||
}
|
||||
|
37
config/cloudinit/datasource/metadata/digitalocean/metadata_test.go
Normal file → Executable file
37
config/cloudinit/datasource/metadata/digitalocean/metadata_test.go
Normal file → Executable file
@@ -20,6 +20,8 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/rancher/os/netconf"
|
||||
|
||||
"github.com/rancher/os/config/cloudinit/datasource"
|
||||
"github.com/rancher/os/config/cloudinit/datasource/metadata"
|
||||
"github.com/rancher/os/config/cloudinit/datasource/metadata/test"
|
||||
@@ -90,26 +92,23 @@ func TestFetchMetadata(t *testing.T) {
|
||||
"0": "publickey1",
|
||||
"1": "publickey2",
|
||||
},
|
||||
NetworkConfig: Metadata{
|
||||
Interfaces: Interfaces{
|
||||
Public: []Interface{
|
||||
{
|
||||
IPv4: &Address{
|
||||
IPAddress: "192.168.1.2",
|
||||
Netmask: "255.255.255.0",
|
||||
Gateway: "192.168.1.1",
|
||||
},
|
||||
IPv6: &Address{
|
||||
IPAddress: "fe00::",
|
||||
Cidr: 126,
|
||||
Gateway: "fe00::",
|
||||
},
|
||||
MAC: "ab:cd:ef:gh:ij",
|
||||
Type: "public",
|
||||
NetworkConfig: netconf.NetworkConfig{
|
||||
Interfaces: map[string]netconf.InterfaceConfig{
|
||||
"eth0": netconf.InterfaceConfig{
|
||||
Addresses: []string{
|
||||
"192.168.1.2/255.255.255.0",
|
||||
"fe00::",
|
||||
},
|
||||
//Netmask: "255.255.255.0",
|
||||
Gateway: "192.168.1.1",
|
||||
|
||||
//Cidr: 126,
|
||||
GatewayIpv6: "fe00::",
|
||||
//MAC: "ab:cd:ef:gh:ij",
|
||||
//Type: "public",
|
||||
},
|
||||
},
|
||||
PublicKeys: []string{"publickey1", "publickey2"},
|
||||
//PublicKeys: []string{"publickey1", "publickey2"},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -127,10 +126,10 @@ func TestFetchMetadata(t *testing.T) {
|
||||
}
|
||||
metadata, err := service.FetchMetadata()
|
||||
if Error(err) != Error(tt.expectErr) {
|
||||
t.Fatalf("bad error (%q): want %q, got %q", tt.resources, tt.expectErr, err)
|
||||
t.Fatalf("bad error (%q): \nwant %#v,\n got %#v", tt.resources, tt.expectErr, err)
|
||||
}
|
||||
if !reflect.DeepEqual(tt.expect, metadata) {
|
||||
t.Fatalf("bad fetch (%q): want %#q, got %#q", tt.resources, tt.expect, metadata)
|
||||
t.Fatalf("bad fetch (%q): \nwant %#v,\n got %#v", tt.resources, tt.expect, metadata)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -22,6 +22,8 @@ import (
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/rancher/os/netconf"
|
||||
|
||||
"github.com/rancher/os/config/cloudinit/datasource"
|
||||
"github.com/rancher/os/config/cloudinit/datasource/metadata"
|
||||
"github.com/rancher/os/config/cloudinit/pkg"
|
||||
@@ -47,6 +49,7 @@ func NewDatasource(root string) *MetadataService {
|
||||
|
||||
func (ms MetadataService) FetchMetadata() (datasource.Metadata, error) {
|
||||
metadata := datasource.Metadata{}
|
||||
metadata.NetworkConfig = netconf.NetworkConfig{}
|
||||
|
||||
if keynames, err := ms.fetchAttributes(fmt.Sprintf("%s/public-keys", ms.MetadataURL())); err == nil {
|
||||
keyIDs := make(map[string]string)
|
||||
@@ -77,18 +80,25 @@ func (ms MetadataService) FetchMetadata() (datasource.Metadata, error) {
|
||||
return metadata, err
|
||||
}
|
||||
|
||||
network := netconf.InterfaceConfig{}
|
||||
if localAddr, err := ms.fetchAttribute(fmt.Sprintf("%s/local-ipv4", ms.MetadataURL())); err == nil {
|
||||
metadata.PrivateIPv4 = net.ParseIP(localAddr)
|
||||
network.Addresses = append(network.Addresses, localAddr)
|
||||
|
||||
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
||||
return metadata, err
|
||||
}
|
||||
|
||||
if publicAddr, err := ms.fetchAttribute(fmt.Sprintf("%s/public-ipv4", ms.MetadataURL())); err == nil {
|
||||
metadata.PublicIPv4 = net.ParseIP(publicAddr)
|
||||
network.Addresses = append(network.Addresses, publicAddr)
|
||||
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
||||
return metadata, err
|
||||
}
|
||||
|
||||
metadata.NetworkConfig.Interfaces = make(map[string]netconf.InterfaceConfig)
|
||||
metadata.NetworkConfig.Interfaces["eth0"] = network
|
||||
|
||||
return metadata, nil
|
||||
}
|
||||
|
||||
|
21
config/cloudinit/datasource/metadata/ec2/metadata_test.go
Normal file → Executable file
21
config/cloudinit/datasource/metadata/ec2/metadata_test.go
Normal file → Executable file
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/rancher/os/config/cloudinit/datasource/metadata"
|
||||
"github.com/rancher/os/config/cloudinit/datasource/metadata/test"
|
||||
"github.com/rancher/os/config/cloudinit/pkg"
|
||||
"github.com/rancher/os/netconf"
|
||||
)
|
||||
|
||||
func TestType(t *testing.T) {
|
||||
@@ -174,6 +175,16 @@ func TestFetchMetadata(t *testing.T) {
|
||||
PrivateIPv4: net.ParseIP("1.2.3.4"),
|
||||
PublicIPv4: net.ParseIP("5.6.7.8"),
|
||||
SSHPublicKeys: map[string]string{"test1": "key"},
|
||||
NetworkConfig: netconf.NetworkConfig{
|
||||
Interfaces: map[string]netconf.InterfaceConfig{
|
||||
"eth0": netconf.InterfaceConfig{
|
||||
Addresses: []string{
|
||||
"1.2.3.4",
|
||||
"5.6.7.8",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -192,6 +203,16 @@ func TestFetchMetadata(t *testing.T) {
|
||||
PrivateIPv4: net.ParseIP("1.2.3.4"),
|
||||
PublicIPv4: net.ParseIP("5.6.7.8"),
|
||||
SSHPublicKeys: map[string]string{"test1": "key"},
|
||||
NetworkConfig: netconf.NetworkConfig{
|
||||
Interfaces: map[string]netconf.InterfaceConfig{
|
||||
"eth0": netconf.InterfaceConfig{
|
||||
Addresses: []string{
|
||||
"1.2.3.4",
|
||||
"5.6.7.8",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@@ -21,6 +21,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/rancher/os/netconf"
|
||||
|
||||
"github.com/rancher/os/config/cloudinit/datasource"
|
||||
"github.com/rancher/os/config/cloudinit/datasource/metadata"
|
||||
)
|
||||
@@ -72,6 +74,22 @@ func (ms MetadataService) FetchMetadata() (datasource.Metadata, error) {
|
||||
SSHPublicKeys: nil,
|
||||
}
|
||||
|
||||
addresses := []string{}
|
||||
if public != nil {
|
||||
addresses = append(addresses, public.String())
|
||||
}
|
||||
if local != nil {
|
||||
addresses = append(addresses, local.String())
|
||||
}
|
||||
if len(addresses) > 0 {
|
||||
network := netconf.InterfaceConfig{
|
||||
Addresses: addresses,
|
||||
}
|
||||
|
||||
md.NetworkConfig.Interfaces = make(map[string]netconf.InterfaceConfig)
|
||||
md.NetworkConfig.Interfaces["eth0"] = network
|
||||
}
|
||||
|
||||
keyStrings := strings.Split(projectSSHKeys+"\n"+instanceSSHKeys, "\n")
|
||||
|
||||
i := 0
|
||||
|
@@ -20,6 +20,8 @@ import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/rancher/os/netconf"
|
||||
|
||||
"github.com/rancher/os/config/cloudinit/datasource"
|
||||
"github.com/rancher/os/config/cloudinit/datasource/metadata"
|
||||
"github.com/rancher/os/config/cloudinit/datasource/metadata/test"
|
||||
@@ -73,6 +75,16 @@ func TestFetchMetadata(t *testing.T) {
|
||||
Hostname: "host",
|
||||
PrivateIPv4: net.ParseIP("1.2.3.4"),
|
||||
PublicIPv4: net.ParseIP("5.6.7.8"),
|
||||
NetworkConfig: netconf.NetworkConfig{
|
||||
Interfaces: map[string]netconf.InterfaceConfig{
|
||||
"eth0": netconf.InterfaceConfig{
|
||||
Addresses: []string{
|
||||
"5.6.7.8",
|
||||
"1.2.3.4",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@@ -20,6 +20,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/rancher/os/config/cloudinit/pkg"
|
||||
"github.com/rancher/os/log"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
@@ -40,6 +41,9 @@ func NewDatasource(root, apiVersion, userdataPath, metadataPath string, header h
|
||||
|
||||
func (ms Service) IsAvailable() bool {
|
||||
_, ms.lastError = ms.Client.Get(ms.Root + ms.APIVersion)
|
||||
if ms.lastError != nil {
|
||||
log.Errorf("%s: %s (lastError: %s)", "IsAvailable", ms.Root+":"+ms.UserdataPath, ms.lastError)
|
||||
}
|
||||
return (ms.lastError == nil)
|
||||
}
|
||||
|
||||
@@ -48,7 +52,7 @@ func (ms *Service) Finish() error {
|
||||
}
|
||||
|
||||
func (ms *Service) String() string {
|
||||
return fmt.Sprintf("%s: %s (lastError: %s)", "metadata", ms.Root+":"+ms.UserdataPath, ms.lastError)
|
||||
return fmt.Sprintf("%s: %s (lastError: %s)", "metadata", ms.Root+ms.UserdataPath, ms.lastError)
|
||||
}
|
||||
|
||||
func (ms Service) AvailabilityChanges() bool {
|
||||
|
@@ -15,12 +15,18 @@
|
||||
package packet
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/rancher/os/config/cloudinit/datasource"
|
||||
"github.com/rancher/os/config/cloudinit/datasource/metadata"
|
||||
"github.com/rancher/os/log"
|
||||
"github.com/rancher/os/netconf"
|
||||
|
||||
yaml "github.com/cloudfoundry-incubator/candiedyaml"
|
||||
packetMetadata "github.com/packethost/packngo/metadata"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -30,33 +36,6 @@ const (
|
||||
metadataPath = "metadata"
|
||||
)
|
||||
|
||||
type Netblock struct {
|
||||
Address net.IP `json:"address"`
|
||||
Cidr int `json:"cidr"`
|
||||
Netmask net.IP `json:"netmask"`
|
||||
Gateway net.IP `json:"gateway"`
|
||||
AddressFamily int `json:"address_family"`
|
||||
Public bool `json:"public"`
|
||||
}
|
||||
|
||||
type Nic struct {
|
||||
Name string `json:"name"`
|
||||
Mac string `json:"mac"`
|
||||
}
|
||||
|
||||
type NetworkData struct {
|
||||
Interfaces []Nic `json:"interfaces"`
|
||||
Netblocks []Netblock `json:"addresses"`
|
||||
DNS []net.IP `json:"dns"`
|
||||
}
|
||||
|
||||
// Metadata that will be pulled from the https://metadata.packet.net/metadata only. We have the opportunity to add more later.
|
||||
type Metadata struct {
|
||||
Hostname string `json:"hostname"`
|
||||
SSHKeys []string `json:"ssh_keys"`
|
||||
NetworkData NetworkData `json:"network"`
|
||||
}
|
||||
|
||||
type MetadataService struct {
|
||||
metadata.Service
|
||||
}
|
||||
@@ -70,37 +49,85 @@ func NewDatasource(root string) *MetadataService {
|
||||
}
|
||||
|
||||
func (ms *MetadataService) FetchMetadata() (metadata datasource.Metadata, err error) {
|
||||
var data []byte
|
||||
var m Metadata
|
||||
|
||||
if data, err = ms.FetchData(ms.MetadataURL()); err != nil || len(data) == 0 {
|
||||
c := packetMetadata.NewClient(http.DefaultClient)
|
||||
m, err := c.Metadata.Get()
|
||||
if err != nil {
|
||||
log.Errorf("Failed to get Packet metadata: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(data, &m); err != nil {
|
||||
return
|
||||
bondCfg := netconf.InterfaceConfig{
|
||||
Addresses: []string{},
|
||||
BondOpts: map[string]string{
|
||||
"lacp_rate": "1",
|
||||
"xmit_hash_policy": "layer3+4",
|
||||
"downdelay": "200",
|
||||
"updelay": "200",
|
||||
"miimon": "100",
|
||||
"mode": "4",
|
||||
},
|
||||
}
|
||||
|
||||
if len(m.NetworkData.Netblocks) > 0 {
|
||||
for _, Netblock := range m.NetworkData.Netblocks {
|
||||
if Netblock.AddressFamily == 4 {
|
||||
if Netblock.Public == true {
|
||||
metadata.PublicIPv4 = Netblock.Address
|
||||
} else {
|
||||
metadata.PrivateIPv4 = Netblock.Address
|
||||
}
|
||||
} else {
|
||||
metadata.PublicIPv6 = Netblock.Address
|
||||
}
|
||||
netCfg := netconf.NetworkConfig{
|
||||
Interfaces: map[string]netconf.InterfaceConfig{},
|
||||
}
|
||||
for _, iface := range m.Network.Interfaces {
|
||||
netCfg.Interfaces["mac="+iface.Mac] = netconf.InterfaceConfig{
|
||||
Bond: "bond0",
|
||||
}
|
||||
}
|
||||
for _, addr := range m.Network.Addresses {
|
||||
bondCfg.Addresses = append(bondCfg.Addresses, fmt.Sprintf("%s/%d", addr.Address, addr.Cidr))
|
||||
if addr.Gateway != "" {
|
||||
if addr.AddressFamily == 4 {
|
||||
if addr.Public {
|
||||
bondCfg.Gateway = addr.Gateway
|
||||
}
|
||||
} else {
|
||||
bondCfg.GatewayIpv6 = addr.Gateway
|
||||
}
|
||||
}
|
||||
|
||||
if addr.AddressFamily == 4 && strings.HasPrefix(addr.Gateway, "10.") {
|
||||
bondCfg.PostUp = append(bondCfg.PostUp, "ip route add 10.0.0.0/8 via "+addr.Gateway)
|
||||
}
|
||||
}
|
||||
|
||||
netCfg.Interfaces["bond0"] = bondCfg
|
||||
b, _ := yaml.Marshal(netCfg)
|
||||
log.Debugf("Generated network config: %s", string(b))
|
||||
|
||||
// the old code var data []byte
|
||||
/* var m Metadata
|
||||
|
||||
if data, err = ms.FetchData(ms.MetadataURL()); err != nil || len(data) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(data, &m); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if len(m.NetworkData.Netblocks) > 0 {
|
||||
for _, Netblock := range m.NetworkData.Netblocks {
|
||||
if Netblock.AddressFamily == 4 {
|
||||
if Netblock.Public == true {
|
||||
metadata.PublicIPv4 = Netblock.Address
|
||||
} else {
|
||||
metadata.PrivateIPv4 = Netblock.Address
|
||||
}
|
||||
} else {
|
||||
metadata.PublicIPv6 = Netblock.Address
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
metadata.Hostname = m.Hostname
|
||||
metadata.SSHPublicKeys = map[string]string{}
|
||||
for i, key := range m.SSHKeys {
|
||||
for i, key := range m.SshKeys {
|
||||
metadata.SSHPublicKeys[strconv.Itoa(i)] = key
|
||||
}
|
||||
|
||||
metadata.NetworkConfig = m.NetworkData
|
||||
metadata.NetworkConfig = netCfg
|
||||
|
||||
return
|
||||
}
|
||||
|
@@ -125,7 +125,7 @@ func (v VMWare) FetchMetadata() (metadata datasource.Metadata, err error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
metadata.NetworkConfig = netconf
|
||||
// metadata.NetworkConfig = netconf
|
||||
|
||||
return
|
||||
}
|
||||
|
@@ -56,10 +56,10 @@ func TestFetchMetadata(t *testing.T) {
|
||||
"interface.0.dhcp": "yes",
|
||||
},
|
||||
metadata: datasource.Metadata{
|
||||
NetworkConfig: map[string]string{
|
||||
"interface.0.mac": "test mac",
|
||||
"interface.0.dhcp": "yes",
|
||||
},
|
||||
// NetworkConfig: map[string]string{
|
||||
// "interface.0.mac": "test mac",
|
||||
// "interface.0.dhcp": "yes",
|
||||
// },
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -68,10 +68,10 @@ func TestFetchMetadata(t *testing.T) {
|
||||
"interface.0.dhcp": "yes",
|
||||
},
|
||||
metadata: datasource.Metadata{
|
||||
NetworkConfig: map[string]string{
|
||||
"interface.0.name": "test name",
|
||||
"interface.0.dhcp": "yes",
|
||||
},
|
||||
// NetworkConfig: map[string]string{
|
||||
// "interface.0.name": "test name",
|
||||
// "interface.0.dhcp": "yes",
|
||||
// },
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -86,12 +86,12 @@ func TestFetchMetadata(t *testing.T) {
|
||||
metadata: datasource.Metadata{
|
||||
Hostname: "test host",
|
||||
PrivateIPv6: net.ParseIP("fe00::100"),
|
||||
NetworkConfig: map[string]string{
|
||||
"interface.0.mac": "test mac",
|
||||
"interface.0.ip.0.address": "fe00::100/64",
|
||||
"interface.0.route.0.gateway": "fe00::1",
|
||||
"interface.0.route.0.destination": "::",
|
||||
},
|
||||
// NetworkConfig: map[string]string{
|
||||
// "interface.0.mac": "test mac",
|
||||
// "interface.0.ip.0.address": "fe00::100/64",
|
||||
// "interface.0.route.0.gateway": "fe00::1",
|
||||
// "interface.0.route.0.destination": "::",
|
||||
// },
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -113,17 +113,17 @@ func TestFetchMetadata(t *testing.T) {
|
||||
Hostname: "test host",
|
||||
PublicIPv4: net.ParseIP("10.0.0.101"),
|
||||
PrivateIPv4: net.ParseIP("10.0.0.102"),
|
||||
NetworkConfig: map[string]string{
|
||||
"interface.0.name": "test name",
|
||||
"interface.0.ip.0.address": "10.0.0.100/24",
|
||||
"interface.0.ip.1.address": "10.0.0.101/24",
|
||||
"interface.0.route.0.gateway": "10.0.0.1",
|
||||
"interface.0.route.0.destination": "0.0.0.0",
|
||||
"interface.1.mac": "test mac",
|
||||
"interface.1.route.0.gateway": "10.0.0.2",
|
||||
"interface.1.route.0.destination": "0.0.0.0",
|
||||
"interface.1.ip.0.address": "10.0.0.102/24",
|
||||
},
|
||||
// NetworkConfig: map[string]string{
|
||||
// "interface.0.name": "test name",
|
||||
// "interface.0.ip.0.address": "10.0.0.100/24",
|
||||
// "interface.0.ip.1.address": "10.0.0.101/24",
|
||||
// "interface.0.route.0.gateway": "10.0.0.1",
|
||||
// "interface.0.route.0.destination": "0.0.0.0",
|
||||
// "interface.1.mac": "test mac",
|
||||
// "interface.1.route.0.gateway": "10.0.0.2",
|
||||
// "interface.1.route.0.destination": "0.0.0.0",
|
||||
// "interface.1.ip.0.address": "10.0.0.102/24",
|
||||
// },
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -257,17 +257,17 @@ func TestOvfTransport(t *testing.T) {
|
||||
Hostname: "test host",
|
||||
PublicIPv4: net.ParseIP("10.0.0.101"),
|
||||
PrivateIPv4: net.ParseIP("10.0.0.102"),
|
||||
NetworkConfig: map[string]string{
|
||||
"interface.0.name": "test name",
|
||||
"interface.0.ip.0.address": "10.0.0.100/24",
|
||||
"interface.0.ip.1.address": "10.0.0.101/24",
|
||||
"interface.0.route.0.gateway": "10.0.0.1",
|
||||
"interface.0.route.0.destination": "0.0.0.0",
|
||||
"interface.1.mac": "test mac",
|
||||
"interface.1.route.0.gateway": "10.0.0.2",
|
||||
"interface.1.route.0.destination": "0.0.0.0",
|
||||
"interface.1.ip.0.address": "10.0.0.102/24",
|
||||
},
|
||||
//NetworkConfig: map[string]string{
|
||||
// "interface.0.name": "test name",
|
||||
// "interface.0.ip.0.address": "10.0.0.100/24",
|
||||
// "interface.0.ip.1.address": "10.0.0.101/24",
|
||||
// "interface.0.route.0.gateway": "10.0.0.1",
|
||||
// "interface.0.route.0.destination": "0.0.0.0",
|
||||
// "interface.1.mac": "test mac",
|
||||
// "interface.1.route.0.gateway": "10.0.0.2",
|
||||
// "interface.1.route.0.destination": "0.0.0.0",
|
||||
// "interface.1.ip.0.address": "10.0.0.102/24",
|
||||
// },
|
||||
},
|
||||
userdata: []byte("test config"),
|
||||
},
|
||||
|
72
config/cloudinit/network/packet.go
Normal file → Executable file
72
config/cloudinit/network/packet.go
Normal file → Executable file
@@ -17,14 +17,15 @@ package network
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/rancher/os/config/cloudinit/datasource/metadata/packet"
|
||||
"github.com/rancher/os/netconf"
|
||||
)
|
||||
|
||||
func ProcessPacketNetconf(netdata packet.NetworkData) ([]InterfaceGenerator, error) {
|
||||
func ProcessPacketNetconf(netdata netconf.NetworkConfig) ([]InterfaceGenerator, error) {
|
||||
var nameservers []net.IP
|
||||
if netdata.DNS != nil {
|
||||
nameservers = netdata.DNS
|
||||
} else {
|
||||
for _, v := range netdata.DNS.Nameservers {
|
||||
nameservers = append(nameservers, net.ParseIP(v))
|
||||
}
|
||||
if len(nameservers) == 0 {
|
||||
nameservers = append(nameservers, net.ParseIP("8.8.8.8"), net.ParseIP("8.8.4.4"))
|
||||
}
|
||||
|
||||
@@ -36,43 +37,44 @@ func ProcessPacketNetconf(netdata packet.NetworkData) ([]InterfaceGenerator, err
|
||||
return generators, nil
|
||||
}
|
||||
|
||||
func parseNetwork(netdata packet.NetworkData, nameservers []net.IP) ([]InterfaceGenerator, error) {
|
||||
func parseNetwork(netdata netconf.NetworkConfig, nameservers []net.IP) ([]InterfaceGenerator, error) {
|
||||
var interfaces []InterfaceGenerator
|
||||
var addresses []net.IPNet
|
||||
var routes []route
|
||||
for _, netblock := range netdata.Netblocks {
|
||||
addresses = append(addresses, net.IPNet{
|
||||
IP: netblock.Address,
|
||||
Mask: net.IPMask(netblock.Netmask),
|
||||
})
|
||||
if netblock.Public == false {
|
||||
routes = append(routes, route{
|
||||
destination: net.IPNet{
|
||||
IP: net.IPv4(10, 0, 0, 0),
|
||||
Mask: net.IPv4Mask(255, 0, 0, 0),
|
||||
},
|
||||
gateway: netblock.Gateway,
|
||||
/* for _, netblock := range netdata.Netblocks {
|
||||
addresses = append(addresses, net.IPNet{
|
||||
IP: netblock.Address,
|
||||
Mask: net.IPMask(netblock.Netmask),
|
||||
})
|
||||
} else {
|
||||
if netblock.AddressFamily == 4 {
|
||||
if netblock.Public == false {
|
||||
routes = append(routes, route{
|
||||
destination: net.IPNet{
|
||||
IP: net.IPv4zero,
|
||||
Mask: net.IPMask(net.IPv4zero),
|
||||
IP: net.IPv4(10, 0, 0, 0),
|
||||
Mask: net.IPv4Mask(255, 0, 0, 0),
|
||||
},
|
||||
gateway: netblock.Gateway,
|
||||
})
|
||||
} else {
|
||||
routes = append(routes, route{
|
||||
destination: net.IPNet{
|
||||
IP: net.IPv6zero,
|
||||
Mask: net.IPMask(net.IPv6zero),
|
||||
},
|
||||
gateway: netblock.Gateway,
|
||||
})
|
||||
if netblock.AddressFamily == 4 {
|
||||
routes = append(routes, route{
|
||||
destination: net.IPNet{
|
||||
IP: net.IPv4zero,
|
||||
Mask: net.IPMask(net.IPv4zero),
|
||||
},
|
||||
gateway: netblock.Gateway,
|
||||
})
|
||||
} else {
|
||||
routes = append(routes, route{
|
||||
destination: net.IPNet{
|
||||
IP: net.IPv6zero,
|
||||
Mask: net.IPMask(net.IPv6zero),
|
||||
},
|
||||
gateway: netblock.Gateway,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
bond := bondInterface{
|
||||
logicalInterface: logicalInterface{
|
||||
@@ -92,14 +94,15 @@ func parseNetwork(netdata packet.NetworkData, nameservers []net.IP) ([]Interface
|
||||
},
|
||||
}
|
||||
|
||||
bond.hwaddr, _ = net.ParseMAC(netdata.Interfaces[0].Mac)
|
||||
//bond.hwaddr, _ = net.ParseMAC(netdata.Interfaces[0].Mac)
|
||||
|
||||
for index, iface := range netdata.Interfaces {
|
||||
bond.slaves = append(bond.slaves, iface.Name)
|
||||
index := 0
|
||||
for name := range netdata.Interfaces {
|
||||
bond.slaves = append(bond.slaves, name)
|
||||
|
||||
interfaces = append(interfaces, &physicalInterface{
|
||||
logicalInterface: logicalInterface{
|
||||
name: iface.Name,
|
||||
name: name,
|
||||
config: configMethodStatic{
|
||||
nameservers: nameservers,
|
||||
},
|
||||
@@ -107,6 +110,7 @@ func parseNetwork(netdata packet.NetworkData, nameservers []net.IP) ([]Interface
|
||||
configDepth: index,
|
||||
},
|
||||
})
|
||||
index = index + 1
|
||||
}
|
||||
|
||||
interfaces = append(interfaces, &bond)
|
||||
|
Reference in New Issue
Block a user