Skip to content

Commit

Permalink
neonvm-controller/ipam: implement cleanup action
Browse files Browse the repository at this point in the history
We need it to cleanup stale allocations.

Signed-off-by: Oleg Vasilev <[email protected]>
  • Loading branch information
Omrigan committed Feb 25, 2025
1 parent d43e86d commit 88bb6b9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
18 changes: 12 additions & 6 deletions neonvm-controller/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"github.com/neondatabase/autoscaling/pkg/neonvm/controllers"
"github.com/neondatabase/autoscaling/pkg/neonvm/ipam"
"github.com/neondatabase/autoscaling/pkg/util"
"github.com/neondatabase/autoscaling/pkg/util/taskgroup"
)

var (
Expand Down Expand Up @@ -154,7 +155,8 @@ func main() {
logConfig.Sampling = nil // Disabling sampling; it's enabled by default for zap's production configs.
logConfig.Level.SetLevel(zap.InfoLevel)
logConfig.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
logger := zapr.NewLogger(zap.Must(logConfig.Build(zap.AddStacktrace(zapcore.PanicLevel))))
zapLogger := zap.Must(logConfig.Build(zap.AddStacktrace(zapcore.PanicLevel)))
logger := zapr.NewLogger(zapLogger)

ctrl.SetLogger(logger)
// define klog settings (used in LeaderElector)
Expand Down Expand Up @@ -272,12 +274,16 @@ func main() {
setupLog.Error(err, "unable to set up failing refresher")
panic(err)
}
tg := taskgroup.NewGroup(zapLogger)
tg.Go("run manager", func(logger *zap.Logger) error {
return run(mgr)
})
tg.Go("run ipam cleanup", func(logger *zap.Logger) error {
return ipam.RunCleanup(context.Background(), "default")
})

// NOTE: THE CONTROLLER MUST IMMEDIATELY EXIT AFTER RUNNING THE MANAGER.
if err := run(mgr); err != nil {
setupLog.Error(err, "run manager error")
panic(err)
}
err = tg.Wait()
panic(err)
}

func debugServerFunc(reconcilers ...controllers.ReconcilerWithMetrics) manager.RunnableFunc {
Expand Down
21 changes: 21 additions & 0 deletions pkg/neonvm/ipam/allocate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ func getReleaseAction(vmName types.NamespacedName) ipamAction {
}
}

func getCleanupAction(vms map[string]bool) ipamAction {
return func(ipRange RangeConfiguration, reservation []whereaboutstypes.IPReservation) (net.IPNet, []whereaboutstypes.IPReservation, error) {
return doCleanup(ipRange, reservation, vms)
}
}

func doAcquire(
ipRange RangeConfiguration,
reservation []whereaboutstypes.IPReservation,
Expand Down Expand Up @@ -76,6 +82,21 @@ func doRelease(
return net.IPNet{IP: ip, Mask: ipnet.Mask}, newReservation, nil
}

func doCleanup(
_ RangeConfiguration,
reservations []whereaboutstypes.IPReservation,
vms map[string]bool,
) (net.IPNet, []whereaboutstypes.IPReservation, error) {
var result []whereaboutstypes.IPReservation
for _, r := range reservations {
if vms[r.ContainerID] {
result = append(result, r)
}
}
var ip net.IPNet
return ip, result, nil
}

func getMatchingIPReservationIndex(reservation []whereaboutstypes.IPReservation, id string) int {
foundidx := -1
for idx, v := range reservation {
Expand Down
25 changes: 25 additions & 0 deletions pkg/neonvm/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const (
DefaultLeaderLeaseDurationMs = 3000
DefaultLeaderRenewDeadlineMs = 2500
DefaultLeaderRetryPeriodMs = 2000

CleanupInterval = 15 * time.Minute
)

type Temporary interface {
Expand Down Expand Up @@ -71,6 +73,29 @@ func (i *IPAM) ReleaseIP(ctx context.Context, vmName types.NamespacedName) (net.
return ip, nil
}

func (i *IPAM) RunCleanup(ctx context.Context, namespace string) error {
for {
vms, err := i.vmClient.NeonvmV1().VirtualMachines(namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("error listing virtual machines: %w", err)
}
vmsMap := make(map[string]bool)
for _, vm := range vms.Items {
vmsMap[vm.Name] = true
}
_, err = i.runIPAM(ctx, getCleanupAction(vmsMap))
if err != nil {
return fmt.Errorf("error cleaning up IPAM: %w", err)
}

select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(CleanupInterval):
}
}
}

// New returns a new IPAM object with ipam config and k8s/crd clients
func New(nadName string, nadNamespace string) (*IPAM, error) {
// get Kubernetes client config
Expand Down

0 comments on commit 88bb6b9

Please sign in to comment.