Skip to content

Commit

Permalink
Add flag to skaffold.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Jun 25, 2020
1 parent 7c34330 commit 0cdd299
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/content/en/schemas/v2beta5.json
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,11 @@
"description": "should images be pushed to a registry. If not specified, images are pushed only if the current Kubernetes context connects to a remote cluster.",
"x-intellij-html-description": "should images be pushed to a registry. If not specified, images are pushed only if the current Kubernetes context connects to a remote cluster."
},
"tryImportMissing": {
"type": "boolean",
"description": "whether to attempt to import artifacts from Docker (either a local or remote registry) if not in the cache.",
"x-intellij-html-description": "whether to attempt to import artifacts from Docker (either a local or remote registry) if not in the cache."
},
"useBuildkit": {
"type": "boolean",
"description": "use BuildKit to build Docker images.",
Expand All @@ -1710,6 +1715,7 @@
},
"preferredOrder": [
"push",
"tryImportMissing",
"useDockerCLI",
"useBuildkit",
"concurrency"
Expand Down
4 changes: 3 additions & 1 deletion pkg/skaffold/build/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ type cache struct {
insecureRegistries map[string]bool
cacheFile string
imagesAreLocal bool
tryImportMissing bool
hashForArtifact func(ctx context.Context, a *latest.Artifact) (string, error)
}

// DependencyLister fetches a list of dependencies for an artifact
type DependencyLister func(ctx context.Context, artifact *latest.Artifact) ([]string, error)

// NewCache returns the current state of the cache
func NewCache(runCtx *runcontext.RunContext, imagesAreLocal bool, dependencies DependencyLister) (Cache, error) {
func NewCache(runCtx *runcontext.RunContext, imagesAreLocal bool, tryImportMissing bool, dependencies DependencyLister) (Cache, error) {
if !runCtx.Opts.CacheArtifacts {
return &noCache{}, nil
}
Expand Down Expand Up @@ -84,6 +85,7 @@ func NewCache(runCtx *runcontext.RunContext, imagesAreLocal bool, dependencies D
insecureRegistries: runCtx.InsecureRegistries,
cacheFile: cacheFile,
imagesAreLocal: imagesAreLocal,
tryImportMissing: tryImportMissing,
hashForArtifact: func(ctx context.Context, a *latest.Artifact) (string, error) {
return getHashForArtifact(ctx, dependencies, a, runCtx.Opts.IsDevMode())
},
Expand Down
4 changes: 4 additions & 0 deletions pkg/skaffold/build/cache/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ func (c *cache) lookupRemote(ctx context.Context, hash, tag string, entry ImageD
}

func (c *cache) tryImport(ctx context.Context, a *latest.Artifact, tag string, hash string) (ImageDetails, error) {
if !c.tryImportMissing {
return ImageDetails{}, fmt.Errorf("import of missing images disabled")
}

entry := ImageDetails{}

if !c.client.ImageExists(ctx, tag) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/build/cache/retrieve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestCacheBuildLocal(t *testing.T) {
})

// Create cache
artifactCache, err := NewCache(runCtx, true, deps)
artifactCache, err := NewCache(runCtx, true, false, deps)
t.CheckNoError(err)

// First build: Need to build both artifacts
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestCacheBuildRemote(t *testing.T) {
})

// Create cache
artifactCache, err := NewCache(runCtx, false, deps)
artifactCache, err := NewCache(runCtx, false, false, deps)
t.CheckNoError(err)

// First build: Need to build both artifacts
Expand Down
11 changes: 11 additions & 0 deletions pkg/skaffold/build/local/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Builder struct {
localDocker docker.LocalDaemon
localCluster bool
pushImages bool
tryImportMissing bool
prune bool
pruneChildren bool
skipTests bool
Expand Down Expand Up @@ -75,12 +76,18 @@ func NewBuilder(runCtx *runcontext.RunContext) (*Builder, error) {
pushImages = *runCtx.Cfg.Build.LocalBuild.Push
}

tryImportMissing := false
if runCtx.Cfg.Build.LocalBuild.TryImportMissing != nil {
tryImportMissing = *runCtx.Cfg.Build.LocalBuild.TryImportMissing
}

return &Builder{
cfg: *runCtx.Cfg.Build.LocalBuild,
kubeContext: runCtx.KubeContext,
localDocker: localDocker,
localCluster: localCluster,
pushImages: pushImages,
tryImportMissing: tryImportMissing,
skipTests: runCtx.Opts.SkipTests,
devMode: runCtx.Opts.IsDevMode(),
prune: runCtx.Opts.Prune(),
Expand All @@ -93,6 +100,10 @@ func (b *Builder) PushImages() bool {
return b.pushImages
}

func (b *Builder) TryImportMissing() bool {
return b.tryImportMissing
}

// Labels are labels specific to local builder.
func (b *Builder) Labels() map[string]string {
labels := map[string]string{
Expand Down
7 changes: 6 additions & 1 deletion pkg/skaffold/runner/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func NewForConfig(runCtx *runcontext.RunContext) (*SkaffoldRunner, error) {
return nil, fmt.Errorf("creating builder: %w", err)
}

tryImportMissing := false
if localBuilder, ok := builder.(*local.Builder); ok {
tryImportMissing = localBuilder.TryImportMissing()
}

tester := getTester(runCtx, imagesAreLocal)
syncer := getSyncer(runCtx)
deployer := getDeployer(runCtx)
Expand All @@ -73,7 +78,7 @@ func NewForConfig(runCtx *runcontext.RunContext) (*SkaffoldRunner, error) {
return append(buildDependencies, testDependencies...), nil
}

artifactCache, err := cache.NewCache(runCtx, imagesAreLocal, depLister)
artifactCache, err := cache.NewCache(runCtx, imagesAreLocal, tryImportMissing, depLister)
if err != nil {
return nil, fmt.Errorf("initializing cache: %w", err)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/skaffold/schema/latest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ type LocalBuild struct {
// connects to a remote cluster.
Push *bool `yaml:"push,omitempty"`

// TryImportMissing whether to attempt to import artifacts from
// Docker (either a local or remote registry) if not in the cache.
TryImportMissing *bool `yaml:"tryImportMissing,omitempty"`

// UseDockerCLI use `docker` command-line interface instead of Docker Engine APIs.
UseDockerCLI bool `yaml:"useDockerCLI,omitempty"`

Expand Down

0 comments on commit 0cdd299

Please sign in to comment.