diff --git a/README.md b/README.md index b6184d978..2e49d811a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ The majority of Dockerfile commands can be executed with kaniko, but we're still * SHELL * HEALTHCHECK * STOPSIGNAL -* ONBUILD * ARG We're currently in the process of building kaniko, so as of now it isn't production ready. diff --git a/executor/cmd/root.go b/executor/cmd/root.go index 1c938606a..509e53870 100644 --- a/executor/cmd/root.go +++ b/executor/cmd/root.go @@ -23,6 +23,8 @@ import ( "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" @@ -141,6 +143,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 { @@ -174,6 +179,21 @@ func execute() error { return image.PushImage(sourceImage, destination) } +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 // config.json and docker-credential-gcr can be accessed func setDefaultEnv() error { diff --git a/integration_tests/dockerfiles/Dockerfile_onbuild_base b/integration_tests/dockerfiles/Dockerfile_onbuild_base new file mode 100644 index 000000000..85532c1a0 --- /dev/null +++ b/integration_tests/dockerfiles/Dockerfile_onbuild_base @@ -0,0 +1,6 @@ +FROM gcr.io/google-appengine/debian9:latest +ENV dir /tmp/dir/ +ONBUILD RUN echo "onbuild" > /tmp/onbuild +ONBUILD RUN mkdir $dir +ONBUILD RUN echo "onbuild 2" > ${dir}/onbuild2 +ONBUILD WORKDIR /new/workdir diff --git a/integration_tests/dockerfiles/Dockerfile_test_onbuild b/integration_tests/dockerfiles/Dockerfile_test_onbuild new file mode 100644 index 000000000..2968c0c62 --- /dev/null +++ b/integration_tests/dockerfiles/Dockerfile_test_onbuild @@ -0,0 +1,6 @@ +FROM gcr.io/kaniko-test/onbuild-base:latest +COPY context/foo foo +ENV dir /new/workdir/ +ONBUILD RUN echo "onbuild" > /tmp/onbuild +ONBUILD RUN echo "onbuild 2" > ${dir} +ONBUILD WORKDIR /new/workdir diff --git a/integration_tests/dockerfiles/config_test_onbuild.json b/integration_tests/dockerfiles/config_test_onbuild.json new file mode 100644 index 000000000..e7fa9a1e4 --- /dev/null +++ b/integration_tests/dockerfiles/config_test_onbuild.json @@ -0,0 +1,12 @@ +[ + { + "Image1": "gcr.io/kaniko-test/docker-test-onbuild:latest", + "Image2": "gcr.io/kaniko-test/kaniko-test-onbuild:latest", + "DiffType": "File", + "Diff": { + "Adds": null, + "Dels": null, + "Mods": null + } + } +] \ No newline at end of file diff --git a/integration_tests/dockerfiles/config_test_run.json b/integration_tests/dockerfiles/config_test_run.json index 4bd52a28a..ae2b3d8f2 100644 --- a/integration_tests/dockerfiles/config_test_run.json +++ b/integration_tests/dockerfiles/config_test_run.json @@ -14,8 +14,8 @@ }, { "Name": "/var/log/apt/term.log", - "Size1": 24421, - "Size2": 24421 + "Size1": 23671, + "Size2": 23671 }, { "Name": "/var/cache/ldconfig/aux-cache", @@ -24,8 +24,8 @@ }, { "Name": "/var/log/apt/history.log", - "Size1": 5415, - "Size2": 5415 + "Size1": 5661, + "Size2": 5661 }, { "Name": "/var/log/alternatives.log", diff --git a/integration_tests/dockerfiles/config_test_volume.json b/integration_tests/dockerfiles/config_test_volume.json index c4ba05ffe..706bda2c3 100644 --- a/integration_tests/dockerfiles/config_test_volume.json +++ b/integration_tests/dockerfiles/config_test_volume.json @@ -9,4 +9,4 @@ "Mods": null } } -] +] \ No newline at end of file diff --git a/integration_tests/integration_test_yaml.go b/integration_tests/integration_test_yaml.go index f08dab881..defd591a9 100644 --- a/integration_tests/integration_test_yaml.go +++ b/integration_tests/integration_test_yaml.go @@ -34,6 +34,7 @@ const ( kanikoTestBucket = "kaniko-test-bucket" buildcontextPath = "/workspace/integration_tests" dockerfilesPath = "/workspace/integration_tests/dockerfiles" + onbuildBaseImage = testRepo + "onbuild-base:latest" ) var fileTests = []struct { @@ -110,6 +111,14 @@ var fileTests = []struct { kanikoContext: buildcontextPath, repo: "test-add", }, + { + description: "test onbuild", + dockerfilePath: "/workspace/integration_tests/dockerfiles/Dockerfile_test_onbuild", + configPath: "/workspace/integration_tests/dockerfiles/config_test_onbuild.json", + dockerContext: buildcontextPath, + kanikoContext: buildcontextPath, + repo: "test-onbuild", + }, } var structureTests = []struct { @@ -190,8 +199,19 @@ func main() { Name: dockerImage, Args: []string{"build", "-t", executorImage, "-f", "deploy/Dockerfile", "."}, } + + // Build and push onbuild base images + buildOnbuildImage := step{ + Name: dockerImage, + Args: []string{"build", "-t", onbuildBaseImage, "-f", "/workspace/integration_tests/dockerfiles/Dockerfile_onbuild_base", "."}, + } + pushOnbuildBase := step{ + Name: dockerImage, + Args: []string{"push", onbuildBaseImage}, + } y := testyaml{ - Steps: []step{containerDiffStep, containerDiffPermissions, structureTestsStep, structureTestPermissions, GCSBucketTarBuildContext, uploadTarBuildContext, buildExecutorImage}, + Steps: []step{containerDiffStep, containerDiffPermissions, structureTestsStep, structureTestPermissions, GCSBucketTarBuildContext, uploadTarBuildContext, buildExecutorImage, + buildOnbuildImage, pushOnbuildBase}, } for _, test := range fileTests { // First, build the image with docker diff --git a/pkg/commands/commands.go b/pkg/commands/commands.go index 1c56faaa5..f2dd30fb2 100644 --- a/pkg/commands/commands.go +++ b/pkg/commands/commands.go @@ -56,6 +56,8 @@ func GetCommand(cmd instructions.Command, buildcontext string) (DockerCommand, e return &LabelCommand{cmd: c}, nil case *instructions.UserCommand: return &UserCommand{cmd: c}, nil + case *instructions.OnbuildCommand: + return &OnBuildCommand{cmd: c}, nil case *instructions.VolumeCommand: return &VolumeCommand{cmd: c}, nil } diff --git a/pkg/commands/onbuild.go b/pkg/commands/onbuild.go new file mode 100644 index 000000000..db4711604 --- /dev/null +++ b/pkg/commands/onbuild.go @@ -0,0 +1,54 @@ +/* +Copyright 2018 Google LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package commands + +import ( + "github.com/GoogleCloudPlatform/k8s-container-builder/pkg/util" + "github.com/containers/image/manifest" + "github.com/docker/docker/builder/dockerfile/instructions" + "github.com/sirupsen/logrus" +) + +type OnBuildCommand struct { + cmd *instructions.OnbuildCommand +} + +//ExecuteCommand adds the specified expression in Onbuild to the config +func (o *OnBuildCommand) ExecuteCommand(config *manifest.Schema2Config) error { + logrus.Info("cmd: ONBUILD") + logrus.Infof("args: %s", o.cmd.Expression) + resolvedExpression, err := util.ResolveEnvironmentReplacement(o.cmd.Expression, config.Env, false) + if err != nil { + return err + } + if config.OnBuild == nil { + config.OnBuild = []string{resolvedExpression} + } else { + config.OnBuild = append(config.OnBuild, resolvedExpression) + } + return nil +} + +// FilesToSnapshot returns that no files have changed, this command only touches metadata. +func (o *OnBuildCommand) FilesToSnapshot() []string { + return []string{} +} + +// CreatedBy returns some information about the command for the image config history +func (o *OnBuildCommand) CreatedBy() string { + return o.cmd.Expression +} diff --git a/pkg/commands/onbuild_test.go b/pkg/commands/onbuild_test.go new file mode 100644 index 000000000..3004e99c9 --- /dev/null +++ b/pkg/commands/onbuild_test.go @@ -0,0 +1,69 @@ +/* +Copyright 2018 Google LLC + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package commands + +import ( + "github.com/GoogleCloudPlatform/k8s-container-builder/testutil" + "github.com/containers/image/manifest" + "github.com/docker/docker/builder/dockerfile/instructions" + "testing" +) + +var onbuildTests = []struct { + expression string + onbuildArray []string + expectedArray []string +}{ + { + expression: "RUN echo \\\"hi\\\" > $dir", + onbuildArray: nil, + expectedArray: []string{ + "RUN echo \"hi\" > /some/dir", + }, + }, + { + expression: "COPY foo foo", + onbuildArray: []string{ + "RUN echo \"hi\" > /some/dir", + }, + expectedArray: []string{ + "RUN echo \"hi\" > /some/dir", + "COPY foo foo", + }, + }, +} + +func TestExecuteOnbuild(t *testing.T) { + for _, test := range onbuildTests { + cfg := &manifest.Schema2Config{ + Env: []string{ + "dir=/some/dir", + }, + OnBuild: test.onbuildArray, + } + + onbuildCmd := &OnBuildCommand{ + &instructions.OnbuildCommand{ + Expression: test.expression, + }, + } + + err := onbuildCmd.ExecuteCommand(cfg) + testutil.CheckErrorAndDeepEqual(t, false, err, test.expectedArray, cfg.OnBuild) + } + +} diff --git a/pkg/dockerfile/dockerfile.go b/pkg/dockerfile/dockerfile.go index 948cffa50..f755a547e 100644 --- a/pkg/dockerfile/dockerfile.go +++ b/pkg/dockerfile/dockerfile.go @@ -18,6 +18,7 @@ package dockerfile import ( "bytes" + "strings" "github.com/docker/docker/builder/dockerfile/instructions" "github.com/docker/docker/builder/dockerfile/parser" @@ -35,3 +36,21 @@ func Parse(b []byte) ([]instructions.Stage, error) { } return stages, err } + +// ParseCommands parses an array of commands into an array of instructions.Command; used for onbuild +func ParseCommands(cmdArray []string) ([]instructions.Command, error) { + var cmds []instructions.Command + cmdString := strings.Join(cmdArray, "\n") + ast, err := parser.Parse(strings.NewReader(cmdString)) + if err != nil { + return nil, err + } + for _, child := range ast.AST.Children { + cmd, err := instructions.ParseCommand(child) + if err != nil { + return nil, err + } + cmds = append(cmds, cmd) + } + return cmds, nil +}