Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recompute docker dependencies across dev loops. #5121

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/skaffold/docker/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

func CreateDockerTarContext(ctx context.Context, w io.Writer, workspace string, a *latest.DockerArtifact, cfg Config) error {
paths, err := GetDependencies(ctx, workspace, a.DockerfilePath, a.BuildArgs, cfg)
paths, err := GetDependenciesCached(ctx, workspace, a.DockerfilePath, a.BuildArgs, cfg)
if err != nil {
return fmt.Errorf("getting relative tar paths: %w", err)
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/skaffold/docker/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,33 @@ func NormalizeDockerfilePath(context, dockerfile string) (string, error) {
}

// GetDependencies finds the sources dependency for the given docker artifact.
// it caches the results for the computed dependency which can be used by `GetDependenciesCached`
// All paths are relative to the workspace.
func GetDependencies(ctx context.Context, workspace string, dockerfilePath string, buildArgs map[string]*string, cfg Config) ([]string, error) {
absDockerfilePath, err := NormalizeDockerfilePath(workspace, dockerfilePath)
if err != nil {
return nil, fmt.Errorf("normalizing dockerfile path: %w", err)
}
result := getDependencies(workspace, dockerfilePath, absDockerfilePath, buildArgs, cfg)
dependencyCache.Store(absDockerfilePath, result)
return resultPair(result)
}

// GetDependenciesCached reads from cache finds the sources dependency for the given docker artifact.
// All paths are relative to the workspace.
func GetDependenciesCached(ctx context.Context, workspace string, dockerfilePath string, buildArgs map[string]*string, cfg Config) ([]string, error) {
absDockerfilePath, err := NormalizeDockerfilePath(workspace, dockerfilePath)
if err != nil {
return nil, fmt.Errorf("normalizing dockerfile path: %w", err)
}

deps := dependencyCache.Exec(absDockerfilePath, func() interface{} {
return getDependencies(workspace, dockerfilePath, absDockerfilePath, buildArgs, cfg)
})
return resultPair(deps)
}

func resultPair(deps interface{}) ([]string, error) {
switch t := deps.(type) {
case error:
return nil, t
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/docker/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ func TestGetDependenciesCached(t *testing.T) {
return v
})
}
deps, err := GetDependencies(context.Background(), tmpDir.Root(), "Dockerfile", map[string]*string{}, nil)
deps, err := GetDependenciesCached(context.Background(), tmpDir.Root(), "Dockerfile", map[string]*string{}, nil)
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, deps)
})
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/skaffold/util/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ func (o *SyncStore) Exec(key string, f func() interface{}) interface{} {
return val
}

// Store will store the results for a key in a cache
// It makes sure only one execution is in-flight for a given key at any time.
func (o *SyncStore) Store(key string, r interface{}) {
o.sf.Do(fmt.Sprintf("%s-put", key), func() (interface{}, error) {
gsquared94 marked this conversation as resolved.
Show resolved Hide resolved
o.results.Store(key, r)
return nil, nil
})
}

// NewSyncStore returns a new instance of `SyncStore`
func NewSyncStore() *SyncStore {
return &SyncStore{
Expand Down