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

root: add --registry-mirror flag #836

Merged
merged 25 commits into from
Feb 19, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
3873aa0
root: add --registry-mirror flag
greut Oct 25, 2019
235fc4a
util: add test to RegistryMirror option
greut Oct 25, 2019
dd9758c
integration: build using registry-mirror
greut Nov 8, 2019
73f7ea0
fixup! integration: build using registry-mirror
greut Nov 8, 2019
6e2287c
readme: add --registry-mirror
greut Nov 10, 2019
980c87a
Merge remote-tracking branch 'origin/master' into registry-mirror
greut Dec 5, 2019
6e331de
fixup! Merge remote-tracking branch 'origin/master' into registry-mirror
greut Dec 6, 2019
fbdb8f3
Merge branch 'master' into registry-mirror
tejal29 Dec 9, 2019
3678b70
Merge remote-tracking branch 'origin/master' into registry-mirror
greut Dec 22, 2019
908025b
integration: removing test on docker
greut Dec 22, 2019
c7d173c
integration-test: using registry-mirror
greut Dec 22, 2019
9fb2111
fixup! integration-test: using registry-mirror
greut Dec 22, 2019
e05bdd5
fixup! fixup! integration-test: using registry-mirror
greut Dec 23, 2019
3ad9f7d
fixup! fixup! fixup! integration-test: using registry-mirror
greut Dec 23, 2019
f721212
fix base image link.
tejal29 Jan 17, 2020
2a4cb90
remove registry name from dockerfile
tejal29 Jan 17, 2020
20604df
fix integration test
tejal29 Jan 17, 2020
a6add78
do not use the git repo as context
tejal29 Jan 17, 2020
140879f
Merge remote-tracking branch 'origin/master' into registry-mirror
greut Jan 18, 2020
e26bd67
fixup! Merge remote-tracking branch 'origin/master' into registry-mirror
greut Jan 18, 2020
47ab3fe
Merge branch 'master' into registry-mirror
tejal29 Jan 29, 2020
611ba51
fix web editor conflicts resolutions
tejal29 Jan 29, 2020
3cd216f
add the registry mirror flag
tejal29 Jan 30, 2020
edc4eb3
Merge remote-tracking branch 'origin/master' into registry-mirror
greut Feb 16, 2020
474fdcf
image: fix registry mirror setup
greut Feb 16, 2020
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ _If you are interested in contributing to kaniko, see [DEVELOPMENT.md](DEVELOPME
- [--insecure](#--insecure)
- [--insecure-pull](#--insecure-pull)
- [--no-push](#--no-push)
- [--registry-mirror](#--registry-mirror)
- [--reproducible](#--reproducible)
- [--single-snapshot](#--single-snapshot)
- [--skip-tls-verify](#--skip-tls-verify)
Expand Down Expand Up @@ -476,6 +477,10 @@ Set this flag if you want to pull images from a plain HTTP registry. It is suppo

Set this flag if you only want to build the image, without pushing to a registry.

#### --registry-mirror

Set this flag if you want to use a registry mirror instead of default `index.docker.io`.

#### --reproducible

Set this flag to strip timestamps out of the built image and make it reproducible.
Expand Down
1 change: 1 addition & 0 deletions cmd/executor/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func addKanikoOptionsFlags() {
RootCmd.PersistentFlags().DurationVarP(&opts.CacheTTL, "cache-ttl", "", time.Hour*336, "Cache timeout in hours. Defaults to two weeks.")
RootCmd.PersistentFlags().VarP(&opts.InsecureRegistries, "insecure-registry", "", "Insecure registry using plain HTTP to push and pull. Set it repeatedly for multiple registries.")
RootCmd.PersistentFlags().VarP(&opts.SkipTLSVerifyRegistries, "skip-tls-verify-registry", "", "Insecure registry ignoring TLS verify to push and pull. Set it repeatedly for multiple registries.")
RootCmd.PersistentFlags().StringVarP(&opts.RegistryMirror, "registry-mirror", "", "", "Registry mirror to use has pull-through cache instead of docker.io.")
}

// addHiddenFlags marks certain flags as hidden from the executor help text
Expand Down
20 changes: 20 additions & 0 deletions integration/dockerfiles/Dockerfile_test_run_3
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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 google-appengine/debian9@sha256:1d6a9a6d106bd795098f60f4abb7083626354fa6735e81743c7f8cfca11259f0
RUN echo "hey"
MAINTAINER kaniko
47 changes: 45 additions & 2 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const (
}
}
]`
dockerfileTestRun = "integration/dockerfiles/Dockerfile_test_run_2"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -183,7 +184,6 @@ func TestRun(t *testing.T) {

func TestGitBuildcontext(t *testing.T) {
repo := "github.com/GoogleContainerTools/kaniko"
dockerfile := "integration/dockerfiles/Dockerfile_test_run_2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causing the integration tests to fail as the references to dockerfile have not been updated

# github.com/GoogleContainerTools/kaniko/integration [github.com/GoogleContainerTools/kaniko/integration.test]
./integration_test.go:193:10: undefined: dockerfile
./integration_test.go:206:10: undefined: dockerfile
./integration_test.go:248:10: undefined: dockerfile
./integration_test.go:286:2: undefined: dockerfile
./integration_test.go:291:10: undefined: dockerfile


// Build with docker
dockerImage := GetDockerImage(config.imageRepo, "Dockerfile_test_git")
Expand Down Expand Up @@ -226,14 +226,55 @@ func TestGitBuildcontext(t *testing.T) {

func TestGitBuildContextWithBranch(t *testing.T) {
repo := "github.com/GoogleContainerTools/kaniko#refs/tags/v0.10.0"
dockerfile := "integration/dockerfiles/Dockerfile_test_run_2"

// Build with docker
dockerImage := GetDockerImage(config.imageRepo, "Dockerfile_test_git")
dockerCmd := exec.Command("docker",
append([]string{"build",
"-t", dockerImage,
"-f", dockerfileTestRun,
repo})...)
out, err := RunCommandWithoutTest(dockerCmd)
if err != nil {
t.Errorf("Failed to build image %s with docker command \"%s\": %s %s", dockerImage, dockerCmd.Args, err, string(out))
}

// Build with kaniko
kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_git")
kanikoCmd := exec.Command("docker",
append([]string{"run",
"-v", os.Getenv("HOME") + "/.config/gcloud:/root/.config/gcloud",
ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
"-c", fmt.Sprintf("git://%s", repo)})...)

out, err = RunCommandWithoutTest(kanikoCmd)
if err != nil {
t.Errorf("Failed to build image %s with kaniko command \"%s\": %v %s", dockerImage, kanikoCmd.Args, err, string(out))
}

// container-diff
daemonDockerImage := daemonPrefix + dockerImage
containerdiffCmd := exec.Command("container-diff", "diff", "--no-cache",
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)
}

func TestBuildViaRegistryMirror(t *testing.T) {
repo := "github.com/GoogleContainerTools/kaniko"

// Build with docker
dockerImage := GetDockerImage(config.imageRepo, "Dockerfile_test_git")
dockerCmd := exec.Command("docker",
append([]string{"build",
"-t", dockerImage,
"-f", dockerfileTestRun,
repo})...)
out, err := RunCommandWithoutTest(dockerCmd)
if err != nil {
Expand All @@ -242,12 +283,14 @@ func TestGitBuildContextWithBranch(t *testing.T) {

// Build with kaniko
kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_git")
dockerfile = "integration/dockerfiles/Dockerfile_test_run_3"
kanikoCmd := exec.Command("docker",
append([]string{"run",
"-v", os.Getenv("HOME") + "/.config/gcloud:/root/.config/gcloud",
ExecutorImage,
"-f", dockerfile,
"-d", kanikoImage,
"--registry-mirror", "gcr.io",
"-c", fmt.Sprintf("git://%s", repo)})...)

out, err = RunCommandWithoutTest(kanikoCmd)
Expand Down
1 change: 1 addition & 0 deletions pkg/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type KanikoOptions struct {
Cleanup bool
InsecureRegistries multiArg
SkipTLSVerifyRegistries multiArg
RegistryMirror string
}

// WarmerOptions are options that are set by command line arguments to the cache warmer.
Expand Down
17 changes: 16 additions & 1 deletion pkg/util/image_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,26 @@ func remoteImage(image string, opts *config.KanikoOptions) (v1.Image, error) {

func prepareRemoteRequest(ref name.Reference, opts *config.KanikoOptions) ([]remote.Option, error) {
registryName := ref.Context().RegistryStr()
var newReg name.Registry
var err error

if opts.RegistryMirror != "" && registryName == name.DefaultRegistry {
greut marked this conversation as resolved.
Show resolved Hide resolved
registryName = opts.RegistryMirror

newReg, err = name.NewRegistry(opts.RegistryMirror, name.StrictValidation)
if err != nil {
return nil, err
}
}

if opts.InsecurePull || opts.InsecureRegistries.Contains(registryName) {
newReg, err := name.NewRegistry(registryName, name.WeakValidation, name.Insecure)
newReg, err = name.NewRegistry(registryName, name.WeakValidation, name.Insecure)
if err != nil {
return nil, err
}
}

if newReg != ref.Context().Registry {
if tag, ok := ref.(name.Tag); ok {
tag.Repository.Registry = newReg
ref = tag
Expand Down
15 changes: 15 additions & 0 deletions pkg/util/image_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func Test_StandardImage(t *testing.T) {
}, &config.KanikoOptions{})
testutil.CheckErrorAndDeepEqual(t, false, err, nil, actual)
}

func Test_ScratchImage(t *testing.T) {
stages, err := parse(dockerfile)
if err != nil {
Expand Down Expand Up @@ -93,6 +94,20 @@ func Test_TarImage(t *testing.T) {
testutil.CheckErrorAndDeepEqual(t, false, err, nil, actual)
}

func Test_ScratchImageFromMirror(t *testing.T) {
stages, err := parse(dockerfile)
if err != nil {
t.Error(err)
}
actual, err := RetrieveSourceImage(config.KanikoStage{
Stage: stages[1],
}, &config.KanikoOptions{
RegistryMirror: "mirror.gcr.io",
})
expected := empty.Image
testutil.CheckErrorAndDeepEqual(t, false, err, expected, actual)
}

// parse parses the contents of a Dockerfile and returns a list of commands
func parse(s string) ([]instructions.Stage, error) {
p, err := parser.Parse(bytes.NewReader([]byte(s)))
Expand Down