From f91dae5dc642c3ad8088b73544ea4f28fcf50381 Mon Sep 17 00:00:00 2001 From: Martin Zihlmann Date: Sun, 28 Jun 2026 17:02:03 +0100 Subject: [PATCH] drop GCS-specific integration test scaffolding --- DEVELOPMENT.md | 26 +--- cmd/executor/cmd/root.go | 30 ++--- integration/benchmark_test.go | 64 +--------- integration/cleanup.go | 37 ------ integration/config.go | 15 --- integration/images.go | 132 +++++++-------------- integration/integration_test.go | 107 +++-------------- integration/integration_with_stdin_test.go | 2 +- pkg/buildcontext/gcs.go | 14 --- pkg/util/bucket/bucket_util.go | 24 ---- scripts/integration-test.sh | 11 +- 11 files changed, 82 insertions(+), 380 deletions(-) delete mode 100644 integration/cleanup.go diff --git a/DEVELOPMENT.md b/DEVELOPMENT.md index ca5063fe6..b165230a0 100644 --- a/DEVELOPMENT.md +++ b/DEVELOPMENT.md @@ -177,36 +177,20 @@ In either case, you will need the following tools: * [`diffoci`](https://github.com/mzihlmann/diffoci#installation) -#### GCloud +#### Registry -To run integration tests with your GCloud Storage, you will also need the following tools: +To run integration tests against a real registry, you will need: -* [`gcloud`](https://cloud.google.com/sdk/install) -* [`gsutil`](https://cloud.google.com/storage/docs/gsutil_install) -* A bucket in [GCS](https://cloud.google.com/storage/) which you have write access to via - the user currently logged into `gcloud` -* An image repo which you have write access to via the user currently logged into `gcloud` +* An image repo which you have write access to * A docker account and a `~/.docker/config.json` with login credentials if you run into rate limiting problems during tests. -Once this step done, you must override the project using environment variables: - -* `GCS_BUCKET` - The name of your GCS bucket -* `IMAGE_REPO` - The path to your Docker image repo on your registry host - -This can be done as follows: +Override the target via the `IMAGE_REPO` environment variable: ```shell -export GCS_BUCKET="gs://" export IMAGE_REPO="YOUR-REGISTRY/YOUR-REPO" ``` -Login for both user and application credentials -```shell -gcloud auth login -gcloud auth application-default login -``` - Then you can launch integration tests as follows: ```shell @@ -260,7 +244,7 @@ Additionally, the integration tests can output benchmarking information to a `be `integration` directory if the `BENCHMARK` environment variable is set to `true.` ```shell -BENCHMARK=true go test -v --bucket $GCS_BUCKET --repo $IMAGE_REPO +BENCHMARK=true go test -v --repo $IMAGE_REPO ``` #### Profiling diff --git a/cmd/executor/cmd/root.go b/cmd/executor/cmd/root.go index 46b4696e2..d8decc4c3 100644 --- a/cmd/executor/cmd/root.go +++ b/cmd/executor/cmd/root.go @@ -235,26 +235,18 @@ var RootCmd = &cobra.Command{ logrus.Warnf("Unable to write benchmark file: %s", err) return } - if strings.HasPrefix(benchmarkFile, "gs://") { - logrus.Info("Uploading to gcs") - if err := buildcontext.UploadToBucket(strings.NewReader(s), benchmarkFile); err != nil { - logrus.Infof("Unable to upload %s due to %v", benchmarkFile, err) - } - logrus.Infof("Benchmark file written at %s", benchmarkFile) - } else { - f, err := os.Create(benchmarkFile) - if err != nil { - logrus.Warnf("Unable to create benchmarking file %s: %s", benchmarkFile, err) - return - } - defer f.Close() - _, err = f.WriteString(s) - if err != nil { - logrus.Warnf("Unable to write to benchmarking file %s: %s", benchmarkFile, err) - return - } - logrus.Infof("Benchmark file written at %s", benchmarkFile) + f, err := os.Create(benchmarkFile) + if err != nil { + logrus.Warnf("Unable to create benchmarking file %s: %s", benchmarkFile, err) + return + } + defer f.Close() + _, err = f.WriteString(s) + if err != nil { + logrus.Warnf("Unable to write to benchmarking file %s: %s", benchmarkFile, err) + return } + logrus.Infof("Benchmark file written at %s", benchmarkFile) } }, } diff --git a/integration/benchmark_test.go b/integration/benchmark_test.go index 815887ef5..e5ff2df25 100644 --- a/integration/benchmark_test.go +++ b/integration/benchmark_test.go @@ -21,7 +21,6 @@ import ( "fmt" "io" "os" - "os/exec" "path/filepath" "strconv" "sync" @@ -61,8 +60,7 @@ func TestSnapshotBenchmark(t *testing.T) { buildArgs := []string{"--build-arg", fmt.Sprintf("NUM=%d", num)} var benchmarkDir string benchmarkDir, *err = buildKanikoImage(t.Logf, "", dockerfile, - buildArgs, []string{}, kanikoImage, contextDir, config.gcsBucket, config.gcsClient, - config.serviceAccount, false, "", "") + buildArgs, []string{}, kanikoImage, contextDir, "", "") if *err != nil { return } @@ -114,63 +112,3 @@ func newResult(t *testing.T, f string) result { t.Log(r) return r } - -func TestSnapshotBenchmarkGcloud(t *testing.T) { - if b, err := strconv.ParseBool(os.Getenv("BENCHMARK")); err != nil || !b { - t.SkipNow() - } - cwd, err := os.Getwd() - if err != nil { - t.Fatal(err) - } - contextDir := filepath.Join(cwd, "benchmark_fs") - - nums := []int{10000, 50000, 100000, 200000, 300000, 500000, 700000} - - var wg sync.WaitGroup - t.Log("Number of Files,Total Build Time,Walking Filesystem, Resolving Files") - for _, num := range nums { - t.Run(fmt.Sprintf("test_benchmark_%d", num), func(t *testing.T) { - t.Parallel() - wg.Add(1) - go func(num int) { - dir, err := runInGcloud(contextDir, num) - if err != nil { - t.Errorf("error when running in gcloud %v", err) - return - } - r := newResult(t, filepath.Join(dir, "results")) - t.Logf("%d,%f,%f,%f, %f", num, r.totalBuildTime, r.walkingFiles, r.resolvingFiles, r.hashingFiles) - wg.Done() - defer os.Remove(dir) - defer os.Chdir(cwd) - }(num) - }) - } - wg.Wait() -} - -func runInGcloud(dir string, num int) (string, error) { - os.Chdir(dir) - cmd := exec.Command("gcloud", "builds", - "submit", "--config=cloudbuild.yaml", - fmt.Sprintf("--substitutions=_COUNT=%d", num)) - _, err := RunCommandWithoutTest(cmd) - if err != nil { - return "", err - } - - // grab gcs and to temp dir and return - tmpDir, err := os.MkdirTemp("", strconv.Itoa(num)) - if err != nil { - return "", err - } - src := fmt.Sprintf("%s/gcb/benchmark_file_%d", config.gcsBucket, num) - dest := filepath.Join(tmpDir, "results") - copyCommand := exec.Command("gsutil", "cp", src, dest) - _, err = RunCommandWithoutTest(copyCommand) - if err != nil { - return "", fmt.Errorf("failed to download file to GCS bucket %s: %w", src, err) - } - return tmpDir, nil -} diff --git a/integration/cleanup.go b/integration/cleanup.go deleted file mode 100644 index 135f4a014..000000000 --- a/integration/cleanup.go +++ /dev/null @@ -1,37 +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 ( - "log" - "os" - "os/signal" -) - -// RunOnInterrupt will execute the function f if execution is interrupted with the -// interrupt signal. -func RunOnInterrupt(f func()) { - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - go func() { - for range c { - log.Println("Interrupted, cleaning up.") - f() - os.Exit(1) - } - }() -} diff --git a/integration/config.go b/integration/config.go index 6ad993e1b..1e5e8f299 100644 --- a/integration/config.go +++ b/integration/config.go @@ -16,14 +16,7 @@ limitations under the License. package integration -import ( - "strings" - - "cloud.google.com/go/storage" -) - type integrationTestConfig struct { - gcsBucket string imageRepo string onbuildBaseImage string onbuildCopyImage string @@ -31,14 +24,6 @@ type integrationTestConfig struct { hijackBaseImage string malformedOCIImage string nvidiaOperatorBaseImage string - serviceAccount string dockerMajorVersion int - gcsClient *storage.Client dockerfilesPattern string } - -const gcrRepoPrefix string = "gcr.io/" - -func (config *integrationTestConfig) isGcrRepository() bool { - return strings.HasPrefix(config.imageRepo, gcrRepoPrefix) -} diff --git a/integration/images.go b/integration/images.go index d562468ee..99ed359cd 100644 --- a/integration/images.go +++ b/integration/images.go @@ -18,7 +18,6 @@ package integration import ( "bytes" - "context" "fmt" "os" "os/exec" @@ -29,13 +28,10 @@ import ( "strings" "sync" "testing" - "time" - "cloud.google.com/go/storage" v1 "github.com/google/go-containerregistry/pkg/v1" "github.com/osscontainertools/kaniko/pkg/timing" "github.com/osscontainertools/kaniko/pkg/util" - "github.com/osscontainertools/kaniko/pkg/util/bucket" ) const ( @@ -393,8 +389,6 @@ func checkArgsNotPrinted(dockerfile string, out []byte) error { return nil } -var bucketContextTests = []string{"Dockerfile_test_copy_bucket"} - // GetDockerImage constructs the name of the docker image that would be built with // dockerfile if it was tagged with imageRepo. func GetDockerImage(imageRepo, dockerfile string) string { @@ -503,21 +497,10 @@ func NewDockerFileBuilder() *DockerFileBuilder { return &d } -func addServiceAccountFlags(flags []string, serviceAccount string) []string { - if len(serviceAccount) > 0 { - flags = append(flags, "-e", - "GOOGLE_APPLICATION_CREDENTIALS=/secret/"+filepath.Base(serviceAccount), - "-v", filepath.Dir(serviceAccount)+":/secret/") - } else { - gcloudConfig := os.Getenv("HOME") + "/.config/gcloud" - if util.FilepathExists(gcloudConfig) { - flags = append(flags, "-v", gcloudConfig+":/root/.config/gcloud") - } - - dockerConfig := os.Getenv("HOME") + "/.docker/config.json" - if util.FilepathExists(dockerConfig) { - flags = append(flags, "-v", dockerConfig+":/root/.docker/config.json", "-e", "DOCKER_CONFIG=/root/.docker") - } +func addAuthFlags(flags []string) []string { + dockerConfig := os.Getenv("HOME") + "/.docker/config.json" + if util.FilepathExists(dockerConfig) { + flags = append(flags, "-v", dockerConfig+":/root/.docker/config.json", "-e", "DOCKER_CONFIG=/root/.docker") } return flags } @@ -570,8 +553,7 @@ func (d *DockerFileBuilder) BuildDockerImage(t *testing.T, imageRepo, dockerfile } // BuildImage will build dockerfile (located at dockerfilesPath) using both kaniko and docker. -// The resulting image will be tagged with imageRepo. If the dockerfile will be built with -// context (i.e. it is in `buildContextTests`) the context will be pulled from gcsBucket. +// The resulting image will be tagged with imageRepo. func (d *DockerFileBuilder) BuildImage(t *testing.T, config *integrationTestConfig, dockerfilesPath, dockerfile string) error { _, ex, _, _ := runtime.Caller(0) cwd := filepath.Dir(ex) @@ -597,7 +579,7 @@ func (d *DockerFileBuilder) BuildKanikoImage(t *testing.T, config *integrationTe timer := timing.Start(dockerfile + "_kaniko") defer timing.DefaultRun.Stop(timer) _, err := buildKanikoImage(t.Logf, dockerfilesPath, dockerfile, buildArgs, additionalKanikoFlags, kanikoImage, - cwd, config.gcsBucket, config.gcsClient, config.serviceAccount, false, "", "") + cwd, "", "") return err } @@ -609,7 +591,7 @@ func (d *DockerFileBuilder) BuildImageWithContext(t *testing.T, config *integrat } func (d *DockerFileBuilder) buildImage(t *testing.T, config *integrationTestConfig, dockerfilesPath, dockerfile, contextDir string) error { - gcsBucket, gcsClient, serviceAccount, imageRepo := config.gcsBucket, config.gcsClient, config.serviceAccount, config.imageRepo + imageRepo := config.imageRepo var buildArgs []string buildArgFlag := "--build-arg" @@ -625,17 +607,8 @@ func (d *DockerFileBuilder) buildImage(t *testing.T, config *integrationTestConf timing.DefaultRun.Stop(timer) - contextFlag := "-c" - contextPath := buildContextPath - for _, d := range bucketContextTests { - if d == dockerfile { - contextFlag = "-b" - contextPath = gcsBucket - } - } - additionalKanikoFlags := additionalKanikoFlagsMap[dockerfile] - additionalKanikoFlags = append(additionalKanikoFlags, contextFlag, contextPath) + additionalKanikoFlags = append(additionalKanikoFlags, "-c", buildContextPath) if _, ok := d.TestReproducibleDockerfiles[dockerfile]; ok { additionalKanikoFlags = append(additionalKanikoFlags, "--reproducible") } @@ -643,7 +616,7 @@ func (d *DockerFileBuilder) buildImage(t *testing.T, config *integrationTestConf kanikoImage := GetKanikoImage(imageRepo, dockerfile) timer = timing.Start(dockerfile + "_kaniko") if _, err := buildKanikoImage(t.Logf, dockerfilesPath, dockerfile, buildArgs, additionalKanikoFlags, kanikoImage, - contextDir, gcsBucket, gcsClient, serviceAccount, true, "", ""); err != nil { + contextDir, "", ""); err != nil { return err } timing.DefaultRun.Stop(timer) @@ -651,38 +624,9 @@ func (d *DockerFileBuilder) buildImage(t *testing.T, config *integrationTestConf return nil } -func populateVolumeCache(logf logger, serviceAccount string) error { - fmt.Println("Populating warmer cache") - _, ex, _, _ := runtime.Caller(0) - cwd := filepath.Dir(ex) - cmd := []string{ - "run", "--net=host", - "-v", os.Getenv("HOME") + "/.config/gcloud:/root/.config/gcloud", - "-v", cwd + ":/workspace", - } - for _, envVariable := range WarmerEnv { - cmd = append(cmd, "-e", envVariable) - } - cmd = addServiceAccountFlags(cmd, serviceAccount) - cmd = addCoverageFlags(cmd) - cmd = append(cmd, - WarmerImage, - "-c", cacheDir, - "-i", baseImageToCache, - ) - - warmerCmd := exec.Command("docker", cmd...) - out, err := RunCommandWithoutTest(warmerCmd) - logf(string(out)) - if err != nil { - return fmt.Errorf("failed to warm kaniko cache: %w", err) - } - return nil -} - // buildCachedImage builds the image for testing caching via kaniko where version is the nth time this image has been built func (d *DockerFileBuilder) buildCachedImage(logf logger, config *integrationTestConfig, cacheRepo, dockerfilesPath, dockerfile string, version int, args []string) error { - imageRepo, serviceAccount := config.imageRepo, config.serviceAccount + imageRepo := config.imageRepo _, ex, _, _ := runtime.Caller(0) cwd := filepath.Dir(ex) @@ -724,7 +668,7 @@ func (d *DockerFileBuilder) buildCachedImage(logf logger, config *integrationTes } buildArgs = append(buildArgs, "--build-arg", "IMAGE_REPO="+config.imageRepo) - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = addCoverageFlags(dockerRunFlags) dockerRunFlags = append(dockerRunFlags, executorImage, "-f", path.Join(buildContextPath, dockerfilesPath, dockerfile), @@ -776,7 +720,7 @@ func (d *DockerFileBuilder) buildCachedImageInContext(logf logger, config *integ for _, envVariable := range KanikoEnv { dockerRunFlags = append(dockerRunFlags, "-e", envVariable) } - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = addCoverageFlags(dockerRunFlags) dockerRunFlags = append(dockerRunFlags, ExecutorImage, "-f", path.Join(buildContextPath, dockerfile), @@ -792,9 +736,37 @@ func (d *DockerFileBuilder) buildCachedImageInContext(logf logger, config *integ return err } +func populateVolumeCache(logf logger) error { + fmt.Println("Populating warmer cache") + _, ex, _, _ := runtime.Caller(0) + cwd := filepath.Dir(ex) + cmd := []string{ + "run", "--net=host", + "-v", cwd + ":/workspace", + } + for _, envVariable := range WarmerEnv { + cmd = append(cmd, "-e", envVariable) + } + cmd = addAuthFlags(cmd) + cmd = addCoverageFlags(cmd) + cmd = append(cmd, + WarmerImage, + "-c", cacheDir, + "-i", baseImageToCache, + ) + + warmerCmd := exec.Command("docker", cmd...) + out, err := RunCommandWithoutTest(warmerCmd) + logf(string(out)) + if err != nil { + return fmt.Errorf("failed to warm kaniko cache: %w", err) + } + return nil +} + // buildCachedImage builds the image for testing caching via kaniko warmer cache where version is the nth time this image has been built func (d *DockerFileBuilder) buildWarmerImage(logf logger, config *integrationTestConfig, dockerfilesPath, dockerfile string, version int, args []string, cache bool) error { - imageRepo, serviceAccount := config.imageRepo, config.serviceAccount + imageRepo := config.imageRepo _, ex, _, _ := runtime.Caller(0) cwd := filepath.Dir(ex) @@ -811,7 +783,7 @@ func (d *DockerFileBuilder) buildWarmerImage(logf logger, config *integrationTes if exec, ok := executorImages[dockerfile]; ok { executorImage = exec } - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = addCoverageFlags(dockerRunFlags) dockerRunFlags = append(dockerRunFlags, executorImage, "-f", path.Join(buildContextPath, dockerfilesPath, dockerfile), @@ -850,7 +822,7 @@ func (d *DockerFileBuilder) buildWarmerImage(logf logger, config *integrationTes } // buildRelativePathsImage builds the images for testing passing relatives paths to Kaniko -func (d *DockerFileBuilder) buildRelativePathsImage(logf logger, imageRepo, dockerfile, serviceAccount, buildContextPath string) error { +func (d *DockerFileBuilder) buildRelativePathsImage(logf logger, imageRepo, dockerfile, buildContextPath string) error { _, ex, _, _ := runtime.Caller(0) cwd := filepath.Dir(ex) @@ -882,7 +854,7 @@ func (d *DockerFileBuilder) buildRelativePathsImage(logf logger, imageRepo, dock if exec, ok := executorImages[dockerfile]; ok { executorImage = exec } - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = addCoverageFlags(dockerRunFlags) dockerRunFlags = append(dockerRunFlags, executorImage, "-f", dockerfile, @@ -926,10 +898,6 @@ func buildKanikoImage( kanikoArgs []string, kanikoImage string, contextDir string, - gcsBucket string, - gcsClient *storage.Client, - serviceAccount string, - shdUpload bool, tlsCACert string, dockerConfig string, ) (string, error) { @@ -941,16 +909,6 @@ func buildKanikoImage( if b, err := strconv.ParseBool(os.Getenv("BENCHMARK")); err == nil && b { benchmarkEnv = "BENCHMARK_FILE=/benchmarks/" + dockerfile - if shdUpload { - benchmarkFile := path.Join(benchmarkDir, dockerfile) - fileName := fmt.Sprintf("run_%s_%s", time.Now().Format("2006-01-02-15:04"), dockerfile) - dst := path.Join("benchmarks", fileName) - file, err := os.Open(benchmarkFile) - if err != nil { - return "", err - } - defer bucket.Upload(context.Background(), gcsBucket, dst, file, gcsClient) - } } // build kaniko image @@ -981,7 +939,7 @@ func buildKanikoImage( if dockerConfig != "" { dockerRunFlags = append(dockerRunFlags, "-v", dockerConfig+":/kaniko/.docker/config.json:ro") } else { - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) } kanikoDockerfilePath := path.Join(buildContextPath, dockerfilesPath, dockerfile) diff --git a/integration/integration_test.go b/integration/integration_test.go index 86248ee00..275c6a656 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -19,7 +19,6 @@ package integration import ( "archive/tar" "bytes" - "context" "errors" "flag" "fmt" @@ -43,9 +42,7 @@ import ( "github.com/google/go-containerregistry/pkg/v1/remote" "github.com/google/go-containerregistry/pkg/v1/tarball" "github.com/osscontainertools/kaniko/pkg/timing" - "github.com/osscontainertools/kaniko/pkg/util/bucket" "github.com/osscontainertools/kaniko/testutil" - "google.golang.org/api/option" ) var ( @@ -75,35 +72,6 @@ func getDockerMajorVersion() int { } func launchTests(m *testing.M) (int, error) { - if config.isGcrRepository() { - contextFilePath, err := CreateIntegrationTarball() - if err != nil { - return 1, fmt.Errorf("Failed to create tarball of integration files for build context: %w", err) - } - - bucketName, item, err := bucket.GetNameAndFilepathFromURI(config.gcsBucket) - if err != nil { - return 1, fmt.Errorf("failed to get bucket name from uri: %w", err) - } - contextFile, err := os.Open(contextFilePath) - if err != nil { - return 1, fmt.Errorf("failed to read file at path %v: %w", contextFilePath, err) - } - err = bucket.Upload(context.Background(), bucketName, item, contextFile, config.gcsClient) - if err != nil { - return 1, fmt.Errorf("Failed to upload build context: %w", err) - } - - if err = os.Remove(contextFilePath); err != nil { - return 1, fmt.Errorf("Failed to remove tarball at %s: %w", contextFilePath, err) - } - - deleteFunc := func() { - bucket.Delete(context.Background(), bucketName, item, config.gcsClient) - } - RunOnInterrupt(deleteFunc) - defer deleteFunc() - } if err := buildRequiredImages(); err != nil { return 1, fmt.Errorf("Error while building images: %w", err) } @@ -340,7 +308,7 @@ func testGitBuildcontextHelper(t *testing.T, url string, commit string, branch s // Build with kaniko kanikoImage := GetKanikoImage(config.imageRepo, imageName) dockerRunFlags := []string{"run", "--net=host"} - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = addCoverageFlags(dockerRunFlags) dockerRunFlags = append(dockerRunFlags, ExecutorImage, "-f", dockerfile, @@ -411,7 +379,7 @@ func TestGitBuildcontextSubPath(t *testing.T) { // Build with kaniko kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_git_subpath") dockerRunFlags := []string{"run", "--net=host"} - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = addCoverageFlags(dockerRunFlags) dockerRunFlags = append( dockerRunFlags, @@ -455,7 +423,7 @@ func TestBuildViaRegistryMirrors(t *testing.T) { // Build with kaniko kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_registry_mirror") dockerRunFlags := []string{"run", "--net=host"} - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = addCoverageFlags(dockerRunFlags) dockerRunFlags = append(dockerRunFlags, ExecutorImage, "-f", dockerfile, @@ -497,7 +465,7 @@ func TestBuildViaRegistryMap(t *testing.T) { // Build with kaniko kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_registry_map") dockerRunFlags := []string{"run", "--net=host"} - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = addCoverageFlags(dockerRunFlags) dockerRunFlags = append(dockerRunFlags, ExecutorImage, "-f", dockerfile, @@ -524,7 +492,7 @@ func TestBuildSkipFallback(t *testing.T) { // Build with kaniko kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_registry_skip_fallback") dockerRunFlags := []string{"run", "--net=host"} - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = addCoverageFlags(dockerRunFlags) dockerRunFlags = append(dockerRunFlags, ExecutorImage, "-f", dockerfile, @@ -565,7 +533,7 @@ func TestKanikoDir(t *testing.T) { // Build with kaniko kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_kaniko_dir") dockerRunFlags := []string{"run", "--net=host"} - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = addCoverageFlags(dockerRunFlags) dockerRunFlags = append(dockerRunFlags, ExecutorImage, "-f", dockerfile, @@ -609,7 +577,7 @@ func TestBuildWithLabels(t *testing.T) { // Build with kaniko kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_label") dockerRunFlags := []string{"run", "--net=host"} - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = addCoverageFlags(dockerRunFlags) dockerRunFlags = append(dockerRunFlags, ExecutorImage, "-f", dockerfile, @@ -650,7 +618,7 @@ func TestBuildWithHTTPError(t *testing.T) { // Build with kaniko kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_add_404") dockerRunFlags := []string{"run", "--net=host"} - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = addCoverageFlags(dockerRunFlags) dockerRunFlags = append(dockerRunFlags, ExecutorImage, "-f", dockerfile, @@ -929,7 +897,7 @@ func TestSnapshotModes(t *testing.T) { t.Helper() tag := GetKanikoImage(config.imageRepo, dockerfile+"-snapshot-"+mode) kanikoArgs := []string{"-c", buildContextPath, "--snapshot-mode=" + mode} - if _, err := buildKanikoImage(t.Logf, dockerfilesPath, dockerfile, buildArgs, kanikoArgs, tag, cwd, config.gcsBucket, config.gcsClient, config.serviceAccount, false, "", ""); err != nil { + if _, err := buildKanikoImage(t.Logf, dockerfilesPath, dockerfile, buildArgs, kanikoArgs, tag, cwd, "", ""); err != nil { t.Fatalf("kaniko build with --snapshot-mode=%s: %v", mode, err) } return tag @@ -971,7 +939,7 @@ func TestReproducible(t *testing.T) { flags := []string{"-c", buildContextPath, "--reproducible"} flags = append(flags, additionalKanikoFlagsMap[dockerfile]...) _, err := buildKanikoImage(t.Logf, dockerfilesPath, dockerfile, buildArgs, flags, ref, - cwd, config.gcsBucket, config.gcsClient, config.serviceAccount, false, "", "") + cwd, "", "") if err != nil { t.Fatalf("build %s -> %s: %v", dockerfile, ref, err) } @@ -1033,7 +1001,7 @@ func TestCache(t *testing.T) { func TestWarmer(t *testing.T) { t.Parallel() - populateVolumeCache(t.Logf, config.serviceAccount) + populateVolumeCache(t.Logf) for dockerfile := range imageBuilder.TestWarmerDockerfiles { t.Run("test_warmer_"+dockerfile, func(t *testing.T) { @@ -1086,7 +1054,7 @@ func TestWarmerTwice(t *testing.T) { dockerRunFlags := []string{"run", "--net=host"} // mz364: run as host user so cache files can be removed dockerRunFlags = append(dockerRunFlags, "-u", fmt.Sprintf("%d:%d", os.Getuid(), os.Getgid())) - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) for _, envVariable := range WarmerEnv { dockerRunFlags = append(dockerRunFlags, "-e", envVariable) } @@ -1193,7 +1161,6 @@ func TestRelativePaths(t *testing.T) { t.Logf, config.imageRepo, dockerfile, - config.serviceAccount, contextPath, ) if err != nil { @@ -1253,7 +1220,7 @@ func TestExitCodePropagation(t *testing.T) { "run", "-v", contextVolume, } - dockerFlags = addServiceAccountFlags(dockerFlags, "") + dockerFlags = addAuthFlags(dockerFlags) dockerFlags = addCoverageFlags(dockerFlags) dockerFlags = append(dockerFlags, ExecutorImage, "-c", "dir:///workspace/", @@ -1306,7 +1273,7 @@ func TestBuildWithAnnotations(t *testing.T) { // Build with kaniko kanikoImage := GetKanikoImage(config.imageRepo, "Dockerfile_test_annotation") dockerRunFlags := []string{"run", "--net=host"} - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = addCoverageFlags(dockerRunFlags) dockerRunFlags = append(dockerRunFlags, ExecutorImage, "-f", dockerfile, @@ -1345,7 +1312,7 @@ func TestPushFromArtifact(t *testing.T) { runExecutor := func(executorArgs ...string) { t.Helper() flags := []string{"run", "--rm", "--net=host", "-v", outFlag + ":/out"} - flags = addServiceAccountFlags(flags, config.serviceAccount) + flags = addAuthFlags(flags) flags = addCoverageFlags(flags) flags = append(flags, ExecutorImage) flags = append(flags, executorArgs...) @@ -1539,57 +1506,19 @@ func (i imageDetails) String() string { func initIntegrationTestConfig() *integrationTestConfig { var c integrationTestConfig - var gcsEndpoint string - var disableGcsAuth bool - flag.StringVar(&c.gcsBucket, "bucket", "gs://kaniko-test-bucket", "The gcs bucket argument to uploaded the tar-ed contents of the `integration` dir to.") - flag.StringVar(&c.imageRepo, "repo", "gcr.io/kaniko-test", "The (docker) image repo to build and push images to during the test. `gcloud` must be authenticated with this repo or serviceAccount must be set.") - flag.StringVar(&c.serviceAccount, "serviceAccount", "", "The path to the service account push images to GCR and upload/download files to GCS.") - flag.StringVar(&gcsEndpoint, "gcs-endpoint", "", "Custom endpoint for GCS. Used for local integration tests") - flag.BoolVar(&disableGcsAuth, "disable-gcs-auth", false, "Disable GCS Authentication. Used for local integration tests") + flag.StringVar(&c.imageRepo, "repo", "gcr.io/kaniko-test", "The (docker) image repo to build and push images to during the test.") // adds the possibility to run a single dockerfile. This is useful since running all images can exhaust the dockerhub pull limit flag.StringVar(&c.dockerfilesPattern, "dockerfiles-pattern", "Dockerfile_test*", "The pattern to match dockerfiles with") flag.StringVar(&coverageDir, "coverage-dir", "", "Collect executor coverage data into this directory.") flag.Parse() - if len(c.serviceAccount) > 0 { - absPath, err := filepath.Abs("../" + c.serviceAccount) - if err != nil { - log.Fatalf("Error getting absolute path for service account: %s\n", c.serviceAccount) - } - if _, err := os.Stat(absPath); os.IsNotExist(err) { - log.Fatalf("Service account does not exist: %s\n", absPath) - } - c.serviceAccount = absPath - os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", absPath) - } - if c.imageRepo == "" { log.Fatal("You must provide a image repository") } - - if c.isGcrRepository() && c.gcsBucket == "" { - log.Fatalf("You must provide a gcs bucket when using a Google Container Registry (\"%s\" was provided)", c.imageRepo) - } if !strings.HasSuffix(c.imageRepo, "/") { c.imageRepo = c.imageRepo + "/" } - if c.gcsBucket != "" { - var opts []option.ClientOption - if gcsEndpoint != "" { - opts = append(opts, option.WithEndpoint(gcsEndpoint)) - } - if disableGcsAuth { - opts = append(opts, option.WithoutAuthentication()) - } - - gcsClient, err := bucket.NewClient(context.Background(), opts...) - if err != nil { - log.Fatalf("Could not create a new Google Storage Client: %s", err) - } - c.gcsClient = gcsClient - } - c.dockerMajorVersion = getDockerMajorVersion() c.onbuildBaseImage = c.imageRepo + "onbuild-base:latest" c.onbuildCopyImage = c.imageRepo + "onbuild-copy:latest" @@ -1650,7 +1579,7 @@ func TestAlpineTLS(t *testing.T) { []string{"-c", buildContextPath}, dest, cwd, - "", nil, config.serviceAccount, false, caCert, filepath.Join(dockerConfigDir, "config.json"), + caCert, filepath.Join(dockerConfigDir, "config.json"), ) if err != nil { t.Error(err) @@ -1672,7 +1601,7 @@ func TestCustomPlatformVariant(t *testing.T) { []string{"--custom-platform=linux/arm/v7", "-c", buildContextPath}, kanikoImage, cwd, - "", nil, config.serviceAccount, false, "", "", + "", "", ) if err != nil { t.Fatal(err) diff --git a/integration/integration_with_stdin_test.go b/integration/integration_with_stdin_test.go index e7e6d5059..646d7fb9c 100644 --- a/integration/integration_with_stdin_test.go +++ b/integration/integration_with_stdin_test.go @@ -103,7 +103,7 @@ func TestBuildWithStdin(t *testing.T) { gzCmd := exec.Command("gzip", "-9") dockerRunFlags := []string{"run", "--interactive", "--net=host", "-v", cwd + ":/workspace"} - dockerRunFlags = addServiceAccountFlags(dockerRunFlags, config.serviceAccount) + dockerRunFlags = addAuthFlags(dockerRunFlags) dockerRunFlags = append(dockerRunFlags, ExecutorImage, "-f", dockerfile, diff --git a/pkg/buildcontext/gcs.go b/pkg/buildcontext/gcs.go index 6e10151dd..221c25b4d 100644 --- a/pkg/buildcontext/gcs.go +++ b/pkg/buildcontext/gcs.go @@ -19,7 +19,6 @@ package buildcontext import ( "context" "fmt" - "io" "os" "path/filepath" @@ -43,19 +42,6 @@ func (g *GCS) UnpackTarFromBuildContext() (string, error) { return kConfig.BuildContextDir, unpackTarFromGCSBucket(bucketName, filepath, kConfig.BuildContextDir) } -func UploadToBucket(r io.Reader, dest string) error { - ctx := context.Background() - bucketName, filepath, err := bucket.GetNameAndFilepathFromURI(dest) - if err != nil { - return fmt.Errorf("getting bucketname and filepath from dest: %w", err) - } - client, err := bucket.NewClient(ctx) - if err != nil { - return err - } - return bucket.Upload(ctx, bucketName, filepath, r, client) -} - // unpackTarFromGCSBucket unpacks the context.tar.gz file in the given bucket to the given directory func unpackTarFromGCSBucket(bucketName, item, directory string) error { // Get the tar from the bucket diff --git a/pkg/util/bucket/bucket_util.go b/pkg/util/bucket/bucket_util.go index f62d496d3..34a66a986 100644 --- a/pkg/util/bucket/bucket_util.go +++ b/pkg/util/bucket/bucket_util.go @@ -18,7 +18,6 @@ package bucket import ( "context" - "fmt" "io" "net/url" "strings" @@ -28,29 +27,6 @@ import ( "google.golang.org/api/option" ) -// Upload uploads everything from Reader to the bucket under path -func Upload(ctx context.Context, bucketName string, path string, r io.Reader, client *storage.Client) error { - bucket := client.Bucket(bucketName) - w := bucket.Object(path).NewWriter(ctx) - if _, err := io.Copy(w, r); err != nil { - return err - } - if err := w.Close(); err != nil { - return err - } - return nil -} - -// Delete will remove the content at path. path should be the full path -// to a file in GCS. -func Delete(ctx context.Context, bucketName string, path string, client *storage.Client) error { - err := client.Bucket(bucketName).Object(path).Delete(ctx) - if err != nil { - return fmt.Errorf("failed to delete file at %s in gcs bucket %v: %w", path, bucketName, err) - } - return err -} - // ReadCloser will create io.ReadCloser for the specified bucket and path func ReadCloser(ctx context.Context, bucketName string, path string, client *storage.Client) (io.ReadCloser, error) { bucket := client.Bucket(bucketName) diff --git a/scripts/integration-test.sh b/scripts/integration-test.sh index a3bb63e02..9ce17901a 100755 --- a/scripts/integration-test.sh +++ b/scripts/integration-test.sh @@ -38,13 +38,6 @@ function start_local_tls_registry { fi } -# TODO: to get this working, we need a way to override the gcs endpoint of kaniko at runtime -# If this is done, integration test main includes flags --gcs-endpoint and --disable-gcs-auth -# to mock the gcs endpoints and upload files to the fake-gcs-server -function start_fake_gcs_server { - docker start fake-gcs-server || docker run -d -p 4443:4443 --name fake-gcs-server fsouza/fake-gcs-server -scheme http -} - IMAGE_REPO="${IMAGE_REPO:-gcr.io/kaniko-test}" docker version @@ -62,16 +55,14 @@ if [[ -n ${DOCKERFILE_PATTERN} ]]; then fi if [[ -n ${LOCAL} ]]; then - echo "running in local mode, mocking registry and gcs bucket..." + echo "running in local mode, mocking registry..." start_local_registry start_local_tls_registry IMAGE_REPO="localhost:5000" - GCS_BUCKET="" fi FLAGS+=( - "--bucket=${GCS_BUCKET}" "--repo=${IMAGE_REPO}" )