Remove transformers from k8sutil downloader

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
This commit is contained in:
Paul Meyer 2022-11-10 16:53:42 +01:00
parent d41174659b
commit d025fe1e98
5 changed files with 17 additions and 47 deletions

View file

@ -21,7 +21,6 @@ import (
"github.com/edgelesssys/constellation/v2/internal/retry"
"github.com/spf13/afero"
"golang.org/x/text/transform"
"k8s.io/utils/clock"
)
@ -53,10 +52,9 @@ func newOSInstaller() *osInstaller {
// Install downloads a resource from a URL, applies any given text transformations and extracts the resulting file if required.
// The resulting file(s) are copied to all destinations.
func (i *osInstaller) Install(
ctx context.Context, sourceURL string, destinations []string, perm fs.FileMode,
extract bool, transforms ...transform.Transformer,
ctx context.Context, sourceURL string, destinations []string, perm fs.FileMode, extract bool,
) error {
tempPath, err := i.retryDownloadToTempDir(ctx, sourceURL, transforms...)
tempPath, err := i.retryDownloadToTempDir(ctx, sourceURL)
if err != nil {
return err
}
@ -150,10 +148,9 @@ func (i *osInstaller) extractArchive(archivePath, prefix string, perm fs.FileMod
}
}
func (i *osInstaller) retryDownloadToTempDir(ctx context.Context, url string, transforms ...transform.Transformer) (fileName string, someError error) {
func (i *osInstaller) retryDownloadToTempDir(ctx context.Context, url string) (fileName string, someError error) {
doer := downloadDoer{
url: url,
transforms: transforms,
downloader: i,
}
@ -167,8 +164,8 @@ func (i *osInstaller) retryDownloadToTempDir(ctx context.Context, url string, tr
return doer.path, nil
}
// downloadToTempDir downloads a file to a temporary location, applying transform on-the-fly.
func (i *osInstaller) downloadToTempDir(ctx context.Context, url string, transforms ...transform.Transformer) (fileName string, retErr error) {
// downloadToTempDir downloads a file to a temporary location.
func (i *osInstaller) downloadToTempDir(ctx context.Context, url string) (fileName string, retErr error) {
out, err := afero.TempFile(i.fs, "", "")
if err != nil {
return "", fmt.Errorf("creating destination temp file: %w", err)
@ -195,9 +192,7 @@ func (i *osInstaller) downloadToTempDir(ctx context.Context, url string, transfo
}
defer resp.Body.Close()
transformReader := transform.NewReader(resp.Body, transform.Chain(transforms...))
if _, err = io.Copy(out, transformReader); err != nil {
if _, err = io.Copy(out, resp.Body); err != nil {
return "", fmt.Errorf("downloading %q: %w", url, err)
}
return out.Name(), nil
@ -233,17 +228,16 @@ func (i *osInstaller) copy(oldname, newname string, perm fs.FileMode) (err error
type downloadDoer struct {
url string
transforms []transform.Transformer
downloader downloader
path string
}
type downloader interface {
downloadToTempDir(ctx context.Context, url string, transforms ...transform.Transformer) (string, error)
downloadToTempDir(ctx context.Context, url string) (string, error)
}
func (d *downloadDoer) Do(ctx context.Context) error {
path, err := d.downloader.downloadToTempDir(ctx, d.url, d.transforms...)
path, err := d.downloader.downloadToTempDir(ctx, d.url)
d.path = path
return err
}

View file

@ -23,11 +23,9 @@ import (
"time"
"github.com/hashicorp/go-multierror"
"github.com/icholy/replace"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/text/transform"
"google.golang.org/grpc/test/bufconn"
testclock "k8s.io/utils/clock/testing"
)
@ -37,7 +35,6 @@ func TestInstall(t *testing.T) {
server httpBufconnServer
destination string
extract bool
transforms []transform.Transformer
readonly bool
wantErr bool
wantFiles map[string][]byte
@ -53,14 +50,6 @@ func TestInstall(t *testing.T) {
extract: true,
wantFiles: map[string][]byte{"/prefix/destination": []byte("file-contents")},
},
"download with transform works": {
server: newHTTPBufconnServerWithBody([]byte("/usr/bin/kubelet")),
destination: "/destination",
transforms: []transform.Transformer{
replace.String("/usr/bin", "/run/state/bin"),
},
wantFiles: map[string][]byte{"/destination": []byte("/run/state/bin/kubelet")},
},
"download fails": {
server: newHTTPBufconnServer(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(500) }),
destination: "/destination",
@ -92,7 +81,7 @@ func TestInstall(t *testing.T) {
retriable: func(err error) bool { return false },
}
err := inst.Install(context.Background(), "http://server/path", []string{tc.destination}, fs.ModePerm, tc.extract, tc.transforms...)
err := inst.Install(context.Background(), "http://server/path", []string{tc.destination}, fs.ModePerm, tc.extract)
if tc.wantErr {
assert.Error(err)
return
@ -309,7 +298,7 @@ func TestRetryDownloadToTempDir(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
path, downloadErr = inst.retryDownloadToTempDir(ctx, "http://server/path", []transform.Transformer{}...)
path, downloadErr = inst.retryDownloadToTempDir(ctx, "http://server/path")
}()
// control the server's responses through stateCh.
@ -338,23 +327,15 @@ func TestRetryDownloadToTempDir(t *testing.T) {
func TestDownloadToTempDir(t *testing.T) {
testCases := map[string]struct {
server httpBufconnServer
transforms []transform.Transformer
readonly bool
wantErr bool
wantFile []byte
server httpBufconnServer
readonly bool
wantErr bool
wantFile []byte
}{
"download works": {
server: newHTTPBufconnServerWithBody([]byte("file-contents")),
wantFile: []byte("file-contents"),
},
"download with transform works": {
server: newHTTPBufconnServerWithBody([]byte("/usr/bin/kubelet")),
transforms: []transform.Transformer{
replace.String("/usr/bin", "/run/state/bin"),
},
wantFile: []byte("/run/state/bin/kubelet"),
},
"download fails": {
server: newHTTPBufconnServer(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(500) }),
wantErr: true,
@ -397,7 +378,7 @@ func TestDownloadToTempDir(t *testing.T) {
fs: &afero.Afero{Fs: afs},
hClient: &hClient,
}
path, err := inst.downloadToTempDir(context.Background(), "http://server/path", tc.transforms...)
path, err := inst.downloadToTempDir(context.Background(), "http://server/path")
if tc.wantErr {
assert.Error(err)
return

View file

@ -36,7 +36,6 @@ import (
"github.com/edgelesssys/constellation/v2/internal/versions"
"github.com/spf13/afero"
"go.uber.org/zap"
"golang.org/x/text/transform"
corev1 "k8s.io/api/core/v1"
)
@ -60,8 +59,7 @@ type Client interface {
type installer interface {
Install(
ctx context.Context, sourceURL string, destinations []string, perm fs.FileMode,
extract bool, transforms ...transform.Transformer,
ctx context.Context, sourceURL string, destinations []string, perm fs.FileMode, extract bool,
) error
}