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

Bug 1772599: Handle compressed images for libvirt and baremetal IPI #2657

Merged
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
13 changes: 4 additions & 9 deletions pkg/rhcos/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rhcos
import (
"context"
"net/url"
"strings"

"github.com/pkg/errors"
)
Expand All @@ -28,14 +27,10 @@ func OpenStack(ctx context.Context) (string, error) {

baseURL := base.ResolveReference(relOpenStack).String()

// Attach sha256 checksum to the URL. If the file has the ".gz" extension, then the
// data is compressed and we use SHA256 value; otherwise we work with uncompressed
// data and therefore need UncompressedSHA256.
if strings.HasSuffix(baseURL, ".gz") {
baseURL += "?sha256=" + meta.Images.OpenStack.SHA256
} else {
baseURL += "?sha256=" + meta.Images.OpenStack.UncompressedSHA256
}
// Attach sha256 checksum to the URL. Always provide the
// uncompressed SHA256; the cache will take care of
// uncompressing before checksumming.
baseURL += "?sha256=" + meta.Images.OpenStack.UncompressedSHA256

// Check that we have generated a valid URL
_, err = url.ParseRequestURI(baseURL)
Expand Down
15 changes: 14 additions & 1 deletion pkg/rhcos/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,18 @@ func QEMU(ctx context.Context) (string, error) {
return "", err
}

return base.ResolveReference(relQEMU).String(), nil
baseURL := base.ResolveReference(relQEMU).String()

// Attach sha256 checksum to the URL. Always provide the
// uncompressed SHA256; the cache will take care of
// uncompressing before checksumming.
baseURL += "?sha256=" + meta.Images.QEMU.UncompressedSHA256

// Check that we have generated a valid URL
_, err = url.ParseRequestURI(baseURL)
if err != nil {
return "", err
}

return baseURL, nil
}
4 changes: 2 additions & 2 deletions pkg/tfvars/baremetal/baremetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"github.com/metal3-io/baremetal-operator/pkg/bmc"
"github.com/metal3-io/baremetal-operator/pkg/hardware"
libvirttfvars "github.com/openshift/installer/pkg/tfvars/libvirt"
"github.com/openshift/installer/pkg/tfvars/internal/cache"
"github.com/openshift/installer/pkg/types/baremetal"
"github.com/pkg/errors"
"net/url"
Expand All @@ -31,7 +31,7 @@ type config struct {

// TFVars generates bare metal specific Terraform variables.
func TFVars(libvirtURI, bootstrapProvisioningIP, bootstrapOSImage, externalBridge, provisioningBridge string, platformHosts []*baremetal.Host, image string) ([]byte, error) {
bootstrapOSImage, err := libvirttfvars.CachedImage(bootstrapOSImage)
bootstrapOSImage, err := cache.DownloadImageFile(bootstrapOSImage)
if err != nil {
return nil, errors.Wrap(err, "failed to use cached bootstrap libvirt image")
}
Expand Down
29 changes: 28 additions & 1 deletion pkg/tfvars/internal/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cache

import (
"bytes"
"compress/gzip"
"crypto/md5"
"crypto/sha256"
"fmt"
Expand All @@ -19,6 +21,7 @@ import (
const (
applicationName = "openshift-installer"
imageDataType = "image"
gzipFileType = "application/x-gzip"
)

// getCacheDir returns a local path of the cache, where the installer should put the data:
Expand Down Expand Up @@ -107,6 +110,29 @@ func cacheFile(reader io.Reader, filePath string, sha256Checksum string) (err er
}
}()

// Detect whether we know how to decompress the file
// See http://golang.org/pkg/net/http/#DetectContentType for why we use 512
buf := make([]byte, 512)
_, err = reader.Read(buf)
if err != nil {
return err
}

reader = io.MultiReader(bytes.NewReader(buf), reader)
fileType := http.DetectContentType(buf)
logrus.Debugf("content type of %s is %s", filePath, fileType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://prow.svc.ci.openshift.org/view/gcs/origin-ci-test/pr-logs/pull/openshift_installer/2657/pull-ci-openshift-installer-master-e2e-openstack/2837#1:build-log.txt%3A1233

the output looks like is printing the entire os.File object.

maybe Detected content-type as <type> ?? because we are no longer using the file to detect content.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filePath is the filename. I think it's useful this way, so if something goes wrong somebody can look in the cache to see which file was misdetected or whatnot.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(that failure was old, when I had file rather than filePath in there)

switch fileType {
case gzipFileType:
uncompressor, err := gzip.NewReader(reader)
if err != nil {
return err
}
defer uncompressor.Close()
reader = uncompressor
default:
// No need for an interposer otherwise
}

// Wrap the reader in TeeReader to calculate sha256 checksum on the fly
hasher := sha256.New()
if sha256Checksum != "" {
Expand Down Expand Up @@ -193,7 +219,8 @@ func DownloadFile(baseURL string, dataType string) (string, error) {
}

// DownloadImageFile is a helper function that obtains an image file from a given URL,
// puts it in the cache and returns the local file path.
// puts it in the cache and returns the local file path. If the file is compressed
// by a known compressor, the file is uncompressed prior to being returned.
func DownloadImageFile(baseURL string) (string, error) {
logrus.Infof("Obtaining RHCOS image file from '%v'", baseURL)

Expand Down
176 changes: 0 additions & 176 deletions pkg/tfvars/libvirt/cache.go

This file was deleted.

3 changes: 2 additions & 1 deletion pkg/tfvars/libvirt/libvirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/apparentlymart/go-cidr/cidr"
"github.com/openshift/cluster-api-provider-libvirt/pkg/apis/libvirtproviderconfig/v1beta1"
"github.com/openshift/installer/pkg/tfvars/internal/cache"
"github.com/pkg/errors"
)

Expand All @@ -34,7 +35,7 @@ func TFVars(masterConfig *v1beta1.LibvirtMachineProviderConfig, osImage string,
return nil, err
}

osImage, err = cachedImage(osImage)
osImage, err = cache.DownloadImageFile(osImage)
if err != nil {
return nil, errors.Wrap(err, "failed to use cached libvirt image")
}
Expand Down
Loading