Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add http zstd reader for cdi #2593

Merged
merged 4 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/image/filefmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ var knownHeaders = Headers{
SizeOff: 0,
SizeLen: 0,
},
"zst": Header{
Format: "zst",
magicNumber: []byte{0x28, 0xb5, 0x2f, 0xfd},
SizeOff: 0,
SizeLen: 0,
},
"qcow2": Header{
Format: "qcow2",
magicNumber: []byte{'Q', 'F', 'I', 0xfb},
Expand Down
2 changes: 2 additions & 0 deletions pkg/image/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
ExtTar = ".tar"
// ExtXz is a constant for the .xz extenstion
ExtXz = ".xz"
// ExtZst is a constant for the .zst extenstion
ExtZst = ".zst"
// ExtTarXz is a constant for the .tar.xz extenstion
ExtTarXz = ExtTar + ExtXz
// ExtTarGz is a constant for the .tar.gz extenstion
Expand Down
1 change: 1 addition & 0 deletions pkg/importer/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ go_library(
"//vendor/github.com/containers/image/v5/oci/archive:go_default_library",
"//vendor/github.com/containers/image/v5/pkg/blobinfocache:go_default_library",
"//vendor/github.com/containers/image/v5/types:go_default_library",
"//vendor/github.com/klauspost/compress/zstd:go_default_library",
"//vendor/github.com/ovirt/go-ovirt:go_default_library",
"//vendor/github.com/ovirt/go-ovirt-client:go_default_library",
"//vendor/github.com/ovirt/go-ovirt-client-log-klog:go_default_library",
Expand Down
17 changes: 17 additions & 0 deletions pkg/importer/format-readers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"strconv"

"github.com/klauspost/compress/zstd"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/ulikunitz/xz"
Expand Down Expand Up @@ -72,6 +73,7 @@ type FormatReaders struct {
Archived bool
ArchiveXz bool
ArchiveGz bool
ArchiveZstd bool
progressReader *prometheusutil.ProgressReader
}

Expand Down Expand Up @@ -167,6 +169,12 @@ func (fr *FormatReaders) fileFormatSelector(hdr *image.Header) {
fr.Archived = true
fr.ArchiveGz = true
}
case "zst":
r, err = fr.zstReader()
if err == nil {
fr.Archived = true
fr.ArchiveZstd = true
}
case "qcow2":
r, err = fr.qcow2NopReader(hdr)
fr.Convert = true
Expand Down Expand Up @@ -208,6 +216,15 @@ func (fr *FormatReaders) gzReader() (io.ReadCloser, error) {
return gz, nil
}

// Return the zst reader.
func (fr *FormatReaders) zstReader() (io.ReadCloser, error) {
zst, err := zstd.NewReader(fr.TopReader())
if err != nil {
return nil, errors.Wrap(err, "could not create zst reader")
}
return zst.IOReadCloser(), nil
}

// Return the size of the endpoint "through the eye" of the previous reader. Note: there is no
// qcow2 reader so nil is returned so that nothing is appended to the reader stack.
// Note: size is stored at offset 24 in the qcow2 header.
Expand Down
27 changes: 27 additions & 0 deletions tests/datavolume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ var _ = Describe("[vendor:cnv-qe@redhat.com][level:component]DataVolume tests",
httpsTinyCoreVhdxURL := func() string {
return fmt.Sprintf(utils.HTTPSTinyCoreVhdxURL, f.CdiInstallNs)
}
httpsTinyCoreZstURL := func() string {
return fmt.Sprintf(utils.HTTPSTinyCoreZstURL, f.CdiInstallNs)
}
tinyCoreQcow2URL := func() string {
return fmt.Sprintf(utils.TinyCoreQcow2URL+".gz", f.CdiInstallNs)
}
Expand Down Expand Up @@ -600,6 +603,30 @@ var _ = Describe("[vendor:cnv-qe@redhat.com][level:component]DataVolume tests",
Message: "Import Complete",
Reason: "Completed",
}}),
table.Entry("[rfe_id:1115][crit:high][test_id:1379]succeed creating import dv with given valid zst url (https)", dataVolumeTestArguments{
name: "dv-https-import-zst",
size: "1Gi",
url: httpsTinyCoreZstURL,
dvFunc: createHTTPSDataVolume,
eventReason: dvc.ImportSucceeded,
phase: cdiv1.Succeeded,
checkPermissions: true,
readyCondition: &cdiv1.DataVolumeCondition{
Type: cdiv1.DataVolumeReady,
Status: v1.ConditionTrue,
},
boundCondition: &cdiv1.DataVolumeCondition{
Type: cdiv1.DataVolumeBound,
Status: v1.ConditionTrue,
Message: "PVC dv-https-import-zst Bound",
Reason: "Bound",
},
runningCondition: &cdiv1.DataVolumeCondition{
Type: cdiv1.DataVolumeRunning,
Status: v1.ConditionFalse,
Message: "Import Complete",
Reason: "Completed",
}}),
table.Entry("succeed creating import dv with custom https cert that has a weird filename", dataVolumeTestArguments{
name: "dv-https-import-qcow2",
size: "1Gi",
Expand Down
1 change: 1 addition & 0 deletions tests/utils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ go_library(
"//pkg/util/naming:go_default_library",
"//staging/src/kubevirt.io/containerized-data-importer-api/pkg/apis/core/v1beta1:go_default_library",
"//staging/src/kubevirt.io/containerized-data-importer-api/pkg/apis/upload/v1beta1:go_default_library",
"//vendor/github.com/klauspost/compress/zstd:go_default_library",
"//vendor/github.com/onsi/ginkgo:go_default_library",
"//vendor/github.com/onsi/gomega:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
Expand Down
2 changes: 2 additions & 0 deletions tests/utils/datavolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const (
HTTPSTinyCoreIsoURL = "https://cdi-file-host.%s/tinyCore.iso"
// HTTPSTinyCoreQcow2URL provides a test (https) url for the tineyCore qcow2 image
HTTPSTinyCoreQcow2URL = "https://cdi-file-host.%s/tinyCore.qcow2"
// HTTPSTinyCoreZstURL provides a test (https) url for the tineyCore zst image
HTTPSTinyCoreZstURL = "https://cdi-file-host.%s/tinyCore.iso.zst"
// TinyCoreQcow2URLRateLimit provides a test url for the tineyCore qcow2 image via rate-limiting proxy
TinyCoreQcow2URLRateLimit = "http://cdi-file-host.%s:82/tinyCore.qcow2"
// TinyCoreQcow2GzURLRateLimit provides a test url for the tineyCore qcow2.gz image via rate-limiting proxy
Expand Down
25 changes: 25 additions & 0 deletions tests/utils/fileConversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"

"github.com/klauspost/compress/zstd"
"github.com/pkg/errors"
"github.com/ulikunitz/xz"

Expand All @@ -18,6 +19,7 @@ import (
var formatTable = map[string]func(string, string, string) (string, error){
image.ExtGz: toGz,
image.ExtXz: toXz,
image.ExtZst: toZst,
image.ExtTar: toTar,
image.ExtQcow2: convertUsingQemuImg,
image.ExtVmdk: convertUsingQemuImg,
Expand Down Expand Up @@ -111,6 +113,29 @@ func toGz(src, tgtDir, ext string) (string, error) {
return tgtPath, nil
}

func toZst(src, tgtDir, ext string) (string, error) {
tgtFile, tgtPath, _ := createTargetFile(src, tgtDir, image.ExtZst)
defer tgtFile.Close()

w, err := zstd.NewWriter(tgtFile)
if err != nil {
return "", errors.Wrapf(err, "Error getting zst writer for file %s", tgtPath)
}
defer w.Close()

srcFile, err := os.Open(src)
if err != nil {
return "", errors.Wrapf(err, "Error opening file %s", src)
}
defer srcFile.Close()

_, err = io.Copy(w, srcFile)
if err != nil {
return "", errors.Wrapf(err, "Error writing to file %s", tgtPath)
}
return tgtPath, nil
}

func toXz(src, tgtDir, ext string) (string, error) {
tgtFile, tgtPath, _ := createTargetFile(src, tgtDir, image.ExtXz)
defer tgtFile.Close()
Expand Down
2 changes: 2 additions & 0 deletions tools/cdi-func-test-file-host-init/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,14 @@ func main() {
[]string{""},
[]string{".gz"},
[]string{".xz"},
[]string{".zst"},
[]string{".qcow2"},
[]string{".vmdk"},
[]string{".vhd"},
[]string{".vhdx"},
[]string{".qcow2", ".gz"},
[]string{".qcow2", ".xz"},
[]string{".qcow2", ".zst"},
}

if err := utils.CreateCertForTestService(util.GetNamespace(), serviceName, configMapName, *certDir, certFile, keyFile); err != nil {
Expand Down