diff --git a/cluster/deploy.sh b/cluster/deploy.sh index e166c11e2bba..49e75e72088c 100755 --- a/cluster/deploy.sh +++ b/cluster/deploy.sh @@ -26,7 +26,7 @@ externalServiceManifests() source hack/config.sh # Pretty much equivalent to `kubectl expose service ...` - for SVC in spice-proxy:3128 virt-api:8182 haproxy:8184 virt-manifest:8186; + for SVC in spice-proxy:3128 virt-api:8182 haproxy:8184; do IFS=: read NAME PORT <<<$SVC cat < - -RUN dnf -y install libvirt-client \ - && dnf -y clean all - -COPY virt-manifest /virt-manifest - -ENTRYPOINT [ "/virt-manifest" ] diff --git a/cmd/virt-manifest/virt-manifest.go b/cmd/virt-manifest/virt-manifest.go deleted file mode 100644 index 543ed813e789..000000000000 --- a/cmd/virt-manifest/virt-manifest.go +++ /dev/null @@ -1,98 +0,0 @@ -/* - * 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 2017 Red Hat, Inc. - * - */ - -package main - -import ( - "fmt" - "net/http" - "time" - - "github.com/emicklei/go-restful" - - "kubevirt.io/kubevirt/pkg/log" - "kubevirt.io/kubevirt/pkg/service" - "kubevirt.io/kubevirt/pkg/virt-handler/virtwrap/cli" - "kubevirt.io/kubevirt/pkg/virt-manifest/rest" -) - -const ( - // Default port that virt-manifest listens on. - defaultPort = 8186 - - // Default address that virt-manifest listens on. - defaultHost = "0.0.0.0" - - libvirtUri = "qemu:///system" -) - -type virtManifestApp struct { - service.ServiceListen - service.ServiceLibvirt - LibvirtUri string -} - -var _ service.Service = &virtManifestApp{} - -func (app *virtManifestApp) Run() { - logger := log.Log - logger.Info("Starting virt-manifest server") - - logger.Info("Connecting to libvirt") - - domainConn, err := cli.NewConnection(app.LibvirtUri, "", "", 60*time.Second) - if err != nil { - logger.Reason(err).Error("cannot connect to libvirt") - panic(fmt.Sprintf("failed to connect to libvirt: %v", err)) - } - defer domainConn.Close() - - logger.Info("Connected to libvirt") - - ws, err := rest.ManifestService(domainConn) - if err != nil { - logger.Reason(err).Error("Unable to create REST server.") - } - - restful.DefaultContainer.Add(ws) - server := &http.Server{Addr: app.Address(), Handler: restful.DefaultContainer} - logger.Info("Listening for client connections") - - if err := server.ListenAndServe(); err != nil { - logger.Reason(err).Error("Unable to start web server.") - } -} - -func (app *virtManifestApp) AddFlags() { - app.InitFlags() - - app.BindAddress = defaultHost - app.Port = defaultPort - app.LibvirtUri = libvirtUri - - app.AddCommonFlags() - app.AddLibvirtFlags() -} - -func main() { - log.InitializeLogging("virt-manifest") - app := virtManifestApp{} - service.Setup(&app) - app.Run() -} diff --git a/hack/config-default.sh b/hack/config-default.sh index 308e0c5f28c4..e75c5c92bbe1 100644 --- a/hack/config-default.sh +++ b/hack/config-default.sh @@ -1,5 +1,5 @@ -binaries="cmd/virt-controller cmd/virt-launcher cmd/virt-handler cmd/virt-api cmd/virtctl cmd/virt-manifest cmd/fake-qemu-process cmd/virt-dhcp cmd/fake-dnsmasq-process" -docker_images="cmd/virt-controller cmd/virt-launcher cmd/virt-handler cmd/virt-api cmd/virt-manifest images/haproxy images/iscsi-demo-target-tgtd images/vm-killer images/libvirt-kubevirt images/spice-proxy cmd/virt-migrator cmd/registry-disk-v1alpha images/cirros-registry-disk-demo cmd/virt-dhcp" +binaries="cmd/virt-controller cmd/virt-launcher cmd/virt-handler cmd/virt-api cmd/virtctl cmd/fake-qemu-process cmd/virt-dhcp cmd/fake-dnsmasq-process" +docker_images="cmd/virt-controller cmd/virt-launcher cmd/virt-handler cmd/virt-api images/haproxy images/iscsi-demo-target-tgtd images/vm-killer images/libvirt-kubevirt images/spice-proxy cmd/virt-migrator cmd/registry-disk-v1alpha images/cirros-registry-disk-demo cmd/virt-dhcp" optional_docker_images="cmd/registry-disk-v1alpha images/fedora-atomic-registry-disk-demo" docker_prefix=kubevirt docker_tag=${DOCKER_TAG:-latest} diff --git a/manifests/virt-manifest.yaml.in b/manifests/virt-manifest.yaml.in deleted file mode 100644 index b0e4ef072c04..000000000000 --- a/manifests/virt-manifest.yaml.in +++ /dev/null @@ -1,51 +0,0 @@ -apiVersion: extensions/v1beta1 -kind: Deployment -metadata: - name: virt-manifest - namespace: kube-system -spec: - template: - metadata: - labels: - app: virt-manifest - spec: - containers: - - name: virt-manifest - image: {{ docker_prefix }}/virt-manifest:{{ docker_tag }} - imagePullPolicy: IfNotPresent - ports: - - containerPort: 8186 - name: "virt-manifest" - protocol: "TCP" - volumeMounts: - - name: libvirt-runtime - mountPath: /var/run/libvirt - command: - - "/virt-manifest" - - "--port" - - "8186" - livenessProbe: - httpGet: - path: /api/v1/status - port: 8186 - initialDelaySeconds: 10 - periodSeconds: 3 - readinessProbe: - httpGet: - path: /api/v1/status - port: 8186 - initialDelaySeconds: 10 - periodSeconds: 3 - - name: manifest-libvirtd - image: {{ docker_prefix }}/libvirt-kubevirt:{{ docker_tag }} - imagePullPolicy: IfNotPresent - securityContext: - privileged: true - runAsUser: 0 - volumeMounts: - - name: libvirt-runtime - mountPath: /var/run/libvirt - command: ["/libvirtd-limited.sh"] - volumes: - - name: libvirt-runtime - emptyDir: {} diff --git a/pkg/virt-controller/watch/application.go b/pkg/virt-controller/watch/application.go index 1c512096a022..f222df694f48 100644 --- a/pkg/virt-controller/watch/application.go +++ b/pkg/virt-controller/watch/application.go @@ -28,10 +28,8 @@ import ( ) const ( - // Default port that virt-manifest listens on. defaultPort = 8182 - // Default address that virt-manifest listens on. defaultHost = "0.0.0.0" launcherImage = "virt-launcher" diff --git a/pkg/virt-manifest/defaults.go b/pkg/virt-manifest/defaults.go deleted file mode 100644 index 1f6f473c6566..000000000000 --- a/pkg/virt-manifest/defaults.go +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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 2017 Red Hat, Inc. - * - */ - -package virt_manifest - -import ( - "kubevirt.io/kubevirt/pkg/api/v1" -) - -func AddMinimalVMSpec(vm *v1.VirtualMachine) { - // Make sure the domain name matches the VM name - if vm.Spec.Domain == nil { - vm.Spec.Domain = new(v1.DomainSpec) - } - - AddMinimalDomainSpec(vm.Spec.Domain) -} - -func AddMinimalDomainSpec(dom *v1.DomainSpec) { - for idx, graphics := range dom.Devices.Graphics { - if graphics.Type == "spice" { - if graphics.Listen.Type == "" { - dom.Devices.Graphics[idx].Listen.Type = "address" - } - if ((graphics.Listen.Type == "address") || - (graphics.Listen.Type == "")) && - (graphics.Listen.Address == "") { - dom.Devices.Graphics[idx].Listen.Address = "0.0.0.0" - } - } - } -} diff --git a/pkg/virt-manifest/defaults_test.go b/pkg/virt-manifest/defaults_test.go deleted file mode 100644 index 904ed2691525..000000000000 --- a/pkg/virt-manifest/defaults_test.go +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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 2017 Red Hat, Inc. - * - */ - -package virt_manifest - -import ( - "testing" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - "kubevirt.io/kubevirt/pkg/api/v1" - "kubevirt.io/kubevirt/tests" -) - -var _ = Describe("Defaults", func() { - Context("Default Domains", func() { - var dom *v1.DomainSpec - BeforeEach(func() { - dom = new(v1.DomainSpec) - }) - - It("Shouldn't add graphics devices", func() { - AddMinimalDomainSpec(dom) - Expect(len(dom.Devices.Graphics)).To(Equal(0)) - }) - - It("Should ensure spice devices have proper defaults", func() { - dom.Devices.Graphics = []v1.Graphics{v1.Graphics{Type: "spice"}} - // Ensure there's no listen address info - Expect(dom.Devices.Graphics[0].Listen.Type).To(Equal("")) - Expect(dom.Devices.Graphics[0].Listen.Address).To(Equal("")) - - AddMinimalDomainSpec(dom) - // Ensure the listen address has been added - Expect(dom.Devices.Graphics[0].Type).To(Equal("spice")) - Expect(dom.Devices.Graphics[0].Listen.Type).To(Equal("address")) - Expect(dom.Devices.Graphics[0].Listen.Address).To(Equal("0.0.0.0")) - }) - - It("Should ensure spice devices have proper defaults", func() { - dom.Devices.Graphics = []v1.Graphics{v1.Graphics{Type: "spice", - Listen: v1.Listen{Type: "address"}}} - // Ensure there's no listen address info - Expect(dom.Devices.Graphics[0].Listen.Type).To(Equal("address")) - Expect(dom.Devices.Graphics[0].Listen.Address).To(Equal("")) - - AddMinimalDomainSpec(dom) - // Ensure the listen address has been added - Expect(dom.Devices.Graphics[0].Type).To(Equal("spice")) - Expect(dom.Devices.Graphics[0].Listen.Type).To(Equal("address")) - Expect(dom.Devices.Graphics[0].Listen.Address).To(Equal("0.0.0.0")) - }) - }) - - Context("Default VMs", func() { - var vm *v1.VirtualMachine - BeforeEach(func() { - vm = tests.NewRandomVM() - }) - - It("should assign a random name to domain spec", func() { - vm.Spec.Domain = nil - AddMinimalVMSpec(vm) - Expect(vm.GetObjectMeta().GetName()).ToNot(BeNil()) - }) - - It("should create missing domain", func() { - vm.Spec.Domain = nil - AddMinimalVMSpec(vm) - - Expect(vm.Spec.Domain).ToNot(BeNil()) - }) - - It("should not modify an existing domain", func() { - vm.Spec.Domain = &v1.DomainSpec{Type: "invalid"} - - AddMinimalVMSpec(vm) - Expect(vm.Spec.Domain.Type).To(Equal("invalid")) - }) - }) -}) - -func TestDefaults(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "Defaults") -} diff --git a/pkg/virt-manifest/mapper.go b/pkg/virt-manifest/mapper.go deleted file mode 100644 index ed75da87b866..000000000000 --- a/pkg/virt-manifest/mapper.go +++ /dev/null @@ -1,131 +0,0 @@ -/* - * 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 2017 Red Hat, Inc. - * - */ - -package virt_manifest - -import ( - "encoding/xml" - - "github.com/jeevatkm/go-model" - "github.com/libvirt/libvirt-go" - "k8s.io/apimachinery/pkg/util/errors" - - "kubevirt.io/kubevirt/pkg/api/v1" - "kubevirt.io/kubevirt/pkg/log" - "kubevirt.io/kubevirt/pkg/virt-handler/virtwrap/api" - "kubevirt.io/kubevirt/pkg/virt-handler/virtwrap/cli" -) - -const ( - Type_PersistentVolumeClaim = "PersistentVolumeClaim" - Type_Network = "network" -) - -type savedDisk struct { - idx int - disk v1.Disk -} - -func ExtractPvc(dom *v1.DomainSpec) (*v1.DomainSpec, []savedDisk) { - specCopy := &v1.DomainSpec{} - model.Copy(specCopy, dom) - - pvcDisks := []savedDisk{} - allDisks := []v1.Disk{} - - for idx, disk := range specCopy.Devices.Disks { - if disk.Type == Type_PersistentVolumeClaim { - // Save the disk so we can fix it later - diskCopy := v1.Disk{} - model.Copy(&diskCopy, disk) - pvcDisks = append(pvcDisks, savedDisk{disk: diskCopy, idx: idx}) - - // Alter the disk record so that libvirt will accept it - disk.Type = Type_Network - disk.Source.Protocol = "iscsi" - } - allDisks = append(allDisks, disk) - } - // Replace the Domain's disks with modified records - specCopy.Devices.Disks = allDisks - - return specCopy, pvcDisks -} - -// This is a simplified version of the domain creation portion of SyncVM. This is intended primarily -// for mapping the VM spec without starting a domain. -func MapVM(con cli.Connection, vm *v1.VirtualMachine) (*v1.VirtualMachine, error) { - logger := log.Log - - vmCopy := &v1.VirtualMachine{} - model.Copy(vmCopy, vm) - - specCopy, pvcs := ExtractPvc(vm.Spec.Domain) - - var wantedSpec api.DomainSpec - mappingErrs := model.Copy(&wantedSpec, specCopy) - - if len(mappingErrs) > 0 { - return nil, errors.NewAggregate(mappingErrs) - } - - wantedSpec.Name = vmCopy.GetObjectMeta().GetName() - wantedSpec.UUID = string(vmCopy.GetObjectMeta().GetUID()) - xmlStr, err := xml.Marshal(&wantedSpec) - if err != nil { - logger.Object(vm).Reason(err).Error("Generating the domain XML failed.") - return nil, err - } - - logger.Object(vm).V(3).Info("Domain XML generated.") - dom, err := con.DomainDefineXML(string(xmlStr)) - if err != nil { - logger.Object(vm).Reason(err).Error("Defining the VM failed.") - return nil, err - } - logger.Object(vm).Info("Domain defined.") - - defer func() { - err = dom.Undefine() - if err != nil { - logger.Object(vm).Reason(err).Warning("Undefining the domain failed.") - } else { - logger.Object(vm).Info("Domain defined.") - } - }() - - domXml, err := dom.GetXMLDesc(libvirt.DOMAIN_XML_MIGRATABLE) - if err != nil { - logger.Object(vm).Reason(err).Error("Error retrieving domain XML.") - return nil, err - } - - // api.DomainSpec has xml struct tags. - mappedDom := api.DomainSpec{} - xml.Unmarshal([]byte(domXml), &mappedDom) - model.Copy(vmCopy.Spec.Domain, mappedDom) - - // Re-add the PersistentVolumeClaims that were stripped earlier - for _, pvc := range pvcs { - vmCopy.Spec.Domain.Devices.Disks[pvc.idx].Type = Type_PersistentVolumeClaim - vmCopy.Spec.Domain.Devices.Disks[pvc.idx].Source.Protocol = pvc.disk.Source.Protocol - } - - return vmCopy, nil -} diff --git a/pkg/virt-manifest/mapper_test.go b/pkg/virt-manifest/mapper_test.go deleted file mode 100644 index cd29c9b62257..000000000000 --- a/pkg/virt-manifest/mapper_test.go +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 2017 Red Hat, Inc. - * - */ - -package virt_manifest - -import ( - "testing" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - "kubevirt.io/kubevirt/pkg/api/v1" -) - -var _ = Describe("Mapper", func() { - Context("Map PersistentVolumeClaims", func() { - var dom *v1.DomainSpec - BeforeEach(func() { - dom = new(v1.DomainSpec) - dom.Devices.Disks = []v1.Disk{ - v1.Disk{Type: Type_Network, - Source: v1.DiskSource{Name: "network"}}, - v1.Disk{Type: Type_PersistentVolumeClaim, - Source: v1.DiskSource{Name: "pvc"}}, - } - }) - - It("Should extract PVCs from disks", func() { - domCopy, pvcs := ExtractPvc(dom) - Expect(len(domCopy.Devices.Disks)).To(Equal(2)) - Expect(domCopy.Devices.Disks[0].Type).To(Equal(Type_Network)) - - Expect(len(pvcs)).To(Equal(1)) - Expect(pvcs[0].disk.Type).To(Equal(Type_PersistentVolumeClaim)) - - }) - }) - -}) - -func TestMapper(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "Mapper") -} diff --git a/pkg/virt-manifest/rest/manifest.go b/pkg/virt-manifest/rest/manifest.go deleted file mode 100644 index c6b3b4d7315a..000000000000 --- a/pkg/virt-manifest/rest/manifest.go +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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 2017 Red Hat, Inc. - * - */ - -package rest - -import ( - "net/http" - - "github.com/emicklei/go-restful" - gokithttp "github.com/go-kit/kit/transport/http" - "golang.org/x/net/context" - - "kubevirt.io/kubevirt/pkg/api/v1" - "kubevirt.io/kubevirt/pkg/middleware" - "kubevirt.io/kubevirt/pkg/rest" - "kubevirt.io/kubevirt/pkg/rest/endpoints" - "kubevirt.io/kubevirt/pkg/virt-handler/virtwrap/cli" - "kubevirt.io/kubevirt/pkg/virt-manifest" -) - -type manifest struct { - connection cli.Connection -} - -// TODO: this should be a generalized decoder in rest/endpoints -func NewNonNamespacedJsonPostDecodeRequestFunc(payloadTypePtr interface{}) gokithttp.DecodeRequestFunc { - jsonDecodeRequestFunc := endpoints.NewMimeTypeAwareDecodeRequestFunc( - endpoints.NewJsonDecodeRequestFunc(payloadTypePtr), - map[string]gokithttp.DecodeRequestFunc{ - rest.MIME_JSON: endpoints.NewJsonDecodeRequestFunc(payloadTypePtr), - rest.MIME_YAML: endpoints.NewYamlDecodeRequestFunc(payloadTypePtr), - }, - ) - return func(ctx context.Context, r *http.Request) (interface{}, error) { - payload, err := jsonDecodeRequestFunc(ctx, r) - if err != nil { - return nil, err - } - return &endpoints.PutObject{Payload: payload}, nil - } -} - -func ManifestService(connection cli.Connection) (*restful.WebService, error) { - ws := new(restful.WebService) - - m := manifest{ - connection: connection, - } - - ctx := context.Background() - aliveEndpoint := endpoints.MakeGoRestfulWrapper(endpoints.NewHandlerBuilder(). - Get(). - Decoder(endpoints.NoopDecoder). - Endpoint(alive). - Build(ctx)) - - ctx = context.Background() - mapManifestEndpoint := endpoints.MakeGoRestfulWrapper(endpoints.NewHandlerBuilder(). - Post(&v1.VirtualMachine{}). - Decoder(NewNonNamespacedJsonPostDecodeRequestFunc(&v1.VirtualMachine{})). - Endpoint(m.mapManifest). - Build(ctx)) - - ws.Path("/"). - Consumes(restful.MIME_JSON, rest.MIME_YAML). - Produces(restful.MIME_JSON, rest.MIME_YAML) - ws.Route(ws.GET("/api/v1/status").To(aliveEndpoint)) - ws.Route(ws.POST("/apis/" + v1.GroupVersion.String() + "/manifest").To(mapManifestEndpoint)) - return ws, nil -} - -func alive(_ context.Context, request interface{}) (interface{}, error) { - return map[string]interface{}{"status": "ok"}, nil -} - -func (m manifest) mapManifest(_ context.Context, request interface{}) (interface{}, error) { - vm := request.(*endpoints.PutObject).Payload.(*v1.VirtualMachine) - - virt_manifest.AddMinimalVMSpec(vm) - - mappedVm, err := virt_manifest.MapVM(m.connection, vm) - if err != nil { - return nil, middleware.NewInternalServerError(err) - } else { - return mappedVm, nil - } -} diff --git a/pkg/virt-manifest/virt_manifest_suite_test.go b/pkg/virt-manifest/virt_manifest_suite_test.go deleted file mode 100644 index 7b73a05e0c72..000000000000 --- a/pkg/virt-manifest/virt_manifest_suite_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package virt_manifest_test - -import ( - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - "testing" -) - -func TestVirtManifest(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "VirtManifest Suite") -} diff --git a/tests/virtmanifest_test.go b/tests/virtmanifest_test.go deleted file mode 100644 index 8141ea46534c..000000000000 --- a/tests/virtmanifest_test.go +++ /dev/null @@ -1,110 +0,0 @@ -/* - * 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 2017 Red Hat, Inc. - * - */ - -package tests_test - -import ( - "flag" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - "k8s.io/apimachinery/pkg/util/json" - "k8s.io/client-go/rest" - - "kubevirt.io/kubevirt/pkg/api/v1" - "kubevirt.io/kubevirt/pkg/kubecli" - "kubevirt.io/kubevirt/pkg/virt-manifest" - "kubevirt.io/kubevirt/tests" -) - -var _ = Describe("Virtmanifest", func() { - Context("Manifest Service", func() { - flag.Parse() - - var manifestClient *rest.RESTClient - var vm *v1.VirtualMachine - - BeforeEach(func() { - tests.BeforeTestCleanup() - - virtClient, err := kubecli.GetKubevirtClient() - Expect(err).ToNot(HaveOccurred()) - - manifestClient = virtClient.RestClient() - Expect(err).ToNot(HaveOccurred()) - - vm = tests.NewRandomVM() - }) - - It("Should report server status", func() { - ref := map[string]string{"status": "ok"} - data := map[string]string{} - - res, err := manifestClient.Get().RequestURI("/api/v1/status").DoRaw() - Expect(err).ToNot(HaveOccurred()) - - err = json.Unmarshal(res, &data) - Expect(err).ToNot(HaveOccurred()) - Expect(data).To(Equal(ref)) - }) - - It("Should return YAML if requested", func() { - ref := "status: ok\n" - res, err := manifestClient.Get().RequestURI("/api/v1/status").SetHeader("Accept", "application/yaml").DoRaw() - Expect(err).ToNot(HaveOccurred()) - - Expect(string(res)).To(Equal(ref)) - }) - - It("Should map a VM manifest", func() { - vmName := vm.ObjectMeta.Name - mappedVm := v1.VirtualMachine{} - - request, err := json.Marshal(vm) - Expect(err).ToNot(HaveOccurred()) - - res, err := manifestClient.Post().SetHeader("Content-type", "application/json").Resource("manifest").Body(request).DoRaw() - Expect(err).ToNot(HaveOccurred()) - - err = json.Unmarshal(res, &mappedVm) - Expect(mappedVm.ObjectMeta.Name).To(Equal(vmName)) - Expect(mappedVm.Spec.Domain.Type).To(Equal("qemu")) - }) - - It("Should map PersistentVolumeClaims", func() { - mappedVm := v1.VirtualMachine{} - vm.Spec.Domain.Devices.Disks = []v1.Disk{v1.Disk{ - Device: "disk", - Type: virt_manifest.Type_PersistentVolumeClaim, - Source: v1.DiskSource{Name: "test"}, - Target: v1.DiskTarget{Bus: "scsi", Device: "vda"}, - }} - - request, err := json.Marshal(vm) - Expect(err).ToNot(HaveOccurred()) - - res, err := manifestClient.Post().SetHeader("Content-type", "application/json").Resource("manifest").Body(request).DoRaw() - Expect(err).ToNot(HaveOccurred()) - - err = json.Unmarshal(res, &mappedVm) - Expect(len(mappedVm.Spec.Domain.Devices.Disks)).To(Equal(1)) - Expect(mappedVm.Spec.Domain.Devices.Disks[0].Type).To(Equal(virt_manifest.Type_PersistentVolumeClaim)) - }) - }) -})