Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run command #33

Merged
merged 7 commits into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 33 additions & 1 deletion executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package cmd

import (
"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"
Expand Down Expand Up @@ -88,8 +89,39 @@ func execute() error {
return err
}

// Execute commands here
// Set environment variables within the image
if err := image.SetEnvVariables(sourceImage); err != nil {
return err
}

imageConfig := sourceImage.Config()
// Currently only supports single stage builds
for _, stage := range stages {
for _, cmd := range stage.Commands {
dockerCommand, err := commands.GetCommand(cmd)
if err != nil {
return err
}
if err := dockerCommand.ExecuteCommand(imageConfig); err != nil {
return err
}
// Now, we get the files to snapshot from this command and take the snapshot
snapshotFiles := dockerCommand.FilesToSnapshot()
contents, err := snapshotter.TakeSnapshot(snapshotFiles)
if err != nil {
return err
}
if contents == nil {
logrus.Info("No files were changed, appending empty layer to config.")
sourceImage.AppendConfigHistory(constants.Author, true)
continue
}
// Append the layer to the image
if err := sourceImage.AppendLayer(contents, constants.Author); err != nil {
return err
}
}
}
// Push the image
return image.PushImage(sourceImage, destination)
}
19 changes: 19 additions & 0 deletions integration_tests/dockerfiles/Dockerfile_test_run
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2018 Google, Inc. All rights reserved.
#
# 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.

FROM gcr.io/google-appengine/debian9
RUN echo "hey" > /etc/foo
RUN apt-get update && apt-get install -y \
bzr \
cvs \
19 changes: 19 additions & 0 deletions integration_tests/dockerfiles/Dockerfile_test_run_2
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2018 Google, Inc. All rights reserved.
#
# 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.

# Test to make sure the executor builds an image correctly
# when no files are changed

FROM gcr.io/google-appengine/debian9
RUN echo "hey"
48 changes: 48 additions & 0 deletions integration_tests/dockerfiles/config_test_run.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[
{
"Image1": "gcr.io/kbuild-test/docker-test-run:latest",
"Image2": "gcr.io/kbuild-test/kbuild-test-run:latest",
"DiffType": "File",
"Diff": {
"Adds": null,
"Dels": null,
"Mods": [
{
"Name": "/var/log/dpkg.log",
"Size1": 57425,
"Size2": 57425
},
{
"Name": "/var/log/apt/term.log",
"Size1": 24400,
"Size2": 24400
},
{
"Name": "/var/cache/ldconfig/aux-cache",
"Size1": 8057,
"Size2": 8057
},
{
"Name": "/var/log/apt/history.log",
"Size1": 5089,
"Size2": 5089
},
{
"Name": "/var/log/alternatives.log",
"Size1": 2579,
"Size2": 2579
},
{
"Name": "/usr/lib/python2.7/dist-packages/keyrings/__init__.pyc",
"Size1": 140,
"Size2": 140
},
{
"Name": "/usr/lib/python2.7/dist-packages/lazr/__init__.pyc",
"Size1": 136,
"Size2": 136
}
]
}
}
]
12 changes: 12 additions & 0 deletions integration_tests/dockerfiles/config_test_run_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"Image1": "gcr.io/kbuild-test/docker-test-run-2:latest",
"Image2": "gcr.io/kbuild-test/kbuild-test-run-2:latest",
"DiffType": "File",
"Diff": {
"Adds": null,
"Dels": null,
"Mods": null
}
}
]
14 changes: 14 additions & 0 deletions integration_tests/integration_test_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ var tests = []struct {
context: "integration_tests/dockerfiles/",
repo: "extract-filesystem",
},
{
description: "test run",
dockerfilePath: "/workspace/integration_tests/dockerfiles/Dockerfile_test_run",
configPath: "/workspace/integration_tests/dockerfiles/config_test_run.json",
context: "integration_tests/dockerfiles/",
repo: "test-run",
},
{
description: "test run no files changed",
dockerfilePath: "/workspace/integration_tests/dockerfiles/Dockerfile_test_run_2",
configPath: "/workspace/integration_tests/dockerfiles/config_test_run_2.json",
context: "integration_tests/dockerfiles/",
repo: "test-run-2",
},
}

type step struct {
Expand Down
43 changes: 43 additions & 0 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
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/containers/image/manifest"
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/pkg/errors"
)

type DockerCommand interface {
// ExecuteCommand is responsible for:
// 1. Making required changes to the filesystem (ex. copying files for ADD/COPY or setting ENV variables)
// 2. Updating metadata fields in the config
// It should not change the config history.
ExecuteCommand(*manifest.Schema2Config) error
// The config history has a "created by" field, should return information about the command
CreatedBy() string
// A list of files to snapshot, empty for metadata commands or nil if we don't know
FilesToSnapshot() []string
}

func GetCommand(cmd instructions.Command) (DockerCommand, error) {
switch c := cmd.(type) {
case *instructions.RunCommand:
return &RunCommand{cmd: c}, nil
}
return nil, errors.Errorf("%s is not a supported command", cmd.Name())
}
66 changes: 66 additions & 0 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
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/containers/image/manifest"
"github.com/docker/docker/builder/dockerfile/instructions"
"github.com/sirupsen/logrus"
"os"
"os/exec"
"strings"
)

type RunCommand struct {
cmd *instructions.RunCommand
}

func (r *RunCommand) ExecuteCommand(config *manifest.Schema2Config) error {
var newCommand []string
if r.cmd.PrependShell {
// This is the default shell on Linux
// TODO: Support shell command here
shell := []string{"/bin/sh", "-c"}
newCommand = append(shell, strings.Join(r.cmd.CmdLine, " "))
} else {
newCommand = r.cmd.CmdLine
}

logrus.Infof("cmd: %s", newCommand[0])
logrus.Infof("args: %s", newCommand[1:])

cmd := exec.Command(newCommand[0], newCommand[1:]...)
cmd.Stdout = os.Stdout
return cmd.Run()
}

// FilesToSnapshot returns nil for this command because we don't know which files
// have changed, so we snapshot the entire system.
func (r *RunCommand) FilesToSnapshot() []string {
return nil
}

// Author returns some information about the command for the image config
func (r *RunCommand) CreatedBy() string {
cmdLine := strings.Join(r.cmd.CmdLine, " ")
if r.cmd.PrependShell {
// TODO: Support shell command here
shell := []string{"/bin/sh", "-c"}
return strings.Join(append(shell, cmdLine), " ")
}
return cmdLine
}
2 changes: 2 additions & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ const (
WorkspaceDir = "/workspace"

WhitelistPath = "/proc/self/mountinfo"

Author = "kbuild"
)
13 changes: 13 additions & 0 deletions pkg/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/containers/image/signature"
"github.com/containers/image/transports/alltransports"
"github.com/sirupsen/logrus"
"os"
)

// sourceImage is the image that will be modified by the executor
Expand Down Expand Up @@ -55,6 +56,18 @@ func PushImage(ms *img.MutableSource, destImg string) error {
return copy.Image(policyContext, destRef, srcRef, nil)
}

// SetEnvVariables sets environment variables as specified in the image
func SetEnvVariables(ms *img.MutableSource) error {
envVars := ms.Env()
for key, val := range envVars {
if err := os.Setenv(key, val); err != nil {
return err
}
logrus.Debugf("Setting environment variable %s=%s", key, val)
}
return nil
}

func getPolicyContext() (*signature.PolicyContext, error) {
policyContext, err := signature.NewPolicyContext(&signature.Policy{
Default: signature.PolicyRequirements{signature.NewPRInsecureAcceptAnything()},
Expand Down
Loading