Fixed merge conflict

This commit is contained in:
Priya Wadhwa 2018-04-06 12:09:31 -07:00
commit c10c293f6b
No known key found for this signature in database
GPG Key ID: 0D0DAFD8F7AA73AE
8 changed files with 175 additions and 0 deletions

View File

@ -155,6 +155,7 @@ func execute() error {
if err != nil {
return err
}
util.MoveVolumeWhitelistToWhitelist()
if contents == nil {
logrus.Info("No files were changed, appending empty layer to config.")
sourceImage.AppendConfigHistory(constants.Author, true)

View File

@ -0,0 +1,7 @@
FROM gcr.io/google-appengine/debian9
RUN mkdir /foo
RUN echo "hello" > /foo/hey
VOLUME /foo/bar /tmp
ENV VOL /baz/bat
VOLUME ["${VOL}"]
RUN echo "hello again" > /tmp/hey

View File

@ -0,0 +1,12 @@
[
{
"Image1": "gcr.io/kbuild-test/docker-test-volume:latest",
"Image2": "gcr.io/kbuild-test/kbuild-test-volume:latest",
"DiffType": "File",
"Diff": {
"Adds": null,
"Dels": null,
"Mods": null
}
}
]

View File

@ -94,6 +94,14 @@ var fileTests = []struct {
kanikoContext: buildcontextPath,
repo: "test-workdir",
},
{
description: "test volume",
dockerfilePath: "/workspace/integration_tests/dockerfiles/Dockerfile_test_volume",
configPath: "/workspace/integration_tests/dockerfiles/config_test_volume.json",
dockerContext: buildcontextPath,
kbuildContext: buildcontextPath,
repo: "test-volume",
},
{
description: "test add",
dockerfilePath: "/workspace/integration_tests/dockerfiles/Dockerfile_test_add",

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.VolumeCommand:
return &VolumeCommand{cmd: c}, nil
}
return nil, errors.Errorf("%s is not a supported command", cmd.Name())
}

71
pkg/commands/volume.go Normal file
View File

@ -0,0 +1,71 @@
/*
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"
"os"
"strings"
)
type VolumeCommand struct {
cmd *instructions.VolumeCommand
snapshotFiles []string
}
func (v *VolumeCommand) ExecuteCommand(config *manifest.Schema2Config) error {
logrus.Info("cmd: VOLUME")
volumes := v.cmd.Volumes
resolvedVolumes, err := util.ResolveEnvironmentReplacementList(volumes, config.Env, true)
if err != nil {
return err
}
existingVolumes := config.Volumes
if existingVolumes == nil {
existingVolumes = map[string]struct{}{}
}
for _, volume := range resolvedVolumes {
var x struct{}
existingVolumes[volume] = x
err := util.AddPathToVolumeWhitelist(volume)
if err != nil {
return err
}
logrus.Infof("Creating directory %s", volume)
if err := os.MkdirAll(volume, 0755); err != nil {
return err
}
//Check if directory already exists?
v.snapshotFiles = append(v.snapshotFiles, volume)
}
config.Volumes = existingVolumes
return nil
}
func (v *VolumeCommand) FilesToSnapshot() []string {
return v.snapshotFiles
}
func (v *VolumeCommand) CreatedBy() string {
return strings.Join(append([]string{v.cmd.Name()}, v.cmd.Volumes...), " ")
}

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/testutil"
"github.com/containers/image/manifest"
"github.com/docker/docker/builder/dockerfile/instructions"
"testing"
)
func TestUpdateVolume(t *testing.T) {
cfg := &manifest.Schema2Config{
Env: []string{
"VOLUME=/etc",
},
Volumes: map[string]struct{}{},
}
volumes := []string{
"/tmp",
"/var/lib",
"$VOLUME",
}
volumeCmd := &VolumeCommand{
cmd: &instructions.VolumeCommand{
Volumes: volumes,
},
snapshotFiles: []string{},
}
expectedVolumes := map[string]struct{}{
"/tmp": {},
"/var/lib": {},
"/etc": {},
}
err := volumeCmd.ExecuteCommand(cfg)
testutil.CheckErrorAndDeepEqual(t, false, err, expectedVolumes, cfg.Volumes)
}

View File

@ -31,6 +31,7 @@ import (
)
var whitelist = []string{"/kaniko"}
var volumeWhitelist = []string{}
// ExtractFileSystemFromImage pulls an image and unpacks it to a file system at root
func ExtractFileSystemFromImage(img string) error {
@ -156,6 +157,25 @@ func CreateFile(path string, reader io.Reader, perm os.FileMode) error {
return dest.Chmod(perm)
}
// AddPathToVolumeWhitelist adds the given path to the volume whitelist
// It will get snapshotted when the VOLUME command is run then ignored
// for subsequent commands.
func AddPathToVolumeWhitelist(path string) error {
logrus.Infof("adding %s to volume whitelist", path)
volumeWhitelist = append(volumeWhitelist, path)
return nil
}
// MoveVolumeWhitelistToWhitelist copies over all directories that were volume mounted
// in this step to be whitelisted for all subsequent docker commands.
func MoveVolumeWhitelistToWhitelist() error {
if len(volumeWhitelist) > 0 {
whitelist = append(whitelist, volumeWhitelist...)
volumeWhitelist = []string{}
}
return nil
}
// DownloadFileToDest downloads the file at rawurl to the given dest for the ADD command
// From add command docs:
// 1. If <src> is a remote file URL: