Merge pull request #66 from priyawadhwa/onbuild

Onbuild command
This commit is contained in:
priyawadhwa
2018-04-12 10:08:13 -07:00
committed by GitHub
12 changed files with 214 additions and 7 deletions
-1
View File
@@ -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.
+20
View File
@@ -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 {
@@ -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
@@ -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
@@ -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
}
}
]
@@ -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",
@@ -9,4 +9,4 @@
"Mods": null
}
}
]
]
+21 -1
View File
@@ -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
+2
View File
@@ -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
}
+54
View File
@@ -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
}
+69
View File
@@ -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)
}
}
+19
View File
@@ -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
}