diff --git a/pkg/skaffold/build/kaniko/run.go b/pkg/skaffold/build/kaniko/run.go index f4be5e92389..071d762eca5 100644 --- a/pkg/skaffold/build/kaniko/run.go +++ b/pkg/skaffold/build/kaniko/run.go @@ -45,11 +45,11 @@ func (b *Builder) run(ctx context.Context, out io.Writer, artifact *latest.Artif return "", errors.Wrap(err, "retrieving build context") } - context, err := s.Setup(ctx, out, artifact, cfg, initialTag) + context, err := s.Setup(ctx, out, artifact, initialTag) if err != nil { return "", errors.Wrap(err, "setting up build context") } - defer s.Cleanup(ctx, cfg) + defer s.Cleanup(ctx) client, err := kubernetes.GetClientset() if err != nil { @@ -66,7 +66,7 @@ func (b *Builder) run(ctx context.Context, out io.Writer, artifact *latest.Artif args = append(args, docker.GetBuildArgs(artifact.DockerArtifact)...) pods := client.CoreV1().Pods(cfg.Namespace) - p, err := pods.Create(s.Pod(cfg, args)) + p, err := pods.Create(s.Pod(args)) if err != nil { return "", errors.Wrap(err, "creating kaniko pod") } diff --git a/pkg/skaffold/build/kaniko/sources/gcs.go b/pkg/skaffold/build/kaniko/sources/gcs.go index 7ee714d728b..082d70f8465 100644 --- a/pkg/skaffold/build/kaniko/sources/gcs.go +++ b/pkg/skaffold/build/kaniko/sources/gcs.go @@ -31,12 +31,13 @@ import ( ) type GCSBucket struct { + cfg *latest.KanikoBuild tarName string } // Setup uploads the context to the provided GCS bucket -func (g *GCSBucket) Setup(ctx context.Context, out io.Writer, artifact *latest.Artifact, cfg *latest.KanikoBuild, initialTag string) (string, error) { - bucket := cfg.BuildContext.GCSBucket +func (g *GCSBucket) Setup(ctx context.Context, out io.Writer, artifact *latest.Artifact, initialTag string) (string, error) { + bucket := g.cfg.BuildContext.GCSBucket if bucket == "" { guessedProjectID, err := gcp.ExtractProjectID(artifact.ImageName) if err != nil { @@ -53,13 +54,13 @@ func (g *GCSBucket) Setup(ctx context.Context, out io.Writer, artifact *latest.A return "", errors.Wrap(err, "uploading sources to GCS") } - context := fmt.Sprintf("gs://%s/%s", cfg.BuildContext.GCSBucket, g.tarName) + context := fmt.Sprintf("gs://%s/%s", g.cfg.BuildContext.GCSBucket, g.tarName) return context, nil } // Pod returns the pod template for this builder -func (g *GCSBucket) Pod(cfg *latest.KanikoBuild, args []string) *v1.Pod { - return podTemplate(cfg, args) +func (g *GCSBucket) Pod(args []string) *v1.Pod { + return podTemplate(g.cfg, args) } // ModifyPod does nothing here, since we just need to let kaniko run to completion @@ -68,11 +69,12 @@ func (g *GCSBucket) ModifyPod(ctx context.Context, p *v1.Pod) error { } // Cleanup deletes the tarball from the GCS bucket -func (g *GCSBucket) Cleanup(ctx context.Context, cfg *latest.KanikoBuild) error { +func (g *GCSBucket) Cleanup(ctx context.Context) error { c, err := cstorage.NewClient(ctx) if err != nil { return err } defer c.Close() - return c.Bucket(cfg.BuildContext.GCSBucket).Object(g.tarName).Delete(ctx) + + return c.Bucket(g.cfg.BuildContext.GCSBucket).Object(g.tarName).Delete(ctx) } diff --git a/pkg/skaffold/build/kaniko/sources/localdir.go b/pkg/skaffold/build/kaniko/sources/localdir.go index 3deb80f2790..3a9acb496df 100644 --- a/pkg/skaffold/build/kaniko/sources/localdir.go +++ b/pkg/skaffold/build/kaniko/sources/localdir.go @@ -42,11 +42,12 @@ const ( // LocalDir refers to kaniko using a local directory as a buildcontext // skaffold copies the buildcontext into the local directory via kubectl cp type LocalDir struct { + cfg *latest.KanikoBuild tarPath string } // Setup for LocalDir creates a tarball of the buildcontext and stores it in /tmp -func (g *LocalDir) Setup(ctx context.Context, out io.Writer, artifact *latest.Artifact, cfg *latest.KanikoBuild, initialTag string) (string, error) { +func (g *LocalDir) Setup(ctx context.Context, out io.Writer, artifact *latest.Artifact, initialTag string) (string, error) { g.tarPath = filepath.Join("/tmp", fmt.Sprintf("context-%s.tar.gz", initialTag)) color.Default.Fprintln(out, "Storing build context at", g.tarPath) @@ -63,8 +64,8 @@ func (g *LocalDir) Setup(ctx context.Context, out io.Writer, artifact *latest.Ar } // Pod returns the pod template to ModifyPod -func (g *LocalDir) Pod(cfg *latest.KanikoBuild, args []string) *v1.Pod { - p := podTemplate(cfg, args) +func (g *LocalDir) Pod(args []string) *v1.Pod { + p := podTemplate(g.cfg, args) // Include the emptyDir volume and volume source in both containers v := v1.Volume{ Name: constants.DefaultKanikoEmptyDirName, @@ -119,6 +120,6 @@ func (g *LocalDir) ModifyPod(ctx context.Context, p *v1.Pod) error { } // Cleanup deletes the buidcontext tarball stored on the local filesystem -func (g *LocalDir) Cleanup(ctx context.Context, cfg *latest.KanikoBuild) error { +func (g *LocalDir) Cleanup(ctx context.Context) error { return os.Remove(g.tarPath) } diff --git a/pkg/skaffold/build/kaniko/sources/sources.go b/pkg/skaffold/build/kaniko/sources/sources.go index a51df326c84..0bcb08097e7 100644 --- a/pkg/skaffold/build/kaniko/sources/sources.go +++ b/pkg/skaffold/build/kaniko/sources/sources.go @@ -28,10 +28,10 @@ import ( // BuildContextSource is the generic type for the different build context sources the kaniko builder can use type BuildContextSource interface { - Setup(ctx context.Context, out io.Writer, artifact *latest.Artifact, cfg *latest.KanikoBuild, initialTag string) (string, error) - Pod(cfg *latest.KanikoBuild, args []string) *v1.Pod + Setup(ctx context.Context, out io.Writer, artifact *latest.Artifact, initialTag string) (string, error) + Pod(args []string) *v1.Pod ModifyPod(ctx context.Context, p *v1.Pod) error - Cleanup(ctx context.Context, cfg *latest.KanikoBuild) error + Cleanup(ctx context.Context) error } // Retrieve returns the correct build context based on the config