config: remove network config from CloudConfig

This commit is contained in:
Alex Crawford 2015-01-26 17:35:08 -08:00
parent 9605b5edf2
commit 536f8acf2a
5 changed files with 35 additions and 42 deletions

View File

@ -27,13 +27,12 @@ import (
// directly to YAML. Fields that cannot be set in the cloud-config (fields // directly to YAML. Fields that cannot be set in the cloud-config (fields
// used for internal use) have the YAML tag '-' so that they aren't marshalled. // used for internal use) have the YAML tag '-' so that they aren't marshalled.
type CloudConfig struct { type CloudConfig struct {
SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"` SSHAuthorizedKeys []string `yaml:"ssh_authorized_keys"`
CoreOS CoreOS `yaml:"coreos"` CoreOS CoreOS `yaml:"coreos"`
WriteFiles []File `yaml:"write_files"` WriteFiles []File `yaml:"write_files"`
Hostname string `yaml:"hostname"` Hostname string `yaml:"hostname"`
Users []User `yaml:"users"` Users []User `yaml:"users"`
ManageEtcHosts EtcHosts `yaml:"manage_etc_hosts"` ManageEtcHosts EtcHosts `yaml:"manage_etc_hosts"`
Internal Internals `yaml:"-"`
} }
type CoreOS struct { type CoreOS struct {
@ -46,10 +45,6 @@ type CoreOS struct {
Units []Unit `yaml:"units"` Units []Unit `yaml:"units"`
} }
type Internals struct {
NetworkConfig []byte
}
func IsCloudConfig(userdata string) bool { func IsCloudConfig(userdata string) bool {
header := strings.SplitN(userdata, "\n", 2)[0] header := strings.SplitN(userdata, "\n", 2)[0]

View File

@ -33,6 +33,7 @@ import (
"github.com/coreos/coreos-cloudinit/datasource/url" "github.com/coreos/coreos-cloudinit/datasource/url"
"github.com/coreos/coreos-cloudinit/datasource/waagent" "github.com/coreos/coreos-cloudinit/datasource/waagent"
"github.com/coreos/coreos-cloudinit/initialize" "github.com/coreos/coreos-cloudinit/initialize"
"github.com/coreos/coreos-cloudinit/network"
"github.com/coreos/coreos-cloudinit/pkg" "github.com/coreos/coreos-cloudinit/pkg"
"github.com/coreos/coreos-cloudinit/system" "github.com/coreos/coreos-cloudinit/system"
) )
@ -183,7 +184,7 @@ func main() {
} }
// Apply environment to user-data // Apply environment to user-data
env := initialize.NewEnvironment("/", ds.ConfigRoot(), flags.workspace, flags.convertNetconf, flags.sshKeyName, metadata) env := initialize.NewEnvironment("/", ds.ConfigRoot(), flags.workspace, flags.sshKeyName, metadata)
userdata := env.Apply(string(userdataBytes)) userdata := env.Apply(string(userdataBytes))
var ccu *config.CloudConfig var ccu *config.CloudConfig
@ -203,7 +204,24 @@ 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 err = initialize.Apply(cc, env); err != nil { var ifaces []network.InterfaceGenerator
if flags.convertNetconf != "" {
var err error
switch flags.convertNetconf {
case "debian":
ifaces, err = network.ProcessDebianNetconf(metadata.NetworkConfig)
case "digitalocean":
ifaces, err = network.ProcessDigitalOceanNetconf(metadata.NetworkConfig)
default:
err = fmt.Errorf("Unsupported network config format %q", flags.convertNetconf)
}
if err != nil {
fmt.Printf("Failed to generate interfaces: %v\n", err)
os.Exit(1)
}
}
if err = initialize.Apply(cc, ifaces, 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)
} }
@ -238,7 +256,6 @@ 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

@ -42,7 +42,7 @@ type CloudConfigUnit interface {
// Apply renders a CloudConfig to an Environment. This can involve things like // Apply renders a CloudConfig to an Environment. This can involve things like
// configuring the hostname, adding new users, writing various configuration // configuring the hostname, adding new users, writing various configuration
// files to disk, and manipulating systemd services. // files to disk, and manipulating systemd services.
func Apply(cfg config.CloudConfig, env *Environment) error { func Apply(cfg config.CloudConfig, ifaces []network.InterfaceGenerator, env *Environment) error {
if cfg.Hostname != "" { if cfg.Hostname != "" {
if err := system.SetHostname(cfg.Hostname); err != nil { if err := system.SetHostname(cfg.Hostname); err != nil {
return err return err
@ -165,23 +165,9 @@ func Apply(cfg config.CloudConfig, env *Environment) error {
} }
} }
if env.NetconfType() != "" { if len(ifaces) > 0 {
var interfaces []network.InterfaceGenerator units = append(units, createNetworkingUnits(ifaces)...)
var err error if err := system.RestartNetwork(ifaces); err != nil {
switch env.NetconfType() {
case "debian":
interfaces, err = network.ProcessDebianNetconf(cfg.Internal.NetworkConfig)
case "digitalocean":
interfaces, err = network.ProcessDigitalOceanNetconf(cfg.Internal.NetworkConfig)
default:
err = fmt.Errorf("Unsupported network config format %q", env.NetconfType())
}
if err != nil {
return err
}
units = append(units, createNetworkingUnits(interfaces)...)
if err := system.RestartNetwork(interfaces); err != nil {
return err return err
} }
} }

View File

@ -32,13 +32,12 @@ type Environment struct {
root string root string
configRoot string configRoot string
workspace string workspace string
netconfType string
sshKeyName string sshKeyName string
substitutions map[string]string substitutions map[string]string
} }
// TODO(jonboulle): this is getting unwieldy, should be able to simplify the interface somehow // TODO(jonboulle): this is getting unwieldy, should be able to simplify the interface somehow
func NewEnvironment(root, configRoot, workspace, netconfType, sshKeyName string, metadata datasource.Metadata) *Environment { func NewEnvironment(root, configRoot, workspace, sshKeyName string, metadata datasource.Metadata) *Environment {
firstNonNull := func(ip net.IP, env string) string { firstNonNull := func(ip net.IP, env string) string {
if ip == nil { if ip == nil {
return env return env
@ -51,7 +50,7 @@ func NewEnvironment(root, configRoot, workspace, netconfType, sshKeyName string,
"$public_ipv6": firstNonNull(metadata.PublicIPv6, os.Getenv("COREOS_PUBLIC_IPV6")), "$public_ipv6": firstNonNull(metadata.PublicIPv6, os.Getenv("COREOS_PUBLIC_IPV6")),
"$private_ipv6": firstNonNull(metadata.PrivateIPv6, os.Getenv("COREOS_PRIVATE_IPV6")), "$private_ipv6": firstNonNull(metadata.PrivateIPv6, os.Getenv("COREOS_PRIVATE_IPV6")),
} }
return &Environment{root, configRoot, workspace, netconfType, sshKeyName, substitutions} return &Environment{root, configRoot, workspace, sshKeyName, substitutions}
} }
func (e *Environment) Workspace() string { func (e *Environment) Workspace() string {
@ -66,10 +65,6 @@ func (e *Environment) ConfigRoot() string {
return e.configRoot return e.configRoot
} }
func (e *Environment) NetconfType() string {
return e.netconfType
}
func (e *Environment) SSHKeyName() string { func (e *Environment) SSHKeyName() string {
return e.sshKeyName return e.sshKeyName
} }

View File

@ -95,7 +95,7 @@ addr: $private_ipv4
}, },
} { } {
env := NewEnvironment("./", "./", "./", "", "", tt.metadata) env := NewEnvironment("./", "./", "./", "", tt.metadata)
got := env.Apply(tt.input) got := env.Apply(tt.input)
if got != tt.out { if got != tt.out {
t.Fatalf("Environment incorrectly applied.\ngot:\n%s\nwant:\n%s", got, tt.out) t.Fatalf("Environment incorrectly applied.\ngot:\n%s\nwant:\n%s", got, tt.out)
@ -118,7 +118,7 @@ func TestEnvironmentFile(t *testing.T) {
} }
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
env := NewEnvironment("./", "./", "./", "", "", metadata) env := NewEnvironment("./", "./", "./", "", metadata)
ef := env.DefaultEnvironmentFile() ef := env.DefaultEnvironmentFile()
err = system.WriteEnvFile(ef, dir) err = system.WriteEnvFile(ef, dir)
if err != nil { if err != nil {
@ -140,7 +140,7 @@ func TestEnvironmentFileNil(t *testing.T) {
os.Clearenv() os.Clearenv()
metadata := datasource.Metadata{} metadata := datasource.Metadata{}
env := NewEnvironment("./", "./", "./", "", "", metadata) env := NewEnvironment("./", "./", "./", "", metadata)
ef := env.DefaultEnvironmentFile() ef := env.DefaultEnvironmentFile()
if ef != nil { if ef != nil {
t.Fatalf("Environment file not nil: %v", ef) t.Fatalf("Environment file not nil: %v", ef)