forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
129 lines (105 loc) · 3.6 KB
/
api.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
package virt_api
import (
"log"
"net/http"
"github.com/emicklei/go-restful"
"github.com/emicklei/go-restful-openapi"
openapispec "github.com/go-openapi/spec"
flag "github.com/spf13/pflag"
"golang.org/x/net/context"
"k8s.io/apimachinery/pkg/runtime/schema"
"kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/healthz"
"kubevirt.io/kubevirt/pkg/rest/filter"
"kubevirt.io/kubevirt/pkg/service"
"kubevirt.io/kubevirt/pkg/virt-api/rest"
)
const (
// Default port that virt-api listens on.
defaultPort = 8183
// Default address that virt-api listens on.
defaultHost = "0.0.0.0"
)
type VirtAPIApp struct {
service.ServiceListen
SwaggerUI string
}
var _ service.Service = &VirtAPIApp{}
func (app *VirtAPIApp) Compose() {
ctx := context.Background()
vmGVR := schema.GroupVersionResource{Group: v1.GroupVersion.Group, Version: v1.GroupVersion.Version, Resource: "virtualmachines"}
vmrsGVR := schema.GroupVersionResource{Group: v1.GroupVersion.Group, Version: v1.GroupVersion.Version, Resource: "virtualmachinereplicasets"}
vmpGVR := schema.GroupVersionResource{Group: v1.GroupVersion.Group, Version: v1.GroupVersion.Version, Resource: "virtualmachinepresets"}
ws, err := rest.GroupVersionProxyBase(ctx, v1.GroupVersion)
if err != nil {
log.Fatal(err)
}
ws, err = rest.GenericResourceProxy(ws, ctx, vmGVR, &v1.VirtualMachine{}, v1.VirtualMachineGroupVersionKind.Kind, &v1.VirtualMachineList{})
if err != nil {
log.Fatal(err)
}
ws, err = rest.GenericResourceProxy(ws, ctx, vmrsGVR, &v1.VirtualMachineReplicaSet{}, v1.VMReplicaSetGroupVersionKind.Kind, &v1.VirtualMachineReplicaSetList{})
if err != nil {
log.Fatal(err)
}
ws, err = rest.GenericResourceProxy(ws, ctx, vmpGVR, &v1.VirtualMachinePreset{}, v1.VirtualMachineGroupVersionKind.Kind, &v1.VirtualMachinePresetList{})
if err != nil {
log.Fatal(err)
}
restful.Add(ws)
ws.Route(ws.GET("/healthz").
To(healthz.KubeConnectionHealthzFunc).
Consumes(restful.MIME_JSON).
Produces(restful.MIME_JSON).
Operation("checkHealth").
Doc("Health endpoint").
Returns(http.StatusOK, "OK", nil).
Returns(http.StatusInternalServerError, "Unhealthy", nil))
ws, err = rest.ResourceProxyAutodiscovery(ctx, vmGVR)
if err != nil {
log.Fatal(err)
}
restful.Add(ws)
restful.Filter(filter.RequestLoggingFilter())
restful.Filter(restful.OPTIONSFilter())
}
func (app *VirtAPIApp) ConfigureOpenAPIService() {
restful.DefaultContainer.Add(restfulspec.NewOpenAPIService(CreateOpenAPIConfig()))
http.Handle("/swagger-ui/", http.StripPrefix("/swagger-ui/", http.FileServer(http.Dir(app.SwaggerUI))))
}
func CreateOpenAPIConfig() restfulspec.Config {
return restfulspec.Config{
WebServices: restful.RegisteredWebServices(),
WebServicesURL: "",
APIPath: "/swaggerapi",
PostBuildSwaggerObjectHandler: addInfoToSwaggerObject,
}
}
func addInfoToSwaggerObject(swo *openapispec.Swagger) {
swo.Info = &openapispec.Info{
InfoProps: openapispec.InfoProps{
Title: "KubeVirt API",
Description: "This is KubeVirt API an add-on for Kubernetes.",
Contact: &openapispec.ContactInfo{
Name: "kubevirt-dev",
Email: "kubevirt-dev@googlegroups.com",
URL: "https://github.com/kubevirt/kubevirt",
},
License: &openapispec.License{
Name: "Apache 2.0",
URL: "https://www.apache.org/licenses/LICENSE-2.0",
},
},
}
}
func (app *VirtAPIApp) Run() {
log.Fatal(http.ListenAndServe(app.Address(), nil))
}
func (app *VirtAPIApp) AddFlags() {
app.InitFlags()
app.BindAddress = defaultHost
app.Port = defaultPort
app.AddCommonFlags()
flag.StringVar(&app.SwaggerUI, "swagger-ui", "third_party/swagger-ui",
"swagger-ui location")
}