-
Notifications
You must be signed in to change notification settings - Fork 33
fix(remediation): use ctrl-runtime business logic #544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,8 +30,6 @@ global: | |
| maxAgeDays: 7 | ||
| compress: true | ||
|
|
||
| ctrlRuntimeEnabled: true | ||
|
|
||
| nodeSelector: {} | ||
|
|
||
| tolerations: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,6 @@ | |
|
|
||
| global: | ||
| dryRun: false | ||
| ctrlRuntimeEnabled: false | ||
| image: | ||
| tag: "main" | ||
| initContainerImage: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,26 +16,22 @@ package main | |
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "flag" | ||
| "fmt" | ||
| "log/slog" | ||
| "net/http" | ||
| "os" | ||
| "os/signal" | ||
| "strconv" | ||
| "strings" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| "golang.org/x/sync/errgroup" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/healthz" | ||
| metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server" | ||
|
|
||
| "github.com/nvidia/nvsentinel/commons/pkg/auditlogger" | ||
| "github.com/nvidia/nvsentinel/commons/pkg/logger" | ||
| "github.com/nvidia/nvsentinel/commons/pkg/server" | ||
| "github.com/nvidia/nvsentinel/fault-remediation/pkg/initializer" | ||
| ) | ||
|
|
||
|
|
@@ -49,7 +45,6 @@ var ( | |
|
|
||
| // These variables are populated by parsing flags | ||
| enableLeaderElection bool | ||
| enableControllerRuntime bool | ||
| leaderElectionLeaseDuration time.Duration | ||
| leaderElectionRenewDeadline time.Duration | ||
| leaderElectionRetryPeriod time.Duration | ||
|
|
@@ -88,96 +83,27 @@ func main() { | |
| func run() error { | ||
| parseFlags() | ||
|
|
||
| if !enableControllerRuntime && enableLeaderElection { | ||
| return errors.New("leader-election requires controller-runtime") | ||
| } | ||
|
|
||
| ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) | ||
| defer stop() | ||
|
|
||
| params := initializer.InitializationParams{ | ||
| KubeconfigPath: kubeconfigPath, | ||
| TomlConfigPath: tomlConfigPath, | ||
| DryRun: dryRun, | ||
| EnableLogCollector: enableLogCollector, | ||
| } | ||
|
|
||
| components, err := initializer.InitializeAll(ctx, params) | ||
| err := setupCtrlRuntimeManagement(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("initialization failed: %w", err) | ||
| } | ||
|
|
||
| reconciler := components.FaultRemediationReconciler | ||
|
|
||
| defer func() { | ||
| if err := reconciler.CloseAll(ctx); err != nil { | ||
| slog.Error("failed to close datastore components", "error", err) | ||
| } | ||
| }() | ||
|
|
||
| if enableControllerRuntime { | ||
| err = setupCtrlRuntimeManagement(ctx, components) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } else { | ||
| err = setupNonCtrlRuntimeManaged(ctx, components) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func setupNonCtrlRuntimeManaged(ctx context.Context, components *initializer.Components) error { | ||
| slog.Info("Running without controller runtime management") | ||
|
|
||
| metricsAddr = strings.TrimPrefix(metricsAddr, ":") | ||
|
|
||
| portInt, err := strconv.Atoi(metricsAddr) | ||
| if err != nil { | ||
| return fmt.Errorf("invalid metrics port: %w", err) | ||
| } | ||
|
|
||
| srv := server.NewServer( | ||
| server.WithPort(portInt), | ||
| server.WithPrometheusMetricsCtrlRuntime(), | ||
| server.WithSimpleHealth(), | ||
| ) | ||
|
|
||
| g, gCtx := errgroup.WithContext(ctx) | ||
|
|
||
| g.Go(func() error { | ||
| slog.Info("Starting metrics server", "port", portInt) | ||
|
|
||
| if err := srv.Serve(gCtx); err != nil { | ||
| slog.Error("Metrics server failed - continuing without metrics", "error", err) | ||
| } | ||
|
|
||
| return nil | ||
| }) | ||
|
|
||
| g.Go(func() error { | ||
| components.FaultRemediationReconciler.StartWatcherStream(gCtx) | ||
|
|
||
| slog.Info("Listening for events on the channel...") | ||
|
|
||
| for event := range components.FaultRemediationReconciler.Watcher.Events() { | ||
| slog.Info("Event received", "event", event) | ||
| _, _ = components.FaultRemediationReconciler.Reconcile(gCtx, &event) | ||
| } | ||
| func setupCtrlRuntimeManagement(ctx context.Context) error { | ||
| slog.Info("Running in controller runtime managed mode") | ||
|
|
||
| return nil | ||
| cfg := ctrl.GetConfigOrDie() | ||
| cfg.Wrap(func(rt http.RoundTripper) http.RoundTripper { | ||
| return auditlogger.NewAuditingRoundTripper(rt) | ||
| }) | ||
|
|
||
| return g.Wait() | ||
| } | ||
|
|
||
| func setupCtrlRuntimeManagement(ctx context.Context, components *initializer.Components) error { | ||
| slog.Info("Running in controller runtime managed mode") | ||
|
|
||
| mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ | ||
| //TODO: setup informers for node and job | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π οΈ Refactor suggestion | π Major TODO comment should reference an issue. Per coding guidelines, TODO comments should reference a GitHub issue for tracking. Based on coding guidelines. π€ Prompt for AI Agents |
||
| mgr, err := ctrl.NewManager(cfg, ctrl.Options{ | ||
| Scheme: scheme, | ||
| Metrics: metricsserver.Options{ | ||
| BindAddress: metricsAddr, | ||
|
|
@@ -205,6 +131,26 @@ func setupCtrlRuntimeManagement(ctx context.Context, components *initializer.Com | |
| return err | ||
| } | ||
|
|
||
| params := initializer.InitializationParams{ | ||
| TomlConfigPath: tomlConfigPath, | ||
| DryRun: dryRun, | ||
| EnableLogCollector: enableLogCollector, | ||
| Config: mgr.GetConfig(), | ||
| } | ||
|
|
||
| components, err := initializer.InitializeAll(ctx, params, mgr.GetClient()) | ||
| if err != nil { | ||
| return fmt.Errorf("initialization failed: %w", err) | ||
| } | ||
|
|
||
| reconciler := components.FaultRemediationReconciler | ||
|
|
||
| defer func() { | ||
| if err := reconciler.CloseAll(ctx); err != nil { | ||
| slog.Error("failed to close datastore components", "error", err) | ||
| } | ||
| }() | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| err = components.FaultRemediationReconciler.SetupWithManager(ctx, mgr) | ||
| if err != nil { | ||
| return fmt.Errorf("SetupWithManager failed: %w", err) | ||
|
|
@@ -235,20 +181,13 @@ func parseFlags() { | |
| " (otherwise metrics and health are on same port).", | ||
| ) | ||
|
|
||
| flag.StringVar(&kubeconfigPath, "kubeconfig-path", "", "path to kubeconfig file") | ||
| flag.StringVar(&kubeconfigPath, "kubeconfig", "", "path to kubeconfig file") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π§© Analysis chainπ Script executed: #!/bin/bash
# Check all references to kubeconfigPath variable
rg -nP '\bkubeconfigPath\b' --type goRepository: NVIDIA/NVSentinel Length of output: 3067 π Script executed: cat -n fault-remediation/main.go | sed -n '50,120p'Repository: NVIDIA/NVSentinel Length of output: 2658 Remove unused kubeconfig flag or wire it to config loading. The π€ Prompt for AI Agents |
||
|
|
||
| flag.StringVar(&tomlConfigPath, "config-path", "/etc/config/config.toml", | ||
| "path where the fault remediation config file is present") | ||
|
|
||
| flag.BoolVar(&dryRun, "dry-run", false, "flag to run fault remediation module in dry-run mode.") | ||
|
|
||
| flag.BoolVar( | ||
| &enableControllerRuntime, | ||
| "controller-runtime", | ||
| false, | ||
| "Enable controller runtime management of the reconciler.", | ||
| ) | ||
|
|
||
| flag.BoolVar( | ||
| &enableLeaderElection, | ||
| "leader-elect", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.