Skip to content

Commit 3669f34

Browse files
committed
use AppRunner in main
1 parent b8576a8 commit 3669f34

File tree

3 files changed

+120
-131
lines changed

3 files changed

+120
-131
lines changed

cmd/compute-domain-controller/main.go

Lines changed: 3 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -17,93 +17,11 @@
1717
package main
1818

1919
import (
20-
"context"
21-
"flag"
22-
"fmt"
23-
"os"
24-
25-
"github.com/spf13/pflag"
26-
"go.uber.org/zap/zapcore"
27-
28-
resourceapi "k8s.io/api/resource/v1"
29-
"k8s.io/apimachinery/pkg/runtime"
30-
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
31-
ctrl "sigs.k8s.io/controller-runtime"
32-
"sigs.k8s.io/controller-runtime/pkg/healthz"
33-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
34-
20+
"github.com/run-ai/fake-gpu-operator/internal/common/app"
3521
controller "github.com/run-ai/fake-gpu-operator/internal/compute-domain-controller"
36-
37-
computedomainv1beta1 "github.com/NVIDIA/k8s-dra-driver-gpu/api/nvidia.com/resource/v1beta1"
38-
)
39-
40-
var (
41-
scheme = runtime.NewScheme()
42-
setupLog = ctrl.Log.WithName("setup")
4322
)
4423

45-
func init() {
46-
_ = clientgoscheme.AddToScheme(scheme)
47-
_ = resourceapi.AddToScheme(scheme)
48-
_ = computedomainv1beta1.AddToScheme(scheme)
49-
}
50-
5124
func main() {
52-
opts := NewOptions()
53-
54-
fs := pflag.NewFlagSet("fake-compute-domain-controller", pflag.ExitOnError)
55-
opts.AddFlags(fs)
56-
57-
zapOpts := zap.Options{
58-
Development: true,
59-
TimeEncoder: zapcore.ISO8601TimeEncoder,
60-
}
61-
zapOpts.BindFlags(flag.CommandLine)
62-
63-
fs.AddGoFlagSet(flag.CommandLine)
64-
65-
if err := fs.Parse(os.Args[1:]); err != nil {
66-
fmt.Fprintf(os.Stderr, "failed to parse flags: %v\n", err)
67-
os.Exit(1)
68-
}
69-
70-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zapOpts)))
71-
ctx := ctrl.SetupSignalHandler()
72-
73-
if err := run(ctx, opts); err != nil {
74-
setupLog.Error(err, "controller exited with error")
75-
os.Exit(1)
76-
}
77-
}
78-
79-
func run(ctx context.Context, options *Options) error {
80-
cfg := ctrl.GetConfigOrDie()
81-
82-
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
83-
Scheme: scheme,
84-
HealthProbeBindAddress: options.HealthProbeAddress,
85-
LeaderElection: options.LeaderElection,
86-
LeaderElectionID: "fake-compute-domain-controller",
87-
})
88-
if err != nil {
89-
return fmt.Errorf("failed to create controller manager: %w", err)
90-
}
91-
92-
reconciler := &controller.ComputeDomainReconciler{
93-
Client: mgr.GetClient(),
94-
Scheme: mgr.GetScheme(),
95-
}
96-
if err := reconciler.SetupWithManager(mgr); err != nil {
97-
return fmt.Errorf("failed to setup reconciler: %w", err)
98-
}
99-
100-
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
101-
return fmt.Errorf("failed to add health check: %w", err)
102-
}
103-
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
104-
return fmt.Errorf("failed to add ready check: %w", err)
105-
}
106-
107-
setupLog.Info("starting manager")
108-
return mgr.Start(ctx)
25+
appRunner := app.NewAppRunner(controller.NewComputeDomainApp())
26+
appRunner.Run()
10927
}

cmd/compute-domain-controller/options.go

Lines changed: 0 additions & 46 deletions
This file was deleted.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package computedomaincontroller
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"go.uber.org/zap/zapcore"
8+
resourceapi "k8s.io/api/resource/v1"
9+
"k8s.io/apimachinery/pkg/runtime"
10+
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
11+
ctrl "sigs.k8s.io/controller-runtime"
12+
"sigs.k8s.io/controller-runtime/pkg/healthz"
13+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
14+
15+
computedomainv1beta1 "github.com/NVIDIA/k8s-dra-driver-gpu/api/nvidia.com/resource/v1beta1"
16+
"github.com/run-ai/fake-gpu-operator/internal/common/app"
17+
)
18+
19+
var (
20+
scheme = runtime.NewScheme()
21+
)
22+
23+
func init() {
24+
_ = clientgoscheme.AddToScheme(scheme)
25+
_ = resourceapi.AddToScheme(scheme)
26+
_ = computedomainv1beta1.AddToScheme(scheme)
27+
}
28+
29+
type Config struct {
30+
MetricsBindAddress string `mapstructure:"METRICS_BIND_ADDRESS"`
31+
HealthProbeAddress string `mapstructure:"HEALTH_PROBE_BIND_ADDRESS"`
32+
LeaderElection bool `mapstructure:"LEADER_ELECT"`
33+
}
34+
35+
type ComputeDomainApp struct {
36+
config *Config
37+
stop chan struct{}
38+
}
39+
40+
func NewComputeDomainApp() *ComputeDomainApp {
41+
return &ComputeDomainApp{}
42+
}
43+
44+
func (app *ComputeDomainApp) Name() string {
45+
return "ComputeDomainController"
46+
}
47+
48+
func (app *ComputeDomainApp) GetConfig() interface{} {
49+
if app.config == nil {
50+
app.config = &Config{
51+
MetricsBindAddress: ":8080",
52+
HealthProbeAddress: ":8081",
53+
LeaderElection: false,
54+
}
55+
}
56+
return app.config
57+
}
58+
59+
func (app *ComputeDomainApp) Init(stop chan struct{}) {
60+
app.stop = stop
61+
}
62+
63+
func (app *ComputeDomainApp) Run() {
64+
opts := zap.Options{
65+
Development: true,
66+
TimeEncoder: zapcore.ISO8601TimeEncoder,
67+
}
68+
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
69+
70+
ctx, cancel := context.WithCancel(context.Background())
71+
defer cancel()
72+
73+
// Handle stop signal
74+
go func() {
75+
<-app.stop
76+
cancel()
77+
}()
78+
79+
if err := app.runController(ctx); err != nil {
80+
ctrl.Log.Error(err, "controller exited with error")
81+
}
82+
}
83+
84+
func (app *ComputeDomainApp) runController(ctx context.Context) error {
85+
cfg := ctrl.GetConfigOrDie()
86+
87+
mgr, err := ctrl.NewManager(cfg, ctrl.Options{
88+
Scheme: scheme,
89+
HealthProbeBindAddress: app.config.HealthProbeAddress,
90+
LeaderElection: app.config.LeaderElection,
91+
LeaderElectionID: "fake-compute-domain-controller",
92+
})
93+
if err != nil {
94+
return fmt.Errorf("failed to create controller manager: %w", err)
95+
}
96+
97+
reconciler := &ComputeDomainReconciler{
98+
Client: mgr.GetClient(),
99+
Scheme: mgr.GetScheme(),
100+
}
101+
if err := reconciler.SetupWithManager(mgr); err != nil {
102+
return fmt.Errorf("failed to setup reconciler: %w", err)
103+
}
104+
105+
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
106+
return fmt.Errorf("failed to add health check: %w", err)
107+
}
108+
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
109+
return fmt.Errorf("failed to add ready check: %w", err)
110+
}
111+
112+
ctrl.Log.Info("starting manager")
113+
return mgr.Start(ctx)
114+
}
115+
116+
// Verify that ComputeDomainApp implements the App interface
117+
var _ app.App = &ComputeDomainApp{}

0 commit comments

Comments
 (0)