Fixed merge conflict, added validation

This commit is contained in:
Priya Wadhwa
2018-04-12 10:30:25 -07:00
31 changed files with 653 additions and 108 deletions
+53 -15
View File
@@ -17,12 +17,15 @@ limitations under the License.
package cmd
import (
"fmt"
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/commands"
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/constants"
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/dockerfile"
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/image"
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/snapshot"
"github.com/GoogleCloudPlatform/k8s-container-builder/pkg/util"
"github.com/containers/image/manifest"
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@@ -52,13 +55,15 @@ func init() {
var RootCmd = &cobra.Command{
Use: "executor",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
return util.SetLogLevel(logLevel)
if err := util.SetLogLevel(logLevel); err != nil {
return err
}
if err := resolveSourceContext(); err != nil {
return err
}
return checkDockerfilePath()
},
Run: func(cmd *cobra.Command, args []string) {
if err := resolveSourceContext(); err != nil {
logrus.Error(err)
os.Exit(1)
}
if err := execute(); err != nil {
logrus.Error(err)
os.Exit(1)
@@ -66,6 +71,18 @@ var RootCmd = &cobra.Command{
},
}
func checkDockerfilePath() error {
if util.FilepathExists(dockerfilePath) {
return nil
}
// Otherwise, check if the path relative to the build context exists
if util.FilepathExists(filepath.Join(srcContext, dockerfilePath)) {
dockerfilePath = filepath.Join(srcContext, dockerfilePath)
return nil
}
return errors.New("please provide a valid path to a Dockerfile within the build context")
}
// 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
func resolveSourceContext() error {
@@ -85,11 +102,6 @@ func resolveSourceContext() error {
}
logrus.Debugf("Unpacked tar from %s to path %s", bucket, buildContextPath)
srcContext = buildContextPath
// If path to dockerfile doesn't exist, assume it is in the unpacked tar
if !util.FilepathExists(dockerfilePath) {
logrus.Debugf("Expecting dockerfile to be located at %s within the tar build context", dockerfilePath)
dockerfilePath = filepath.Join(srcContext, dockerfilePath)
}
return nil
}
@@ -112,7 +124,11 @@ func execute() error {
return err
}
l := snapshot.NewLayeredMap(getHasher())
hasher, err := getHasher()
if err != nil {
return err
}
l := snapshot.NewLayeredMap(hasher)
snapshotter := snapshot.NewSnapshotter(l, constants.RootDir)
// Take initial snapshot
@@ -134,6 +150,9 @@ func execute() error {
imageConfig := sourceImage.Config()
// Currently only supports single stage builds
for _, stage := range stages {
if err := resolveOnBuild(&stage, imageConfig); err != nil {
return err
}
for _, cmd := range stage.Commands {
dockerCommand, err := commands.GetCommand(cmd, srcContext)
if err != nil {
@@ -148,6 +167,7 @@ func execute() error {
if err != nil {
return err
}
util.MoveVolumeWhitelistToWhitelist()
if contents == nil {
logrus.Info("No files were changed, appending empty layer to config.")
sourceImage.AppendConfigHistory(constants.Author, true)
@@ -166,12 +186,30 @@ func execute() error {
return image.PushImage(sourceImage, destination)
}
func getHasher() func(string) (string, error) {
if snapshotMode == "time" {
func getHasher() (func(string) (string, error), error) {
if snapshotMode == constants.SnapshotModeTime {
logrus.Info("Only file modification time will be considered when snapshotting")
return util.MtimeHasher()
return util.MtimeHasher(), nil
}
return util.Hasher()
if snapshotMode == constants.SnapshotModeFull {
return util.Hasher(), nil
}
return nil, fmt.Errorf("%s is not a valid snapshot mode", snapshotMode)
}
func resolveOnBuild(stage *instructions.Stage, config *manifest.Schema2Config) error {
if config.OnBuild == nil {
return nil
}
// Otherwise, parse into commands
cmds, err := dockerfile.ParseCommands(config.OnBuild)
if err != nil {
return err
}
// Append to the beginning of the commands in the stage
stage.Commands = append(cmds, stage.Commands...)
logrus.Infof("Executing %v build triggers", len(cmds))
return nil
}
// setDefaultEnv sets default values for HOME and PATH so that