From 9ba25550a1086bc2381a828f6f2c5c0796e25967 Mon Sep 17 00:00:00 2001 From: Alex Crawford Date: Fri, 15 May 2015 12:40:23 -0700 Subject: [PATCH] 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. --- coreos-cloudinit.go | 6 +-- datasource/datasource.go | 2 +- datasource/metadata/digitalocean/metadata.go | 2 +- .../metadata/digitalocean/metadata_test.go | 49 ++++++++----------- datasource/metadata/packet/metadata.go | 2 +- network/digitalocean.go | 29 ++++------- network/digitalocean_test.go | 26 +++++++--- network/packet.go | 8 +-- 8 files changed, 56 insertions(+), 68 deletions(-) diff --git a/coreos-cloudinit.go b/coreos-cloudinit.go index c8ed1ee..46a1148 100644 --- a/coreos-cloudinit.go +++ b/coreos-cloudinit.go @@ -225,11 +225,11 @@ func main() { var err error switch flags.convertNetconf { case "debian": - ifaces, err = network.ProcessDebianNetconf(metadata.NetworkConfig) + ifaces, err = network.ProcessDebianNetconf(metadata.NetworkConfig.([]byte)) case "digitalocean": - ifaces, err = network.ProcessDigitalOceanNetconf(metadata.NetworkConfig) + ifaces, err = network.ProcessDigitalOceanNetconf(metadata.NetworkConfig.(digitalocean.Metadata)) case "packet": - ifaces, err = network.ProcessPacketNetconf(metadata.NetworkConfig) + ifaces, err = network.ProcessPacketNetconf(metadata.NetworkConfig.(packet.NetworkData)) default: err = fmt.Errorf("Unsupported network config format %q", flags.convertNetconf) } diff --git a/datasource/datasource.go b/datasource/datasource.go index 766f9a7..e459303 100644 --- a/datasource/datasource.go +++ b/datasource/datasource.go @@ -34,5 +34,5 @@ type Metadata struct { PrivateIPv6 net.IP Hostname string SSHPublicKeys map[string]string - NetworkConfig []byte + NetworkConfig interface{} } diff --git a/datasource/metadata/digitalocean/metadata.go b/datasource/metadata/digitalocean/metadata.go index a42796e..d8b71ae 100644 --- a/datasource/metadata/digitalocean/metadata.go +++ b/datasource/metadata/digitalocean/metadata.go @@ -100,7 +100,7 @@ func (ms *metadataService) FetchMetadata() (metadata datasource.Metadata, err er for i, key := range m.PublicKeys { metadata.SSHPublicKeys[strconv.Itoa(i)] = key } - metadata.NetworkConfig = data + metadata.NetworkConfig = m return } diff --git a/datasource/metadata/digitalocean/metadata_test.go b/datasource/metadata/digitalocean/metadata_test.go index 881a352..fd8a0a0 100644 --- a/datasource/metadata/digitalocean/metadata_test.go +++ b/datasource/metadata/digitalocean/metadata_test.go @@ -90,34 +90,27 @@ func TestFetchMetadata(t *testing.T) { "0": "publickey1", "1": "publickey2", }, - NetworkConfig: []byte(`{ - "droplet_id": 1, - "user_data": "hello", - "vendor_data": "hello", - "public_keys": [ - "publickey1", - "publickey2" - ], - "region": "nyc2", - "interfaces": { - "public": [ - { - "ipv4": { - "ip_address": "192.168.1.2", - "netmask": "255.255.255.0", - "gateway": "192.168.1.1" - }, - "ipv6": { - "ip_address": "fe00::", - "cidr": 126, - "gateway": "fe00::" - }, - "mac": "ab:cd:ef:gh:ij", - "type": "public" - } - ] - } -}`), + NetworkConfig: Metadata{ + Interfaces: Interfaces{ + Public: []Interface{ + 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", + }, + }, + }, + PublicKeys: []string{"publickey1", "publickey2"}, + }, }, }, { diff --git a/datasource/metadata/packet/metadata.go b/datasource/metadata/packet/metadata.go index 3928a2c..3c7d499 100644 --- a/datasource/metadata/packet/metadata.go +++ b/datasource/metadata/packet/metadata.go @@ -96,7 +96,7 @@ func (ms *metadataService) FetchMetadata() (metadata datasource.Metadata, err er metadata.SSHPublicKeys[strconv.Itoa(i)] = key } - metadata.NetworkConfig, err = json.Marshal(m.NetworkData) + metadata.NetworkConfig = m.NetworkData return } diff --git a/network/digitalocean.go b/network/digitalocean.go index d55681a..f31f989 100644 --- a/network/digitalocean.go +++ b/network/digitalocean.go @@ -15,7 +15,6 @@ package network import ( - "encoding/json" "fmt" "log" "net" @@ -23,26 +22,18 @@ import ( "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") - 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") - nameservers, err := parseNameservers(cfg.DNS) + nameservers, err := parseNameservers(config.DNS) if err != nil { return nil, err } log.Printf("Parsed %d nameservers\n", len(nameservers)) log.Println("Parsing interfaces") - generators, err := parseInterfaces(cfg.Interfaces, nameservers) + generators, err := parseInterfaces(config.Interfaces, nameservers) if err != nil { return nil, err } @@ -52,9 +43,9 @@ func ProcessDigitalOceanNetconf(config []byte) ([]InterfaceGenerator, error) { return generators, nil } -func parseNameservers(cfg digitalocean.DNS) ([]net.IP, error) { - nameservers := make([]net.IP, 0, len(cfg.Nameservers)) - for _, ns := range cfg.Nameservers { +func parseNameservers(config digitalocean.DNS) ([]net.IP, error) { + nameservers := make([]net.IP, 0, len(config.Nameservers)) + for _, ns := range config.Nameservers { if ip := net.ParseIP(ns); ip == nil { return nil, fmt.Errorf("could not parse %q as nameserver IP address", ns) } else { @@ -64,16 +55,16 @@ func parseNameservers(cfg digitalocean.DNS) ([]net.IP, error) { return nameservers, nil } -func parseInterfaces(cfg digitalocean.Interfaces, nameservers []net.IP) ([]InterfaceGenerator, error) { - generators := make([]InterfaceGenerator, 0, len(cfg.Public)+len(cfg.Private)) - for _, iface := range cfg.Public { +func parseInterfaces(config digitalocean.Interfaces, nameservers []net.IP) ([]InterfaceGenerator, error) { + generators := make([]InterfaceGenerator, 0, len(config.Public)+len(config.Private)) + for _, iface := range config.Public { if generator, err := parseInterface(iface, nameservers, true); err == nil { generators = append(generators, &physicalInterface{*generator}) } else { return nil, err } } - for _, iface := range cfg.Private { + for _, iface := range config.Private { if generator, err := parseInterface(iface, []net.IP{}, false); err == nil { generators = append(generators, &physicalInterface{*generator}) } else { diff --git a/network/digitalocean_test.go b/network/digitalocean_test.go index 5cb7257..381ce2c 100644 --- a/network/digitalocean_test.go +++ b/network/digitalocean_test.go @@ -358,27 +358,37 @@ func TestParseInterfaces(t *testing.T) { func TestProcessDigitalOceanNetconf(t *testing.T) { for _, tt := range []struct { - cfg string + cfg digitalocean.Metadata ifaces []InterfaceGenerator err error }{ { - cfg: ``, - }, - { - cfg: `{"dns":{"nameservers":["bad"]}}`, + cfg: digitalocean.Metadata{ + DNS: digitalocean.DNS{ + Nameservers: []string{"bad"}, + }, + }, 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"), }, { - cfg: `{}`, ifaces: []InterfaceGenerator{}, }, } { - ifaces, err := ProcessDigitalOceanNetconf([]byte(tt.cfg)) + ifaces, err := ProcessDigitalOceanNetconf(tt.cfg) if !errorsEqual(tt.err, err) { t.Fatalf("bad error (%q): want %q, got %q", tt.cfg, tt.err, err) } diff --git a/network/packet.go b/network/packet.go index e763049..12000ad 100644 --- a/network/packet.go +++ b/network/packet.go @@ -15,18 +15,12 @@ package network import ( - "encoding/json" "net" "github.com/coreos/coreos-cloudinit/datasource/metadata/packet" ) -func ProcessPacketNetconf(config []byte) ([]InterfaceGenerator, error) { - var netdata packet.NetworkData - if err := json.Unmarshal(config, &netdata); err != nil { - return nil, err - } - +func ProcessPacketNetconf(netdata packet.NetworkData) ([]InterfaceGenerator, error) { var nameservers []net.IP if netdata.DNS != nil { nameservers = netdata.DNS