Skip to content

Commit

Permalink
Merge pull request #336 from priyawadhwa/string
Browse files Browse the repository at this point in the history
Add CacheCommand to DockerCommand interface
  • Loading branch information
priyawadhwa authored Sep 7, 2018
2 parents 7a7d544 + e300101 commit 637f14e
Show file tree
Hide file tree
Showing 18 changed files with 133 additions and 126 deletions.
22 changes: 10 additions & 12 deletions pkg/commands/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package commands

import (
"path/filepath"
"strings"

"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"

Expand All @@ -44,21 +43,15 @@ type AddCommand struct {
// 2. If <src> is a local tar archive:
// -If <src> is a local tar archive, it is unpacked at the dest, as 'tar -x' would
func (a *AddCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
srcs := a.cmd.SourcesAndDest[:len(a.cmd.SourcesAndDest)-1]
dest := a.cmd.SourcesAndDest[len(a.cmd.SourcesAndDest)-1]

logrus.Infof("cmd: Add %s", srcs)
logrus.Infof("dest: %s", dest)

// First, resolve any environment replacement
replacementEnvs := buildArgs.ReplacementEnvs(config.Env)
resolvedEnvs, err := util.ResolveEnvironmentReplacementList(a.cmd.SourcesAndDest, replacementEnvs, true)
if err != nil {
return err
}
dest = resolvedEnvs[len(resolvedEnvs)-1]
dest := resolvedEnvs[len(resolvedEnvs)-1]
// Resolve wildcards and get a list of resolved sources
srcs, err = util.ResolveSources(resolvedEnvs, a.buildcontext)
srcs, err := util.ResolveSources(resolvedEnvs, a.buildcontext)
if err != nil {
return err
}
Expand Down Expand Up @@ -112,7 +105,12 @@ func (a *AddCommand) FilesToSnapshot() []string {
return a.snapshotFiles
}

// CreatedBy returns some information about the command for the image config
func (a *AddCommand) CreatedBy() string {
return strings.Join(a.cmd.SourcesAndDest, " ")
// String returns some information about the command for the image config
func (a *AddCommand) String() string {
return a.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (a *AddCommand) CacheCommand() bool {
return false
}
15 changes: 8 additions & 7 deletions pkg/commands/arg.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ limitations under the License.
package commands

import (
"strings"

"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/sirupsen/logrus"
)

type ArgCommand struct {
Expand All @@ -32,7 +29,6 @@ type ArgCommand struct {

// ExecuteCommand only needs to add this ARG key/value as seen
func (r *ArgCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
logrus.Info("ARG")
replacementEnvs := buildArgs.ReplacementEnvs(config.Env)
resolvedKey, err := util.ResolveEnvironmentReplacement(r.cmd.Key, replacementEnvs, false)
if err != nil {
Expand All @@ -55,7 +51,12 @@ func (r *ArgCommand) FilesToSnapshot() []string {
return []string{}
}

// CreatedBy returns some information about the command for the image config history
func (r *ArgCommand) CreatedBy() string {
return strings.Join([]string{r.cmd.Name(), r.cmd.Key}, " ")
// String returns some information about the command for the image config history
func (r *ArgCommand) String() string {
return r.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (r *ArgCommand) CacheCommand() bool {
return false
}
20 changes: 8 additions & 12 deletions pkg/commands/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type CmdCommand struct {
// ExecuteCommand executes the CMD command
// Argument handling is the same as RUN.
func (c *CmdCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
logrus.Info("cmd: CMD")
var newCommand []string
if c.cmd.PrependShell {
// This is the default shell on Linux
Expand All @@ -60,15 +59,12 @@ func (c *CmdCommand) FilesToSnapshot() []string {
return []string{}
}

// CreatedBy returns some information about the command for the image config history
func (c *CmdCommand) CreatedBy() string {
cmd := []string{"CMD"}
cmdLine := strings.Join(c.cmd.CmdLine, " ")
if c.cmd.PrependShell {
// TODO: Support shell command here
shell := []string{"/bin/sh", "-c"}
appendedShell := append(cmd, shell...)
return strings.Join(append(appendedShell, cmdLine), " ")
}
return strings.Join(append(cmd, cmdLine), " ")
// String returns some information about the command for the image config history
func (c *CmdCommand) String() string {
return c.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (c *CmdCommand) CacheCommand() bool {
return false
}
7 changes: 5 additions & 2 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ type DockerCommand interface {
// 2. Updating metadata fields in the config
// It should not change the config history.
ExecuteCommand(*v1.Config, *dockerfile.BuildArgs) error
// The config history has a "created by" field, should return information about the command
CreatedBy() string
// Returns a string representation of the command
String() string
// A list of files to snapshot, empty for metadata commands or nil if we don't know
FilesToSnapshot() []string
// Return true if this command should be true
// Currently only true for RUN
CacheCommand() bool
}

func GetCommand(cmd instructions.Command, buildcontext string) (DockerCommand, error) {
Expand Down
23 changes: 10 additions & 13 deletions pkg/commands/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ package commands
import (
"os"
"path/filepath"
"strings"

"github.com/GoogleContainerTools/kaniko/pkg/constants"

"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/sirupsen/logrus"
)

type CopyCommand struct {
Expand All @@ -37,12 +35,6 @@ type CopyCommand struct {
}

func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
srcs := c.cmd.SourcesAndDest[:len(c.cmd.SourcesAndDest)-1]
dest := c.cmd.SourcesAndDest[len(c.cmd.SourcesAndDest)-1]

logrus.Infof("cmd: copy %s", srcs)
logrus.Infof("dest: %s", dest)

// Resolve from
if c.cmd.From != "" {
c.buildcontext = filepath.Join(constants.KanikoDir, c.cmd.From)
Expand All @@ -53,9 +45,9 @@ func (c *CopyCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.Bu
if err != nil {
return err
}
dest = resolvedEnvs[len(resolvedEnvs)-1]
dest := resolvedEnvs[len(resolvedEnvs)-1]
// Resolve wildcards and get a list of resolved sources
srcs, err = util.ResolveSources(resolvedEnvs, c.buildcontext)
srcs, err := util.ResolveSources(resolvedEnvs, c.buildcontext)
if err != nil {
return err
}
Expand Down Expand Up @@ -106,7 +98,12 @@ func (c *CopyCommand) FilesToSnapshot() []string {
return c.snapshotFiles
}

// CreatedBy returns some information about the command for the image config
func (c *CopyCommand) CreatedBy() string {
return strings.Join(c.cmd.SourcesAndDest, " ")
// String returns some information about the command for the image config
func (c *CopyCommand) String() string {
return c.cmd.String()
}

// CacheCommand returns true since this command should be cached
func (c *CopyCommand) CacheCommand() bool {
return false
}
20 changes: 8 additions & 12 deletions pkg/commands/entrypoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ type EntrypointCommand struct {

// ExecuteCommand handles command processing similar to CMD and RUN,
func (e *EntrypointCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
logrus.Info("cmd: ENTRYPOINT")
var newCommand []string
if e.cmd.PrependShell {
// This is the default shell on Linux
Expand All @@ -58,15 +57,12 @@ func (e *EntrypointCommand) FilesToSnapshot() []string {
return []string{}
}

// CreatedBy returns some information about the command for the image config history
func (e *EntrypointCommand) CreatedBy() string {
entrypoint := []string{"ENTRYPOINT"}
cmdLine := strings.Join(e.cmd.CmdLine, " ")
if e.cmd.PrependShell {
// TODO: Support shell command here
shell := []string{"/bin/sh", "-c"}
appendedShell := append(entrypoint, shell...)
return strings.Join(append(appendedShell, cmdLine), " ")
}
return strings.Join(append(entrypoint, cmdLine), " ")
// String returns some information about the command for the image config history
func (e *EntrypointCommand) String() string {
return e.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (e *EntrypointCommand) CacheCommand() bool {
return false
}
19 changes: 8 additions & 11 deletions pkg/commands/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,18 @@ limitations under the License.
package commands

import (
"strings"

"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"

"github.com/GoogleContainerTools/kaniko/pkg/util"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/sirupsen/logrus"
)

type EnvCommand struct {
cmd *instructions.EnvCommand
}

func (e *EnvCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
logrus.Info("cmd: ENV")
newEnvs := e.cmd.Env
replacementEnvs := buildArgs.ReplacementEnvs(config.Env)
return util.UpdateConfigEnv(newEnvs, config, replacementEnvs)
Expand All @@ -43,11 +39,12 @@ func (e *EnvCommand) FilesToSnapshot() []string {
return []string{}
}

// CreatedBy returns some information about the command for the image config history
func (e *EnvCommand) CreatedBy() string {
envArray := []string{e.cmd.Name()}
for _, pair := range e.cmd.Env {
envArray = append(envArray, pair.Key+"="+pair.Value)
}
return strings.Join(envArray, " ")
// String returns some information about the command for the image config history
func (e *EnvCommand) String() string {
return e.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (e *EnvCommand) CacheCommand() bool {
return false
}
10 changes: 7 additions & 3 deletions pkg/commands/expose.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ func (r *ExposeCommand) FilesToSnapshot() []string {
return []string{}
}

func (r *ExposeCommand) CreatedBy() string {
s := []string{r.cmd.Name()}
return strings.Join(append(s, r.cmd.Ports...), " ")
func (r *ExposeCommand) String() string {
return r.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (r *ExposeCommand) CacheCommand() bool {
return false
}
16 changes: 7 additions & 9 deletions pkg/commands/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ limitations under the License.
package commands

import (
"strings"

"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/sirupsen/logrus"
)

type HealthCheckCommand struct {
Expand All @@ -31,8 +28,6 @@ type HealthCheckCommand struct {

// ExecuteCommand handles command processing similar to CMD and RUN,
func (h *HealthCheckCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
logrus.Info("cmd: HEALTHCHECK")

check := v1.HealthConfig(*h.cmd.Health)
config.Healthcheck = &check

Expand All @@ -44,9 +39,12 @@ func (h *HealthCheckCommand) FilesToSnapshot() []string {
return []string{}
}

// CreatedBy returns some information about the command for the image config history
func (h *HealthCheckCommand) CreatedBy() string {
entrypoint := []string{"HEALTHCHECK"}
// String returns some information about the command for the image config history
func (h *HealthCheckCommand) String() string {
return h.cmd.String()
}

return strings.Join(append(entrypoint, strings.Join(h.cmd.Health.Test, " ")), " ")
// CacheCommand returns false since this command shouldn't be cached
func (h *HealthCheckCommand) CacheCommand() bool {
return false
}
18 changes: 8 additions & 10 deletions pkg/commands/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package commands

import (
"strings"

"github.com/GoogleContainerTools/kaniko/pkg/dockerfile"

"github.com/GoogleContainerTools/kaniko/pkg/util"
Expand All @@ -32,7 +30,6 @@ type LabelCommand struct {
}

func (r *LabelCommand) ExecuteCommand(config *v1.Config, buildArgs *dockerfile.BuildArgs) error {
logrus.Info("cmd: LABEL")
return updateLabels(r.cmd.Labels, config, buildArgs)
}

Expand Down Expand Up @@ -72,11 +69,12 @@ func (r *LabelCommand) FilesToSnapshot() []string {
return []string{}
}

// CreatedBy returns some information about the command for the image config history
func (r *LabelCommand) CreatedBy() string {
l := []string{r.cmd.Name()}
for _, kvp := range r.cmd.Labels {
l = append(l, kvp.String())
}
return strings.Join(l, " ")
// String returns some information about the command for the image config history
func (r *LabelCommand) String() string {
return r.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (r *LabelCommand) CacheCommand() bool {
return false
}
11 changes: 8 additions & 3 deletions pkg/commands/onbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ func (o *OnBuildCommand) FilesToSnapshot() []string {
return []string{}
}

// CreatedBy returns some information about the command for the image config history
func (o *OnBuildCommand) CreatedBy() string {
return o.cmd.Expression
// String returns some information about the command for the image config history
func (o *OnBuildCommand) String() string {
return o.cmd.String()
}

// CacheCommand returns false since this command shouldn't be cached
func (o *OnBuildCommand) CacheCommand() bool {
return false
}
17 changes: 8 additions & 9 deletions pkg/commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,12 @@ func (r *RunCommand) FilesToSnapshot() []string {
return nil
}

// CreatedBy returns some information about the command for the image config
func (r *RunCommand) CreatedBy() string {
cmdLine := strings.Join(r.cmd.CmdLine, " ")
if r.cmd.PrependShell {
// TODO: Support shell command here
shell := []string{"/bin/sh", "-c"}
return strings.Join(append(shell, cmdLine), " ")
}
return cmdLine
// String returns some information about the command for the image config
func (r *RunCommand) String() string {
return r.cmd.String()
}

// CacheCommand returns true since this command should be cached
func (r *RunCommand) CacheCommand() bool {
return true
}
Loading

0 comments on commit 637f14e

Please sign in to comment.