Skip to content

Commit

Permalink
Use scratch space for non-archive uploads
Browse files Browse the repository at this point in the history
Basically a follow up to 3219 for upload sources,
which suffer from the same issue of losing sparseness.

Signed-off-by: Alex Kalenyuk <[email protected]>
  • Loading branch information
akalenyu committed Aug 19, 2024
1 parent 0a9de7d commit 0827b34
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
9 changes: 5 additions & 4 deletions cmd/cdi-cloner/clone-source.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/x509"
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
Expand Down Expand Up @@ -55,7 +56,7 @@ func (er *execReader) Close() error {
}

func init() {
flag.StringVar(&contentType, "content-type", "", "filesystem-clone|blockdevice-clone")
flag.StringVar(&contentType, "content-type", "", fmt.Sprintf("%s|%s", common.FilesystemCloneContentType, common.BlockdeviceClone))
flag.StringVar(&mountPoint, "mount", "", "pvc mount point")
flag.Uint64Var(&uploadBytes, "upload-bytes", 0, "approx number of bytes in input")
klog.InitFlags(nil)
Expand Down Expand Up @@ -133,7 +134,7 @@ func pipeToSnappy(reader io.ReadCloser) io.ReadCloser {

func validateContentType() {
switch contentType {
case "filesystem-clone", "blockdevice-clone":
case common.FilesystemCloneContentType, common.BlockdeviceClone:
default:
klog.Fatalf("Invalid content-type %q", contentType)
}
Expand Down Expand Up @@ -198,13 +199,13 @@ func newTarReader(preallocation bool) (io.ReadCloser, error) {

func getInputStream(preallocation bool) io.ReadCloser {
switch contentType {
case "filesystem-clone":
case common.FilesystemCloneContentType:
rc, err := newTarReader(preallocation)
if err != nil {
klog.Fatalf("Error creating tar reader for %q: %+v", mountPoint, err)
}
return rc
case "blockdevice-clone":
case common.BlockdeviceClone:
rc, err := os.Open(mountPoint)
if err != nil {
klog.Fatalf("Error opening block device %q: %+v", mountPoint, err)
Expand Down
4 changes: 0 additions & 4 deletions pkg/importer/upload-datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ func (ud *UploadDataSource) Info() (ProcessingPhase, error) {
if ud.contentType == cdiv1.DataVolumeArchive {
return ProcessingPhaseTransferDataDir, nil
}
if !ud.readers.Convert {
// Uploading a raw file, we can write that directly to the target.
return ProcessingPhaseTransferDataFile, nil
}
return ProcessingPhaseTransferScratch, nil
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/importer/upload-datasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ var _ = Describe("Upload data source", func() {
Expect(ProcessingPhaseTransferDataDir).To(Equal(result))
})

It("Info should return TransferData, when passed in a valid raw image", func() {
It("Info should return TransferScratch, when passed in a valid raw image", func() {
// Don't need to defer close, since ud.Close will close the reader
file, err := os.Open(tinyCoreFilePath)
Expect(err).NotTo(HaveOccurred())
ud = NewUploadDataSource(file, dvKubevirt)
result, err := ud.Info()
Expect(err).NotTo(HaveOccurred())
Expect(ProcessingPhaseTransferDataFile).To(Equal(result))
Expect(ProcessingPhaseTransferScratch).To(Equal(result))
})

DescribeTable("calling transfer should", func(fileName string, dvContentType cdiv1.DataVolumeContentType, expectedPhase ProcessingPhase, scratchPath string, want []byte, wantErr bool) {
Expand Down Expand Up @@ -136,7 +136,7 @@ var _ = Describe("Upload data source", func() {
ud = NewUploadDataSource(sourceFile, dvKubevirt)
result, err := ud.Info()
Expect(err).NotTo(HaveOccurred())
Expect(ProcessingPhaseTransferDataFile).To(Equal(result))
Expect(ProcessingPhaseTransferScratch).To(Equal(result))
result, err = ud.TransferFile(filepath.Join(tmpDir, "file"))
Expect(err).ToNot(HaveOccurred())
Expect(ProcessingPhaseResize).To(Equal(result))
Expand All @@ -149,7 +149,7 @@ var _ = Describe("Upload data source", func() {
ud = NewUploadDataSource(sourceFile, dvKubevirt)
result, err := ud.Info()
Expect(err).NotTo(HaveOccurred())
Expect(ProcessingPhaseTransferDataFile).To(Equal(result))
Expect(ProcessingPhaseTransferScratch).To(Equal(result))
result, err = ud.TransferFile("/invalidpath/invalidfile")
Expect(err).To(HaveOccurred())
Expect(ProcessingPhaseError).To(Equal(result))
Expand Down Expand Up @@ -204,14 +204,14 @@ var _ = Describe("Async Upload data source", func() {
Expect(ProcessingPhaseTransferScratch).To(Equal(result))
})

It("Info should return TransferData, when passed in a valid raw image", func() {
It("Info should return TransferScratch, when passed in a valid raw image", func() {
// Don't need to defer close, since ud.Close will close the reader
file, err := os.Open(tinyCoreFilePath)
Expect(err).NotTo(HaveOccurred())
aud = NewAsyncUploadDataSource(file)
result, err := aud.Info()
Expect(err).NotTo(HaveOccurred())
Expect(ProcessingPhaseTransferDataFile).To(Equal(result))
Expect(ProcessingPhaseTransferScratch).To(Equal(result))
})

DescribeTable("calling transfer should", func(fileName, scratchPath string, want []byte, wantErr bool) {
Expand Down Expand Up @@ -260,7 +260,7 @@ var _ = Describe("Async Upload data source", func() {
aud = NewAsyncUploadDataSource(sourceFile)
result, err := aud.Info()
Expect(err).NotTo(HaveOccurred())
Expect(ProcessingPhaseTransferDataFile).To(Equal(result))
Expect(ProcessingPhaseTransferScratch).To(Equal(result))
result, err = aud.TransferFile(filepath.Join(tmpDir, "file"))
Expect(err).ToNot(HaveOccurred())
Expect(ProcessingPhaseValidatePause).To(Equal(result))
Expand All @@ -274,7 +274,7 @@ var _ = Describe("Async Upload data source", func() {
aud = NewAsyncUploadDataSource(sourceFile)
result, err := aud.Info()
Expect(err).NotTo(HaveOccurred())
Expect(ProcessingPhaseTransferDataFile).To(Equal(result))
Expect(ProcessingPhaseTransferScratch).To(Equal(result))
result, err = aud.TransferFile("/invalidpath/invalidfile")
Expect(err).To(HaveOccurred())
Expect(ProcessingPhaseError).To(Equal(result))
Expand Down
4 changes: 0 additions & 4 deletions tests/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,6 @@ var _ = Describe("[rfe_id:138][crit:high][vendor:[email protected]][level:compon
same, err := f.VerifyTargetPVCContentMD5(f.Namespace, archivePVC, pathInPvc, expectedMd5)
Expect(err).ToNot(HaveOccurred())
Expect(same).To(BeTrue())
By("Verifying the image is sparse")
Expect(f.VerifySparse(f.Namespace, archivePVC, pathInPvc, utils.UploadFileSize)).To(BeTrue())
}
} else {
checkFailureNoValidToken(archivePVC)
Expand Down Expand Up @@ -733,8 +731,6 @@ var _ = Describe("[rfe_id:138][crit:high][vendor:[email protected]][level:compon
same, err := f.VerifyTargetPVCContentMD5(f.Namespace, pvc, pathInPvc, expectedMd5)
Expect(err).ToNot(HaveOccurred())
Expect(same).To(BeTrue())
By("Verifying the image is sparse")
Expect(f.VerifySparse(f.Namespace, pvc, pathInPvc, utils.UploadFileSize)).To(BeTrue())
}
} else {
checkFailureNoValidToken(pvcPrime)
Expand Down

0 comments on commit 0827b34

Please sign in to comment.