/
main.go
217 lines (189 loc) · 6.51 KB
/
main.go
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
/*
* Copyright 2023 The Kubernetes Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"fmt"
"net"
"net/http"
"net/http/pprof"
"os"
"path"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli/v2"
"k8s.io/client-go/informers"
"k8s.io/component-base/metrics/legacyregistry"
"k8s.io/dynamic-resource-allocation/controller"
"k8s.io/klog/v2"
_ "k8s.io/component-base/metrics/prometheus/restclient" // for client metric registration
_ "k8s.io/component-base/metrics/prometheus/version" // for version metric registration
_ "k8s.io/component-base/metrics/prometheus/workqueue" // register work queues in the default legacy registry
"github.com/xcuda-io/k8s-xcuda-dra/pkg/flags"
)
type Flags struct {
kubeClientConfig flags.KubeClientConfig
loggingConfig *flags.LoggingConfig
nasConfig flags.NasConfig
workers int
httpEndpoint string
metricsPath string
profilePath string
}
type Config struct {
namespace string
flags *Flags
clientSets flags.ClientSets
mux *http.ServeMux
}
func main() {
if err := newApp().Run(os.Args); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
}
func newApp() *cli.App {
flags := &Flags{
loggingConfig: flags.NewLoggingConfig(),
}
cliFlags := []cli.Flag{
&cli.IntFlag{
Name: "workers",
Usage: "Concurrency to process multiple claims",
Value: 10,
Destination: &flags.workers,
EnvVars: []string{"WORKERS"},
},
&cli.StringFlag{
Category: "HTTP server:",
Name: "http-endpoint",
Usage: "The TCP network `address` where the HTTP server for diagnostics, including pprof and metrics will listen (example: `:8080`). The default is the empty string, which means the server is disabled.",
Destination: &flags.httpEndpoint,
EnvVars: []string{"HTTP_ENDPOINT"},
},
&cli.StringFlag{
Category: "HTTP server:",
Name: "metrics-path",
Usage: "The HTTP `path` where Prometheus metrics will be exposed, disabled if empty.",
Value: "/metrics",
Destination: &flags.metricsPath,
EnvVars: []string{"METRICS_PATH"},
},
&cli.StringFlag{
Category: "HTTP server:",
Name: "pprof-path",
Usage: "The HTTP `path` where pprof profiling will be available, disabled if empty.",
Destination: &flags.profilePath,
EnvVars: []string{"PPROF_PATH"},
},
}
cliFlags = append(cliFlags, flags.kubeClientConfig.Flags()...)
cliFlags = append(cliFlags, flags.loggingConfig.Flags()...)
flags.nasConfig.HideNodeName = true
cliFlags = append(cliFlags, flags.nasConfig.Flags()...)
app := &cli.App{
Name: "dra-example-controller",
Usage: "dra-example-controller implements a DRA driver controller.",
ArgsUsage: " ",
HideHelpCommand: true,
Flags: cliFlags,
Before: func(c *cli.Context) error {
if c.Args().Len() > 0 {
return fmt.Errorf("arguments not supported: %v", c.Args().Slice())
}
return flags.loggingConfig.Apply()
},
Action: func(c *cli.Context) error {
ctx := c.Context
mux := http.NewServeMux()
clientSets, err := flags.kubeClientConfig.NewClientSets()
if err != nil {
return fmt.Errorf("create client: %v", err)
}
config := &Config{
mux: mux,
flags: flags,
namespace: flags.nasConfig.Namespace,
clientSets: clientSets,
}
if flags.httpEndpoint != "" {
err = SetupHTTPEndpoint(ctx, config)
if err != nil {
return fmt.Errorf("create http endpoint: %v", err)
}
}
err = StartController(ctx, config)
if err != nil {
return fmt.Errorf("start controller: %v", err)
}
return nil
},
}
return app
}
func SetupHTTPEndpoint(ctx context.Context, config *Config) error {
logger := klog.FromContext(ctx)
logger = klog.LoggerWithName(logger, "http-server")
if config.flags.metricsPath != "" {
// To collect metrics data from the metric handler itself, we
// let it register itself and then collect from that registry.
reg := prometheus.NewRegistry()
gatherers := prometheus.Gatherers{
// Include Go runtime and process metrics:
// https://github.com/kubernetes/kubernetes/blob/9780d88cb6a4b5b067256ecb4abf56892093ee87/staging/src/k8s.io/component-base/metrics/legacyregistry/registry.go#L46-L49
legacyregistry.DefaultGatherer,
}
gatherers = append(gatherers, reg)
actualPath := path.Join("/", config.flags.metricsPath)
logger.Info("Starting metrics", "path", actualPath)
// This is similar to k8s.io/component-base/metrics HandlerWithReset
// except that we gather from multiple sources.
config.mux.Handle(actualPath,
promhttp.InstrumentMetricHandler(
reg,
promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{})))
}
if config.flags.profilePath != "" {
actualPath := path.Join("/", config.flags.profilePath)
logger.Info("Starting profiling", "path", actualPath)
config.mux.HandleFunc(actualPath, pprof.Index)
config.mux.HandleFunc(path.Join(actualPath, "cmdline"), pprof.Cmdline)
config.mux.HandleFunc(path.Join(actualPath, "profile"), pprof.Profile)
config.mux.HandleFunc(path.Join(actualPath, "symbol"), pprof.Symbol)
config.mux.HandleFunc(path.Join(actualPath, "trace"), pprof.Trace)
}
listener, err := net.Listen("tcp", config.flags.httpEndpoint)
if err != nil {
return fmt.Errorf("listen on HTTP endpoint: %v", err)
}
go func() {
logger.Info("Starting HTTP server", "endpoint", config.flags.httpEndpoint)
err := http.Serve(listener, config.mux)
if err != nil {
logger.Error(err, "HTTP server failed")
klog.FlushAndExit(klog.ExitFlushTimeout, 1)
}
}()
return nil
}
func StartController(ctx context.Context, config *Config) error {
driver := NewDriver(config)
informerFactory := informers.NewSharedInformerFactory(config.clientSets.Core, 0 /* resync period */)
ctrl := controller.New(ctx, DriverAPIGroup, driver, config.clientSets.Core, informerFactory)
informerFactory.Start(ctx.Done())
ctrl.Run(config.flags.workers)
return nil
}