Remove support for .dockerignore
Remove support for .dockerignore file until I can change it to ignore files in memory instead of deleting them Refer to #466
This commit is contained in:
parent
097ccb67a7
commit
313250f321
|
|
@ -25,10 +25,8 @@ import (
|
||||||
"github.com/GoogleContainerTools/kaniko/pkg/buildcontext"
|
"github.com/GoogleContainerTools/kaniko/pkg/buildcontext"
|
||||||
"github.com/GoogleContainerTools/kaniko/pkg/config"
|
"github.com/GoogleContainerTools/kaniko/pkg/config"
|
||||||
"github.com/GoogleContainerTools/kaniko/pkg/constants"
|
"github.com/GoogleContainerTools/kaniko/pkg/constants"
|
||||||
"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
|
|
||||||
"github.com/GoogleContainerTools/kaniko/pkg/executor"
|
"github.com/GoogleContainerTools/kaniko/pkg/executor"
|
||||||
"github.com/GoogleContainerTools/kaniko/pkg/util"
|
"github.com/GoogleContainerTools/kaniko/pkg/util"
|
||||||
"github.com/docker/docker/pkg/fileutils"
|
|
||||||
"github.com/genuinetools/amicontained/container"
|
"github.com/genuinetools/amicontained/container"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
@ -67,7 +65,7 @@ var RootCmd = &cobra.Command{
|
||||||
if err := resolveDockerfilePath(); err != nil {
|
if err := resolveDockerfilePath(); err != nil {
|
||||||
return errors.Wrap(err, "error resolving dockerfile path")
|
return errors.Wrap(err, "error resolving dockerfile path")
|
||||||
}
|
}
|
||||||
return removeIgnoredFiles()
|
return nil
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if !checkContained() {
|
if !checkContained() {
|
||||||
|
|
@ -200,29 +198,6 @@ func resolveSourceContext() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeIgnoredFiles() error {
|
|
||||||
if !dockerfile.DockerignoreExists(opts) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
ignore, err := dockerfile.ParseDockerignore(opts)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
logrus.Infof("Removing ignored files from build context: %s", ignore)
|
|
||||||
files, err := util.RelativeFiles("", opts.SrcContext)
|
|
||||||
if err != nil {
|
|
||||||
return errors.Wrap(err, "getting all files in src context")
|
|
||||||
}
|
|
||||||
for _, f := range files {
|
|
||||||
if rm, _ := fileutils.Matches(f, ignore); rm {
|
|
||||||
if err := os.RemoveAll(f); err != nil {
|
|
||||||
logrus.Errorf("Error removing %s from build context", f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func exit(err error) {
|
func exit(err error) {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
||||||
|
|
@ -1,112 +0,0 @@
|
||||||
/*
|
|
||||||
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 integration
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path"
|
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
var filesToIgnore = []string{"ignore/fo*", "!ignore/foobar", "ignore/Dockerfile_test_ignore"}
|
|
||||||
|
|
||||||
const (
|
|
||||||
ignoreDir = "ignore"
|
|
||||||
ignoreDockerfile = "Dockerfile_test_ignore"
|
|
||||||
ignoreDockerfileContents = `FROM scratch
|
|
||||||
COPY . .`
|
|
||||||
)
|
|
||||||
|
|
||||||
// Set up a test dir to ignore with the structure:
|
|
||||||
// ignore
|
|
||||||
// -- Dockerfile_test_ignore
|
|
||||||
// -- foo
|
|
||||||
// -- foobar
|
|
||||||
|
|
||||||
func setupIgnoreTestDir() error {
|
|
||||||
if err := os.MkdirAll(ignoreDir, 0750); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
// Create and write contents to dockerfile
|
|
||||||
path := filepath.Join(ignoreDir, ignoreDockerfile)
|
|
||||||
f, err := os.Create(path)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
if _, err := f.Write([]byte(ignoreDockerfileContents)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
additionalFiles := []string{"ignore/foo", "ignore/foobar"}
|
|
||||||
for _, add := range additionalFiles {
|
|
||||||
a, err := os.Create(add)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer a.Close()
|
|
||||||
}
|
|
||||||
return generateDockerIgnore()
|
|
||||||
}
|
|
||||||
|
|
||||||
// generate the .dockerignore file
|
|
||||||
func generateDockerIgnore() error {
|
|
||||||
f, err := os.Create(".dockerignore")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
contents := strings.Join(filesToIgnore, "\n")
|
|
||||||
if _, err := f.Write([]byte(contents)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateDockerignoreImages(imageRepo string) error {
|
|
||||||
|
|
||||||
dockerfilePath := filepath.Join(ignoreDir, ignoreDockerfile)
|
|
||||||
|
|
||||||
dockerImage := strings.ToLower(imageRepo + dockerPrefix + ignoreDockerfile)
|
|
||||||
dockerCmd := exec.Command("docker", "build",
|
|
||||||
"-t", dockerImage,
|
|
||||||
"-f", path.Join(dockerfilePath),
|
|
||||||
".")
|
|
||||||
_, err := RunCommandWithoutTest(dockerCmd)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Failed to build image %s with docker command \"%s\": %s", dockerImage, dockerCmd.Args, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, ex, _, _ := runtime.Caller(0)
|
|
||||||
cwd := filepath.Dir(ex)
|
|
||||||
kanikoImage := GetKanikoImage(imageRepo, ignoreDockerfile)
|
|
||||||
kanikoCmd := exec.Command("docker",
|
|
||||||
"run",
|
|
||||||
"-v", os.Getenv("HOME")+"/.config/gcloud:/root/.config/gcloud",
|
|
||||||
"-v", cwd+":/workspace",
|
|
||||||
ExecutorImage,
|
|
||||||
"-f", path.Join(buildContextPath, dockerfilePath),
|
|
||||||
"-d", kanikoImage,
|
|
||||||
"-c", buildContextPath)
|
|
||||||
|
|
||||||
_, err = RunCommandWithoutTest(kanikoCmd)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
@ -275,31 +275,6 @@ func TestCache(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDockerignore(t *testing.T) {
|
|
||||||
t.Run(fmt.Sprintf("test_%s", ignoreDockerfile), func(t *testing.T) {
|
|
||||||
if err := setupIgnoreTestDir(); err != nil {
|
|
||||||
t.Fatalf("error setting up ignore test dir: %v", err)
|
|
||||||
}
|
|
||||||
if err := generateDockerignoreImages(config.imageRepo); err != nil {
|
|
||||||
t.Fatalf("error generating dockerignore test images: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dockerImage := GetDockerImage(config.imageRepo, ignoreDockerfile)
|
|
||||||
kanikoImage := GetKanikoImage(config.imageRepo, ignoreDockerfile)
|
|
||||||
|
|
||||||
// container-diff
|
|
||||||
daemonDockerImage := daemonPrefix + dockerImage
|
|
||||||
containerdiffCmd := exec.Command("container-diff", "diff",
|
|
||||||
daemonDockerImage, kanikoImage,
|
|
||||||
"-q", "--type=file", "--type=metadata", "--json")
|
|
||||||
diff := RunCommand(containerdiffCmd, t)
|
|
||||||
t.Logf("diff = %s", string(diff))
|
|
||||||
|
|
||||||
expected := fmt.Sprintf(emptyContainerDiff, dockerImage, kanikoImage, dockerImage, kanikoImage)
|
|
||||||
checkContainerDiffOutput(t, diff, expected)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
type fileDiff struct {
|
type fileDiff struct {
|
||||||
Name string
|
Name string
|
||||||
Size int
|
Size int
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue