forked from kubernetes-sigs/kube-scheduler-simulator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexport.go
74 lines (62 loc) · 2.51 KB
/
export.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
package handler
import (
"net/http"
"github.com/labstack/echo/v4"
v1 "k8s.io/client-go/applyconfigurations/core/v1"
schedulingcfgv1 "k8s.io/client-go/applyconfigurations/scheduling/v1"
confstoragev1 "k8s.io/client-go/applyconfigurations/storage/v1"
"k8s.io/klog/v2"
v1beta2config "k8s.io/kube-scheduler/config/v1beta2"
"sigs.k8s.io/kube-scheduler-simulator/simulator/export"
"sigs.k8s.io/kube-scheduler-simulator/simulator/server/di"
)
type ExportHandler struct {
service di.ExportService
}
type ResourcesForImport struct {
Pods []v1.PodApplyConfiguration `json:"pods"`
Nodes []v1.NodeApplyConfiguration `json:"nodes"`
Pvs []v1.PersistentVolumeApplyConfiguration `json:"pvs"`
Pvcs []v1.PersistentVolumeClaimApplyConfiguration `json:"pvcs"`
StorageClasses []confstoragev1.StorageClassApplyConfiguration `json:"storageClasses"`
PriorityClasses []schedulingcfgv1.PriorityClassApplyConfiguration `json:"priorityClasses"`
SchedulerConfig *v1beta2config.KubeSchedulerConfiguration `json:"schedulerConfig"`
}
func NewExportHandler(s di.ExportService) *ExportHandler {
return &ExportHandler{service: s}
}
func (h *ExportHandler) Export(c echo.Context) error {
ctx := c.Request().Context()
rs, err := h.service.Export(ctx)
if err != nil {
klog.Errorf("failed to export all resources: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError)
}
return c.JSON(http.StatusOK, rs)
}
func (h *ExportHandler) Import(c echo.Context) error {
ctx := c.Request().Context()
reqResources := new(ResourcesForImport)
if err := c.Bind(reqResources); err != nil {
klog.Errorf("failed to bind import resources all request: %+v", err)
return echo.NewHTTPError(http.StatusBadRequest)
}
err := h.service.Import(ctx, convertToResourcesApplyConfiguration(reqResources))
if err != nil {
klog.Errorf("failed to import all resources: %+v", err)
return echo.NewHTTPError(http.StatusInternalServerError)
}
return c.NoContent(http.StatusOK)
}
// convertToResourcesApplyConfiguration converts from *ResourcesApplyConfiguration to *export.ResourcesApplyConfiguration.
func convertToResourcesApplyConfiguration(r *ResourcesForImport) *export.ResourcesForImport {
return &export.ResourcesForImport{
Pods: r.Pods,
Nodes: r.Nodes,
Pvs: r.Pvs,
Pvcs: r.Pvcs,
StorageClasses: r.StorageClasses,
PriorityClasses: r.PriorityClasses,
SchedulerConfig: r.SchedulerConfig,
}
}