datasource: use opaque object instead of string

Rather than serializing the network config into a string and then later
deserializing it, just pass the object through directly.
This commit is contained in:
Alex Crawford 2015-05-15 12:40:23 -07:00
parent 81ffa056bd
commit 9ba25550a1
8 changed files with 56 additions and 68 deletions

View File

@ -225,11 +225,11 @@ func main() {
var err error var err error
switch flags.convertNetconf { switch flags.convertNetconf {
case "debian": case "debian":
ifaces, err = network.ProcessDebianNetconf(metadata.NetworkConfig) ifaces, err = network.ProcessDebianNetconf(metadata.NetworkConfig.([]byte))
case "digitalocean": case "digitalocean":
ifaces, err = network.ProcessDigitalOceanNetconf(metadata.NetworkConfig) ifaces, err = network.ProcessDigitalOceanNetconf(metadata.NetworkConfig.(digitalocean.Metadata))
case "packet": case "packet":
ifaces, err = network.ProcessPacketNetconf(metadata.NetworkConfig) ifaces, err = network.ProcessPacketNetconf(metadata.NetworkConfig.(packet.NetworkData))
default: default:
err = fmt.Errorf("Unsupported network config format %q", flags.convertNetconf) err = fmt.Errorf("Unsupported network config format %q", flags.convertNetconf)
} }

View File

@ -34,5 +34,5 @@ type Metadata struct {
PrivateIPv6 net.IP PrivateIPv6 net.IP
Hostname string Hostname string
SSHPublicKeys map[string]string SSHPublicKeys map[string]string
NetworkConfig []byte NetworkConfig interface{}
} }

View File

@ -100,7 +100,7 @@ func (ms *metadataService) FetchMetadata() (metadata datasource.Metadata, err er
for i, key := range m.PublicKeys { for i, key := range m.PublicKeys {
metadata.SSHPublicKeys[strconv.Itoa(i)] = key metadata.SSHPublicKeys[strconv.Itoa(i)] = key
} }
metadata.NetworkConfig = data metadata.NetworkConfig = m
return return
} }

View File

@ -90,34 +90,27 @@ func TestFetchMetadata(t *testing.T) {
"0": "publickey1", "0": "publickey1",
"1": "publickey2", "1": "publickey2",
}, },
NetworkConfig: []byte(`{ NetworkConfig: Metadata{
"droplet_id": 1, Interfaces: Interfaces{
"user_data": "hello", Public: []Interface{
"vendor_data": "hello", Interface{
"public_keys": [ IPv4: &Address{
"publickey1", IPAddress: "192.168.1.2",
"publickey2" Netmask: "255.255.255.0",
], Gateway: "192.168.1.1",
"region": "nyc2", },
"interfaces": { IPv6: &Address{
"public": [ IPAddress: "fe00::",
{ Cidr: 126,
"ipv4": { Gateway: "fe00::",
"ip_address": "192.168.1.2", },
"netmask": "255.255.255.0", MAC: "ab:cd:ef:gh:ij",
"gateway": "192.168.1.1" Type: "public",
}, },
"ipv6": { },
"ip_address": "fe00::", },
"cidr": 126, PublicKeys: []string{"publickey1", "publickey2"},
"gateway": "fe00::" },
},
"mac": "ab:cd:ef:gh:ij",
"type": "public"
}
]
}
}`),
}, },
}, },
{ {

View File

@ -96,7 +96,7 @@ func (ms *metadataService) FetchMetadata() (metadata datasource.Metadata, err er
metadata.SSHPublicKeys[strconv.Itoa(i)] = key metadata.SSHPublicKeys[strconv.Itoa(i)] = key
} }
metadata.NetworkConfig, err = json.Marshal(m.NetworkData) metadata.NetworkConfig = m.NetworkData
return return
} }

View File

@ -15,7 +15,6 @@
package network package network
import ( import (
"encoding/json"
"fmt" "fmt"
"log" "log"
"net" "net"
@ -23,26 +22,18 @@ import (
"github.com/coreos/coreos-cloudinit/datasource/metadata/digitalocean" "github.com/coreos/coreos-cloudinit/datasource/metadata/digitalocean"
) )
func ProcessDigitalOceanNetconf(config []byte) ([]InterfaceGenerator, error) { func ProcessDigitalOceanNetconf(config digitalocean.Metadata) ([]InterfaceGenerator, error) {
log.Println("Processing DigitalOcean network config") log.Println("Processing DigitalOcean network config")
if len(config) == 0 {
return nil, nil
}
var cfg digitalocean.Metadata
if err := json.Unmarshal(config, &cfg); err != nil {
return nil, err
}
log.Println("Parsing nameservers") log.Println("Parsing nameservers")
nameservers, err := parseNameservers(cfg.DNS) nameservers, err := parseNameservers(config.DNS)
if err != nil { if err != nil {
return nil, err return nil, err
} }
log.Printf("Parsed %d nameservers\n", len(nameservers)) log.Printf("Parsed %d nameservers\n", len(nameservers))
log.Println("Parsing interfaces") log.Println("Parsing interfaces")
generators, err := parseInterfaces(cfg.Interfaces, nameservers) generators, err := parseInterfaces(config.Interfaces, nameservers)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -52,9 +43,9 @@ func ProcessDigitalOceanNetconf(config []byte) ([]InterfaceGenerator, error) {
return generators, nil return generators, nil
} }
func parseNameservers(cfg digitalocean.DNS) ([]net.IP, error) { func parseNameservers(config digitalocean.DNS) ([]net.IP, error) {
nameservers := make([]net.IP, 0, len(cfg.Nameservers)) nameservers := make([]net.IP, 0, len(config.Nameservers))
for _, ns := range cfg.Nameservers { for _, ns := range config.Nameservers {
if ip := net.ParseIP(ns); ip == nil { if ip := net.ParseIP(ns); ip == nil {
return nil, fmt.Errorf("could not parse %q as nameserver IP address", ns) return nil, fmt.Errorf("could not parse %q as nameserver IP address", ns)
} else { } else {
@ -64,16 +55,16 @@ func parseNameservers(cfg digitalocean.DNS) ([]net.IP, error) {
return nameservers, nil return nameservers, nil
} }
func parseInterfaces(cfg digitalocean.Interfaces, nameservers []net.IP) ([]InterfaceGenerator, error) { func parseInterfaces(config digitalocean.Interfaces, nameservers []net.IP) ([]InterfaceGenerator, error) {
generators := make([]InterfaceGenerator, 0, len(cfg.Public)+len(cfg.Private)) generators := make([]InterfaceGenerator, 0, len(config.Public)+len(config.Private))
for _, iface := range cfg.Public { for _, iface := range config.Public {
if generator, err := parseInterface(iface, nameservers, true); err == nil { if generator, err := parseInterface(iface, nameservers, true); err == nil {
generators = append(generators, &physicalInterface{*generator}) generators = append(generators, &physicalInterface{*generator})
} else { } else {
return nil, err return nil, err
} }
} }
for _, iface := range cfg.Private { for _, iface := range config.Private {
if generator, err := parseInterface(iface, []net.IP{}, false); err == nil { if generator, err := parseInterface(iface, []net.IP{}, false); err == nil {
generators = append(generators, &physicalInterface{*generator}) generators = append(generators, &physicalInterface{*generator})
} else { } else {

View File

@ -358,27 +358,37 @@ func TestParseInterfaces(t *testing.T) {
func TestProcessDigitalOceanNetconf(t *testing.T) { func TestProcessDigitalOceanNetconf(t *testing.T) {
for _, tt := range []struct { for _, tt := range []struct {
cfg string cfg digitalocean.Metadata
ifaces []InterfaceGenerator ifaces []InterfaceGenerator
err error err error
}{ }{
{ {
cfg: ``, cfg: digitalocean.Metadata{
}, DNS: digitalocean.DNS{
{ Nameservers: []string{"bad"},
cfg: `{"dns":{"nameservers":["bad"]}}`, },
},
err: errors.New("could not parse \"bad\" as nameserver IP address"), err: errors.New("could not parse \"bad\" as nameserver IP address"),
}, },
{ {
cfg: `{"interfaces":{"public":[{"ipv4":{"ip_address":"bad"}}]}}`, cfg: digitalocean.Metadata{
Interfaces: digitalocean.Interfaces{
Public: []digitalocean.Interface{
digitalocean.Interface{
IPv4: &digitalocean.Address{
IPAddress: "bad",
},
},
},
},
},
err: errors.New("could not parse \"bad\" as IPv4 address"), err: errors.New("could not parse \"bad\" as IPv4 address"),
}, },
{ {
cfg: `{}`,
ifaces: []InterfaceGenerator{}, ifaces: []InterfaceGenerator{},
}, },
} { } {
ifaces, err := ProcessDigitalOceanNetconf([]byte(tt.cfg)) ifaces, err := ProcessDigitalOceanNetconf(tt.cfg)
if !errorsEqual(tt.err, err) { if !errorsEqual(tt.err, err) {
t.Fatalf("bad error (%q): want %q, got %q", tt.cfg, tt.err, err) t.Fatalf("bad error (%q): want %q, got %q", tt.cfg, tt.err, err)
} }

View File

@ -15,18 +15,12 @@
package network package network
import ( import (
"encoding/json"
"net" "net"
"github.com/coreos/coreos-cloudinit/datasource/metadata/packet" "github.com/coreos/coreos-cloudinit/datasource/metadata/packet"
) )
func ProcessPacketNetconf(config []byte) ([]InterfaceGenerator, error) { func ProcessPacketNetconf(netdata packet.NetworkData) ([]InterfaceGenerator, error) {
var netdata packet.NetworkData
if err := json.Unmarshal(config, &netdata); err != nil {
return nil, err
}
var nameservers []net.IP var nameservers []net.IP
if netdata.DNS != nil { if netdata.DNS != nil {
nameservers = netdata.DNS nameservers = netdata.DNS