From 2cf20fb025bf3beb7cc57dcde7b9897abedf114f Mon Sep 17 00:00:00 2001 From: Sunny Date: Sun, 26 Jul 2020 22:43:42 +0530 Subject: [PATCH] Error check and return - cleanup 2 --- cmd/ignite/run/create.go | 8 ++++---- cmd/ignite/run/import.go | 8 ++++---- cmd/ignite/run/ssh.go | 6 +++--- pkg/container/firecracker.go | 2 +- pkg/dmlegacy/image_format.go | 12 ++++++------ pkg/dmlegacy/snapshot.go | 24 ++++++++++++------------ pkg/dmlegacy/vm.go | 18 +++++++++--------- 7 files changed, 39 insertions(+), 39 deletions(-) diff --git a/cmd/ignite/run/create.go b/cmd/ignite/run/create.go index cd149a7d0..7703dfed4 100644 --- a/cmd/ignite/run/create.go +++ b/cmd/ignite/run/create.go @@ -218,21 +218,21 @@ func applyVMFlagOverrides(baseVM *api.VM, cf *CreateFlags, fs *flag.FlagSet) err func Create(co *createOptions) (err error) { // Generate a random UID and Name if err = metadata.SetNameAndUID(co.VM, providers.Client); err != nil { - return err + return } // Set VM labels. if err = metadata.SetLabels(co.VM, co.Labels); err != nil { - return err + return } defer util.DeferErr(&err, func() error { return metadata.Cleanup(co.VM, false) }) if err = providers.Client.VMs().Set(co.VM); err != nil { - return err + return } // Allocate and populate the overlay file if err = dmlegacy.AllocateAndPopulateOverlay(co.VM); err != nil { - return err + return } err = metadata.Success(co.VM) diff --git a/cmd/ignite/run/import.go b/cmd/ignite/run/import.go index 43c0f0d1d..881442f13 100644 --- a/cmd/ignite/run/import.go +++ b/cmd/ignite/run/import.go @@ -12,12 +12,12 @@ import ( func ImportImage(source string) (image *api.Image, err error) { ociRef, err := meta.NewOCIImageRef(source) if err != nil { - return nil, err + return } image, err = operations.FindOrImportImage(providers.Client, ociRef) if err != nil { - return nil, err + return } defer util.DeferErr(&err, func() error { return metadata.Cleanup(image, false) }) @@ -29,12 +29,12 @@ func ImportImage(source string) (image *api.Image, err error) { func ImportKernel(source string) (kernel *api.Kernel, err error) { ociRef, err := meta.NewOCIImageRef(source) if err != nil { - return nil, err + return } kernel, err = operations.FindOrImportKernel(providers.Client, ociRef) if err != nil { - return nil, err + return } defer util.DeferErr(&err, func() error { return metadata.Cleanup(kernel, false) }) diff --git a/cmd/ignite/run/ssh.go b/cmd/ignite/run/ssh.go index 6eb52d42f..e222e4454 100644 --- a/cmd/ignite/run/ssh.go +++ b/cmd/ignite/run/ssh.go @@ -104,14 +104,14 @@ func runSSH(vm *api.VM, privKeyFile string, command []string, tty bool, timeout if err != nil { return printErrAndSetExitCode(fmt.Errorf("failed to dial: %v", err), &exitCode, 1) } - defer client.Close() + defer util.DeferErr(&err, client.Close) // Create a session. session, err := client.NewSession() if err != nil { return printErrAndSetExitCode(fmt.Errorf("failed to create session: %v", err), &exitCode, 1) } - defer session.Close() + defer util.DeferErr(&err, session.Close) // Configure tty if requested. if tty { @@ -173,7 +173,7 @@ func runSSH(vm *api.VM, privKeyFile string, command []string, tty bool, timeout return printErrAndSetExitCode(fmt.Errorf("failed to run shell command: %s", err), &exitCode, 1) } } - return err + return } func newSignerForKey(keyPath string) (ssh.Signer, error) { diff --git a/pkg/container/firecracker.go b/pkg/container/firecracker.go index b29eee032..4f61ecd19 100644 --- a/pkg/container/firecracker.go +++ b/pkg/container/firecracker.go @@ -141,7 +141,7 @@ func ExecuteFirecracker(vm *api.VM, dhcpIfaces []DHCPInterface) (err error) { return fmt.Errorf("wait returned an error %s", err) } - return err + return } // Install custom signal handlers: diff --git a/pkg/dmlegacy/image_format.go b/pkg/dmlegacy/image_format.go index 4e8614e85..74dad2197 100644 --- a/pkg/dmlegacy/image_format.go +++ b/pkg/dmlegacy/image_format.go @@ -75,7 +75,7 @@ func addFiles(img *api.Image, src source.Source) (err error) { p := path.Join(img.ObjectPath(), constants.IMAGE_FS) tempDir, err := ioutil.TempDir("", "") if err != nil { - return err + return } defer os.RemoveAll(tempDir) @@ -90,22 +90,22 @@ func addFiles(img *api.Image, src source.Source) (err error) { tarCmd := exec.Command("tar", "-x", "-C", tempDir) reader, err := src.Reader() if err != nil { - return err + return } tarCmd.Stdin = reader if err = tarCmd.Start(); err != nil { - return err + return } if err = tarCmd.Wait(); err != nil { - return err + return } if err = src.Cleanup(); err != nil { // Ignore the cleanup error if the resource no longer exists. if !errors.Is(err, containerderr.ErrNotFound) { - return err + return } } @@ -149,7 +149,7 @@ func resizeToMinimum(img *api.Image) (err error) { } if imageFile, err = os.OpenFile(p, os.O_RDWR, constants.DATA_DIR_FILE_PERM); err != nil { - return err + return } defer util.DeferErr(&err, imageFile.Close) diff --git a/pkg/dmlegacy/snapshot.go b/pkg/dmlegacy/snapshot.go index 06a9d8498..86c39fa52 100644 --- a/pkg/dmlegacy/snapshot.go +++ b/pkg/dmlegacy/snapshot.go @@ -24,13 +24,13 @@ func ActivateSnapshot(vm *api.VM) (err error) { // Return if the snapshot is already setup if util.FileExists(devicePath) { - return nil + return } // Get the image UID from the VM imageUID, err := lookup.ImageUIDForVM(vm, providers.Client) if err != nil { - return err + return } // NOTE: Multiple ignite processes trying to create loop devices at the @@ -50,7 +50,7 @@ func ActivateSnapshot(vm *api.VM) (err error) { return fmt.Errorf("failed to create lock: %v", err) } if err = obtainLock(lock); err != nil { - return err + return } // Release the lock at the end. defer util.DeferErr(&err, lock.Unlock) @@ -58,28 +58,28 @@ func ActivateSnapshot(vm *api.VM) (err error) { // Setup loop device for the image imageLoop, err := newLoopDev(path.Join(constants.IMAGE_DIR, imageUID.String(), constants.IMAGE_FS), true) if err != nil { - return err + return } // Make sure the all directories above the snapshot directory exists if err = os.MkdirAll(path.Dir(vm.OverlayFile()), 0755); err != nil { - return err + return } // Setup loop device for the VM overlay overlayLoop, err := newLoopDev(vm.OverlayFile(), false) if err != nil { - return err + return } imageLoopSize, err := imageLoop.Size512K() if err != nil { - return err + return } overlayLoopSize, err := overlayLoop.Size512K() if err != nil { - return err + return } // If the overlay is larger than the base image, we need to set up an additional dm device @@ -96,7 +96,7 @@ func ActivateSnapshot(vm *api.VM) (err error) { baseDevice := fmt.Sprintf("%s-base", device) if err = runDMSetup(baseDevice, dmBaseTable); err != nil { - return err + return } basePath = fmt.Sprintf("/dev/mapper/%s", baseDevice) @@ -106,7 +106,7 @@ func ActivateSnapshot(vm *api.VM) (err error) { dmTable := []byte(fmt.Sprintf("0 %d snapshot %s %s P 8", overlayLoopSize, basePath, overlayLoop.Path())) if err = runDMSetup(device, dmTable); err != nil { - return err + return } // Repair the filesystem in case it has errors @@ -116,14 +116,14 @@ func ActivateSnapshot(vm *api.VM) (err error) { // If the overlay is larger than the image, call resize2fs to make the filesystem fill the overlay if overlayLoopSize > imageLoopSize { if _, err = util.ExecuteCommand("resize2fs", devicePath); err != nil { - return err + return } } // By detaching the loop devices after setting up the snapshot // they get automatically removed when the snapshot is removed. if err = imageLoop.Detach(); err != nil { - return err + return } err = overlayLoop.Detach() diff --git a/pkg/dmlegacy/vm.go b/pkg/dmlegacy/vm.go index 4e180e4da..b4eb368cb 100644 --- a/pkg/dmlegacy/vm.go +++ b/pkg/dmlegacy/vm.go @@ -85,19 +85,19 @@ func AllocateAndPopulateOverlay(vm *api.VM) error { func copyToOverlay(vm *api.VM) (err error) { err = ActivateSnapshot(vm) if err != nil { - return err + return } defer util.DeferErr(&err, func() error { return cleanup.DeactivateSnapshot(vm) }) mp, err := util.Mount(vm.SnapshotDev()) if err != nil { - return err + return } defer util.DeferErr(&err, mp.Umount) // Copy the kernel files to the VM. TODO: Use snapshot overlaying instead. if err = copyKernelToOverlay(vm, mp.Path); err != nil { - return err + return } // do not mutate vm.Spec.CopyFiles @@ -109,7 +109,7 @@ func copyToOverlay(vm *api.VM) (err error) { // generate a key if PublicKey is empty pubKeyPath, err = newSSHKeypair(vm) if err != nil { - return err + return } } @@ -125,11 +125,11 @@ func copyToOverlay(vm *api.VM) (err error) { for _, mapping := range fileMappings { vmFilePath := path.Join(mp.Path, mapping.VMPath) if err = os.MkdirAll(path.Dir(vmFilePath), constants.DATA_DIR_PERM); err != nil { - return err + return } if err = util.CopyFile(mapping.HostPath, vmFilePath); err != nil { - return err + return } } @@ -140,17 +140,17 @@ func copyToOverlay(vm *api.VM) (err error) { // Write /etc/hosts for the VM if err = writeEtcHosts(mp.Path, vm.GetUID().String(), ip); err != nil { - return err + return } // Write the UID to /etc/hostname for the VM if err = writeEtcHostname(mp.Path, vm.GetUID().String()); err != nil { - return err + return } // Populate /etc/fstab with the VM's volume mounts if err = populateFstab(vm, mp.Path); err != nil { - return err + return } // Set overlay root permissions