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/edgelesssys/constellation/v2/internal/retry"
"github.com/spf13/afero" "github.com/spf13/afero"
"golang.org/x/text/transform"
"k8s.io/utils/clock" "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. // 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. // The resulting file(s) are copied to all destinations.
func (i *osInstaller) Install( func (i *osInstaller) Install(
ctx context.Context, sourceURL string, destinations []string, perm fs.FileMode, ctx context.Context, sourceURL string, destinations []string, perm fs.FileMode, extract bool,
extract bool, transforms ...transform.Transformer,
) error { ) error {
tempPath, err := i.retryDownloadToTempDir(ctx, sourceURL, transforms...) tempPath, err := i.retryDownloadToTempDir(ctx, sourceURL)
if err != nil { if err != nil {
return err 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{ doer := downloadDoer{
url: url, url: url,
transforms: transforms,
downloader: i, downloader: i,
} }
@ -167,8 +164,8 @@ func (i *osInstaller) retryDownloadToTempDir(ctx context.Context, url string, tr
return doer.path, nil return doer.path, nil
} }
// downloadToTempDir downloads a file to a temporary location, applying transform on-the-fly. // downloadToTempDir downloads a file to a temporary location.
func (i *osInstaller) downloadToTempDir(ctx context.Context, url string, transforms ...transform.Transformer) (fileName string, retErr error) { func (i *osInstaller) downloadToTempDir(ctx context.Context, url string) (fileName string, retErr error) {
out, err := afero.TempFile(i.fs, "", "") out, err := afero.TempFile(i.fs, "", "")
if err != nil { if err != nil {
return "", fmt.Errorf("creating destination temp file: %w", err) 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() defer resp.Body.Close()
transformReader := transform.NewReader(resp.Body, transform.Chain(transforms...)) if _, err = io.Copy(out, resp.Body); err != nil {
if _, err = io.Copy(out, transformReader); err != nil {
return "", fmt.Errorf("downloading %q: %w", url, err) return "", fmt.Errorf("downloading %q: %w", url, err)
} }
return out.Name(), nil return out.Name(), nil
@ -233,17 +228,16 @@ func (i *osInstaller) copy(oldname, newname string, perm fs.FileMode) (err error
type downloadDoer struct { type downloadDoer struct {
url string url string
transforms []transform.Transformer
downloader downloader downloader downloader
path string path string
} }
type downloader interface { 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 { 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 d.path = path
return err return err
} }

View File

@ -23,11 +23,9 @@ import (
"time" "time"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
"github.com/icholy/replace"
"github.com/spf13/afero" "github.com/spf13/afero"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"golang.org/x/text/transform"
"google.golang.org/grpc/test/bufconn" "google.golang.org/grpc/test/bufconn"
testclock "k8s.io/utils/clock/testing" testclock "k8s.io/utils/clock/testing"
) )
@ -37,7 +35,6 @@ func TestInstall(t *testing.T) {
server httpBufconnServer server httpBufconnServer
destination string destination string
extract bool extract bool
transforms []transform.Transformer
readonly bool readonly bool
wantErr bool wantErr bool
wantFiles map[string][]byte wantFiles map[string][]byte
@ -53,14 +50,6 @@ func TestInstall(t *testing.T) {
extract: true, extract: true,
wantFiles: map[string][]byte{"/prefix/destination": []byte("file-contents")}, 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": { "download fails": {
server: newHTTPBufconnServer(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(500) }), server: newHTTPBufconnServer(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(500) }),
destination: "/destination", destination: "/destination",
@ -92,7 +81,7 @@ func TestInstall(t *testing.T) {
retriable: func(err error) bool { return false }, 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 { if tc.wantErr {
assert.Error(err) assert.Error(err)
return return
@ -309,7 +298,7 @@ func TestRetryDownloadToTempDir(t *testing.T) {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() 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. // control the server's responses through stateCh.
@ -339,7 +328,6 @@ func TestRetryDownloadToTempDir(t *testing.T) {
func TestDownloadToTempDir(t *testing.T) { func TestDownloadToTempDir(t *testing.T) {
testCases := map[string]struct { testCases := map[string]struct {
server httpBufconnServer server httpBufconnServer
transforms []transform.Transformer
readonly bool readonly bool
wantErr bool wantErr bool
wantFile []byte wantFile []byte
@ -348,13 +336,6 @@ func TestDownloadToTempDir(t *testing.T) {
server: newHTTPBufconnServerWithBody([]byte("file-contents")), server: newHTTPBufconnServerWithBody([]byte("file-contents")),
wantFile: []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": { "download fails": {
server: newHTTPBufconnServer(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(500) }), server: newHTTPBufconnServer(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(500) }),
wantErr: true, wantErr: true,
@ -397,7 +378,7 @@ func TestDownloadToTempDir(t *testing.T) {
fs: &afero.Afero{Fs: afs}, fs: &afero.Afero{Fs: afs},
hClient: &hClient, 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 { if tc.wantErr {
assert.Error(err) assert.Error(err)
return return

View File

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

3
go.mod
View File

@ -73,7 +73,6 @@ require (
github.com/hashicorp/hc-install v0.4.0 github.com/hashicorp/hc-install v0.4.0
github.com/hashicorp/terraform-exec v0.17.3 github.com/hashicorp/terraform-exec v0.17.3
github.com/hashicorp/terraform-json v0.14.0 github.com/hashicorp/terraform-json v0.14.0
github.com/icholy/replace v0.5.0
github.com/manifoldco/promptui v0.9.0 github.com/manifoldco/promptui v0.9.0
github.com/martinjungblut/go-cryptsetup v0.0.0-20220520180014-fd0874fd07a6 github.com/martinjungblut/go-cryptsetup v0.0.0-20220520180014-fd0874fd07a6
github.com/mattn/go-isatty v0.0.16 github.com/mattn/go-isatty v0.0.16
@ -93,7 +92,6 @@ require (
golang.org/x/crypto v0.2.0 golang.org/x/crypto v0.2.0
golang.org/x/mod v0.7.0 golang.org/x/mod v0.7.0
golang.org/x/sys v0.2.0 golang.org/x/sys v0.2.0
golang.org/x/text v0.4.0
google.golang.org/api v0.102.0 google.golang.org/api v0.102.0
google.golang.org/genproto v0.0.0-20221107162902-2d387536bcdd google.golang.org/genproto v0.0.0-20221107162902-2d387536bcdd
google.golang.org/grpc v1.50.1 google.golang.org/grpc v1.50.1
@ -119,6 +117,7 @@ require (
cloud.google.com/go/longrunning v0.3.0 // indirect cloud.google.com/go/longrunning v0.3.0 // indirect
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.1 // indirect github.com/hashicorp/go-retryablehttp v0.7.1 // indirect
golang.org/x/text v0.4.0 // indirect
) )
require ( require (

2
go.sum
View File

@ -811,8 +811,6 @@ github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq
github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg= github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/icholy/replace v0.5.0 h1:Nx80zYQVlowdba+3Y6dvHDnmxaGtBrDlf2wYn9GyIXQ=
github.com/icholy/replace v0.5.0/go.mod h1:zzi8pxElj2t/5wHHHYmH45D+KxytX/t4w3ClY5nlK+g=
github.com/imdario/mergo v0.3.4/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/imdario/mergo v0.3.4/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=