Skip to content

Commit

Permalink
Move rest definitions into a separate package
Browse files Browse the repository at this point in the history
This is needed to resolve import cycle in next commits.

Signed-off-by: Andrej Krejcir <[email protected]>
  • Loading branch information
akrejcir committed Aug 1, 2022
1 parent 289d860 commit 3037f61
Show file tree
Hide file tree
Showing 17 changed files with 147 additions and 103 deletions.
2 changes: 1 addition & 1 deletion pkg/util/openapi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ go_test(
],
deps = [
":go_default_library",
"//pkg/virt-api/rest:go_default_library",
"//pkg/virt-api/definitions:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
"//staging/src/kubevirt.io/client-go/testutils:go_default_library",
"//vendor/github.com/onsi/ginkgo/v2:go_default_library",
Expand Down
5 changes: 2 additions & 3 deletions pkg/util/openapi/openapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package openapi_test
import (
"encoding/json"

"kubevirt.io/kubevirt/pkg/virt-api/rest"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

v1 "kubevirt.io/api/core/v1"

"kubevirt.io/kubevirt/pkg/util/openapi"
"kubevirt.io/kubevirt/pkg/virt-api/definitions"
)

var _ = Describe("Openapi", func() {
Expand All @@ -21,7 +20,7 @@ var _ = Describe("Openapi", func() {
var obj *unstructured.Unstructured

BeforeEach(func() {
validator = openapi.CreateOpenAPIValidator(rest.ComposeAPIDefinitions())
validator = openapi.CreateOpenAPIValidator(definitions.ComposeAPIDefinitions())
vmi = v1.NewVMI("testvm", "")
obj = &unstructured.Unstructured{}
})
Expand Down
1 change: 1 addition & 0 deletions pkg/util/webhooks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/util:go_default_library",
"//pkg/virt-api/definitions:go_default_library",
"//pkg/virt-api/webhooks:go_default_library",
"//pkg/virt-operator/resource/generate/components:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
Expand Down
5 changes: 3 additions & 2 deletions pkg/util/webhooks/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
v12 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/log"

"kubevirt.io/kubevirt/pkg/virt-api/definitions"
"kubevirt.io/kubevirt/pkg/virt-api/webhooks"
)

Expand Down Expand Up @@ -91,7 +92,7 @@ func ValidateSchema(gvk schema.GroupVersionKind, data []byte) *admissionv1.Admis
if err != nil {
return ToAdmissionResponseError(err)
}
errs := webhooks.Validator.Validate(gvk, in)
errs := definitions.Validator.Validate(gvk, in)
if len(errs) > 0 {
return ValidationErrorsToAdmissionResponse(errs)
}
Expand Down Expand Up @@ -122,7 +123,7 @@ func ValidateStatus(data []byte) *admissionv1.AdmissionResponse {
if gvk.Kind == "" {
return ValidationErrorsToAdmissionResponse([]error{fmt.Errorf("could not determine object kind")})
}
errs := webhooks.Validator.ValidateStatus(gvk, in)
errs := definitions.Validator.ValidateStatus(gvk, in)
if len(errs) > 0 {
return ValidationErrorsToAdmissionResponse(errs)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/virt-api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_library(
"//pkg/util/openapi:go_default_library",
"//pkg/util/ratelimiter:go_default_library",
"//pkg/util/webhooks:go_default_library",
"//pkg/virt-api/definitions:go_default_library",
"//pkg/virt-api/rest:go_default_library",
"//pkg/virt-api/webhooks:go_default_library",
"//pkg/virt-api/webhooks/mutating-webhook:go_default_library",
Expand Down
135 changes: 68 additions & 67 deletions pkg/virt-api/api.go

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions pkg/virt-api/definitions/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = [
"definitions.go",
"validator.go",
],
importpath = "kubevirt.io/kubevirt/pkg/virt-api/definitions",
visibility = ["//visibility:public"],
deps = [
"//pkg/rest:go_default_library",
"//pkg/util/openapi:go_default_library",
"//staging/src/kubevirt.io/api/clone:go_default_library",
"//staging/src/kubevirt.io/api/clone/v1alpha1:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
"//staging/src/kubevirt.io/api/export/v1alpha1:go_default_library",
"//staging/src/kubevirt.io/api/instancetype:go_default_library",
"//staging/src/kubevirt.io/api/instancetype/v1alpha1:go_default_library",
"//staging/src/kubevirt.io/api/migrations:go_default_library",
"//staging/src/kubevirt.io/api/migrations/v1alpha1:go_default_library",
"//staging/src/kubevirt.io/api/pool/v1alpha1:go_default_library",
"//staging/src/kubevirt.io/api/snapshot/v1alpha1:go_default_library",
"//vendor/github.com/emicklei/go-restful:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

package rest
package definitions

import (
"fmt"
Expand Down
24 changes: 24 additions & 0 deletions pkg/virt-api/definitions/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* This file is part of the KubeVirt project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2022 Red Hat, Inc.
*
*/

package definitions

import "kubevirt.io/kubevirt/pkg/util/openapi"

var Validator = openapi.CreateOpenAPIValidator(ComposeAPIDefinitions())
15 changes: 2 additions & 13 deletions pkg/virt-api/rest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go_library(
srcs = [
"authorizer.go",
"console.go",
"definitions.go",
"dialers.go",
"generated_mock_authorizer.go",
"portforward.go",
Expand All @@ -20,21 +19,12 @@ go_library(
deps = [
"//pkg/controller:go_default_library",
"//pkg/monitoring/api:go_default_library",
"//pkg/rest:go_default_library",
"//pkg/util:go_default_library",
"//pkg/util/status:go_default_library",
"//pkg/util/types:go_default_library",
"//pkg/virt-api/definitions:go_default_library",
"//pkg/virt-config:go_default_library",
"//staging/src/kubevirt.io/api/clone:go_default_library",
"//staging/src/kubevirt.io/api/clone/v1alpha1:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
"//staging/src/kubevirt.io/api/export/v1alpha1:go_default_library",
"//staging/src/kubevirt.io/api/instancetype:go_default_library",
"//staging/src/kubevirt.io/api/instancetype/v1alpha1:go_default_library",
"//staging/src/kubevirt.io/api/migrations:go_default_library",
"//staging/src/kubevirt.io/api/migrations/v1alpha1:go_default_library",
"//staging/src/kubevirt.io/api/pool/v1alpha1:go_default_library",
"//staging/src/kubevirt.io/api/snapshot/v1alpha1:go_default_library",
"//staging/src/kubevirt.io/client-go/kubecli:go_default_library",
"//staging/src/kubevirt.io/client-go/log:go_default_library",
"//staging/src/kubevirt.io/client-go/util:go_default_library",
Expand All @@ -49,8 +39,6 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/fields:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/types:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/json:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/util/yaml:go_default_library",
Expand All @@ -74,6 +62,7 @@ go_test(
deps = [
"//pkg/testutils:go_default_library",
"//pkg/util/status:go_default_library",
"//pkg/virt-api/definitions:go_default_library",
"//pkg/virt-config:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
"//staging/src/kubevirt.io/client-go/api:go_default_library",
Expand Down
6 changes: 4 additions & 2 deletions pkg/virt-api/rest/dialers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/client-go/log"

"kubevirt.io/kubevirt/pkg/virt-api/definitions"
)

func netDialer(request *restful.Request) dialer {
Expand All @@ -22,13 +24,13 @@ func netDialer(request *restful.Request) dialer {
return nil, errors.NewBadRequest(err.Error())
}

port := request.PathParameter(PortParamName)
port := request.PathParameter(definitions.PortParamName)
if len(port) < 1 {
return nil, errors.NewBadRequest("port must not be empty")
}

protocol := "tcp"
if protocolParam := request.PathParameter(ProtocolParamName); len(protocolParam) > 0 {
if protocolParam := request.PathParameter(definitions.ProtocolParamName); len(protocolParam) > 0 {
protocol = protocolParam
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/virt-api/rest/streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/kubecli"
"kubevirt.io/client-go/log"

"kubevirt.io/kubevirt/pkg/virt-api/definitions"
)

type vmiFetcher func(namespace, name string) (*v1.VirtualMachineInstance, *errors.StatusError)
Expand Down Expand Up @@ -65,8 +67,8 @@ func NewWebsocketStreamer(fetch vmiFetcher, validate validator, dial dialer) *St
}

func (s *Streamer) Handle(request *restful.Request, response *restful.Response) error {
namespace := request.PathParameter(NamespaceParamName)
name := request.PathParameter(NameParamName)
namespace := request.PathParameter(definitions.NamespaceParamName)
name := request.PathParameter(definitions.NameParamName)

vmi, statusErr := s.fetchAndValidateVMI(namespace, name)
if statusErr != nil {
Expand Down
6 changes: 4 additions & 2 deletions pkg/virt-api/rest/streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

v1 "kubevirt.io/api/core/v1"

"kubevirt.io/kubevirt/pkg/virt-api/definitions"
)

var _ = Describe("Streamer", func() {
Expand Down Expand Up @@ -89,8 +91,8 @@ var _ = Describe("Streamer", func() {
})
It("fetches the VMI specified in the request params", func() {
params := req.PathParameters()
params[NamespaceParamName] = testNamespace
params[NameParamName] = testName
params[definitions.NamespaceParamName] = testNamespace
params[definitions.NameParamName] = testName

streamer.Handle(req, resp)
Expect(fetchVMICalled).To(BeTrue())
Expand Down
2 changes: 0 additions & 2 deletions pkg/virt-api/webhooks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ go_library(
importpath = "kubevirt.io/kubevirt/pkg/virt-api/webhooks",
visibility = ["//visibility:public"],
deps = [
"//pkg/util/openapi:go_default_library",
"//pkg/virt-api/rest:go_default_library",
"//pkg/virt-handler/node-labeller/util:go_default_library",
"//pkg/virt-operator/resource/generate/rbac:go_default_library",
"//staging/src/kubevirt.io/api/core/v1:go_default_library",
Expand Down
5 changes: 0 additions & 5 deletions pkg/virt-api/webhooks/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,10 @@ import (

v1 "kubevirt.io/api/core/v1"
clientutil "kubevirt.io/client-go/util"

"kubevirt.io/kubevirt/pkg/util/openapi"
"kubevirt.io/kubevirt/pkg/virt-api/rest"
)

var Arch = runtime.GOARCH

var Validator = openapi.CreateOpenAPIValidator(rest.ComposeAPIDefinitions())

var VirtualMachineInstanceGroupVersionResource = metav1.GroupVersionResource{
Group: v1.VirtualMachineInstanceGroupVersionKind.Group,
Version: v1.VirtualMachineInstanceGroupVersionKind.Version,
Expand Down
2 changes: 1 addition & 1 deletion tools/openapispec/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ go_library(
deps = [
"//pkg/util/openapi:go_default_library",
"//pkg/virt-api:go_default_library",
"//pkg/virt-api/rest:go_default_library",
"//pkg/virt-api/definitions:go_default_library",
"//staging/src/kubevirt.io/client-go/log:go_default_library",
"//vendor/github.com/emicklei/go-restful:go_default_library",
"//vendor/github.com/spf13/pflag:go_default_library",
Expand Down
4 changes: 2 additions & 2 deletions tools/openapispec/openapispec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

"kubevirt.io/kubevirt/pkg/util/openapi"
virt_api "kubevirt.io/kubevirt/pkg/virt-api"
"kubevirt.io/kubevirt/pkg/virt-api/rest"
"kubevirt.io/kubevirt/pkg/virt-api/definitions"
)

func dumpOpenApiSpec(dumppath *string, apiws []*restful.WebService) {
Expand All @@ -59,5 +59,5 @@ func main() {
// arguments for NewVirtAPIApp have no influence on the generated spec
app := virt_api.NewVirtApi()
app.Compose()
dumpOpenApiSpec(dumpapispecpath, rest.ComposeAPIDefinitions())
dumpOpenApiSpec(dumpapispecpath, definitions.ComposeAPIDefinitions())
}

0 comments on commit 3037f61

Please sign in to comment.