Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit 2f3adaa

Browse files
committed
Fix paths in Lambda layer zip (remove opt/)
1 parent b7f1dee commit 2f3adaa

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func ConvertImage(name string) (retErr error) {
7777
}
7878
defer layerStream.Close()
7979

80-
fileCreated, err := RepackLayer(lambdaLayerFilename, layerStream, "opt/**/**")
80+
fileCreated, err := RepackLayer(lambdaLayerFilename, layerStream)
8181
if err != nil {
8282
return err
8383
}

repack_layer.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"path/filepath"
89
"strings"
910

1011
zglob "github.com/mattn/go-zglob"
@@ -15,7 +16,7 @@ import (
1516
// Converts container image layer archive (tar) to Lambda layer archive (zip).
1617
// Filters files from the source and only writes a new archive if at least
1718
// one file in the source matches the filter (i.e. does not create empty archives).
18-
func RepackLayer(outputFilename string, layerContents io.Reader, filterPattern string) (created bool, retError error) {
19+
func RepackLayer(outputFilename string, layerContents io.Reader) (created bool, retError error) {
1920
// TODO: support image types other than local Docker images (docker-daemon transport),
2021
// where the layer format is tar. For example, layers directly from a Docker registry
2122
// will be .tar.gz-formatted. OCI images can be either tar or tar.gz, based on the
@@ -56,7 +57,7 @@ func RepackLayer(outputFilename string, layerContents io.Reader, filterPattern s
5657
}
5758

5859
// Determine if this file should be repacked
59-
repack, err := shouldRepackLayerFile(f, filterPattern)
60+
repack, err := shouldRepackLayerFile(f)
6061
if err != nil {
6162
return false, fmt.Errorf("filtering file in layer tar: %v", err)
6263
}
@@ -95,7 +96,7 @@ func startZipFile(destination string) (zip *archiver.Zip, zipFile *os.File, err
9596
return z, out, nil
9697
}
9798

98-
func shouldRepackLayerFile(f archiver.File, matchPattern string) (should bool, err error) {
99+
func shouldRepackLayerFile(f archiver.File) (should bool, err error) {
99100
header, ok := f.Header.(*tar.Header)
100101
if !ok {
101102
return false, fmt.Errorf("expected header to be *tar.Header but was %T", f.Header)
@@ -110,7 +111,8 @@ func shouldRepackLayerFile(f archiver.File, matchPattern string) (should bool, e
110111
return false, nil
111112
}
112113

113-
return zglob.Match(matchPattern, header.Name)
114+
// Only extract files that can be used for Lambda custom runtimes
115+
return zglob.Match("opt/**/**", header.Name)
114116
}
115117

116118
func repackLayerFile(f archiver.File, z *archiver.Zip) error {
@@ -119,12 +121,14 @@ func repackLayerFile(f archiver.File, z *archiver.Zip) error {
119121
return fmt.Errorf("expected header to be *tar.Header but was %T", f.Header)
120122
}
121123

124+
filename := strings.TrimPrefix(filepath.ToSlash(hdr.Name), "opt/")
125+
122126
switch hdr.Typeflag {
123127
case tar.TypeReg, tar.TypeRegA, tar.TypeChar, tar.TypeBlock, tar.TypeFifo, tar.TypeSymlink, tar.TypeLink:
124128
return z.Write(archiver.File{
125129
FileInfo: archiver.FileInfo{
126130
FileInfo: f.FileInfo,
127-
CustomName: hdr.Name,
131+
CustomName: filename,
128132
},
129133
ReadCloser: f,
130134
})

0 commit comments

Comments
 (0)