Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split the network unit logic from Apply() #20

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 41 additions & 29 deletions coreos-cloudinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func main() {
os.Exit(2)
}

if flags.printVersion == true {
if flags.printVersion {
fmt.Printf("coreos-cloudinit %s\n", version)
os.Exit(0)
}
Expand All @@ -188,6 +188,22 @@ func main() {
os.Exit(1)
}

log.Printf("Fetching meta-data from datasource of type %q\n", ds.Type())
metadata, err := ds.FetchMetadata()
if err != nil {
log.Printf("Failed fetching meta-data from datasource: %v\n", err)
os.Exit(1)
}
env := initialize.NewEnvironment("/", ds.ConfigRoot(), flags.workspace, flags.sshKeyName, metadata)

// Setup networking units
if flags.convertNetconf != "" {
if err := setupNetworkUnits(metadata.NetworkConfig, env, flags.convertNetconf); err != nil {
log.Printf("Failed to setup network units: %v\n", err)
os.Exit(1)
}
}

log.Printf("Fetching user-data from datasource of type %q\n", ds.Type())
userdataBytes, err := ds.FetchUserdata()
if err != nil {
Expand Down Expand Up @@ -216,15 +232,7 @@ func main() {
}
}

log.Printf("Fetching meta-data from datasource of type %q\n", ds.Type())
metadata, err := ds.FetchMetadata()
if err != nil {
log.Printf("Failed fetching meta-data from datasource: %v\n", err)
os.Exit(1)
}

// Apply environment to user-data
env := initialize.NewEnvironment("/", ds.ConfigRoot(), flags.workspace, flags.sshKeyName, metadata)
userdata := env.Apply(string(userdataBytes))

var ccu *config.CloudConfig
Expand All @@ -248,26 +256,7 @@ func main() {
log.Println("Merging cloud-config from meta-data and user-data")
cc := mergeConfigs(ccu, metadata)

var ifaces []network.InterfaceGenerator
if flags.convertNetconf != "" {
var err error
switch flags.convertNetconf {
case "debian":
ifaces, err = network.ProcessDebianNetconf(metadata.NetworkConfig.([]byte))
case "packet":
ifaces, err = network.ProcessPacketNetconf(metadata.NetworkConfig.(packet.NetworkData))
case "vmware":
ifaces, err = network.ProcessVMwareNetconf(metadata.NetworkConfig.(map[string]string))
default:
err = fmt.Errorf("Unsupported network config format %q", flags.convertNetconf)
}
if err != nil {
log.Printf("Failed to generate interfaces: %v\n", err)
os.Exit(1)
}
}

if err = initialize.Apply(cc, ifaces, env); err != nil {
if err = initialize.Apply(cc, env); err != nil {
log.Printf("Failed to apply cloud-config: %v\n", err)
os.Exit(1)
}
Expand All @@ -284,6 +273,29 @@ func main() {
}
}

func setupNetworkUnits(netConfig interface{}, env *initialize.Environment, netconf string) error {
var ifaces []network.InterfaceGenerator
var err error
switch netconf {
case "debian":
ifaces, err = network.ProcessDebianNetconf(netConfig.([]byte))
case "packet":
ifaces, err = network.ProcessPacketNetconf(netConfig.(packet.NetworkData))
case "vmware":
ifaces, err = network.ProcessVMwareNetconf(netConfig.(map[string]string))
default:
err = fmt.Errorf("Unsupported network config format %q", netconf)
}
if err != nil {
return fmt.Errorf("error generating interfaces: %w", err)
}

if err := initialize.ApplyNetworkConfig(ifaces, env); err != nil {
return fmt.Errorf("error applying network config: %w", err)
}
return nil
}

// mergeConfigs merges certain options from md (meta-data from the datasource)
// onto cc (a CloudConfig derived from user-data), if they are not already set
// on cc (i.e. user-data always takes precedence)
Expand Down
31 changes: 21 additions & 10 deletions initialize/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package initialize

import (
"errors"
"fmt"
"log"
"path"
Expand All @@ -39,10 +38,29 @@ type CloudConfigUnit interface {
Units() []system.Unit
}

// ApplyNetworkConfig creates networkd units for the given interfaces, reloads systemd
// and restarts networkd.
func ApplyNetworkConfig(ifaces []network.InterfaceGenerator, env *Environment) error {
if len(ifaces) == 0 {
return nil
}
units := createNetworkingUnits(ifaces)

um := system.NewUnitManager(env.Root())
if err := processUnits(units, env.Root(), um); err != nil {
return fmt.Errorf("error processing network units: %w", err)
}

if err := system.RestartNetwork(ifaces); err != nil {
return fmt.Errorf("error restarting networkd: %w", err)
}
return nil
}

// Apply renders a CloudConfig to an Environment. This can involve things like
// configuring the hostname, adding new users, writing various configuration
// files to disk, and manipulating systemd services.
func Apply(cfg config.CloudConfig, ifaces []network.InterfaceGenerator, env *Environment) error {
func Apply(cfg config.CloudConfig, env *Environment) error {
if cfg.Hostname != "" {
if err := system.SetHostname(cfg.Hostname); err != nil {
return err
Expand Down Expand Up @@ -166,13 +184,6 @@ func Apply(cfg config.CloudConfig, ifaces []network.InterfaceGenerator, env *Env
}
}

if len(ifaces) > 0 {
units = append(units, createNetworkingUnits(ifaces)...)
if err := system.RestartNetwork(ifaces); err != nil {
return err
}
}

um := system.NewUnitManager(env.Root())
return processUnits(units, env.Root(), um)
}
Expand Down Expand Up @@ -267,7 +278,7 @@ func processUnits(units []system.Unit, root string, um system.UnitManager) error

if reload {
if err := um.DaemonReload(); err != nil {
return errors.New(fmt.Sprintf("failed systemd daemon-reload: %s", err))
return fmt.Errorf("failed systemd daemon-reload: %w", err)
}
}

Expand Down