Skip to content

Commit

Permalink
Store iso size into existing volume status instead of new structure
Browse files Browse the repository at this point in the history
Signed-off-by: Jed Lejosne <[email protected]>
  • Loading branch information
jean-edouard committed Oct 4, 2021
1 parent 5e22dad commit ac60662
Show file tree
Hide file tree
Showing 19 changed files with 162 additions and 161 deletions.
13 changes: 5 additions & 8 deletions api/openapi-spec/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -13035,14 +13035,6 @@
"$ref": "#/definitions/v1.VirtualMachineInstanceNetworkInterface"
}
},
"isoSizes": {
"description": "Reprensents the size of the cloud-init isos associated with the VMI",
"type": "object",
"additionalProperties": {
"type": "integer",
"format": "int64"
}
},
"launcherContainerImageVersion": {
"description": "LauncherContainerImageVersion indicates what container image is currently active for the vmi.",
"type": "string"
Expand Down Expand Up @@ -13400,6 +13392,11 @@
"description": "Reason is a brief description of why we are in the current hotplug volume phase",
"type": "string"
},
"size": {
"description": "Represents the size of the volume",
"type": "integer",
"format": "int64"
},
"target": {
"description": "Target is the target name used when adding the volume to the VM, eg: vda",
"type": "string"
Expand Down
13 changes: 5 additions & 8 deletions pkg/cloud-init/cloud-init.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type CloudInitData struct {
UserData string
NetworkData string
DevicesData *[]DeviceData
VolumeName string
}

type PublicSSHKey struct {
Expand Down Expand Up @@ -118,6 +119,7 @@ func ReadCloudInitVolumeDataSource(vmi *v1.VirtualMachineInstance, secretSourceD

cloudInitData, err = readCloudInitNoCloudSource(volume.CloudInitNoCloud)
cloudInitData.NoCloudMetaData = readCloudInitNoCloudMetaData(vmi.Name, hostname, vmi.Namespace)
cloudInitData.VolumeName = volume.Name
return cloudInitData, err
}
if volume.CloudInitConfigDrive != nil {
Expand All @@ -129,6 +131,7 @@ func ReadCloudInitVolumeDataSource(vmi *v1.VirtualMachineInstance, secretSourceD

cloudInitData, err = readCloudInitConfigDriveSource(volume.CloudInitConfigDrive)
cloudInitData.ConfigDriveMetaData = readCloudInitConfigDriveMetaData(string(vmi.UID), vmi.Name, hostname, vmi.Namespace, keys)
cloudInitData.VolumeName = volume.Name
return cloudInitData, err
}
}
Expand Down Expand Up @@ -466,10 +469,9 @@ func removeLocalData(domain string, namespace string) error {
return err
}

func GenerateEmptyIso(vmiName string, namespace string, data *CloudInitData, sizes *v1.VirtualMachineInstanceIsoSizes) error {
func GenerateEmptyIso(vmiName string, namespace string, data *CloudInitData, size int64) error {
precond.MustNotBeEmpty(vmiName)
precond.MustNotBeNil(data)
precond.MustNotBeNil(sizes)

var err error
var isoStaging, iso string
Expand Down Expand Up @@ -498,12 +500,7 @@ func GenerateEmptyIso(vmiName string, namespace string, data *CloudInitData, siz
return fmt.Errorf("failed to create empty iso: '%s'", isoStaging)
}

isoSize, exists := (*sizes)[path.Base(iso)]
if !exists {
f.Close()
return fmt.Errorf("failed to find the size for empty iso: '%s'", iso)
}
err = f.Truncate(isoSize)
err = f.Truncate(size)
if err != nil {
f.Close()
return fmt.Errorf("failed to inflate empty iso: '%s'", isoStaging)
Expand Down
32 changes: 0 additions & 32 deletions pkg/cloud-init/cloud-init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,38 +520,6 @@ var _ = Describe("CloudInit", func() {
})
})

Describe("GenerateEmptyIso", func() {
It("should error when size is missing", func() {
namespace := "fake-namespace"
domain := "fake-domain"
userData := "fake\nuser\ndata\n"
source := &v1.CloudInitNoCloudSource{
UserDataBase64: base64.StdEncoding.EncodeToString([]byte(userData)),
}
cloudInitData, err := readCloudInitNoCloudSource(source)
Expect(err).NotTo(HaveOccurred())

var sizes v1.VirtualMachineInstanceIsoSizes = make(v1.VirtualMachineInstanceIsoSizes)
err = GenerateEmptyIso(domain, namespace, cloudInitData, &sizes)
Expect(err.Error()).To(ContainSubstring("failed to find the size for empty iso: "))
})
It("should succeed when size is present", func() {
namespace := "fake-namespace"
domain := "fake-domain"
userData := "fake\nuser\ndata\n"
source := &v1.CloudInitNoCloudSource{
UserDataBase64: base64.StdEncoding.EncodeToString([]byte(userData)),
}
cloudInitData, err := readCloudInitNoCloudSource(source)
Expect(err).NotTo(HaveOccurred())

var sizes v1.VirtualMachineInstanceIsoSizes = make(v1.VirtualMachineInstanceIsoSizes)
sizes[noCloudFile] = 42
err = GenerateEmptyIso(domain, namespace, cloudInitData, &sizes)
Expect(err).NotTo(HaveOccurred())
})
})

Describe("PrepareLocalPath", func() {
It("should create the correct directory structure", func() {
namespace := "fake-namespace"
Expand Down
8 changes: 4 additions & 4 deletions pkg/config/config-map.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func CreateConfigMapDisks(vmi *v1.VirtualMachineInstance, emptyIso bool) error {
}

disk := GetConfigMapDiskPath(volume.Name)
var vmiIsoSizes *v1.VirtualMachineInstanceIsoSizes
if emptyIso {
vmiIsoSizes = &vmi.Status.IsoSizes
vmiIsoSize, err := findIsoSize(vmi, &volume, emptyIso)
if err != nil {
return err
}
if err := createIsoConfigImage(disk, volume.ConfigMap.VolumeLabel, filesPath, vmiIsoSizes); err != nil {
if err := createIsoConfigImage(disk, volume.ConfigMap.VolumeLabel, filesPath, vmiIsoSize); err != nil {
return err
}

Expand Down
21 changes: 14 additions & 7 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"

v1 "kubevirt.io/client-go/api/v1"
Expand Down Expand Up @@ -153,19 +152,27 @@ func defaultCreateEmptyIsoImage(output string, size int64) error {
return nil
}

func createIsoConfigImage(output string, volID string, files []string, sizes *v1.VirtualMachineInstanceIsoSizes) error {
func createIsoConfigImage(output string, volID string, files []string, size int64) error {
var err error
if sizes == nil {
if size == 0 {
err = createISOImage(output, volID, files)
} else {
size, exists := (*sizes)[path.Base(output)]
if !exists {
return fmt.Errorf("no size is defined for iso '%s", output)
}
err = createEmptyISOImage(output, size)
}
if err != nil {
return err
}
return nil
}

func findIsoSize(vmi *v1.VirtualMachineInstance, volume *v1.Volume, emptyIso bool) (int64, error) {
if emptyIso {
for _, vs := range vmi.Status.VolumeStatus {
if vs.Name == volume.Name {
return vs.Size, nil
}
}
return 0, fmt.Errorf("failed to find the status of volume %s", volume.Name)
}
return 0, nil
}
2 changes: 1 addition & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var _ = Describe("Creating config images", func() {

It("Should create an iso image", func() {
imgPath := filepath.Join(tempISODir, "volume1.iso")
err := createIsoConfigImage(imgPath, "", expectedLayout, nil)
err := createIsoConfigImage(imgPath, "", expectedLayout, 0)
Expect(err).NotTo(HaveOccurred())
_, err = os.Stat(imgPath)
Expect(err).NotTo(HaveOccurred())
Expand Down
8 changes: 4 additions & 4 deletions pkg/config/downwardapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func CreateDownwardAPIDisks(vmi *v1.VirtualMachineInstance, emptyIso bool) error
}

disk := GetDownwardAPIDiskPath(volume.Name)
var vmiIsoSizes *v1.VirtualMachineInstanceIsoSizes
if emptyIso {
vmiIsoSizes = &vmi.Status.IsoSizes
vmiIsoSize, err := findIsoSize(vmi, &volume, emptyIso)
if err != nil {
return err
}
if err := createIsoConfigImage(disk, volume.DownwardAPI.VolumeLabel, filesPath, vmiIsoSizes); err != nil {
if err := createIsoConfigImage(disk, volume.DownwardAPI.VolumeLabel, filesPath, vmiIsoSize); err != nil {
return err
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/config/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ func CreateSecretDisks(vmi *v1.VirtualMachineInstance, emptyIso bool) error {
}

disk := GetSecretDiskPath(volume.Name)
var vmiIsoSizes *v1.VirtualMachineInstanceIsoSizes
if emptyIso {
vmiIsoSizes = &vmi.Status.IsoSizes
vmiIsoSize, err := findIsoSize(vmi, &volume, emptyIso)
if err != nil {
return err
}
if err := createIsoConfigImage(disk, volume.Secret.VolumeLabel, filesPath, vmiIsoSizes); err != nil {
if err := createIsoConfigImage(disk, volume.Secret.VolumeLabel, filesPath, vmiIsoSize); err != nil {
return err
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/config/service-account.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ func CreateServiceAccountDisk(vmi *v1.VirtualMachineInstance, emptyIso bool) err
}

disk := GetServiceAccountDiskPath()
var vmiIsoSizes *v1.VirtualMachineInstanceIsoSizes
if emptyIso {
vmiIsoSizes = &vmi.Status.IsoSizes
vmiIsoSize, err := findIsoSize(vmi, &volume, emptyIso)
if err != nil {
return err
}
if err := createIsoConfigImage(disk, "", filesPath, vmiIsoSizes); err != nil {
if err := createIsoConfigImage(disk, "", filesPath, vmiIsoSize); err != nil {
return err
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/config/sysprep.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ func CreateSysprepDisks(vmi *v1.VirtualMachineInstance, emptyIso bool) error {
if !shouldCreateSysprepDisk(volume.Sysprep) {
continue
}
var vmiIsoSizes *v1.VirtualMachineInstanceIsoSizes
if emptyIso {
vmiIsoSizes = &vmi.Status.IsoSizes
vmiIsoSize, err := findIsoSize(vmi, &volume, emptyIso)
if err != nil {
return err
}
if err := createSysprepDisk(volume.Name, vmiIsoSizes); err != nil {
if err := createSysprepDisk(volume.Name, vmiIsoSize); err != nil {
return err
}
}
Expand All @@ -84,7 +84,7 @@ func shouldCreateSysprepDisk(volumeSysprep *v1.SysprepSource) bool {
return volumeSysprep != nil && sysprepVolumeHasContents(volumeSysprep)
}

func createSysprepDisk(volumeName string, sizes *v1.VirtualMachineInstanceIsoSizes) error {
func createSysprepDisk(volumeName string, size int64) error {
sysprepSourcePath := GetSysprepSourcePath(volumeName)
if err := validateAutounattendPresence(sysprepSourcePath); err != nil {
return err
Expand All @@ -94,12 +94,12 @@ func createSysprepDisk(volumeName string, sizes *v1.VirtualMachineInstanceIsoSiz
return err
}

return createIsoImageAndSetFileOwnership(volumeName, filesPath, sizes)
return createIsoImageAndSetFileOwnership(volumeName, filesPath, size)
}

func createIsoImageAndSetFileOwnership(volumeName string, filesPath []string, sizes *v1.VirtualMachineInstanceIsoSizes) error {
func createIsoImageAndSetFileOwnership(volumeName string, filesPath []string, size int64) error {
disk := GetSysprepDiskPath(volumeName)
if err := createIsoConfigImage(disk, sysprepVolumeLabel, filesPath, sizes); err != nil {
if err := createIsoConfigImage(disk, sysprepVolumeLabel, filesPath, size); err != nil {
return err
}
if err := ephemeraldiskutils.DefaultOwnershipManager.SetFileOwnership(disk); err != nil {
Expand Down
8 changes: 5 additions & 3 deletions pkg/virt-handler/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1198,10 +1198,12 @@ func (d *VirtualMachineController) updateIsoSizeStatus(vmi *v1.VirtualMachineIns
if err != nil {
continue
}
if vmi.Status.IsoSizes == nil {
vmi.Status.IsoSizes = make(v1.VirtualMachineInstanceIsoSizes)
for _, vs := range vmi.Status.VolumeStatus {
if vs.Name == volume.Name {
vs.Size = stats.Size()
continue
}
}
vmi.Status.IsoSizes[path.Base(volPath)] = stats.Size()
}
}

Expand Down
18 changes: 13 additions & 5 deletions pkg/virt-launcher/virtwrap/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ func (l *LibvirtDomainManager) MigrateVMI(vmi *v1.VirtualMachineInstance, option
return l.startMigration(vmi, options)
}

func (l *LibvirtDomainManager) generateSomeCloudInitISO(vmi *v1.VirtualMachineInstance, domPtr *cli.VirDomain, sizes *v1.VirtualMachineInstanceIsoSizes) error {
func (l *LibvirtDomainManager) generateSomeCloudInitISO(vmi *v1.VirtualMachineInstance, domPtr *cli.VirDomain, size int64) error {
var devicesMetadata []cloudinit.DeviceData
// this is the point where we need to build the devices metadata if it was requested.
// This metadata maps the user provided tag to the hypervisor assigned device address.
Expand All @@ -410,8 +410,8 @@ func (l *LibvirtDomainManager) generateSomeCloudInitISO(vmi *v1.VirtualMachineIn
cloudInitDataStore.DevicesData = &devicesMetadata
}
var err error
if sizes != nil {
err = cloudinit.GenerateEmptyIso(vmi.Name, vmi.Namespace, cloudInitDataStore, sizes)
if size != 0 {
err = cloudinit.GenerateEmptyIso(vmi.Name, vmi.Namespace, cloudInitDataStore, size)
} else {
err = cloudinit.GenerateLocalData(vmi.Name, vmi.Namespace, cloudInitDataStore)
}
Expand All @@ -423,11 +423,19 @@ func (l *LibvirtDomainManager) generateSomeCloudInitISO(vmi *v1.VirtualMachineIn
}

func (l *LibvirtDomainManager) generateCloudInitISO(vmi *v1.VirtualMachineInstance, domPtr *cli.VirDomain) error {
return l.generateSomeCloudInitISO(vmi, domPtr, nil)
return l.generateSomeCloudInitISO(vmi, domPtr, 0)
}

func (l *LibvirtDomainManager) generateCloudInitEmptyISO(vmi *v1.VirtualMachineInstance, domPtr *cli.VirDomain) error {
return l.generateSomeCloudInitISO(vmi, domPtr, &vmi.Status.IsoSizes)
if l.cloudInitDataStore == nil {
return nil
}
for _, vs := range vmi.Status.VolumeStatus {
if vs.Name == l.cloudInitDataStore.VolumeName {
return l.generateSomeCloudInitISO(vmi, domPtr, vs.Size)
}
}
return fmt.Errorf("failed to find the status of volume %s", l.cloudInitDataStore.VolumeName)
}

// All local environment setup that needs to occur before VirtualMachineInstance starts
Expand Down
Loading

0 comments on commit ac60662

Please sign in to comment.