-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathapp.go
More file actions
223 lines (193 loc) · 7.34 KB
/
Copy pathapp.go
File metadata and controls
223 lines (193 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright 2025 NVIDIA CORPORATION
// SPDX-License-Identifier: Apache-2.0
package app
import (
"context"
"fmt"
"time"
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
schedulingv1alpha2 "github.com/kai-scheduler/KAI-scheduler/pkg/apis/scheduling/v1alpha2"
featuregates "github.com/kai-scheduler/KAI-scheduler/pkg/common/feature_gates"
draversionawareclient "github.com/kai-scheduler/KAI-scheduler/pkg/common/resources/dra_version_aware_client"
"github.com/kai-scheduler/KAI-scheduler/pkg/binder/binding"
"github.com/kai-scheduler/KAI-scheduler/pkg/binder/binding/resourcereservation"
"github.com/kai-scheduler/KAI-scheduler/pkg/binder/controllers"
"github.com/kai-scheduler/KAI-scheduler/pkg/binder/plugins"
"github.com/kai-scheduler/KAI-scheduler/pkg/common/constants"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(schedulingv1alpha2.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
type App struct {
K8sInterface kubernetes.Interface
Client client.WithWatch
InformerFactory informers.SharedInformerFactory
Options *Options
manager manager.Manager
rrs resourcereservation.Interface
reconcilerParams *controllers.ReconcilerParams
plugins *plugins.BinderPlugins
}
func New(options *Options, config *rest.Config) (*App, error) {
config.QPS = float32(options.QPS)
config.Burst = options.Burst
mgr, err := ctrl.NewManager(config, ctrl.Options{
Scheme: scheme,
Metrics: server.Options{
BindAddress: options.MetricsAddr,
},
HealthProbeBindAddress: options.ProbeAddr,
LeaderElection: options.EnableLeaderElection,
LeaderElectionID: "2ad35f9c.kai.scheduler",
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
// when the Manager ends. This requires the binary to immediately end when the
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
// speeds up voluntary leader transitions as the new leader don't have to wait
// LeaseDuration time first.
//
// In the default scaffold provided, the program ends immediately after
// the manager stops, so would be fine to enable this option. However,
// if you are doing or is intended to do any operation such as perform cleanups
// after the manager stops then its usage might be unsafe.
// LeaderElectionReleaseOnCancel: true,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
return nil, err
}
if err := createIndexesForResourceReservation(mgr); err != nil {
return nil, err
}
clientWithWatch, err := client.NewWithWatch(mgr.GetConfig(), client.Options{
Scheme: scheme,
Cache: &client.CacheOptions{
Reader: mgr.GetCache(),
},
})
if err != nil {
setupLog.Error(err, "unable to create client with watch")
return nil, err
}
kubeClient := draversionawareclient.NewDRAAwareClient(kubernetes.NewForConfigOrDie(config))
informerFactory := informers.NewSharedInformerFactory(kubeClient, 0)
if err := featuregates.SetDRAFeatureGate(kubeClient.Discovery()); err != nil {
setupLog.Error(err, "unable to set DRA feature gate")
}
rrs := resourcereservation.NewService(options.FakeGPUNodes, clientWithWatch, options.ResourceReservationPodImage,
time.Duration(options.ResourceReservationAllocationTimeout)*time.Second,
options.ResourceReservationNamespace, options.ResourceReservationServiceAccount,
options.ResourceReservationAppLabel, options.ScalingPodNamespace, options.RuntimeClassName,
options.ResourceReservationPodResources.Value,
options.ResourceReservationPodSecurityContext.Value,
options.ResourceReservationContainerSecurityContext.Value)
reconcilerParams := &controllers.ReconcilerParams{
MaxConcurrentReconciles: options.MaxConcurrentReconciles,
RateLimiterBaseDelaySeconds: options.RateLimiterBaseDelaySeconds,
RateLimiterMaxDelaySeconds: options.RateLimiterMaxDelaySeconds,
}
app := &App{
K8sInterface: kubeClient,
Client: clientWithWatch,
InformerFactory: informerFactory,
Options: options,
manager: mgr,
rrs: rrs,
reconcilerParams: reconcilerParams,
}
return app, nil
}
func (app *App) RegisterPlugins(plugins *plugins.BinderPlugins) {
app.plugins = plugins
}
// +kubebuilder:rbac:groups=core,resources=configmaps,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=coordination.k8s.io,resources=leases,verbs=get;list;watch;create;update;patch;delete
func (app *App) Run(ctx context.Context) error {
var err error
go func() {
app.manager.GetCache().WaitForCacheSync(context.Background())
setupLog.Info("syncing resource reservation")
err := app.rrs.Sync(context.Background())
if err != nil {
setupLog.Error(err, "unable to sync resource reservation")
panic(err)
}
}()
if err = (&controllers.PodReconciler{
Client: app.manager.GetClient(),
Scheme: app.manager.GetScheme(),
ResourceReservation: app.rrs,
SchedulerName: app.Options.SchedulerName,
}).SetupWithManager(app.manager, app.reconcilerParams); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Pod")
return err
}
binder := binding.NewBinder(app.Client, app.rrs, app.plugins)
app.InformerFactory.Start(ctx.Done())
app.InformerFactory.WaitForCacheSync(ctx.Done())
reconciler := controllers.NewBindRequestReconciler(
app.manager.GetClient(), app.manager.GetScheme(), app.manager.GetEventRecorderFor("binder"), app.reconcilerParams,
binder, app.rrs)
if err = reconciler.SetupWithManager(app.manager); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "BindRequest")
return err
}
// +kubebuilder:scaffold:builder
setupLog.Info("starting manager")
if err = app.manager.Start(ctx); err != nil {
setupLog.Error(err, "problem running manager")
return err
}
return nil
}
func createIndexesForResourceReservation(mgr manager.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(
context.Background(), &corev1.Pod{}, "spec.nodeName",
func(obj client.Object) []string {
nodeName := obj.(*corev1.Pod).Spec.NodeName
if nodeName == "" {
return nil
}
return []string{nodeName}
},
); err != nil {
setupLog.Error(err, "failed to create index for spec.nodeName")
return err
}
if err := mgr.GetFieldIndexer().IndexField(
context.Background(), &corev1.Pod{}, fmt.Sprintf("metadata.labels.%s", constants.GPUGroup),
func(obj client.Object) []string {
labels := obj.(*corev1.Pod).Labels
if labels == nil {
return nil
}
gpuGroup, found := labels[constants.GPUGroup]
if !found {
return nil
}
return []string{gpuGroup}
},
); err != nil {
setupLog.Error(err, "failed to create index for spec.nodeName")
return err
}
return nil
}