From 27f964957baf5bf61b6384e6c8982afd5d24f027 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Wed, 4 Apr 2018 17:06:01 -0700 Subject: [PATCH 1/2] Support for onbuild and unit test --- executor/cmd/root.go | 20 +++++++++++ pkg/commands/commands.go | 2 ++ pkg/commands/onbuild.go | 52 +++++++++++++++++++++++++++ pkg/commands/onbuild_test.go | 69 ++++++++++++++++++++++++++++++++++++ pkg/dockerfile/dockerfile.go | 19 ++++++++++ 5 files changed, 162 insertions(+) create mode 100644 pkg/commands/onbuild.go create mode 100644 pkg/commands/onbuild_test.go diff --git a/executor/cmd/root.go b/executor/cmd/root.go index ef6c23b87..fd0b4fd3c 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 { @@ -173,6 +178,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/pkg/commands/commands.go b/pkg/commands/commands.go index fd040decc..52c55be65 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 } return nil, errors.Errorf("%s is not a supported command", cmd.Name()) } diff --git a/pkg/commands/onbuild.go b/pkg/commands/onbuild.go new file mode 100644 index 000000000..8823e5061 --- /dev/null +++ b/pkg/commands/onbuild.go @@ -0,0 +1,52 @@ +/* +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 +} + +func (o *OnBuildCommand) ExecuteCommand(config *manifest.Schema2Config) error { + logrus.Info("cmd: ONBUILD") + 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 +} + +// 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 +} From 59a5950e9e2cf3d14e5c99d8c443a2b628680909 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Wed, 4 Apr 2018 17:06:09 -0700 Subject: [PATCH 2/2] onbuild integration test --- .../dockerfiles/Dockerfile_onbuild_base | 6 +++++ .../dockerfiles/Dockerfile_test_onbuild | 6 +++++ .../dockerfiles/config_test_onbuild.json | 12 ++++++++++ integration_tests/integration_test_yaml.go | 22 ++++++++++++++++++- pkg/commands/onbuild.go | 4 +++- 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 integration_tests/dockerfiles/Dockerfile_onbuild_base create mode 100644 integration_tests/dockerfiles/Dockerfile_test_onbuild create mode 100644 integration_tests/dockerfiles/config_test_onbuild.json 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/integration_test_yaml.go b/integration_tests/integration_test_yaml.go index 3adb712d0..169594ffe 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 { @@ -102,6 +103,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 { @@ -182,8 +191,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/onbuild.go b/pkg/commands/onbuild.go index 8823e5061..db4711604 100644 --- a/pkg/commands/onbuild.go +++ b/pkg/commands/onbuild.go @@ -27,8 +27,10 @@ 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 @@ -41,7 +43,7 @@ func (o *OnBuildCommand) ExecuteCommand(config *manifest.Schema2Config) error { return nil } -// No files have changed, this command only touches metadata. +// FilesToSnapshot returns that no files have changed, this command only touches metadata. func (o *OnBuildCommand) FilesToSnapshot() []string { return []string{} }