5
5
"fmt"
6
6
"io"
7
7
"os"
8
+ "path/filepath"
8
9
"strings"
9
10
10
11
zglob "github.com/mattn/go-zglob"
@@ -15,7 +16,7 @@ import (
15
16
// Converts container image layer archive (tar) to Lambda layer archive (zip).
16
17
// Filters files from the source and only writes a new archive if at least
17
18
// 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 ) {
19
20
// TODO: support image types other than local Docker images (docker-daemon transport),
20
21
// where the layer format is tar. For example, layers directly from a Docker registry
21
22
// 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
56
57
}
57
58
58
59
// Determine if this file should be repacked
59
- repack , err := shouldRepackLayerFile (f , filterPattern )
60
+ repack , err := shouldRepackLayerFile (f )
60
61
if err != nil {
61
62
return false , fmt .Errorf ("filtering file in layer tar: %v" , err )
62
63
}
@@ -95,7 +96,7 @@ func startZipFile(destination string) (zip *archiver.Zip, zipFile *os.File, err
95
96
return z , out , nil
96
97
}
97
98
98
- func shouldRepackLayerFile (f archiver.File , matchPattern string ) (should bool , err error ) {
99
+ func shouldRepackLayerFile (f archiver.File ) (should bool , err error ) {
99
100
header , ok := f .Header .(* tar.Header )
100
101
if ! ok {
101
102
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
110
111
return false , nil
111
112
}
112
113
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 )
114
116
}
115
117
116
118
func repackLayerFile (f archiver.File , z * archiver.Zip ) error {
@@ -119,12 +121,14 @@ func repackLayerFile(f archiver.File, z *archiver.Zip) error {
119
121
return fmt .Errorf ("expected header to be *tar.Header but was %T" , f .Header )
120
122
}
121
123
124
+ filename := strings .TrimPrefix (filepath .ToSlash (hdr .Name ), "opt/" )
125
+
122
126
switch hdr .Typeflag {
123
127
case tar .TypeReg , tar .TypeRegA , tar .TypeChar , tar .TypeBlock , tar .TypeFifo , tar .TypeSymlink , tar .TypeLink :
124
128
return z .Write (archiver.File {
125
129
FileInfo : archiver.FileInfo {
126
130
FileInfo : f .FileInfo ,
127
- CustomName : hdr . Name ,
131
+ CustomName : filename ,
128
132
},
129
133
ReadCloser : f ,
130
134
})
0 commit comments