metadata: add support for IPv6 variable substitution
This commit is contained in:
parent
24a6f7c49c
commit
56815a6756
@ -28,6 +28,8 @@ func NewEnvironment(root, configRoot, workspace, netconfType, sshKeyName string,
|
|||||||
for k, v := range map[string]string{
|
for k, v := range map[string]string{
|
||||||
"$public_ipv4": os.Getenv("COREOS_PUBLIC_IPV4"),
|
"$public_ipv4": os.Getenv("COREOS_PUBLIC_IPV4"),
|
||||||
"$private_ipv4": os.Getenv("COREOS_PRIVATE_IPV4"),
|
"$private_ipv4": os.Getenv("COREOS_PRIVATE_IPV4"),
|
||||||
|
"$public_ipv6": os.Getenv("COREOS_PUBLIC_IPV6"),
|
||||||
|
"$private_ipv6": os.Getenv("COREOS_PRIVATE_IPV6"),
|
||||||
} {
|
} {
|
||||||
if _, ok := substitutions[k]; !ok {
|
if _, ok := substitutions[k]; !ok {
|
||||||
substitutions[k] = v
|
substitutions[k] = v
|
||||||
@ -80,6 +82,12 @@ func (e *Environment) DefaultEnvironmentFile() *system.EnvFile {
|
|||||||
if ip, ok := e.substitutions["$private_ipv4"]; ok && len(ip) > 0 {
|
if ip, ok := e.substitutions["$private_ipv4"]; ok && len(ip) > 0 {
|
||||||
ef.Vars["COREOS_PRIVATE_IPV4"] = ip
|
ef.Vars["COREOS_PRIVATE_IPV4"] = ip
|
||||||
}
|
}
|
||||||
|
if ip, ok := e.substitutions["$public_ipv6"]; ok && len(ip) > 0 {
|
||||||
|
ef.Vars["COREOS_PUBLIC_IPV6"] = ip
|
||||||
|
}
|
||||||
|
if ip, ok := e.substitutions["$private_ipv6"]; ok && len(ip) > 0 {
|
||||||
|
ef.Vars["COREOS_PRIVATE_IPV6"] = ip
|
||||||
|
}
|
||||||
if len(ef.Vars) == 0 {
|
if len(ef.Vars) == 0 {
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
|
@ -12,6 +12,8 @@ import (
|
|||||||
func TestEnvironmentApply(t *testing.T) {
|
func TestEnvironmentApply(t *testing.T) {
|
||||||
os.Setenv("COREOS_PUBLIC_IPV4", "1.2.3.4")
|
os.Setenv("COREOS_PUBLIC_IPV4", "1.2.3.4")
|
||||||
os.Setenv("COREOS_PRIVATE_IPV4", "5.6.7.8")
|
os.Setenv("COREOS_PRIVATE_IPV4", "5.6.7.8")
|
||||||
|
os.Setenv("COREOS_PUBLIC_IPV6", "1234::")
|
||||||
|
os.Setenv("COREOS_PRIVATE_IPV6", "5678::")
|
||||||
for _, tt := range []struct {
|
for _, tt := range []struct {
|
||||||
subs map[string]string
|
subs map[string]string
|
||||||
input string
|
input string
|
||||||
@ -23,14 +25,16 @@ func TestEnvironmentApply(t *testing.T) {
|
|||||||
map[string]string{
|
map[string]string{
|
||||||
"$public_ipv4": "192.0.2.3",
|
"$public_ipv4": "192.0.2.3",
|
||||||
"$private_ipv4": "192.0.2.203",
|
"$private_ipv4": "192.0.2.203",
|
||||||
|
"$public_ipv6": "fe00:1234::",
|
||||||
|
"$private_ipv6": "fe00:5678::",
|
||||||
},
|
},
|
||||||
`[Service]
|
`[Service]
|
||||||
ExecStart=/usr/bin/echo "$public_ipv4"
|
ExecStart=/usr/bin/echo "$public_ipv4 $public_ipv6"
|
||||||
ExecStop=/usr/bin/echo $private_ipv4
|
ExecStop=/usr/bin/echo $private_ipv4 $private_ipv6
|
||||||
ExecStop=/usr/bin/echo $unknown`,
|
ExecStop=/usr/bin/echo $unknown`,
|
||||||
`[Service]
|
`[Service]
|
||||||
ExecStart=/usr/bin/echo "192.0.2.3"
|
ExecStart=/usr/bin/echo "192.0.2.3 fe00:1234::"
|
||||||
ExecStop=/usr/bin/echo 192.0.2.203
|
ExecStop=/usr/bin/echo 192.0.2.203 fe00:5678::
|
||||||
ExecStop=/usr/bin/echo $unknown`,
|
ExecStop=/usr/bin/echo $unknown`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -65,8 +69,10 @@ func TestEnvironmentFile(t *testing.T) {
|
|||||||
subs := map[string]string{
|
subs := map[string]string{
|
||||||
"$public_ipv4": "1.2.3.4",
|
"$public_ipv4": "1.2.3.4",
|
||||||
"$private_ipv4": "5.6.7.8",
|
"$private_ipv4": "5.6.7.8",
|
||||||
|
"$public_ipv6": "1234::",
|
||||||
|
"$private_ipv6": "5678::",
|
||||||
}
|
}
|
||||||
expect := "COREOS_PRIVATE_IPV4=5.6.7.8\nCOREOS_PUBLIC_IPV4=1.2.3.4\n"
|
expect := "COREOS_PRIVATE_IPV4=5.6.7.8\nCOREOS_PRIVATE_IPV6=5678::\nCOREOS_PUBLIC_IPV4=1.2.3.4\nCOREOS_PUBLIC_IPV6=1234::\n"
|
||||||
|
|
||||||
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
dir, err := ioutil.TempDir(os.TempDir(), "coreos-cloudinit-")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -96,6 +102,8 @@ func TestEnvironmentFileNil(t *testing.T) {
|
|||||||
subs := map[string]string{
|
subs := map[string]string{
|
||||||
"$public_ipv4": "",
|
"$public_ipv4": "",
|
||||||
"$private_ipv4": "",
|
"$private_ipv4": "",
|
||||||
|
"$public_ipv6": "",
|
||||||
|
"$private_ipv6": "",
|
||||||
}
|
}
|
||||||
|
|
||||||
env := NewEnvironment("./", "./", "./", "", "", subs)
|
env := NewEnvironment("./", "./", "./", "", "", subs)
|
||||||
|
@ -5,8 +5,8 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ParseMetaData parses a JSON blob in the OpenStack metadata service format, and
|
// ParseMetaData parses a JSON blob in the OpenStack metadata service format,
|
||||||
// converts it to a partially hydrated CloudConfig
|
// and converts it to a partially hydrated CloudConfig.
|
||||||
func ParseMetaData(contents string) (*CloudConfig, error) {
|
func ParseMetaData(contents string) (*CloudConfig, error) {
|
||||||
if len(contents) == 0 {
|
if len(contents) == 0 {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
@ -34,22 +34,31 @@ func ParseMetaData(contents string) (*CloudConfig, error) {
|
|||||||
return &cfg, nil
|
return &cfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ExtractIPsFromMetaData parses a JSON blob in the OpenStack metadata service format,
|
// ExtractIPsFromMetaData parses a JSON blob in the OpenStack metadata service
|
||||||
// and returns a substitution map possibly containing private_ipv4 and public_ipv4 addresses
|
// format and returns a substitution map possibly containing private_ipv4,
|
||||||
|
// public_ipv4, private_ipv6, and public_ipv6 addresses.
|
||||||
func ExtractIPsFromMetadata(contents []byte) (map[string]string, error) {
|
func ExtractIPsFromMetadata(contents []byte) (map[string]string, error) {
|
||||||
var ips struct {
|
var ips struct {
|
||||||
Public string `json:"public-ipv4"`
|
PublicIPv4 string `json:"public-ipv4"`
|
||||||
Private string `json:"local-ipv4"`
|
PrivateIPv4 string `json:"local-ipv4"`
|
||||||
|
PublicIPv6 string `json:"public-ipv6"`
|
||||||
|
PrivateIPv6 string `json:"local-ipv6"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(contents, &ips); err != nil {
|
if err := json.Unmarshal(contents, &ips); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
m := make(map[string]string)
|
m := make(map[string]string)
|
||||||
if ips.Private != "" {
|
if ips.PrivateIPv4 != "" {
|
||||||
m["$private_ipv4"] = ips.Private
|
m["$private_ipv4"] = ips.PrivateIPv4
|
||||||
}
|
}
|
||||||
if ips.Public != "" {
|
if ips.PublicIPv4 != "" {
|
||||||
m["$public_ipv4"] = ips.Public
|
m["$public_ipv4"] = ips.PublicIPv4
|
||||||
|
}
|
||||||
|
if ips.PrivateIPv6 != "" {
|
||||||
|
m["$private_ipv6"] = ips.PrivateIPv6
|
||||||
|
}
|
||||||
|
if ips.PublicIPv6 != "" {
|
||||||
|
m["$public_ipv6"] = ips.PublicIPv6
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
@ -43,9 +43,9 @@ func TestExtractIPsFromMetadata(t *testing.T) {
|
|||||||
out map[string]string
|
out map[string]string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
[]byte(`{"public-ipv4": "12.34.56.78", "local-ipv4": "1.2.3.4"}`),
|
[]byte(`{"public-ipv4": "12.34.56.78", "local-ipv4": "1.2.3.4", "public-ipv6": "1234::", "local-ipv6": "5678::"}`),
|
||||||
false,
|
false,
|
||||||
map[string]string{"$public_ipv4": "12.34.56.78", "$private_ipv4": "1.2.3.4"},
|
map[string]string{"$public_ipv4": "12.34.56.78", "$private_ipv4": "1.2.3.4", "$public_ipv6": "1234::", "$private_ipv6": "5678::"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
[]byte(`{"local-ipv4": "127.0.0.1", "something_else": "don't care"}`),
|
[]byte(`{"local-ipv4": "127.0.0.1", "something_else": "don't care"}`),
|
||||||
|
Loading…
Reference in New Issue
Block a user