-
Notifications
You must be signed in to change notification settings - Fork 10
feat: Add openapi/v2 endpoint #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
akrejcir
wants to merge
1
commit into
kubevirt:main
Choose a base branch
from
akrejcir:add-openapi-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,10 @@ import ( | |
|
||
"github.com/emicklei/go-restful/v3" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/kube-openapi/pkg/builder" | ||
"k8s.io/kube-openapi/pkg/common" | ||
"k8s.io/kube-openapi/pkg/common/restfuladapter" | ||
"k8s.io/kube-openapi/pkg/validation/spec" | ||
"kubevirt.io/client-go/kubecli" | ||
"kubevirt.io/client-go/log" | ||
|
||
|
@@ -15,6 +19,7 @@ import ( | |
"kubevirt.io/vm-console-proxy/pkg/console/service" | ||
"kubevirt.io/vm-console-proxy/pkg/console/tlsconfig" | ||
"kubevirt.io/vm-console-proxy/pkg/filewatch" | ||
generated "kubevirt.io/vm-console-proxy/pkg/generated/api/v1" | ||
) | ||
|
||
const ( | ||
|
@@ -64,7 +69,12 @@ func Run() error { | |
|
||
handlers := service.NewService(cli, authConfigReader) | ||
|
||
restful.Add(webService(handlers)) | ||
ws, err := webService(handlers) | ||
if err != nil { | ||
return fmt.Errorf("failed to create web service: %w", err) | ||
} | ||
|
||
restful.Add(ws) | ||
restful.Filter(restful.OPTIONSFilter()) | ||
|
||
server := &http.Server{ | ||
|
@@ -85,13 +95,16 @@ func Run() error { | |
return server.ListenAndServeTLS("", "") | ||
} | ||
|
||
func webService(handlers service.Service) *restful.WebService { | ||
func webService(handlers service.Service) (*restful.WebService, error) { | ||
ws := new(restful.WebService) | ||
|
||
ws.Route(ws.GET("/apis/" + api.Group + "/" + api.Version + "/namespaces/{namespace:[a-z0-9][a-z0-9\\-]*}/virtualmachines/{name:[a-z0-9][a-z0-9\\-]*}/vnc"). | ||
ws.Route(ws.GET("/apis/"+api.Group+"/"+api.Version+"/namespaces/{namespace}/virtualmachines/{name}/vnc"). | ||
To(handlers.TokenHandler). | ||
Doc("generate token"). | ||
Operation("token"). | ||
Returns(http.StatusOK, "OK", api.TokenResponse{}). | ||
Returns(http.StatusBadRequest, "BadRequest", ""). | ||
Returns(http.StatusNotFound, "NotFound", ""). | ||
Param(ws.PathParameter("namespace", "namespace").Required(true)). | ||
Param(ws.PathParameter("name", "name").Required(true)). | ||
Param(ws.QueryParameter("duration", "duration"))) | ||
|
@@ -169,14 +182,63 @@ func webService(handlers service.Service) *restful.WebService { | |
response.WriteAsJson(&metav1.RootPaths{ | ||
Paths: []string{ | ||
"/apis", | ||
"/apis/" + api.Group, | ||
"/apis/" + api.Group + "/" + api.Version, | ||
"/openapi/v2", | ||
}, | ||
}) | ||
}). | ||
Operation("getRootPaths"). | ||
Doc("Get API root paths"). | ||
Returns(http.StatusOK, "OK", metav1.RootPaths{})) | ||
|
||
return ws | ||
openApiSpec, err := builder.BuildOpenAPISpecFromRoutes(restfuladapter.AdaptWebServices([]*restful.WebService{ws}), openApiConfig()) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to build OpenAPI spec from routes: %w", err) | ||
} | ||
|
||
ws.Route(ws.GET("openapi/v2"). | ||
Consumes(restful.MIME_JSON). | ||
Produces(restful.MIME_JSON). | ||
To(func(request *restful.Request, response *restful.Response) { | ||
response.WriteAsJson(openApiSpec) | ||
})) | ||
|
||
return ws, nil | ||
} | ||
|
||
func openApiConfig() *common.Config { | ||
return &common.Config{ | ||
CommonResponses: map[int]spec.Response{ | ||
401: { | ||
ResponseProps: spec.ResponseProps{ | ||
Description: "Unauthorized", | ||
}, | ||
}, | ||
}, | ||
Info: &spec.Info{ | ||
InfoProps: spec.InfoProps{ | ||
Title: "KubeVirt VNC token generation API", | ||
Description: "This is the VNC token generation API for Kubevirt.", | ||
Contact: &spec.ContactInfo{ | ||
Name: "kubevirt-dev", | ||
Email: "[email protected]", | ||
URL: "https://github.com/kubevirt/kubevirt", | ||
}, | ||
License: &spec.License{ | ||
Name: "Apache 2.0", | ||
URL: "https://www.apache.org/licenses/LICENSE-2.0", | ||
}, | ||
}, | ||
}, | ||
SecurityDefinitions: &spec.SecurityDefinitions{ | ||
"BearerToken": &spec.SecurityScheme{ | ||
SecuritySchemeProps: spec.SecuritySchemeProps{ | ||
Type: "apiKey", | ||
Name: "authorization", | ||
In: "header", | ||
Description: "Bearer Token authentication", | ||
}, | ||
}, | ||
}, | ||
GetDefinitions: generated.GetOpenAPIDefinitions, | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
side note: How is there
v0.31.0
of this dependency but no version for openapi-gen?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no tagged version of
k8s.io/kube-openapi
, the repo has no tags: https://github.com/kubernetes/kube-openapiThis
v0.31.0
, is an invalid version, that has been propagated from kubevirt client-go: https://github.com/kubevirt/client-go/blob/v1.5.0/go.mod#L25Then later in this
go.mod
we set it to a commit version:We should sometime fix the dependencies of kubevirt/client-go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kube-openapi
has an issue for tagged releases, that no one is working on in a long time:https://github.com/kubernetes/kube-openapi/issues/383
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kubevirt/kubevirt#14399 FWIW I started but didn't finish this.