-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
378 lines (325 loc) · 12.6 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package main
import (
"context"
"flag"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/signal"
"path/filepath"
"time"
kubeflowv1 "github.com/StatCan/kubeflow-apis/apis/kubeflow/v1"
kubeflowv1alpha1 "github.com/StatCan/kubeflow-apis/apis/kubeflow/v1alpha1"
kubeflow "github.com/StatCan/kubeflow-apis/clientset/versioned"
kubeflowv1listers "github.com/StatCan/kubeflow-apis/listers/kubeflow/v1"
kubeflowv1alpha1listers "github.com/StatCan/kubeflow-apis/listers/kubeflow/v1alpha1"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"gopkg.in/yaml.v2"
authorizationv1 "k8s.io/api/authorization/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
v1listers "k8s.io/client-go/listers/core/v1"
storagev1listers "k8s.io/client-go/listers/storage/v1"
_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
)
var kubeconfig string
var spawnerConfigPath string
var userIDHeader string
var staticDirectory string
var listenAddr string
var kubecostURL string
type listers struct {
namespaces v1listers.NamespaceLister
events v1listers.EventLister
storageClasses storagev1listers.StorageClassLister
persistentVolumeClaims v1listers.PersistentVolumeClaimLister
podDefaults kubeflowv1alpha1listers.PodDefaultLister
notebooks kubeflowv1listers.NotebookLister
pods v1listers.PodLister
}
type clientsets struct {
kubernetes *kubernetes.Clientset
kubeflow *kubeflow.Clientset
}
type server struct {
Config Configuration
clientsets clientsets
listers listers
kubecostURL *url.URL
}
func main() {
var err error
gctx, gcancel := context.WithCancel(context.Background())
// Check if we are running inside the cluster, and default to using that instead of kubeconfig if that's the case
_, err = os.Stat("/var/run/secrets/kubernetes.io/serviceaccount")
// Setup the default path to the of the kubeconfig file
if home := homedir.HomeDir(); os.IsNotExist(err) && home != "" {
flag.StringVar(&kubeconfig, "kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.StringVar(&userIDHeader, "userid-header", "kubeflow-userid", "header in the request which identifies the incoming user")
flag.StringVar(&spawnerConfigPath, "spawner-config", "/etc/config/spawner_ui_config.yaml", "path to the spawner configuration file")
flag.StringVar(&staticDirectory, "static-dir", "static/", "path to the static assets")
flag.StringVar(&listenAddr, "listen-addr", lookupEnvironment("LISTEN_ADDRESS", "127.0.0.1:5000"), "server listen address")
flag.StringVar(&kubecostURL, "kubecost-url", lookupEnvironment("KUBECOST_URL", "http://127.0.0.1:9090"), "Url to connect to Kubecost API")
// Parse flags
flag.Parse()
// Setup the server
s := server{}
// Parse config
cfdata, err := ioutil.ReadFile(spawnerConfigPath)
if err != nil {
log.Fatal(err)
}
err = yaml.Unmarshal(cfdata, &s.Config)
if err != nil {
log.Fatal(err)
}
// Parse kubecostURL
s.kubecostURL, err = url.Parse(kubecostURL)
if err != nil {
log.Fatal(err)
}
// Construct the configuration based on the provided flags.
// If no config file is provided, then the in-cluster config is used.
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
log.Fatal(err)
}
// Generate the Kubernetes clientset
s.clientsets.kubernetes, err = kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
}
// Generate the Kubeflow clientset
s.clientsets.kubeflow, err = kubeflow.NewForConfig(config)
if err != nil {
log.Fatal(err)
}
_ = s.setupListers(gctx)
// Generate the Gorilla Mux router
router := mux.NewRouter()
// Setup route handlers
router.HandleFunc("/api/config", s.GetConfig).Methods("GET")
router.HandleFunc("/api/gpus", s.GetGPUVendors).Methods("GET")
router.HandleFunc("/api/storageclasses", s.GetStorageClasses).Methods("GET")
router.HandleFunc("/api/storageclasses/default", s.GetDefaultStorageClass).Methods("GET")
router.HandleFunc("/api/namespaces/{namespace}/cost/allocation", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: corev1.SchemeGroupVersion.Group,
Verb: "list",
Resource: "pods",
Version: corev1.SchemeGroupVersion.Version,
},
},
}, s.GetCost)).Methods("GET")
router.HandleFunc("/api/namespaces", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: corev1.SchemeGroupVersion.Group,
Verb: "list",
Resource: "namespaces",
Version: corev1.SchemeGroupVersion.Version,
},
},
}, s.GetNamespaces)).Methods("GET")
router.HandleFunc("/api/namespaces/{namespace}", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: corev1.SchemeGroupVersion.Group,
Verb: "get",
Resource: "namespaces",
Version: corev1.SchemeGroupVersion.Version,
},
},
}, s.GetNamespaceMetadata)).Methods("GET")
router.HandleFunc("/api/namespaces/{namespace}/notebooks", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: kubeflowv1.SchemeGroupVersion.Group,
Verb: "list",
Resource: "notebooks",
Version: kubeflowv1.SchemeGroupVersion.Version,
},
},
}, s.GetNotebooks)).Methods("GET")
router.HandleFunc("/api/namespaces/{namespace}/notebooks", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: kubeflowv1.SchemeGroupVersion.Group,
Verb: "create",
Resource: "notebooks",
Version: kubeflowv1.SchemeGroupVersion.Version,
},
},
}, s.NewNotebook)).Headers("Content-Type", "application/json").Methods("POST")
router.HandleFunc("/api/namespaces/{namespace}/notebooks/{notebook}", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: kubeflowv1.SchemeGroupVersion.Group,
Verb: "get",
Resource: "notebooks",
Version: kubeflowv1.SchemeGroupVersion.Version,
},
},
}, s.GetNotebook)).Methods("GET")
router.HandleFunc("/api/namespaces/{namespace}/notebooks/{notebook}/pod", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: corev1.SchemeGroupVersion.Group,
Verb: "list",
Resource: "pods",
Version: corev1.SchemeGroupVersion.Version,
},
},
}, s.GetNotebookPod)).Methods("GET")
router.HandleFunc("/api/namespaces/{namespace}/notebooks/{notebook}/pod/{pod}/logs", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: corev1.SchemeGroupVersion.Group,
Verb: "get",
Resource: "pods",
Version: corev1.SchemeGroupVersion.Version,
},
},
}, s.GetNotebookPodLogs)).Methods("GET")
router.HandleFunc("/api/namespaces/{namespace}/notebooks/{notebook}/events", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: corev1.SchemeGroupVersion.Group,
Verb: "list",
Resource: "events",
Version: corev1.SchemeGroupVersion.Version,
},
},
}, s.GetNotebookEvents)).Methods("GET")
router.HandleFunc("/api/namespaces/{namespace}/notebooks/{notebook}", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: kubeflowv1.SchemeGroupVersion.Group,
Verb: "update",
Resource: "notebooks",
Version: kubeflowv1.SchemeGroupVersion.Version,
},
},
}, s.UpdateNotebook)).Methods("PATCH")
router.HandleFunc("/api/namespaces/{namespace}/notebooks/{notebook}", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: kubeflowv1.SchemeGroupVersion.Group,
Verb: "delete",
Resource: "notebooks",
Version: kubeflowv1.SchemeGroupVersion.Version,
},
},
}, s.DeleteNotebook)).Methods("DELETE")
router.HandleFunc("/api/namespaces/{namespace}/pvcs", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: corev1.SchemeGroupVersion.Group,
Verb: "list",
Resource: "persistentvolumeclaims",
Version: corev1.SchemeGroupVersion.Version,
},
},
}, s.GetPersistentVolumeClaims)).Methods("GET")
router.HandleFunc("/api/namespaces/{namespace}/pvcs/{pvc}", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: corev1.SchemeGroupVersion.Group,
Verb: "delete",
Resource: "persistentvolumeclaims",
Version: corev1.SchemeGroupVersion.Version,
},
},
}, s.DeletePvc)).Methods("DELETE")
router.HandleFunc("/api/namespaces/{namespace}/pvcs/{pvc}", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: corev1.SchemeGroupVersion.Group,
Verb: "get",
Resource: "persistentvolumeclaims",
Version: corev1.SchemeGroupVersion.Version,
},
},
}, s.GetPvc)).Methods("GET")
router.HandleFunc("/api/namespaces/{namespace}/pvcs/{pvc}/pods", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: corev1.SchemeGroupVersion.Group,
Verb: "list",
Resource: "pods",
Version: corev1.SchemeGroupVersion.Version,
},
},
}, s.GetPvcPods)).Methods("GET")
router.HandleFunc("/api/namespaces/{namespace}/pvcs/{pvc}/events", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: corev1.SchemeGroupVersion.Group,
Verb: "list",
Resource: "events",
Version: corev1.SchemeGroupVersion.Version,
},
},
}, s.GetPvcEvents)).Methods("GET")
router.HandleFunc("/api/namespaces/{namespace}/poddefaults", s.checkAccess(authorizationv1.SubjectAccessReview{
Spec: authorizationv1.SubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Group: kubeflowv1alpha1.SchemeGroupVersion.Group,
Verb: "list",
Resource: "poddefaults",
Version: kubeflowv1alpha1.SchemeGroupVersion.Version,
},
},
}, s.GetPodDefaults)).Methods("GET")
// Setup the server, with:
// Add combined logging handler
// Default Read/Write timeouts every 15s
srv := &http.Server{
Handler: kubeflowUserHandler(userIDHeader, handlers.CombinedLoggingHandler(os.Stdout, router)),
Addr: listenAddr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Printf("listening on %s", srv.Addr)
// Run our server in a goroutine so that it doesn't block
// (this lets us capture the shutdown request and gracefully exit)
go func() {
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
}()
c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught
signal.Notify(c, os.Interrupt)
// Block until we receive our signal
<-c
// Cancel global context
gcancel()
// Create a deadline to wait for
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline
srv.Shutdown(ctx)
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation
log.Println("shutting down")
os.Exit(0)
}
func lookupEnvironment(name string, defaultValue string) string {
if value, isSet := os.LookupEnv(name); isSet {
return value
}
return defaultValue
}