mirror of
https://github.com/rancher/os.git
synced 2025-09-02 07:15:41 +00:00
Add dns and interface cloud-init data to match CoreOS
Signed-off-by: Sven Dowideit <SvenDowideit@home.org.au>
This commit is contained in:
@@ -17,9 +17,12 @@ package vmware
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/rancher/os/config/cloudinit/config"
|
"github.com/rancher/os/config/cloudinit/config"
|
||||||
"github.com/rancher/os/config/cloudinit/datasource"
|
"github.com/rancher/os/config/cloudinit/datasource"
|
||||||
|
"github.com/rancher/os/log"
|
||||||
|
"github.com/rancher/os/netconf"
|
||||||
)
|
)
|
||||||
|
|
||||||
type readConfigFunction func(key string) (string, error)
|
type readConfigFunction func(key string) (string, error)
|
||||||
@@ -48,51 +51,83 @@ func (v VMWare) ConfigRoot() string {
|
|||||||
return "/"
|
return "/"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v VMWare) read(keytmpl string, args ...interface{}) (string, error) {
|
||||||
|
key := fmt.Sprintf(keytmpl, args...)
|
||||||
|
return v.readConfig(key)
|
||||||
|
}
|
||||||
|
|
||||||
func (v VMWare) FetchMetadata() (metadata datasource.Metadata, err error) {
|
func (v VMWare) FetchMetadata() (metadata datasource.Metadata, err error) {
|
||||||
|
metadata.NetworkConfig = netconf.NetworkConfig{}
|
||||||
metadata.Hostname, _ = v.readConfig("hostname")
|
metadata.Hostname, _ = v.readConfig("hostname")
|
||||||
|
|
||||||
netconf := map[string]string{}
|
//netconf := map[string]string{}
|
||||||
saveConfig := func(key string, args ...interface{}) string {
|
//saveConfig := func(key string, args ...interface{}) string {
|
||||||
key = fmt.Sprintf(key, args...)
|
// key = fmt.Sprintf(key, args...)
|
||||||
val, _ := v.readConfig(key)
|
// val, _ := v.readConfig(key)
|
||||||
if val != "" {
|
// if val != "" {
|
||||||
netconf[key] = val
|
// netconf[key] = val
|
||||||
|
// }
|
||||||
|
// return val
|
||||||
|
//}
|
||||||
|
|
||||||
|
for i := 0; ; i++ {
|
||||||
|
val, _ := v.read("dns.server.%d", i)
|
||||||
|
if val == "" {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
return val
|
metadata.NetworkConfig.DNS.Nameservers = append(metadata.NetworkConfig.DNS.Nameservers, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; ; i++ {
|
for i := 0; ; i++ {
|
||||||
if nameserver := saveConfig("dns.server.%d", i); nameserver == "" {
|
//if domain := saveConfig("dns.domain.%d", i); domain == "" {
|
||||||
break
|
val, _ := v.read("dns.domain.%d", i)
|
||||||
}
|
if val == "" {
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; ; i++ {
|
|
||||||
if domain := saveConfig("dns.domain.%d", i); domain == "" {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
metadata.NetworkConfig.DNS.Search = append(metadata.NetworkConfig.DNS.Search, val)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
metadata.NetworkConfig.Interfaces = make(map[string]netconf.InterfaceConfig)
|
||||||
found := true
|
found := true
|
||||||
for i := 0; found; i++ {
|
for i := 0; found; i++ {
|
||||||
found = false
|
found = false
|
||||||
|
|
||||||
found = (saveConfig("interface.%d.name", i) != "") || found
|
ethName := fmt.Sprintf("eth%d", i)
|
||||||
found = (saveConfig("interface.%d.mac", i) != "") || found
|
netDevice := netconf.InterfaceConfig{
|
||||||
found = (saveConfig("interface.%d.dhcp", i) != "") || found
|
DHCP: true,
|
||||||
|
Match: ethName,
|
||||||
|
Addresses: []string{},
|
||||||
|
}
|
||||||
|
//found = (saveConfig("interface.%d.name", i) != "") || found
|
||||||
|
if val, _ := v.read("interface.%d.name", i); val != "" {
|
||||||
|
netDevice.Match = val
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
//found = (saveConfig("interface.%d.mac", i) != "") || found
|
||||||
|
if val, _ := v.read("interface.%d.mac", i); val != "" {
|
||||||
|
netDevice.Match = "mac:" + val
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
//found = (saveConfig("interface.%d.dhcp", i) != "") || found
|
||||||
|
if val, _ := v.read("interface.%d.dhcp", i); val != "" {
|
||||||
|
netDevice.DHCP = (strings.ToLower(val) != "no")
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
|
||||||
role, _ := v.readConfig(fmt.Sprintf("interface.%d.role", i))
|
role, _ := v.read("interface.%d.role", i)
|
||||||
for a := 0; ; a++ {
|
for a := 0; ; a++ {
|
||||||
address := saveConfig("interface.%d.ip.%d.address", i, a)
|
address, _ := v.read("interface.%d.ip.%d.address", i, a)
|
||||||
if address == "" {
|
if address == "" {
|
||||||
break
|
break
|
||||||
} else {
|
|
||||||
found = true
|
|
||||||
}
|
}
|
||||||
|
netDevice.Addresses = append(netDevice.Addresses, address)
|
||||||
|
found = true
|
||||||
|
netDevice.DHCP = false
|
||||||
|
|
||||||
ip, _, err := net.ParseCIDR(address)
|
ip, _, err := net.ParseCIDR(address)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return metadata, err
|
log.Error(err)
|
||||||
|
//return metadata, err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch role {
|
switch role {
|
||||||
@@ -110,28 +145,34 @@ func (v VMWare) FetchMetadata() (metadata datasource.Metadata, err error) {
|
|||||||
}
|
}
|
||||||
case "":
|
case "":
|
||||||
default:
|
default:
|
||||||
return metadata, fmt.Errorf("unrecognized role: %q", role)
|
//return metadata, fmt.Errorf("unrecognized role: %q", role)
|
||||||
|
log.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for r := 0; ; r++ {
|
for r := 0; ; r++ {
|
||||||
gateway := saveConfig("interface.%d.route.%d.gateway", i, r)
|
gateway, _ := v.read("interface.%d.route.%d.gateway", i, r)
|
||||||
destination := saveConfig("interface.%d.route.%d.destination", i, r)
|
// TODO: do we really not do anything but default routing?
|
||||||
|
//destination, _ := v.read("interface.%d.route.%d.destination", i, r)
|
||||||
|
destination := ""
|
||||||
|
|
||||||
if gateway == "" && destination == "" {
|
if gateway == "" && destination == "" {
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
|
netDevice.Gateway = gateway
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if found {
|
||||||
|
metadata.NetworkConfig.Interfaces[ethName] = netDevice
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// metadata.NetworkConfig = netconf
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v VMWare) FetchUserdata() ([]byte, error) {
|
func (v VMWare) FetchUserdata() ([]byte, error) {
|
||||||
encoding, err := v.readConfig("cloud-init.data.encoding")
|
encoding, err := v.readConfig("cloud-init.config.data.encoding")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@@ -17,6 +17,7 @@ package vmware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
@@ -24,11 +25,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/rancher/os/config/cloudinit/datasource"
|
"github.com/rancher/os/config/cloudinit/datasource"
|
||||||
|
"github.com/rancher/os/netconf"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockHypervisor map[string]string
|
type MockHypervisor map[string]string
|
||||||
|
|
||||||
func (h MockHypervisor) ReadConfig(key string) (string, error) {
|
func (h MockHypervisor) ReadConfig(key string) (string, error) {
|
||||||
|
fmt.Printf("read(%s) %s\n", key, h[key])
|
||||||
return h[key], nil
|
return h[key], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,26 +56,48 @@ func TestFetchMetadata(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
variables: map[string]string{
|
variables: map[string]string{
|
||||||
|
"hostname": "first",
|
||||||
"interface.0.mac": "test mac",
|
"interface.0.mac": "test mac",
|
||||||
"interface.0.dhcp": "yes",
|
"interface.0.dhcp": "yes",
|
||||||
},
|
},
|
||||||
metadata: datasource.Metadata{
|
metadata: datasource.Metadata{
|
||||||
// NetworkConfig: map[string]string{
|
Hostname: "first",
|
||||||
// "interface.0.mac": "test mac",
|
NetworkConfig: netconf.NetworkConfig{
|
||||||
// "interface.0.dhcp": "yes",
|
Interfaces: map[string]netconf.InterfaceConfig{
|
||||||
// },
|
"eth0": netconf.InterfaceConfig{
|
||||||
|
Match: "mac:test mac",
|
||||||
|
DHCP: true,
|
||||||
|
Addresses: []string{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// NetworkConfig: map[string]string{
|
||||||
|
// "interface.0.mac": "test mac",
|
||||||
|
// "interface.0.dhcp": "yes",
|
||||||
|
// },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
variables: map[string]string{
|
variables: map[string]string{
|
||||||
|
"hostname": "second",
|
||||||
"interface.0.name": "test name",
|
"interface.0.name": "test name",
|
||||||
"interface.0.dhcp": "yes",
|
"interface.0.dhcp": "yes",
|
||||||
},
|
},
|
||||||
metadata: datasource.Metadata{
|
metadata: datasource.Metadata{
|
||||||
// NetworkConfig: map[string]string{
|
Hostname: "second",
|
||||||
// "interface.0.name": "test name",
|
NetworkConfig: netconf.NetworkConfig{
|
||||||
// "interface.0.dhcp": "yes",
|
Interfaces: map[string]netconf.InterfaceConfig{
|
||||||
// },
|
"eth0": netconf.InterfaceConfig{
|
||||||
|
Match: "test name",
|
||||||
|
DHCP: true,
|
||||||
|
Addresses: []string{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
// NetworkConfig: map[string]string{
|
||||||
|
// "interface.0.name": "test name",
|
||||||
|
// "interface.0.dhcp": "yes",
|
||||||
|
// },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -87,6 +112,19 @@ func TestFetchMetadata(t *testing.T) {
|
|||||||
metadata: datasource.Metadata{
|
metadata: datasource.Metadata{
|
||||||
Hostname: "test host",
|
Hostname: "test host",
|
||||||
PrivateIPv6: net.ParseIP("fe00::100"),
|
PrivateIPv6: net.ParseIP("fe00::100"),
|
||||||
|
NetworkConfig: netconf.NetworkConfig{
|
||||||
|
Interfaces: map[string]netconf.InterfaceConfig{
|
||||||
|
"eth0": netconf.InterfaceConfig{
|
||||||
|
Match: "mac:test mac",
|
||||||
|
DHCP: false,
|
||||||
|
Addresses: []string{
|
||||||
|
"fe00::100/64",
|
||||||
|
},
|
||||||
|
Gateway: "fe00::1",
|
||||||
|
//TODO: Destination
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
// NetworkConfig: map[string]string{
|
// NetworkConfig: map[string]string{
|
||||||
// "interface.0.mac": "test mac",
|
// "interface.0.mac": "test mac",
|
||||||
// "interface.0.ip.0.address": "fe00::100/64",
|
// "interface.0.ip.0.address": "fe00::100/64",
|
||||||
@@ -114,6 +152,29 @@ func TestFetchMetadata(t *testing.T) {
|
|||||||
Hostname: "test host",
|
Hostname: "test host",
|
||||||
PublicIPv4: net.ParseIP("10.0.0.101"),
|
PublicIPv4: net.ParseIP("10.0.0.101"),
|
||||||
PrivateIPv4: net.ParseIP("10.0.0.102"),
|
PrivateIPv4: net.ParseIP("10.0.0.102"),
|
||||||
|
NetworkConfig: netconf.NetworkConfig{
|
||||||
|
Interfaces: map[string]netconf.InterfaceConfig{
|
||||||
|
"eth0": netconf.InterfaceConfig{
|
||||||
|
Match: "test name",
|
||||||
|
DHCP: false,
|
||||||
|
Addresses: []string{
|
||||||
|
"10.0.0.100/24",
|
||||||
|
"10.0.0.101/24",
|
||||||
|
},
|
||||||
|
Gateway: "10.0.0.1",
|
||||||
|
//TODO: Destination
|
||||||
|
},
|
||||||
|
"eth1": netconf.InterfaceConfig{
|
||||||
|
Match: "mac:test mac",
|
||||||
|
DHCP: false,
|
||||||
|
Addresses: []string{
|
||||||
|
"10.0.0.102/24",
|
||||||
|
},
|
||||||
|
Gateway: "10.0.0.2",
|
||||||
|
//TODO: Destination
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
// NetworkConfig: map[string]string{
|
// NetworkConfig: map[string]string{
|
||||||
// "interface.0.name": "test name",
|
// "interface.0.name": "test name",
|
||||||
// "interface.0.ip.0.address": "10.0.0.100/24",
|
// "interface.0.ip.0.address": "10.0.0.100/24",
|
||||||
@@ -150,45 +211,45 @@ func TestFetchUserdata(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{},
|
{},
|
||||||
{
|
{
|
||||||
variables: map[string]string{"coreos.config.data": "test config"},
|
variables: map[string]string{"cloud-init.config.data": "test config"},
|
||||||
userdata: "test config",
|
userdata: "test config",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
variables: map[string]string{
|
variables: map[string]string{
|
||||||
"coreos.config.data.encoding": "",
|
"cloud-init.config.data.encoding": "",
|
||||||
"coreos.config.data": "test config",
|
"cloud-init.config.data": "test config",
|
||||||
},
|
},
|
||||||
userdata: "test config",
|
userdata: "test config",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
variables: map[string]string{
|
variables: map[string]string{
|
||||||
"coreos.config.data.encoding": "base64",
|
"cloud-init.config.data.encoding": "base64",
|
||||||
"coreos.config.data": "dGVzdCBjb25maWc=",
|
"cloud-init.config.data": "dGVzdCBjb25maWc=",
|
||||||
},
|
},
|
||||||
userdata: "test config",
|
userdata: "test config",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
variables: map[string]string{
|
variables: map[string]string{
|
||||||
"coreos.config.data.encoding": "gzip+base64",
|
"cloud-init.config.data.encoding": "gzip+base64",
|
||||||
"coreos.config.data": "H4sIABaoWlUAAytJLS5RSM7PS8tMBwCQiHNZCwAAAA==",
|
"cloud-init.config.data": "H4sIABaoWlUAAytJLS5RSM7PS8tMBwCQiHNZCwAAAA==",
|
||||||
},
|
},
|
||||||
userdata: "test config",
|
userdata: "test config",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
variables: map[string]string{
|
variables: map[string]string{
|
||||||
"coreos.config.data.encoding": "test encoding",
|
"cloud-init.config.data.encoding": "test encoding",
|
||||||
},
|
},
|
||||||
err: errors.New(`Unsupported encoding "test encoding"`),
|
err: errors.New(`Unsupported encoding "test encoding"`),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
variables: map[string]string{
|
variables: map[string]string{
|
||||||
"coreos.config.url": "http://good.example.com",
|
"cloud-init.config.url": "http://good.example.com",
|
||||||
},
|
},
|
||||||
userdata: "test config",
|
userdata: "test config",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
variables: map[string]string{
|
variables: map[string]string{
|
||||||
"coreos.config.url": "http://bad.example.com",
|
"cloud-init.config.url": "http://bad.example.com",
|
||||||
},
|
},
|
||||||
err: errors.New("Not found"),
|
err: errors.New("Not found"),
|
||||||
},
|
},
|
||||||
@@ -239,7 +300,7 @@ func TestOvfTransport(t *testing.T) {
|
|||||||
</PlatformSection>
|
</PlatformSection>
|
||||||
<PropertySection>
|
<PropertySection>
|
||||||
<Property oe:key="foo" oe:value="42"/>
|
<Property oe:key="foo" oe:value="42"/>
|
||||||
<Property oe:key="guestinfo.coreos.config.url" oe:value="http://good.example.com"/>
|
<Property oe:key="guestinfo.cloud-init.config.url" oe:value="http://good.example.com"/>
|
||||||
<Property oe:key="guestinfo.hostname" oe:value="test host"/>
|
<Property oe:key="guestinfo.hostname" oe:value="test host"/>
|
||||||
<Property oe:key="guestinfo.interface.0.name" oe:value="test name"/>
|
<Property oe:key="guestinfo.interface.0.name" oe:value="test name"/>
|
||||||
<Property oe:key="guestinfo.interface.0.role" oe:value="public"/>
|
<Property oe:key="guestinfo.interface.0.role" oe:value="public"/>
|
||||||
@@ -258,17 +319,29 @@ func TestOvfTransport(t *testing.T) {
|
|||||||
Hostname: "test host",
|
Hostname: "test host",
|
||||||
PublicIPv4: net.ParseIP("10.0.0.101"),
|
PublicIPv4: net.ParseIP("10.0.0.101"),
|
||||||
PrivateIPv4: net.ParseIP("10.0.0.102"),
|
PrivateIPv4: net.ParseIP("10.0.0.102"),
|
||||||
//NetworkConfig: map[string]string{
|
NetworkConfig: netconf.NetworkConfig{
|
||||||
// "interface.0.name": "test name",
|
Interfaces: map[string]netconf.InterfaceConfig{
|
||||||
// "interface.0.ip.0.address": "10.0.0.100/24",
|
"eth0": netconf.InterfaceConfig{
|
||||||
// "interface.0.ip.1.address": "10.0.0.101/24",
|
Match: "test name",
|
||||||
// "interface.0.route.0.gateway": "10.0.0.1",
|
DHCP: false,
|
||||||
// "interface.0.route.0.destination": "0.0.0.0",
|
Addresses: []string{
|
||||||
// "interface.1.mac": "test mac",
|
"10.0.0.100/24",
|
||||||
// "interface.1.route.0.gateway": "10.0.0.2",
|
"10.0.0.101/24",
|
||||||
// "interface.1.route.0.destination": "0.0.0.0",
|
},
|
||||||
// "interface.1.ip.0.address": "10.0.0.102/24",
|
Gateway: "10.0.0.1",
|
||||||
// },
|
//TODO: Destination
|
||||||
|
},
|
||||||
|
"eth1": netconf.InterfaceConfig{
|
||||||
|
Match: "mac:test mac",
|
||||||
|
DHCP: false,
|
||||||
|
Addresses: []string{
|
||||||
|
"10.0.0.102/24",
|
||||||
|
},
|
||||||
|
Gateway: "10.0.0.2",
|
||||||
|
//TODO: Destination
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
userdata: []byte("test config"),
|
userdata: []byte("test config"),
|
||||||
},
|
},
|
||||||
|
@@ -62,10 +62,29 @@ Valid cloud-init datasources for RancherOS.
|
|||||||
| gce | | |
|
| gce | | |
|
||||||
| packet | DefaultAddress | |
|
| packet | DefaultAddress | |
|
||||||
| url | url | |
|
| url | url | |
|
||||||
| vmware | | set `guestinfo.cloud-init.data.data`, `guestinfo.cloud-init.data.encoding`, or `guestinfo.cloud-init.data.url` |
|
| vmware | | set `guestinfo.cloud-init.config.data`, `guestinfo.cloud-init.config.data.encoding`, or `guestinfo.cloud-init.config.url` |
|
||||||
| * | This will add ["configdrive", "vmware", "ec2", "digitalocean", "packet", "gce"] into the list of datasources to try | |
|
| * | This will add ["configdrive", "vmware", "ec2", "digitalocean", "packet", "gce"] into the list of datasources to try | |
|
||||||
|
|
||||||
### Cloud-Config
|
### Cloud-Config
|
||||||
|
|
||||||
When booting via iPXE, RancherOS can be configured using a [cloud-config file]({{site.baseurl}}/os/configuration/#cloud-config).
|
When booting via iPXE, RancherOS can be configured using a [cloud-config file]({{site.baseurl}}/os/configuration/#cloud-config).
|
||||||
|
|
||||||
|
### VMware guestinfo
|
||||||
|
|
||||||
|
| GUESTINFO VARIABLE | TYPE |
|
||||||
|
|---|---|
|
||||||
|
| `hostname | hostname |
|
||||||
|
| `interface.<n>.name` | string |
|
||||||
|
| `interface.<n>.mac` | MAC address (is used to match the ethernet device's MAC address, not to set it) |
|
||||||
|
| `interface.<n>.dhcp` | {"yes", "no"} |
|
||||||
|
| `interface.<n>.role` | {"public", "private"} |
|
||||||
|
| `interface.<n>.ip.<m>.address` | CIDR IP address |
|
||||||
|
| `interface.<n>.route.<l>.gateway` | IP address |
|
||||||
|
| `interface.<n>.route.<l>.destination` | CIDR IP address (not available yet) |
|
||||||
|
| `dns.server.<x>` | IP address |
|
||||||
|
| `dns.domain.<y> | DNS search domain` |
|
||||||
|
| `cloud-init.config.data | string` |
|
||||||
|
| `cloud-init.config.data.encoding` | {"", "base64", "gzip+base64"} |
|
||||||
|
| `cloud-init.config.url` | URL |
|
||||||
|
|
||||||
|
> **Note:** "n", "m", "l", "x" and "y" are 0-indexed, incrementing integers. The identifier for an interface (`<n>`) is used in the generation of the default interface name in the form `eth<n>`.
|
||||||
|
Reference in New Issue
Block a user