github.com/GoogleContainerTools/kaniko@v1.23.0/pkg/buildcontext/gcs.go (about) 1 /* 2 Copyright 2018 Google LLC 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package buildcontext 18 19 import ( 20 "fmt" 21 "io" 22 "os" 23 "path/filepath" 24 25 kConfig "github.com/GoogleContainerTools/kaniko/pkg/config" 26 "github.com/GoogleContainerTools/kaniko/pkg/constants" 27 "github.com/GoogleContainerTools/kaniko/pkg/util" 28 "github.com/GoogleContainerTools/kaniko/pkg/util/bucket" 29 "github.com/sirupsen/logrus" 30 "golang.org/x/net/context" 31 ) 32 33 // GCS struct for Google Cloud Storage processing 34 type GCS struct { 35 context string 36 } 37 38 func (g *GCS) UnpackTarFromBuildContext() (string, error) { 39 bucketName, filepath, err := bucket.GetNameAndFilepathFromURI(g.context) 40 if err != nil { 41 return "", fmt.Errorf("getting bucketname and filepath from context: %w", err) 42 } 43 return kConfig.BuildContextDir, unpackTarFromGCSBucket(bucketName, filepath, kConfig.BuildContextDir) 44 } 45 46 func UploadToBucket(r io.Reader, dest string) error { 47 ctx := context.Background() 48 bucketName, filepath, err := bucket.GetNameAndFilepathFromURI(dest) 49 if err != nil { 50 return fmt.Errorf("getting bucketname and filepath from dest: %w", err) 51 } 52 client, err := bucket.NewClient(ctx) 53 if err != nil { 54 return err 55 } 56 return bucket.Upload(ctx, bucketName, filepath, r, client) 57 } 58 59 // unpackTarFromGCSBucket unpacks the context.tar.gz file in the given bucket to the given directory 60 func unpackTarFromGCSBucket(bucketName, item, directory string) error { 61 // Get the tar from the bucket 62 tarPath, err := getTarFromBucket(bucketName, item, directory) 63 if err != nil { 64 return err 65 } 66 logrus.Debug("Unpacking source context tar...") 67 if err := util.UnpackCompressedTar(tarPath, directory); err != nil { 68 return err 69 } 70 // Remove the tar so it doesn't interfere with subsequent commands 71 logrus.Debugf("Deleting %s", tarPath) 72 return os.Remove(tarPath) 73 } 74 75 // getTarFromBucket gets context.tar.gz from the GCS bucket and saves it to the filesystem 76 // It returns the path to the tar file 77 func getTarFromBucket(bucketName, filepathInBucket, directory string) (string, error) { 78 ctx := context.Background() 79 client, err := bucket.NewClient(ctx) 80 if err != nil { 81 return "", err 82 } 83 // Get the tarfile context.tar.gz from the GCS bucket, and save it to a tar object 84 reader, err := bucket.ReadCloser(ctx, bucketName, filepathInBucket, client) 85 if err != nil { 86 return "", err 87 } 88 defer reader.Close() 89 tarPath := filepath.Join(directory, constants.ContextTar) 90 if err := util.CreateFile(tarPath, reader, 0600, 0, 0); err != nil { 91 return "", err 92 } 93 logrus.Debugf("Copied tarball %s from GCS bucket %s to %s", constants.ContextTar, bucketName, tarPath) 94 return tarPath, nil 95 }