-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bugs with .dockerignore and improve integration test
I improved handling of the .dockerignore file by: 1. Using docker's parser to parse the .dockerignore and using their helper functions to determine if a file should be deleted 2. Copying the Dockerfile we are building to /kaniko/Dockerfile so that if the Dockerfile is specified in .dockerignore it won't be deleted, and if it is specified in the .dockerignore it won't end up in the final image 3. I also improved the integration test to create a temp directory with files to ignore, and updated the .dockerignore to include exclusions (!)
- Loading branch information
Priya Wadhwa
committed
Oct 31, 2018
1 parent
6ef616b
commit 8a4b083
Showing
6 changed files
with
167 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
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"} | ||
|
||
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", path} | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters