tar: read directly from stdin (#1728)

* tar: run directly from stdin

* export UnTar function
This commit is contained in:
Andrei Kvapil 2021-12-26 13:09:26 +01:00 committed by GitHub
parent d2f3e896cd
commit 2ea368dde8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 15 deletions

View File

@ -17,10 +17,9 @@ limitations under the License.
package buildcontext
import (
"compress/gzip"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/GoogleContainerTools/kaniko/pkg/constants"
"github.com/GoogleContainerTools/kaniko/pkg/util"
@ -47,14 +46,15 @@ func (t *Tar) UnpackTarFromBuildContext() (string, error) {
logrus.Infof("To simulate EOF and exit, press 'Ctrl+D'")
// if launched through docker in interactive mode and without piped data
// process will be stuck here until EOF is sent
data, err := util.GetInputFrom(os.Stdin)
gzr, err := gzip.NewReader(os.Stdin)
if err != nil {
return "", errors.Wrap(err, "fail to get standard input")
}
t.context = filepath.Join(directory, constants.ContextTar)
if err := ioutil.WriteFile(t.context, data, 0644); err != nil {
return "", errors.Wrap(err, "fail to redirect standard input into compressed tar file")
return directory, err
}
defer gzr.Close()
_, err = util.UnTar(gzr, directory)
return directory, err
}
return directory, util.UnpackCompressedTar(t.context, directory)

View File

@ -261,8 +261,8 @@ func childDirInIgnoreList(path string) bool {
return false
}
// unTar returns a list of files that have been extracted from the tar archive at r to the path at dest
func unTar(r io.Reader, dest string) ([]string, error) {
// UnTar returns a list of files that have been extracted from the tar archive at r to the path at dest
func UnTar(r io.Reader, dest string) ([]string, error) {
var extractedFiles []string
tr := tar.NewReader(r)
for {

View File

@ -626,7 +626,7 @@ func createUncompressedTar(fileContents map[string]string, tarFileName, testDir
return nil
}
func Test_unTar(t *testing.T) {
func Test_UnTar(t *testing.T) {
tcs := []struct {
name string
setupTarContents map[string]string
@ -671,7 +671,7 @@ func Test_unTar(t *testing.T) {
if err != nil {
t.Fatal(err)
}
fileList, err := unTar(file, tc.destination)
fileList, err := UnTar(file, tc.destination)
if err != nil {
t.Fatal(err)
}

View File

@ -218,7 +218,7 @@ func UnpackLocalTarArchive(path, dest string) ([]string, error) {
return nil, UnpackCompressedTar(path, dest)
} else if compressionLevel == archive.Bzip2 {
bzr := bzip2.NewReader(file)
return unTar(bzr, dest)
return UnTar(bzr, dest)
}
}
if fileIsUncompressedTar(path) {
@ -227,7 +227,7 @@ func UnpackLocalTarArchive(path, dest string) ([]string, error) {
return nil, err
}
defer file.Close()
return unTar(file, dest)
return UnTar(file, dest)
}
return nil, errors.New("path does not lead to local tar archive")
}
@ -286,6 +286,6 @@ func UnpackCompressedTar(path, dir string) error {
return err
}
defer gzr.Close()
_, err = unTar(gzr, dir)
_, err = UnTar(gzr, dir)
return err
}