Feature/contextsources (#195)

* added switch to extract different sources as build context

* first rough implementation of aws s3

* added buildcontext package and interface

* added GetBuildContext func to buildcontext.go
added fallback to gcs
renamed GC struct to GCS

* improved the default behavior of build context retrieval

* renamed gc:// to gs:// in order to follow common standards

* renamed struct File to Dir and some cleanup work

* moved context.tar suffix to the buildcontext processors where it is needed

* added buildcontext retrieval as struct variable

added fallback if prefix in bucket specifier is present

* cleanup if structures

* added prefix to s3

* WIP

* Fixed build context bugs

* refactored build context
This commit is contained in:
Christian Jantz
2018-07-06 06:24:50 -07:00
committed by priyawadhwa
parent 7e437390af
commit 65d7b0a9aa
141 changed files with 49049 additions and 55 deletions
+6 -42
View File
@@ -17,50 +17,14 @@ limitations under the License.
package util
import (
"os"
"path/filepath"
"cloud.google.com/go/storage"
"github.com/GoogleContainerTools/kaniko/pkg/constants"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
"strings"
)
// UnpackTarFromGCSBucket unpacks the context.tar.gz file in the given bucket to the given directory
func UnpackTarFromGCSBucket(bucketName, directory string) error {
// Get the tar from the bucket
tarPath, err := getTarFromBucket(bucketName, directory)
if err != nil {
return err
func GetBucketAndItem(context string) (string, string) {
split := strings.SplitN(context, "/", 2)
if len(split) == 2 {
return split[0], split[1]
}
logrus.Debug("Unpacking source context tar...")
if err := UnpackCompressedTar(tarPath, directory); err != nil {
return err
}
// Remove the tar so it doesn't interfere with subsequent commands
logrus.Debugf("Deleting %s", tarPath)
return os.Remove(tarPath)
}
// getTarFromBucket gets context.tar.gz from the GCS bucket and saves it to the filesystem
// It returns the path to the tar file
func getTarFromBucket(bucketName, directory string) (string, error) {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
return "", err
}
bucket := client.Bucket(bucketName)
// Get the tarfile context.tar.gz from the GCS bucket, and save it to a tar object
reader, err := bucket.Object(constants.ContextTar).NewReader(ctx)
if err != nil {
return "", err
}
defer reader.Close()
tarPath := filepath.Join(directory, constants.ContextTar)
if err := CreateFile(tarPath, reader, 0600, 0, 0); err != nil {
return "", err
}
logrus.Debugf("Copied tarball %s from GCS bucket %s to %s", constants.ContextTar, bucketName, tarPath)
return tarPath, nil
return split[0], constants.ContextTar
}