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