1
0
mirror of https://github.com/rancher/os.git synced 2025-06-27 23:36:49 +00:00

use DHCP for aws datasource

Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
Sven Dowideit 2017-03-13 10:23:57 +10:00
parent 64949bb888
commit d65f9518df
4 changed files with 66 additions and 27 deletions

1
cmd/network/network.go Normal file → Executable file
View File

@ -41,6 +41,7 @@ func ApplyNetworkConfig(cfg *config.CloudConfig) {
log.Error(err)
}
// TODO: seems wrong to do this outside netconf
userSetHostname := cfg.Hostname != ""
if err := netconf.RunDhcp(&cfg.Rancher.Network, !userSetHostname, !userSetDNS); err != nil {
log.Error(err)

View File

@ -31,7 +31,7 @@ import (
const (
DefaultAddress = "http://169.254.169.254/"
apiVersion = "2009-04-04/"
apiVersion = "latest/"
userdataPath = apiVersion + "user-data/"
metadataPath = apiVersion + "meta-data/"
)
@ -48,6 +48,7 @@ func NewDatasource(root string) *MetadataService {
}
func (ms MetadataService) FetchMetadata() (datasource.Metadata, error) {
// see http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html
metadata := datasource.Metadata{}
metadata.NetworkConfig = netconf.NetworkConfig{}
@ -80,24 +81,58 @@ func (ms MetadataService) FetchMetadata() (datasource.Metadata, error) {
return metadata, err
}
network := netconf.InterfaceConfig{}
// TODO: these are only on the first interface - it looks like you can have as many as you need...
if localAddr, err := ms.fetchAttribute("local-ipv4"); 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("public-ipv4"); 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
if macs, err := ms.fetchAttributes("network/interfaces/macs"); err != nil {
for _, mac := range macs {
if deviceNumber, err := ms.fetchAttribute(fmt.Sprintf("network/interfaces/macs/%s/device-number", mac)); err != nil {
network := netconf.InterfaceConfig{
DHCP: true,
}
/* Looks like we must use DHCP for aws
// private ipv4
if subnetCidrBlock, err := ms.fetchAttribute(fmt.Sprintf("network/interfaces/macs/%s/subnet-ipv4-cidr-block", mac)); err != nil {
cidr := strings.Split(subnetCidrBlock, "/")
if localAddr, err := ms.fetchAttributes(fmt.Sprintf("network/interfaces/macs/%s/local-ipv4s", mac)); err != nil {
for _, addr := range localAddr {
network.Addresses = append(network.Addresses, addr+"/"+cidr[1])
}
}
}
// ipv6
if localAddr, err := ms.fetchAttributes(fmt.Sprintf("network/interfaces/macs/%s/ipv6s", mac)); err != nil {
if subnetCidrBlock, err := ms.fetchAttributes(fmt.Sprintf("network/interfaces/macs/%s/subnet-ipv6-cidr-block", mac)); err != nil {
for i, addr := range localAddr {
cidr := strings.Split(subnetCidrBlock[i], "/")
network.Addresses = append(network.Addresses, addr+"/"+cidr[1])
}
}
}
*/
// disabled - it looks to me like you don't actually put the public IP on the eth device
/* if publicAddr, err := ms.fetchAttributes(fmt.Sprintf("network/interfaces/macs/%s/public-ipv4s", mac)); err != nil {
if vpcCidrBlock, err := ms.fetchAttribute(fmt.Sprintf("network/interfaces/macs/%s/vpc-ipv4-cidr-block", mac)); err != nil {
cidr := strings.Split(vpcCidrBlock, "/")
network.Addresses = append(network.Addresses, publicAddr+"/"+cidr[1])
}
}
*/
metadata.NetworkConfig.Interfaces["eth"+deviceNumber] = network
}
}
}
return metadata, nil
}

View File

@ -177,13 +177,13 @@ func TestFetchMetadata(t *testing.T) {
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",
},
},
},
/* "eth0": netconf.InterfaceConfig{
Addresses: []string{
"1.2.3.4",
"5.6.7.8",
},
},
*/},
},
},
},
@ -192,26 +192,26 @@ func TestFetchMetadata(t *testing.T) {
metadataPath: "2009-04-04/meta-data/",
resources: map[string]string{
"/2009-04-04/meta-data/hostname": "host domain another_domain",
"/2009-04-04/meta-data/local-ipv4": "1.2.3.4",
"/2009-04-04/meta-data/public-ipv4": "5.6.7.8",
"/2009-04-04/meta-data/local-ipv4": "21.2.3.4",
"/2009-04-04/meta-data/public-ipv4": "25.6.7.8",
"/2009-04-04/meta-data/public-keys": "0=test1\n",
"/2009-04-04/meta-data/public-keys/0": "openssh-key",
"/2009-04-04/meta-data/public-keys/0/openssh-key": "key",
},
expect: datasource.Metadata{
Hostname: "host",
PrivateIPv4: net.ParseIP("1.2.3.4"),
PublicIPv4: net.ParseIP("5.6.7.8"),
PrivateIPv4: net.ParseIP("21.2.3.4"),
PublicIPv4: net.ParseIP("25.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",
},
},
},
/* "eth0": netconf.InterfaceConfig{
Addresses: []string{
"1.2.3.4",
"5.6.7.8",
},
},
*/},
},
},
},

View File

@ -148,7 +148,10 @@ func populateDefault(netCfg *NetworkConfig) {
if _, ok := netCfg.Interfaces["lo"]; !ok {
netCfg.Interfaces["lo"] = InterfaceConfig{
Address: "127.0.0.1/8",
Addresses: []string{
"127.0.0.1/8",
"::1/128",
},
}
}
}
@ -401,7 +404,7 @@ func applyInterfaceConfig(link netlink.Link, netConf InterfaceConfig) error {
log.Errorf("Fail to set gateway %s", netConf.GatewayIpv6)
}
// TODO: how to remove a GW?
// TODO: how to remove a GW? (on aws it seems to be hard to find out what the gw is :/)
runCmds(netConf.PostUp, link.Attrs().Name)
return nil