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:
parent
42153edbbc
commit
9605b5edf2
@ -188,7 +188,6 @@ func main() {
|
||||
|
||||
var ccu *config.CloudConfig
|
||||
var script *config.Script
|
||||
|
||||
if ud, err := initialize.ParseUserData(userdata); err != nil {
|
||||
fmt.Printf("Failed to parse user-data: %v\nContinuing...\n", err)
|
||||
failure = true
|
||||
@ -204,16 +203,6 @@ func main() {
|
||||
fmt.Println("Merging cloud-config from meta-data and user-data")
|
||||
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 {
|
||||
fmt.Printf("Failed to apply cloud-config: %v\n", err)
|
||||
os.Exit(1)
|
||||
@ -249,6 +238,7 @@ func mergeConfigs(cc *config.CloudConfig, md datasource.Metadata) (out config.Cl
|
||||
for _, key := range md.SSHPublicKeys {
|
||||
out.SSHAuthorizedKeys = append(out.SSHAuthorizedKeys, key)
|
||||
}
|
||||
out.Internal.NetworkConfig = md.NetworkConfig
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -69,7 +69,7 @@ func (cd *configDrive) FetchMetadata() (metadata datasource.Metadata, err error)
|
||||
|
||||
metadata.SSHPublicKeys = m.SSHAuthorizedKeyMap
|
||||
metadata.Hostname = m.Hostname
|
||||
metadata.NetworkConfigPath = m.NetworkConfig.ContentPath
|
||||
metadata.NetworkConfig, err = cd.tryReadFile(path.Join(cd.openstackRoot(), m.NetworkConfig.ContentPath))
|
||||
|
||||
return
|
||||
}
|
||||
@ -78,13 +78,6 @@ func (cd *configDrive) FetchUserdata() ([]byte, error) {
|
||||
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 {
|
||||
return "cloud-drive"
|
||||
}
|
||||
|
@ -39,11 +39,14 @@ func TestFetchMetadata(t *testing.T) {
|
||||
metadata: datasource.Metadata{Hostname: "host"},
|
||||
},
|
||||
{
|
||||
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"}}`},
|
||||
root: "/media/configdrive",
|
||||
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{
|
||||
Hostname: "host",
|
||||
NetworkConfigPath: "path",
|
||||
Hostname: "host",
|
||||
NetworkConfig: []byte("make it work"),
|
||||
SSHPublicKeys: map[string]string{
|
||||
"1": "key1",
|
||||
"2": "key2",
|
||||
|
@ -24,16 +24,15 @@ type Datasource interface {
|
||||
ConfigRoot() string
|
||||
FetchMetadata() (Metadata, error)
|
||||
FetchUserdata() ([]byte, error)
|
||||
FetchNetworkConfig(string) ([]byte, error)
|
||||
Type() string
|
||||
}
|
||||
|
||||
type Metadata struct {
|
||||
PublicIPv4 net.IP
|
||||
PublicIPv6 net.IP
|
||||
PrivateIPv4 net.IP
|
||||
PrivateIPv6 net.IP
|
||||
Hostname string
|
||||
SSHPublicKeys map[string]string
|
||||
NetworkConfigPath string
|
||||
PublicIPv4 net.IP
|
||||
PublicIPv6 net.IP
|
||||
PrivateIPv4 net.IP
|
||||
PrivateIPv6 net.IP
|
||||
Hostname string
|
||||
SSHPublicKeys map[string]string
|
||||
NetworkConfig []byte
|
||||
}
|
||||
|
@ -50,10 +50,6 @@ func (f *localFile) FetchUserdata() ([]byte, error) {
|
||||
return ioutil.ReadFile(f.path)
|
||||
}
|
||||
|
||||
func (f *localFile) FetchNetworkConfig(filename string) ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (f *localFile) Type() string {
|
||||
return "local-file"
|
||||
}
|
||||
|
@ -145,10 +145,6 @@ func (scs *serverContextService) FetchUserdata() ([]byte, error) {
|
||||
return []byte(userData), nil
|
||||
}
|
||||
|
||||
func (scs *serverContextService) FetchNetworkConfig(a string) ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (scs *serverContextService) findLocalIP(mac string) (net.IP, error) {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
|
@ -61,8 +61,6 @@ type Metadata struct {
|
||||
}
|
||||
|
||||
type metadataService struct {
|
||||
interfaces Interfaces
|
||||
dns DNS
|
||||
metadata.MetadataService
|
||||
}
|
||||
|
||||
@ -81,9 +79,6 @@ func (ms *metadataService) FetchMetadata() (metadata datasource.Metadata, err er
|
||||
return
|
||||
}
|
||||
|
||||
ms.interfaces = m.Interfaces
|
||||
ms.dns = m.DNS
|
||||
|
||||
if len(m.Interfaces.Public) > 0 {
|
||||
if m.Interfaces.Public[0].IPv4 != nil {
|
||||
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 {
|
||||
metadata.SSHPublicKeys[strconv.Itoa(i)] = key
|
||||
}
|
||||
metadata.NetworkConfig = data
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (ms metadataService) FetchNetworkConfig(filename string) ([]byte, error) {
|
||||
return json.Marshal(Metadata{
|
||||
Interfaces: ms.interfaces,
|
||||
DNS: ms.dns,
|
||||
})
|
||||
}
|
||||
|
||||
func (ms metadataService) Type() string {
|
||||
return "digitalocean-metadata-service"
|
||||
}
|
||||
|
@ -90,6 +90,34 @@ 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"
|
||||
}
|
||||
]
|
||||
}
|
||||
}`),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -85,12 +85,6 @@ func (ms metadataService) FetchMetadata() (datasource.Metadata, error) {
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -162,40 +162,36 @@ func TestFetchMetadata(t *testing.T) {
|
||||
root: "/",
|
||||
metadataPath: "2009-04-04/meta-data",
|
||||
resources: map[string]string{
|
||||
"/2009-04-04/meta-data/hostname": "host",
|
||||
"/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/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",
|
||||
"/2009-04-04/meta-data/network_config/content_path": "path",
|
||||
"/2009-04-04/meta-data/hostname": "host",
|
||||
"/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/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"),
|
||||
SSHPublicKeys: map[string]string{"test1": "key"},
|
||||
NetworkConfigPath: "path",
|
||||
Hostname: "host",
|
||||
PrivateIPv4: net.ParseIP("1.2.3.4"),
|
||||
PublicIPv4: net.ParseIP("5.6.7.8"),
|
||||
SSHPublicKeys: map[string]string{"test1": "key"},
|
||||
},
|
||||
},
|
||||
{
|
||||
root: "/",
|
||||
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/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",
|
||||
"/2009-04-04/meta-data/network_config/content_path": "path",
|
||||
"/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/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"),
|
||||
SSHPublicKeys: map[string]string{"test1": "key"},
|
||||
NetworkConfigPath: "path",
|
||||
Hostname: "host",
|
||||
PrivateIPv4: net.ParseIP("1.2.3.4"),
|
||||
PublicIPv4: net.ParseIP("5.6.7.8"),
|
||||
SSHPublicKeys: map[string]string{"test1": "key"},
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -52,10 +52,6 @@ func (ms MetadataService) FetchUserdata() ([]byte, error) {
|
||||
return ms.FetchData(ms.UserdataUrl())
|
||||
}
|
||||
|
||||
func (ms MetadataService) FetchNetworkConfig(filename string) ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (ms MetadataService) FetchData(url string) ([]byte, error) {
|
||||
if data, err := ms.Client.GetRetry(url); err == nil {
|
||||
return data, err
|
||||
|
@ -81,10 +81,6 @@ func (c *procCmdline) FetchUserdata() ([]byte, error) {
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (c *procCmdline) FetchNetworkConfig(filename string) ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *procCmdline) Type() string {
|
||||
return "proc-cmdline"
|
||||
}
|
||||
|
@ -50,10 +50,6 @@ func (f *remoteFile) FetchUserdata() ([]byte, error) {
|
||||
return client.GetRetry(f.url)
|
||||
}
|
||||
|
||||
func (f *remoteFile) FetchNetworkConfig(filename string) ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (f *remoteFile) Type() string {
|
||||
return "url"
|
||||
}
|
||||
|
@ -103,10 +103,6 @@ func (a *waagent) FetchUserdata() ([]byte, error) {
|
||||
return a.tryReadFile(path.Join(a.root, "CustomData"))
|
||||
}
|
||||
|
||||
func (a *waagent) FetchNetworkConfig(filename string) ([]byte, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (a *waagent) Type() string {
|
||||
return "waagent"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user