From bb30cf14cf52738d6a83cff880c4d65c71126a98 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Tue, 1 Oct 2024 19:04:35 +0400 Subject: [PATCH] chore: test code to verify fd leaks See #9413 Not sure how to turn this into a useful test. Signed-off-by: Andrey Smirnov --- Dockerfile | 1 + hack/fdspy/go.mod | 3 + hack/fdspy/main.go | 28 +++++++++ .../machined/pkg/controllers/runtime/fdspy.go | 57 +++++++++++++++++++ .../runtime/v1alpha2/v1alpha2_controller.go | 1 + 5 files changed, 90 insertions(+) create mode 100644 hack/fdspy/go.mod create mode 100644 hack/fdspy/main.go create mode 100644 internal/app/machined/pkg/controllers/runtime/fdspy.go diff --git a/Dockerfile b/Dockerfile index d40ac9a78e..0be922be02 100644 --- a/Dockerfile +++ b/Dockerfile @@ -659,6 +659,7 @@ COPY --chmod=0644 hack/cri-containerd.toml /rootfs/etc/cri/containerd.toml COPY --chmod=0644 hack/cri-plugin.part /rootfs/etc/cri/conf.d/00-base.part COPY --chmod=0644 hack/udevd/80-net-name-slot.rules /rootfs/usr/lib/udev/rules.d/ COPY --chmod=0644 hack/lvm.conf /rootfs/etc/lvm/lvm.conf +COPY --chmod=0755 hack/fdspy/fdspy /rootfs/usr/bin/fdspy RUN < ", err) + } else { + fmt.Fprintln(os.Stderr, fd.Name(), " --> ", fname) + } + } + + os.Exit(1) +} diff --git a/internal/app/machined/pkg/controllers/runtime/fdspy.go b/internal/app/machined/pkg/controllers/runtime/fdspy.go new file mode 100644 index 0000000000..084f51e51c --- /dev/null +++ b/internal/app/machined/pkg/controllers/runtime/fdspy.go @@ -0,0 +1,57 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +package runtime + +import ( + "context" + "fmt" + "time" + + "github.com/cosi-project/runtime/pkg/controller" + "github.com/siderolabs/go-cmd/pkg/cmd" + "go.uber.org/zap" +) + +// FDSpyController activates LVM volumes when they are discovered by the block.DiscoveryController. +type FDSpyController struct { +} + +// Name implements controller.Controller interface. +func (ctrl *FDSpyController) Name() string { + return "runtime.FDSpyController" +} + +// Inputs implements controller.Controller interface. +func (ctrl *FDSpyController) Inputs() []controller.Input { + return nil +} + +// Outputs implements controller.Controller interface. +func (ctrl *FDSpyController) Outputs() []controller.Output { + return nil +} + +// Run implements controller.Controller interface. +// +//nolint:gocyclo +func (ctrl *FDSpyController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error { + ticker := time.NewTicker(5 * time.Second) + defer ticker.Stop() + + for { + select { + case <-ctx.Done(): + return nil + case <-r.EventCh(): + case <-ticker.C: + } + + if _, err := cmd.RunContext(ctx, + "/usr/bin/fdspy", + ); err != nil { + return fmt.Errorf("failed to run fdspy: %w", err) + } + } +} diff --git a/internal/app/machined/pkg/runtime/v1alpha2/v1alpha2_controller.go b/internal/app/machined/pkg/runtime/v1alpha2/v1alpha2_controller.go index 93ea26117a..8939406e7b 100644 --- a/internal/app/machined/pkg/runtime/v1alpha2/v1alpha2_controller.go +++ b/internal/app/machined/pkg/runtime/v1alpha2/v1alpha2_controller.go @@ -298,6 +298,7 @@ func (ctrl *Controller) Run(ctx context.Context, drainer *runtime.Drainer) error ConfigPath: constants.ExtensionServiceConfigPath, }, &runtimecontrollers.ExtensionStatusController{}, + &runtimecontrollers.FDSpyController{}, &runtimecontrollers.KernelModuleConfigController{}, &runtimecontrollers.KernelModuleSpecController{ V1Alpha1Mode: ctrl.v1alpha1Runtime.State().Platform().Mode(),