Skip to content

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ KUSTOMIZE ?= $(LOCALBIN)/kustomize
KUSTOMIZE_VERSION ?= v4.5.7
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"

OPENAPI_GEN ?= $(LOCALBIN)/openapi-gen
OPENAPI_VERSION ?= c8a335a

KUBECONFIG ?= ~/.kube/config

.PHONY:build
build: fmt vet
go build -o bin/console main.go

.PHONY: build-container
build-container: fmt vet test
build-container: generate fmt vet test
podman manifest rm ${IMG} || true && \
podman build --build-arg TARGET_ARCH=amd64 --manifest=${IMG} . && \
podman build --build-arg TARGET_ARCH=s390x --manifest=${IMG} . && \
Expand All @@ -42,6 +45,21 @@ kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
$(KUSTOMIZE): $(LOCALBIN)
test -s $(LOCALBIN)/kustomize || curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN)

.PHONY: openapi-gen
openapi-gen: $(OPENAPI_GEN) ## Download openapi-gen locally if necessary.
$(OPENAPI_GEN): $(LOCALBIN)
test -s $(OPENAPI_GEN) || GOBIN=$(LOCALBIN) go install k8s.io/kube-openapi/cmd/openapi-gen@$(OPENAPI_VERSION)

.PHONY: generate
generate: openapi-gen
cd api && $(OPENAPI_GEN) \
--output-file zz_generated.openapi.go \
--output-dir ../pkg/generated/api/v1 \
--output-pkg kubevirt.io/vm-console-proxy/api/v1 \
--report-filename /dev/null \
k8s.io/apimachinery/pkg/apis/meta/v1 \
kubevirt.io/vm-console-proxy/api/v1

.PHONY: release-manifests
release-manifests: kustomize manifests
mkdir -p ./_out
Expand Down
2 changes: 2 additions & 0 deletions api/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
)

// TokenResponse is the response object from /token endpoint.
//
// +k8s:openapi-gen=true
type TokenResponse struct {
Token string `json:"token"`
ExpirationTimestamp metav1.Time `json:"expirationTimestamp"`
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
k8s.io/api v0.31.0
k8s.io/apimachinery v0.32.3
k8s.io/client-go v0.31.0
k8s.io/kube-openapi v0.31.0
Copy link
Member

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?

Copy link
Collaborator Author

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-openapi

This 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#L25

Then later in this go.mod we set it to a commit version:

replace (
	// This is needed by kubevirt.io/client-go
	k8s.io/kube-openapi => k8s.io/kube-openapi v0.0.0-20240430033511-f0e62f92d13f
)

We should sometime fix the dependencies of kubevirt/client-go.

Copy link
Collaborator Author

@akrejcir akrejcir Apr 24, 2025

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should sometime fix the dependencies of kubevirt/client-go

kubevirt/kubevirt#14399 FWIW I started but didn't finish this.

k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738
kubevirt.io/api v1.5.0
kubevirt.io/client-go v1.5.0
Expand Down Expand Up @@ -72,7 +73,6 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/apiextensions-apiserver v0.31.0 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/kube-openapi v0.31.0 // indirect
kubevirt.io/containerized-data-importer-api v1.62.0 // indirect
kubevirt.io/controller-lifecycle-operator-sdk/api v0.2.4 // indirect
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3 // indirect
Expand Down
74 changes: 68 additions & 6 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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 (
Expand Down Expand Up @@ -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{
Expand All @@ -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")))
Expand Down Expand Up @@ -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,
}
}
Loading