datasource: remove FetchNetworkConfig step

Its easier to let each datasource grab all metadata in the FetchMetadata
stage than to break it into multiple stages.
This commit is contained in:
Alex Crawford 2015-01-23 11:31:57 -08:00
parent 42153edbbc
commit 9605b5edf2
14 changed files with 65 additions and 97 deletions

View File

@ -188,7 +188,6 @@ func main() {
var ccu *config.CloudConfig var ccu *config.CloudConfig
var script *config.Script var script *config.Script
if ud, err := initialize.ParseUserData(userdata); err != nil { if ud, err := initialize.ParseUserData(userdata); err != nil {
fmt.Printf("Failed to parse user-data: %v\nContinuing...\n", err) fmt.Printf("Failed to parse user-data: %v\nContinuing...\n", err)
failure = true failure = true
@ -204,16 +203,6 @@ func main() {
fmt.Println("Merging cloud-config from meta-data and user-data") fmt.Println("Merging cloud-config from meta-data and user-data")
cc := mergeConfigs(ccu, metadata) cc := mergeConfigs(ccu, metadata)
if flags.convertNetconf != "" {
fmt.Printf("Fetching network config from datasource of type %q\n", ds.Type())
netconfBytes, err := ds.FetchNetworkConfig(metadata.NetworkConfigPath)
if err != nil {
fmt.Printf("Failed fetching network config from datasource: %v\n", err)
os.Exit(1)
}
cc.Internal.NetworkConfig = netconfBytes
}
if err = initialize.Apply(cc, env); err != nil { if err = initialize.Apply(cc, env); err != nil {
fmt.Printf("Failed to apply cloud-config: %v\n", err) fmt.Printf("Failed to apply cloud-config: %v\n", err)
os.Exit(1) os.Exit(1)
@ -249,6 +238,7 @@ func mergeConfigs(cc *config.CloudConfig, md datasource.Metadata) (out config.Cl
for _, key := range md.SSHPublicKeys { for _, key := range md.SSHPublicKeys {
out.SSHAuthorizedKeys = append(out.SSHAuthorizedKeys, key) out.SSHAuthorizedKeys = append(out.SSHAuthorizedKeys, key)
} }
out.Internal.NetworkConfig = md.NetworkConfig
return return
} }

View File

@ -69,7 +69,7 @@ func (cd *configDrive) FetchMetadata() (metadata datasource.Metadata, err error)
metadata.SSHPublicKeys = m.SSHAuthorizedKeyMap metadata.SSHPublicKeys = m.SSHAuthorizedKeyMap
metadata.Hostname = m.Hostname metadata.Hostname = m.Hostname
metadata.NetworkConfigPath = m.NetworkConfig.ContentPath metadata.NetworkConfig, err = cd.tryReadFile(path.Join(cd.openstackRoot(), m.NetworkConfig.ContentPath))
return return
} }
@ -78,13 +78,6 @@ func (cd *configDrive) FetchUserdata() ([]byte, error) {
return cd.tryReadFile(path.Join(cd.openstackVersionRoot(), "user_data")) return cd.tryReadFile(path.Join(cd.openstackVersionRoot(), "user_data"))
} }
func (cd *configDrive) FetchNetworkConfig(filename string) ([]byte, error) {
if filename == "" {
return []byte{}, nil
}
return cd.tryReadFile(path.Join(cd.openstackRoot(), filename))
}
func (cd *configDrive) Type() string { func (cd *configDrive) Type() string {
return "cloud-drive" return "cloud-drive"
} }

View File

@ -40,10 +40,13 @@ func TestFetchMetadata(t *testing.T) {
}, },
{ {
root: "/media/configdrive", root: "/media/configdrive",
files: test.MockFilesystem{"/media/configdrive/openstack/latest/meta_data.json": `{"hostname": "host", "network_config": {"content_path": "path"}, "public_keys":{"1": "key1", "2": "key2"}}`}, files: test.MockFilesystem{
"/media/configdrive/openstack/latest/meta_data.json": `{"hostname": "host", "network_config": {"content_path": "config_file.json"}, "public_keys":{"1": "key1", "2": "key2"}}`,
"/media/configdrive/openstack/config_file.json": "make it work",
},
metadata: datasource.Metadata{ metadata: datasource.Metadata{
Hostname: "host", Hostname: "host",
NetworkConfigPath: "path", NetworkConfig: []byte("make it work"),
SSHPublicKeys: map[string]string{ SSHPublicKeys: map[string]string{
"1": "key1", "1": "key1",
"2": "key2", "2": "key2",

View File

@ -24,7 +24,6 @@ type Datasource interface {
ConfigRoot() string ConfigRoot() string
FetchMetadata() (Metadata, error) FetchMetadata() (Metadata, error)
FetchUserdata() ([]byte, error) FetchUserdata() ([]byte, error)
FetchNetworkConfig(string) ([]byte, error)
Type() string Type() string
} }
@ -35,5 +34,5 @@ type Metadata struct {
PrivateIPv6 net.IP PrivateIPv6 net.IP
Hostname string Hostname string
SSHPublicKeys map[string]string SSHPublicKeys map[string]string
NetworkConfigPath string NetworkConfig []byte
} }

View File

@ -50,10 +50,6 @@ func (f *localFile) FetchUserdata() ([]byte, error) {
return ioutil.ReadFile(f.path) return ioutil.ReadFile(f.path)
} }
func (f *localFile) FetchNetworkConfig(filename string) ([]byte, error) {
return nil, nil
}
func (f *localFile) Type() string { func (f *localFile) Type() string {
return "local-file" return "local-file"
} }

View File

@ -145,10 +145,6 @@ func (scs *serverContextService) FetchUserdata() ([]byte, error) {
return []byte(userData), nil return []byte(userData), nil
} }
func (scs *serverContextService) FetchNetworkConfig(a string) ([]byte, error) {
return nil, nil
}
func (scs *serverContextService) findLocalIP(mac string) (net.IP, error) { func (scs *serverContextService) findLocalIP(mac string) (net.IP, error) {
ifaces, err := net.Interfaces() ifaces, err := net.Interfaces()
if err != nil { if err != nil {

View File

@ -61,8 +61,6 @@ type Metadata struct {
} }
type metadataService struct { type metadataService struct {
interfaces Interfaces
dns DNS
metadata.MetadataService metadata.MetadataService
} }
@ -81,9 +79,6 @@ func (ms *metadataService) FetchMetadata() (metadata datasource.Metadata, err er
return return
} }
ms.interfaces = m.Interfaces
ms.dns = m.DNS
if len(m.Interfaces.Public) > 0 { if len(m.Interfaces.Public) > 0 {
if m.Interfaces.Public[0].IPv4 != nil { if m.Interfaces.Public[0].IPv4 != nil {
metadata.PublicIPv4 = net.ParseIP(m.Interfaces.Public[0].IPv4.IPAddress) metadata.PublicIPv4 = net.ParseIP(m.Interfaces.Public[0].IPv4.IPAddress)
@ -105,17 +100,11 @@ 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
return return
} }
func (ms metadataService) FetchNetworkConfig(filename string) ([]byte, error) {
return json.Marshal(Metadata{
Interfaces: ms.interfaces,
DNS: ms.dns,
})
}
func (ms metadataService) Type() string { func (ms metadataService) Type() string {
return "digitalocean-metadata-service" return "digitalocean-metadata-service"
} }

View File

@ -90,6 +90,34 @@ func TestFetchMetadata(t *testing.T) {
"0": "publickey1", "0": "publickey1",
"1": "publickey2", "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"
}
]
}
}`),
}, },
}, },
{ {

View File

@ -85,12 +85,6 @@ func (ms metadataService) FetchMetadata() (datasource.Metadata, error) {
return metadata, err return metadata, err
} }
if contentPath, err := ms.fetchAttribute(fmt.Sprintf("%s/network_config/content_path", ms.MetadataUrl())); err == nil {
metadata.NetworkConfigPath = contentPath
} else if _, ok := err.(pkg.ErrNotFound); !ok {
return metadata, err
}
return metadata, nil return metadata, nil
} }

View File

@ -168,14 +168,12 @@ func TestFetchMetadata(t *testing.T) {
"/2009-04-04/meta-data/public-keys": "0=test1\n", "/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",
"/2009-04-04/meta-data/public-keys/0/openssh-key": "key", "/2009-04-04/meta-data/public-keys/0/openssh-key": "key",
"/2009-04-04/meta-data/network_config/content_path": "path",
}, },
expect: datasource.Metadata{ expect: datasource.Metadata{
Hostname: "host", Hostname: "host",
PrivateIPv4: net.ParseIP("1.2.3.4"), PrivateIPv4: net.ParseIP("1.2.3.4"),
PublicIPv4: net.ParseIP("5.6.7.8"), PublicIPv4: net.ParseIP("5.6.7.8"),
SSHPublicKeys: map[string]string{"test1": "key"}, SSHPublicKeys: map[string]string{"test1": "key"},
NetworkConfigPath: "path",
}, },
}, },
{ {
@ -188,14 +186,12 @@ func TestFetchMetadata(t *testing.T) {
"/2009-04-04/meta-data/public-keys": "0=test1\n", "/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",
"/2009-04-04/meta-data/public-keys/0/openssh-key": "key", "/2009-04-04/meta-data/public-keys/0/openssh-key": "key",
"/2009-04-04/meta-data/network_config/content_path": "path",
}, },
expect: datasource.Metadata{ expect: datasource.Metadata{
Hostname: "host", Hostname: "host",
PrivateIPv4: net.ParseIP("1.2.3.4"), PrivateIPv4: net.ParseIP("1.2.3.4"),
PublicIPv4: net.ParseIP("5.6.7.8"), PublicIPv4: net.ParseIP("5.6.7.8"),
SSHPublicKeys: map[string]string{"test1": "key"}, SSHPublicKeys: map[string]string{"test1": "key"},
NetworkConfigPath: "path",
}, },
}, },
{ {

View File

@ -52,10 +52,6 @@ func (ms MetadataService) FetchUserdata() ([]byte, error) {
return ms.FetchData(ms.UserdataUrl()) return ms.FetchData(ms.UserdataUrl())
} }
func (ms MetadataService) FetchNetworkConfig(filename string) ([]byte, error) {
return nil, nil
}
func (ms MetadataService) FetchData(url string) ([]byte, error) { func (ms MetadataService) FetchData(url string) ([]byte, error) {
if data, err := ms.Client.GetRetry(url); err == nil { if data, err := ms.Client.GetRetry(url); err == nil {
return data, err return data, err

View File

@ -81,10 +81,6 @@ func (c *procCmdline) FetchUserdata() ([]byte, error) {
return cfg, nil return cfg, nil
} }
func (c *procCmdline) FetchNetworkConfig(filename string) ([]byte, error) {
return nil, nil
}
func (c *procCmdline) Type() string { func (c *procCmdline) Type() string {
return "proc-cmdline" return "proc-cmdline"
} }

View File

@ -50,10 +50,6 @@ func (f *remoteFile) FetchUserdata() ([]byte, error) {
return client.GetRetry(f.url) return client.GetRetry(f.url)
} }
func (f *remoteFile) FetchNetworkConfig(filename string) ([]byte, error) {
return nil, nil
}
func (f *remoteFile) Type() string { func (f *remoteFile) Type() string {
return "url" return "url"
} }

View File

@ -103,10 +103,6 @@ func (a *waagent) FetchUserdata() ([]byte, error) {
return a.tryReadFile(path.Join(a.root, "CustomData")) return a.tryReadFile(path.Join(a.root, "CustomData"))
} }
func (a *waagent) FetchNetworkConfig(filename string) ([]byte, error) {
return nil, nil
}
func (a *waagent) Type() string { func (a *waagent) Type() string {
return "waagent" return "waagent"
} }