Skip to content

Commit

Permalink
Simplify signature
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Oct 16, 2018
1 parent ccda622 commit c26e30d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
12 changes: 4 additions & 8 deletions pkg/skaffold/build/kaniko/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,12 @@ import (
func (b *Builder) run(ctx context.Context, out io.Writer, artifact *latest.Artifact, cfg *latest.KanikoBuild) (string, error) {
initialTag := util.RandomID()

s, err := sources.Retrieve(cfg)
if err != nil {
return "", errors.Wrap(err, "retrieving build context")
}

context, err := s.Setup(ctx, out, artifact, cfg, initialTag)
s := sources.Retrieve(cfg)
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 {
Expand All @@ -66,7 +62,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")
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/skaffold/build/kaniko/sources/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
}
9 changes: 5 additions & 4 deletions pkg/skaffold/build/kaniko/sources/localdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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,
Expand Down Expand Up @@ -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)
}
17 changes: 11 additions & 6 deletions pkg/skaffold/build/kaniko/sources/sources.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,23 @@ 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
func Retrieve(cfg *latest.KanikoBuild) (BuildContextSource, error) {
func Retrieve(cfg *latest.KanikoBuild) BuildContextSource {
if cfg.BuildContext.LocalDir != nil {
return &LocalDir{}, nil
return &LocalDir{
cfg: cfg,
}
}

return &GCSBucket{
cfg: cfg,
}
return &GCSBucket{}, nil
}

func podTemplate(cfg *latest.KanikoBuild, args []string) *v1.Pod {
Expand Down

0 comments on commit c26e30d

Please sign in to comment.