Skip to content

Commit bab18a7

Browse files
committed
rewrite by golib
1 parent 10eb672 commit bab18a7

2 files changed

Lines changed: 13 additions & 23 deletions

File tree

node/bridge.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package node
33
import (
44
"context"
55
"fmt"
6+
"net"
67
"os/exec"
78
"strings"
89

@@ -11,31 +12,29 @@ import (
1112

1213
const cni0Bridge = "cni0"
1314

14-
// setupBridge creates the cni0 bridge and assigns the gateway IP.
1515
func setupBridge(ctx context.Context, gatewayIP, subnetCIDR string) error {
1616
logger := log.WithFunc("node.setupBridge")
1717

18-
// Create bridge if it does not exist.
1918
addCmd := exec.CommandContext(ctx, "ip", "link", "add", cni0Bridge, "type", "bridge")
2019
out, err := addCmd.CombinedOutput()
2120
if err != nil && !strings.Contains(string(out), "File exists") {
2221
return fmt.Errorf("create bridge %s: %w: %s", cni0Bridge, err, out)
2322
}
2423

25-
// Assign gateway address (idempotent via replace).
26-
_, mask, err := parseCIDR(subnetCIDR)
24+
_, ipNet, err := net.ParseCIDR(subnetCIDR)
2725
if err != nil {
2826
return fmt.Errorf("parse subnet cidr: %w", err)
2927
}
30-
cidr := fmt.Sprintf("%s/%s", gatewayIP, mask)
28+
ones, _ := ipNet.Mask.Size()
29+
cidr := fmt.Sprintf("%s/%d", gatewayIP, ones)
30+
3131
//nolint:gosec // ip args from trusted config
3232
addrCmd := exec.CommandContext(ctx, "ip", "addr", "replace", cidr, "dev", cni0Bridge)
3333
out, err = addrCmd.CombinedOutput()
3434
if err != nil {
3535
return fmt.Errorf("assign %s to %s: %w: %s", cidr, cni0Bridge, err, out)
3636
}
3737

38-
// Bring up the bridge.
3938
upCmd := exec.CommandContext(ctx, "ip", "link", "set", cni0Bridge, "up")
4039
out, err = upCmd.CombinedOutput()
4140
if err != nil {
@@ -45,12 +44,3 @@ func setupBridge(ctx context.Context, gatewayIP, subnetCIDR string) error {
4544
logger.Infof(ctx, "bridge %s configured with gateway %s", cni0Bridge, cidr)
4645
return nil
4746
}
48-
49-
// parseCIDR extracts the mask prefix from a CIDR string (e.g. "24" from "172.20.100.0/24").
50-
func parseCIDR(cidr string) (network, mask string, err error) {
51-
parts := strings.SplitN(cidr, "/", 2)
52-
if len(parts) != 2 {
53-
return "", "", fmt.Errorf("invalid cidr: %s", cidr)
54-
}
55-
return parts[0], parts[1], nil
56-
}

node/sysctl.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package node
33
import (
44
"context"
55
"fmt"
6-
"os/exec"
6+
"os"
7+
"path/filepath"
8+
"strings"
79

810
"github.com/projecteru2/core/log"
911
)
1012

11-
// setupSysctl applies kernel parameters required for VPC-native VM networking.
13+
const procSysBase = "/proc/sys"
14+
1215
func setupSysctl(ctx context.Context, primaryNIC string, secondaryNICs []string) error {
1316
logger := log.WithFunc("node.setupSysctl")
1417

@@ -26,12 +29,9 @@ func setupSysctl(ctx context.Context, primaryNIC string, secondaryNICs []string)
2629
}
2730

2831
for key, val := range settings {
29-
param := fmt.Sprintf("%s=%s", key, val)
30-
//nolint:gosec // sysctl args from trusted config
31-
cmd := exec.CommandContext(ctx, "sysctl", "-w", param)
32-
out, err := cmd.CombinedOutput()
33-
if err != nil {
34-
return fmt.Errorf("sysctl %s: %w: %s", param, err, out)
32+
path := filepath.Join(procSysBase, strings.ReplaceAll(key, ".", "/"))
33+
if err := os.WriteFile(path, []byte(val), 0o644); err != nil { //nolint:gosec // sysctl tuning
34+
return fmt.Errorf("write sysctl %s=%s: %w", key, val, err)
3535
}
3636
}
3737
logger.Info(ctx, "sysctl parameters applied")

0 commit comments

Comments
 (0)