Matches ctl version to upstream
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
)
|
||||
|
||||
type NetworkConfig struct {
|
||||
Hostname string
|
||||
MgmtIface string
|
||||
MgmtAddress string
|
||||
MgmtGateway string
|
||||
@@ -48,11 +49,11 @@ func ConfigureMgmtInterface(cfg NetworkConfig) Step {
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := nctx.System.Run(ctx, "ip", "link", "show", "dev", cfg.MgmtIface); err != nil {
|
||||
if _, err := nctx.SystemRunner.Run(ctx, "ip", "link", "show", "dev", cfg.MgmtIface); err != nil {
|
||||
return fmt.Errorf("interface not found: %s: %w", cfg.MgmtIface, err)
|
||||
}
|
||||
|
||||
if _, err := nctx.System.Run(ctx, "ip", "link", "set", "dev", cfg.MgmtIface, "up"); err != nil {
|
||||
if _, err := nctx.SystemRunner.Run(ctx, "ip", "link", "set", "dev", cfg.MgmtIface, "up"); err != nil {
|
||||
return fmt.Errorf("failed to bring up interface %s: %w", cfg.MgmtIface, err)
|
||||
}
|
||||
|
||||
@@ -64,13 +65,13 @@ func ConfigureMgmtInterface(cfg NetworkConfig) Step {
|
||||
if hasAddr {
|
||||
klog.Infof("address already present on %s: %s", cfg.MgmtIface, wantCIDR)
|
||||
} else {
|
||||
if _, err := nctx.System.Run(ctx, "ip", "addr", "add", wantCIDR, "dev", cfg.MgmtIface); err != nil {
|
||||
if _, err := nctx.SystemRunner.Run(ctx, "ip", "addr", "add", wantCIDR, "dev", cfg.MgmtIface); err != nil {
|
||||
return fmt.Errorf("failed assigning %s to %s: %w", wantCIDR, cfg.MgmtIface, err)
|
||||
}
|
||||
}
|
||||
|
||||
if gw := strings.TrimSpace(cfg.MgmtGateway); gw != "" {
|
||||
if _, err := nctx.System.Run(ctx, "ip", "route", "replace", "default", "via", gw, "dev", cfg.MgmtIface); err != nil {
|
||||
if _, err := nctx.SystemRunner.Run(ctx, "ip", "route", "replace", "default", "via", gw, "dev", cfg.MgmtIface); err != nil {
|
||||
return fmt.Errorf("failed setting default route via %s dev %s: %w", gw, cfg.MgmtIface, err)
|
||||
}
|
||||
}
|
||||
@@ -85,7 +86,44 @@ func maskSize(m net.IPMask) int {
|
||||
}
|
||||
|
||||
func EnsureIPForward(ctx context.Context, n *NodeContext) error {
|
||||
return system.EnsureSysctl(ctx, n.System, "net.ipv4.ip_forward", "1")
|
||||
return system.EnsureSysctl(ctx, n.SystemRunner, "net.ipv4.ip_forward", "1")
|
||||
}
|
||||
|
||||
func ConfigureHostname(cfg NetworkConfig) Step {
|
||||
return func(context.Context, *NodeContext) error {
|
||||
want := strings.TrimSpace(cfg.Hostname)
|
||||
if want == "" {
|
||||
return fmt.Errorf("hostname is required")
|
||||
}
|
||||
|
||||
current, err := os.Hostname()
|
||||
if err != nil {
|
||||
current = ""
|
||||
}
|
||||
|
||||
if current == want {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := system.SetHostname(want); err != nil {
|
||||
return fmt.Errorf("set hostname to %q: %w", want, err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile("/etc/hostname", []byte(want+"\n"), 0o644); err != nil {
|
||||
return fmt.Errorf("write /etc/hostname: %w", err)
|
||||
}
|
||||
|
||||
current, err = os.Hostname()
|
||||
if err != nil {
|
||||
current = ""
|
||||
}
|
||||
|
||||
if current != want {
|
||||
return fmt.Errorf("Unable to set hostname: %q", want)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func ConfigureDNS(cfg NetworkConfig) Step {
|
||||
@@ -159,7 +197,7 @@ func ConfigureDNS(cfg NetworkConfig) Step {
|
||||
}
|
||||
|
||||
func interfaceHasIPv4(ctx context.Context, nctx *NodeContext, iface, wantIP string) (bool, error) {
|
||||
res, err := nctx.System.Run(ctx, "ip", "-o", "-4", "addr", "show", "dev", iface)
|
||||
res, err := nctx.SystemRunner.Run(ctx, "ip", "-o", "-4", "addr", "show", "dev", iface)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user