Skip to content

Commit

Permalink
Refactor interaction between dispatcher.from and dispatchState
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Nephin <[email protected]>
  • Loading branch information
dnephin committed May 10, 2017
1 parent b3bc7b2 commit ab3a037
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 123 deletions.
2 changes: 1 addition & 1 deletion builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@ type Image interface {
// ReleaseableLayer is an image layer that can be mounted and released
type ReleaseableLayer interface {
Release() error
Mount() (string, error)
Mount(string) (string, error)
}
73 changes: 57 additions & 16 deletions builder/dockerfile/dispatchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/docker/docker/api/types/backend"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/builder/dockerfile/parser"
"github.com/docker/docker/pkg/signal"
"github.com/docker/go-connections/nat"
"github.com/pkg/errors"
Expand Down Expand Up @@ -196,24 +197,21 @@ func from(req dispatchRequest) error {
}

req.builder.resetImageCache()
req.state.noBaseImage = false
req.state.stageName = stageName
image, err := req.builder.getFromImage(req.shlex, req.args[0])
if err != nil {
return err
}
if image == nil {
req.state.imageID = ""
req.state.noBaseImage = true
image = newImageMount(nil, nil)
}
if err := req.builder.imageContexts.add(stageName, image); err != nil {
return err
}
req.state.baseImage = image

req.state.beginStage(stageName, image)
req.builder.buildArgs.ResetAllowed()
return req.builder.processImageFrom(req.state, image)
if image.ImageID() == "" {
// Typically this means they used "FROM scratch"
return nil
}

return processOnBuild(req)
}

func parseBuildStageName(args []string) (string, error) {
Expand Down Expand Up @@ -243,19 +241,15 @@ func (b *Builder) getFromImage(shlex *ShellLex, name string) (*imageMount, error
}

if im, ok := b.imageContexts.byName[name]; ok {
if len(im.ImageID()) > 0 {
return im, nil
}
// FROM scratch does not have an ImageID
return nil, nil
return im, nil
}

// Windows cannot support a container with no base image.
if name == api.NoBaseImageSpecifier {
if runtime.GOOS == "windows" {
return nil, errors.New("Windows does not support FROM scratch")
}
return nil, nil
return newImageMount(nil, nil), nil
}
return b.getImage(name)
}
Expand All @@ -272,6 +266,53 @@ func (b *Builder) getImage(name string) (*imageMount, error) {
return newImageMount(image, layer), nil
}

func processOnBuild(req dispatchRequest) error {
dispatchState := req.state
// Process ONBUILD triggers if they exist
if nTriggers := len(dispatchState.runConfig.OnBuild); nTriggers != 0 {
word := "trigger"
if nTriggers > 1 {
word = "triggers"
}
fmt.Fprintf(req.builder.Stderr, "# Executing %d build %s...\n", nTriggers, word)
}

// Copy the ONBUILD triggers, and remove them from the config, since the config will be committed.
onBuildTriggers := dispatchState.runConfig.OnBuild
dispatchState.runConfig.OnBuild = []string{}

// Reset stdin settings as all build actions run without stdin
dispatchState.runConfig.OpenStdin = false
dispatchState.runConfig.StdinOnce = false

// parse the ONBUILD triggers by invoking the parser
for _, step := range onBuildTriggers {
dockerfile, err := parser.Parse(strings.NewReader(step))
if err != nil {
return err
}

for _, n := range dockerfile.AST.Children {
if err := checkDispatch(n); err != nil {
return err
}

upperCasedCmd := strings.ToUpper(n.Value)
switch upperCasedCmd {
case "ONBUILD":
return errors.New("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
case "MAINTAINER", "FROM":
return errors.Errorf("%s isn't allowed as an ONBUILD trigger", upperCasedCmd)
}
}

if _, err := dispatchFromDockerfile(req.builder, dockerfile, dispatchState); err != nil {
return err
}
}
return nil
}

// ONBUILD RUN echo yo
//
// ONBUILD triggers run when the image is used in a FROM statement.
Expand Down
4 changes: 3 additions & 1 deletion builder/dockerfile/dispatchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/builder"
"github.com/docker/docker/builder/dockerfile/parser"
"github.com/docker/docker/pkg/system"
"github.com/docker/docker/pkg/testutil"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -192,8 +193,9 @@ func TestFromScratch(t *testing.T) {
}

require.NoError(t, err)
assert.True(t, req.state.hasFromImage())
assert.Equal(t, "", req.state.imageID)
assert.Equal(t, true, req.state.noBaseImage)
assert.Equal(t, []string{"PATH=" + system.DefaultPathEnv}, req.state.runConfig.Env)
}

func TestFromWithArg(t *testing.T) {
Expand Down
53 changes: 36 additions & 17 deletions builder/dockerfile/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/docker/docker/builder"
"github.com/docker/docker/builder/dockerfile/command"
"github.com/docker/docker/builder/dockerfile/parser"
"github.com/docker/docker/pkg/system"
"github.com/docker/docker/runconfig/opts"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -184,37 +185,55 @@ type dispatchOptions struct {

// dispatchState is a data object which is modified by dispatchers
type dispatchState struct {
runConfig *container.Config
maintainer string
cmdSet bool
noBaseImage bool
imageID string
baseImage builder.Image
stageName string
runConfig *container.Config
maintainer string
cmdSet bool
imageID string
baseImage builder.Image
stageName string
}

func newDispatchState() *dispatchState {
return &dispatchState{runConfig: &container.Config{}}
}

func (r *dispatchState) updateRunConfig() {
r.runConfig.Image = r.imageID
func (s *dispatchState) updateRunConfig() {
s.runConfig.Image = s.imageID
}

// hasFromImage returns true if the builder has processed a `FROM <image>` line
func (r *dispatchState) hasFromImage() bool {
return r.imageID != "" || r.noBaseImage
func (s *dispatchState) hasFromImage() bool {
return s.imageID != "" || (s.baseImage != nil && s.baseImage.ImageID() == "")
}

func (r *dispatchState) runConfigEnvMapping() map[string]string {
return opts.ConvertKVStringsToMap(r.runConfig.Env)
}

func (r *dispatchState) isCurrentStage(target string) bool {
func (s *dispatchState) isCurrentStage(target string) bool {
if target == "" {
return false
}
return strings.EqualFold(r.stageName, target)
return strings.EqualFold(s.stageName, target)
}

func (s *dispatchState) beginStage(stageName string, image builder.Image) {
s.stageName = stageName
s.imageID = image.ImageID()

if image.RunConfig() != nil {
s.runConfig = image.RunConfig()
}
s.baseImage = image
s.setDefaultPath()
}

// Add the default PATH to runConfig.ENV if one exists for the platform and there
// is no PATH set. Note that windows won't have one as it's set by HCS
func (s *dispatchState) setDefaultPath() {
if system.DefaultPathEnv == "" {
return
}
envMap := opts.ConvertKVStringsToMap(s.runConfig.Env)
if _, ok := envMap["PATH"]; !ok {
s.runConfig.Env = append(s.runConfig.Env, "PATH="+system.DefaultPathEnv)
}
}

func handleOnBuildNode(ast *parser.Node, msg *bytes.Buffer) (*parser.Node, []string, error) {
Expand Down
9 changes: 6 additions & 3 deletions builder/dockerfile/imagecontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func (ic *imageContexts) update(imageID string, runConfig *container.Config) {
func (ic *imageContexts) validate(i int) error {
if i < 0 || i >= len(ic.list)-1 {
if i == len(ic.list)-1 {
return errors.Errorf("%d refers to current build stage", i)
return errors.New("refers to current build stage")
}
return errors.Errorf("index out of bounds")
return errors.New("index out of bounds")
}
return nil
}
Expand Down Expand Up @@ -122,7 +122,7 @@ func (im *imageMount) context() (builder.Source, error) {
if im.id == "" || im.layer == nil {
return nil, errors.Errorf("empty context")
}
mountPath, err := im.layer.Mount()
mountPath, err := im.layer.Mount(im.id)
if err != nil {
return nil, errors.Wrapf(err, "failed to mount %s", im.id)
}
Expand All @@ -136,6 +136,9 @@ func (im *imageMount) context() (builder.Source, error) {
}

func (im *imageMount) unmount() error {
if im.layer == nil {
return nil
}
if err := im.layer.Release(); err != nil {
return errors.Wrapf(err, "failed to unmount previous build image %s", im.id)
}
Expand Down
69 changes: 0 additions & 69 deletions builder/dockerfile/internals.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/docker/docker/api/types/backend"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/builder"
"github.com/docker/docker/builder/dockerfile/parser"
"github.com/docker/docker/builder/remotecontext"
"github.com/docker/docker/pkg/httputils"
"github.com/docker/docker/pkg/ioutils"
Expand Down Expand Up @@ -480,74 +479,6 @@ func (b *Builder) calcCopyInfo(cmdName, origPath string, allowLocalDecompression
return copyInfos, nil
}

func (b *Builder) processImageFrom(dispatchState *dispatchState, img builder.Image) error {
if img != nil {
dispatchState.imageID = img.ImageID()

if img.RunConfig() != nil {
dispatchState.runConfig = img.RunConfig()
}
}

// Check to see if we have a default PATH, note that windows won't
// have one as it's set by HCS
if system.DefaultPathEnv != "" {
if _, ok := dispatchState.runConfigEnvMapping()["PATH"]; !ok {
dispatchState.runConfig.Env = append(dispatchState.runConfig.Env,
"PATH="+system.DefaultPathEnv)
}
}

if img == nil {
// Typically this means they used "FROM scratch"
return nil
}

// Process ONBUILD triggers if they exist
if nTriggers := len(dispatchState.runConfig.OnBuild); nTriggers != 0 {
word := "trigger"
if nTriggers > 1 {
word = "triggers"
}
fmt.Fprintf(b.Stderr, "# Executing %d build %s...\n", nTriggers, word)
}

// Copy the ONBUILD triggers, and remove them from the config, since the config will be committed.
onBuildTriggers := dispatchState.runConfig.OnBuild
dispatchState.runConfig.OnBuild = []string{}

// Reset stdin settings as all build actions run without stdin
dispatchState.runConfig.OpenStdin = false
dispatchState.runConfig.StdinOnce = false

// parse the ONBUILD triggers by invoking the parser
for _, step := range onBuildTriggers {
dockerfile, err := parser.Parse(strings.NewReader(step))
if err != nil {
return err
}

for _, n := range dockerfile.AST.Children {
if err := checkDispatch(n); err != nil {
return err
}

upperCasedCmd := strings.ToUpper(n.Value)
switch upperCasedCmd {
case "ONBUILD":
return errors.New("Chaining ONBUILD via `ONBUILD ONBUILD` isn't allowed")
case "MAINTAINER", "FROM":
return errors.Errorf("%s isn't allowed as an ONBUILD trigger", upperCasedCmd)
}
}

if _, err := dispatchFromDockerfile(b, dockerfile, dispatchState); err != nil {
return err
}
}
return nil
}

// probeCache checks if cache match can be found for current build instruction.
// If an image is found, probeCache returns `(true, nil)`.
// If no image is found, it returns `(false, nil)`.
Expand Down
2 changes: 1 addition & 1 deletion builder/dockerfile/mockbackend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,6 @@ func (l *mockLayer) Release() error {
return nil
}

func (l *mockLayer) Mount() (string, error) {
func (l *mockLayer) Mount(_ string) (string, error) {
return "mountPath", nil
}
Loading

0 comments on commit ab3a037

Please sign in to comment.