Skip to content
Merged
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
1 change: 1 addition & 0 deletions cmd/adopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func runAdopt(cmd *cobra.Command, _ []string) error {
fmt.Printf(" gateway: %s\n", gateway)
fmt.Printf(" primary-nic: %s\n", primaryNIC)
fmt.Printf(" secondary-nics: %s\n", strings.Join(secondaryNICs, ","))
fmt.Printf(" enis: %s\n", cmp.Or(strings.Join(eniIDs, ","), "none"))
if len(ips) > 0 {
fmt.Printf(" pool-size: %d (first=%s, last=%s)\n", len(ips), ips[0], ips[len(ips)-1])
} else {
Expand Down
7 changes: 5 additions & 2 deletions cmd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net"
"net/http"
"os"
"path/filepath"
"time"

"github.com/projecteru2/core/log"
Expand All @@ -24,7 +25,6 @@ import (
)

const (
defaultLeaseFile = "/var/lib/cocoon/net/leases.json"
defaultControlSocket = "/run/cocoon-net/control.sock"
defaultMetricsAddr = ":9092"

Expand Down Expand Up @@ -52,7 +52,7 @@ they expire.`,
RunE: runDaemon,
}
cmd.Flags().StringVar(&flagStateDir, "state-dir", defaultStateDir, "directory containing pool.json")
cmd.Flags().StringVar(&flagLeaseFile, "lease-file", defaultLeaseFile, "path to lease persistence file")
cmd.Flags().StringVar(&flagLeaseFile, "lease-file", defaultLeaseFile, "lease persistence file")
cmd.Flags().StringVar(&flagControlSocket, "control-socket", cmp.Or(os.Getenv("COCOON_NET_CONTROL_SOCKET"), defaultControlSocket), "root-only Unix socket for local lease lifecycle operations (empty to disable)")
cmd.Flags().BoolVar(&flagSkipIPTables, "skip-iptables", false, "skip iptables setup (for pre-configured nodes)")
cmd.Flags().StringVar(&flagMetricsAddr, "metrics-addr", cmp.Or(os.Getenv("COCOON_NET_METRICS_ADDR"), defaultMetricsAddr), "prometheus metrics listen address (empty to disable)")
Expand Down Expand Up @@ -104,6 +104,9 @@ func runDaemon(cmd *cobra.Command, _ []string) error {
}
dnsIPs := parseIPs(dnsList)

if err := os.MkdirAll(filepath.Dir(flagLeaseFile), 0o750); err != nil {
return fmt.Errorf("create lease dir: %w", err)
}
srv := dhcp.New(dhcp.Config{
Interface: node.BridgeName,
Gateway: gateway,
Expand Down
5 changes: 1 addition & 4 deletions cmd/teardown.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io/fs"
"os"
"path/filepath"

"github.com/projecteru2/core/log"
"github.com/spf13/cobra"
Expand All @@ -14,8 +13,6 @@ import (
"github.com/cocoonstack/cocoon-net/platform"
)

const leaseFileName = "leases.json"

func newTeardownCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "teardown",
Expand Down Expand Up @@ -67,7 +64,7 @@ func runTeardown(cmd *cobra.Command, _ []string) error {
logger.Warnf(ctx, "delete pool state: %v", err)
}

leasePath := filepath.Join(flagStateDir, leaseFileName)
leasePath := defaultLeaseFile
if err := os.Remove(leasePath); err != nil && !errors.Is(err, fs.ErrNotExist) {
logger.Warnf(ctx, "delete lease file %s: %v", leasePath, err)
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import (
"github.com/cocoonstack/cocoon-net/pool"
)

const defaultStateDir = "/var/lib/cocoon/net"
const (
defaultStateDir = "/var/lib/cocoon/net"
leaseFileName = "leases.json"
defaultLeaseFile = defaultStateDir + "/" + leaseFileName
)

var (
flagPlatform string
Expand All @@ -38,7 +42,7 @@ func registerCommonFlags(cmd *cobra.Command, defaultPoolSize int) {
cmd.Flags().StringVar(&flagSubnet, "subnet", "", "VM subnet CIDR, e.g. 172.20.100.0/24 (required)")
cmd.Flags().IntVar(&flagPoolSize, "pool-size", defaultPoolSize, "number of IPs in the pool")
cmd.Flags().StringVar(&flagGateway, "gateway", "", "gateway IP on cni0 (default: first IP in subnet)")
cmd.Flags().StringVar(&flagPrimaryNIC, "primary-nic", "", "host primary NIC (auto-detect if empty)")
cmd.Flags().StringVar(&flagPrimaryNIC, "primary-nic", "", "host primary NIC (default: eth0 on volcengine, ens4 otherwise)")
cmd.Flags().StringVar(&flagDNS, "dns", "8.8.8.8,1.1.1.1", "comma-separated DNS servers for DHCP clients")
cmd.Flags().StringVar(&flagStateDir, "state-dir", defaultStateDir, "state directory")
cmd.Flags().BoolVar(&flagDryRun, "dry-run", false, "show what would be done without making changes")
Expand Down
7 changes: 4 additions & 3 deletions docs/dhcp.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ VPC-routable IP directly from this server.
`204` after a successful or already-completed release, `400` for an invalid
MAC, and `500` when persistence fails; callers may safely retry a `500`.
- Leases are persisted to the `--lease-file` (default
`/var/lib/cocoon/net/leases.json`) on every allocation/release, and
`/var/lib/cocoon/net/leases.json`, independent of `--state-dir` so
vk-cocoon's default reader path matches) on every allocation/release, and
reloaded at daemon startup so a restart doesn't strand or double-assign
leases.

Expand Down Expand Up @@ -125,8 +126,8 @@ The DHCP server binds UDP port 67 on all addresses, not just `cni0`; a host
`dnsmasq` or `dhcpd` already holding that port makes the daemon fail to start.

On `cocoon-net teardown`, the cloud resources (ENIs on Volcengine, the alias
range on GKE), the tagged `cocoon-net-drop` iptables rules, `pool.json`, and
`<state-dir>/leases.json` are all removed (on GKE the boot cron job that
range on GKE), the tagged `cocoon-net-drop` iptables rules, `pool.json`, and the
default lease file `/var/lib/cocoon/net/leases.json` are all removed (on GKE the boot cron job that
reapplies the guest-agent route fix is removed too); a daemon started with a
custom `--lease-file` keeps that file. The `cni0` bridge, the FORWARD ACCEPT /
NAT MASQUERADE rules, and the CNI conflist are left in place.
1 change: 0 additions & 1 deletion platform/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func TestSubnetIPs_Slash24SkipsBroadcast(t *testing.T) {
func TestSubnetIPs_Slash28(t *testing.T) {
t.Parallel()

// /28 has 16 addresses, 14 hosts, minus gateway = 13.
got, err := SubnetIPs("192.168.10.0/28", "192.168.10.1", 32)
if err != nil {
t.Fatalf("SubnetIPs: %v", err)
Expand Down
1 change: 1 addition & 0 deletions platform/volcengine/adopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import (
"github.com/cocoonstack/cocoon-net/platform"
)

// Adopt is a no-op: the VPC routes the VM subnet to the secondary ENIs, so no host route hijacks it.
func (v *Volcengine) Adopt(context.Context, *platform.Config) error { return nil }