Add --bucket flag to access context from GCS bucket
This commit is contained in:
parent
b7d6de83d8
commit
f5fcd73458
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/image"
|
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/image"
|
||||||
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/snapshot"
|
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/snapshot"
|
||||||
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/util"
|
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/util"
|
||||||
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
@ -33,12 +34,14 @@ var (
|
||||||
dockerfilePath string
|
dockerfilePath string
|
||||||
destination string
|
destination string
|
||||||
srcContext string
|
srcContext string
|
||||||
|
bucket string
|
||||||
logLevel string
|
logLevel string
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RootCmd.PersistentFlags().StringVarP(&dockerfilePath, "dockerfile", "f", "/workspace/Dockerfile", "Path to the dockerfile to be built.")
|
RootCmd.PersistentFlags().StringVarP(&dockerfilePath, "dockerfile", "f", "/workspace/Dockerfile", "Path to the dockerfile to be built.")
|
||||||
RootCmd.PersistentFlags().StringVarP(&srcContext, "context", "c", "", "Path to the dockerfile build context.")
|
RootCmd.PersistentFlags().StringVarP(&srcContext, "context", "c", "", "Path to the dockerfile build context.")
|
||||||
|
RootCmd.PersistentFlags().StringVarP(&bucket, "bucket", "b", "", "Name of the GCS bucket from which to access build context as tarball.")
|
||||||
RootCmd.PersistentFlags().StringVarP(&destination, "destination", "d", "", "Registry the final image should be pushed to (ex: gcr.io/test/example:latest)")
|
RootCmd.PersistentFlags().StringVarP(&destination, "destination", "d", "", "Registry the final image should be pushed to (ex: gcr.io/test/example:latest)")
|
||||||
RootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", constants.DefaultLogLevel, "Log level (debug, info, warn, error, fatal, panic")
|
RootCmd.PersistentFlags().StringVarP(&logLevel, "verbosity", "v", constants.DefaultLogLevel, "Log level (debug, info, warn, error, fatal, panic")
|
||||||
}
|
}
|
||||||
|
|
@ -63,16 +66,21 @@ var RootCmd = &cobra.Command{
|
||||||
// resolveSourceContext unpacks the source context if it is a tar in a GCS bucket
|
// resolveSourceContext unpacks the source context if it is a tar in a GCS bucket
|
||||||
// it resets srcContext to be the path to the unpacked build context within the image
|
// it resets srcContext to be the path to the unpacked build context within the image
|
||||||
func resolveSourceContext() error {
|
func resolveSourceContext() error {
|
||||||
if util.FilepathExists(srcContext) {
|
if srcContext == "" && bucket == "" {
|
||||||
|
return errors.New("please specify a path to the build context with the --context flag or a GCS bucket with the --bucket flag")
|
||||||
|
}
|
||||||
|
if srcContext != "" && bucket != "" {
|
||||||
|
return errors.New("please specify either --bucket or --context as the desired build context")
|
||||||
|
}
|
||||||
|
if srcContext != "" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// Else, assume the source context is the name of a bucket
|
logrus.Infof("Using GCS bucket %s as source context", bucket)
|
||||||
logrus.Infof("Using GCS bucket %s as source context", srcContext)
|
|
||||||
buildContextPath := constants.BuildContextDir
|
buildContextPath := constants.BuildContextDir
|
||||||
if err := util.UnpackTarFromGCSBucket(srcContext, buildContextPath); err != nil {
|
if err := util.UnpackTarFromGCSBucket(bucket, buildContextPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logrus.Debugf("Unpacked tar from %s to path %s", srcContext, buildContextPath)
|
logrus.Debugf("Unpacked tar from %s to path %s", bucket, buildContextPath)
|
||||||
srcContext = buildContextPath
|
srcContext = buildContextPath
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -43,6 +43,7 @@ var fileTests = []struct {
|
||||||
configPath string
|
configPath string
|
||||||
dockerContext string
|
dockerContext string
|
||||||
kbuildContext string
|
kbuildContext string
|
||||||
|
kbuildContextBucket bool
|
||||||
repo string
|
repo string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
|
|
@ -83,6 +84,7 @@ var fileTests = []struct {
|
||||||
configPath: "/workspace/integration_tests/dockerfiles/config_test_bucket_buildcontext.json",
|
configPath: "/workspace/integration_tests/dockerfiles/config_test_bucket_buildcontext.json",
|
||||||
dockerContext: buildcontextPath,
|
dockerContext: buildcontextPath,
|
||||||
kbuildContext: kbuildTestBucket,
|
kbuildContext: kbuildTestBucket,
|
||||||
|
kbuildContextBucket: true,
|
||||||
repo: "test-bucket-buildcontext",
|
repo: "test-bucket-buildcontext",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
@ -162,9 +164,13 @@ func main() {
|
||||||
|
|
||||||
// Then, buld the image with kbuild
|
// Then, buld the image with kbuild
|
||||||
kbuildImage := testRepo + kbuildPrefix + test.repo
|
kbuildImage := testRepo + kbuildPrefix + test.repo
|
||||||
|
contextFlag := "--context"
|
||||||
|
if test.kbuildContextBucket {
|
||||||
|
contextFlag = "--bucket"
|
||||||
|
}
|
||||||
kbuild := step{
|
kbuild := step{
|
||||||
Name: executorImage,
|
Name: executorImage,
|
||||||
Args: []string{executorCommand, "--destination", kbuildImage, "--dockerfile", test.dockerfilePath, "--context", test.kbuildContext},
|
Args: []string{executorCommand, "--destination", kbuildImage, "--dockerfile", test.dockerfilePath, contextFlag, test.kbuildContext},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pull the kbuild image
|
// Pull the kbuild image
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue