Skip to content

Commit

Permalink
[v3] (2/3) Move v1 specific runner components to runner/v1.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwenma committed May 6, 2021
1 parent ce34201 commit ebcbdd9
Show file tree
Hide file tree
Showing 27 changed files with 307 additions and 268 deletions.
3 changes: 2 additions & 1 deletion cmd/skaffold/app/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/parser"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
v1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/v1"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/defaults"
latest_v1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/validation"
Expand Down Expand Up @@ -64,7 +65,7 @@ func createNewRunner(out io.Writer, opts config.SkaffoldOptions) (runner.Runner,
}

instrumentation.InitMeterFromConfig(configs, opts.User)
runner, err := runner.NewForConfig(runCtx)
runner, err := v1.NewForConfig(runCtx)
if err != nil {
event.InititializationFailed(err)
return nil, nil, nil, fmt.Errorf("creating runner: %w", err)
Expand Down
48 changes: 32 additions & 16 deletions pkg/skaffold/runner/build_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,22 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

func NewBuilder(builder build.Builder, tagger tag.Tagger, cache cache.Cache, podSelector *kubernetes.ImageList,
runCtx *runcontext.RunContext) *Builder {
return &Builder{
Builder: builder,
tagger: tagger,
cache: cache,
podSelector: podSelector,
runCtx: runCtx,
}
}

type Builder struct {
builder build.Builder
Builder build.Builder
tagger tag.Tagger
cache cache.Cache
builds []graph.Artifact
Builds []graph.Artifact

// podSelector is used to determine relevant pods for logging and portForwarding
podSelector *kubernetes.ImageList
Expand All @@ -52,16 +63,21 @@ type Builder struct {
runCtx *runcontext.RunContext
}

// GetBuilds returns the builds value.
func (r *Builder) GetBuilds() []graph.Artifact {
return r.Builds
}

// Build builds a list of artifacts.
func (r *Builder) Build(ctx context.Context, out io.Writer, artifacts []*latest_v1.Artifact) ([]graph.Artifact, error) {
eventV2.TaskInProgress(constants.Build)

// Use tags directly from the Kubernetes manifests.
if r.runCtx.DigestSource() == noneDigestSource {
if r.runCtx.DigestSource() == NoneDigestSource {
return []graph.Artifact{}, nil
}

if err := checkWorkspaces(artifacts); err != nil {
if err := CheckWorkspaces(artifacts); err != nil {
eventV2.TaskFailed(constants.Build, err)
return nil, err
}
Expand All @@ -77,10 +93,10 @@ func (r *Builder) Build(ctx context.Context, out io.Writer, artifacts []*latest_
case r.runCtx.DryRun():
color.Yellow.Fprintln(out, "Skipping build phase since --dry-run=true")
return artifactsWithTags(tags, artifacts), nil
case r.runCtx.RenderOnly() && r.runCtx.DigestSource() == remoteDigestSource:
case r.runCtx.RenderOnly() && r.runCtx.DigestSource() == RemoteDigestSource:
color.Yellow.Fprintln(out, "Skipping build phase since --digest-source=remote")
return artifactsWithTags(tags, artifacts), nil
case r.runCtx.RenderOnly() && r.runCtx.DigestSource() == tagDigestSource:
case r.runCtx.RenderOnly() && r.runCtx.DigestSource() == TagDigestSource:
color.Yellow.Fprintln(out, "Skipping build phase since --digest-source=tag")
return artifactsWithTags(tags, artifacts), nil
default:
Expand All @@ -93,7 +109,7 @@ func (r *Builder) Build(ctx context.Context, out io.Writer, artifacts []*latest_

r.hasBuilt = true

bRes, err := r.builder.Build(ctx, out, tags, artifacts)
bRes, err := r.Builder.Build(ctx, out, tags, artifacts)
if err != nil {
return nil, err
}
Expand All @@ -106,10 +122,10 @@ func (r *Builder) Build(ctx context.Context, out io.Writer, artifacts []*latest_
}

// Update which images are logged.
r.addTagsToPodSelector(bRes)
r.AddTagsToPodSelector(bRes)

// Make sure all artifacts are redeployed. Not only those that were just built.
r.builds = build.MergeWithPreviousBuilds(bRes, r.builds)
r.Builds = build.MergeWithPreviousBuilds(bRes, r.Builds)

eventV2.TaskSucceeded(constants.Build)
return bRes, nil
Expand All @@ -133,7 +149,7 @@ func artifactsWithTags(tags tag.ImageTags, artifacts []*latest_v1.Artifact) []gr
}

// Update which images are logged.
func (r *Builder) addTagsToPodSelector(artifacts []graph.Artifact) {
func (r *Builder) AddTagsToPodSelector(artifacts []graph.Artifact) {
for _, artifact := range artifacts {
r.podSelector.Add(artifact.Tag)
}
Expand Down Expand Up @@ -161,8 +177,8 @@ func (r *Builder) imageTags(ctx context.Context, out io.Writer, artifacts []*lat

i := i
go func() {
tag, err := tag.GenerateFullyQualifiedImageName(r.tagger, *artifacts[i])
tagErrs[i] <- tagErr{tag: tag, err: err}
_tag, err := tag.GenerateFullyQualifiedImageName(r.tagger, *artifacts[i])
tagErrs[i] <- tagErr{tag: _tag, err: err}
}()
}

Expand Down Expand Up @@ -191,13 +207,13 @@ func (r *Builder) imageTags(ctx context.Context, out io.Writer, artifacts []*lat
showWarning = true
}

tag, err := r.ApplyDefaultRepo(t.tag)
_tag, err := r.ApplyDefaultRepo(t.tag)
if err != nil {
return nil, err
}

fmt.Fprintln(out, tag)
imageTags[imageName] = tag
fmt.Fprintln(out, _tag)
imageTags[imageName] = _tag
}
}

Expand All @@ -209,7 +225,7 @@ func (r *Builder) imageTags(ctx context.Context, out io.Writer, artifacts []*lat
return imageTags, nil
}

func checkWorkspaces(artifacts []*latest_v1.Artifact) error {
func CheckWorkspaces(artifacts []*latest_v1.Artifact) error {
for _, a := range artifacts {
if a.Workspace != "" {
if info, err := os.Stat(a.Workspace); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/runner/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func (b *builderCtx) SourceDependenciesResolver() graph.SourceDependenciesCache
return b.sourceDependenciesCache
}

// getBuilder creates a builder from a given RunContext and build pipeline type.
func getBuilder(r *runcontext.RunContext, s build.ArtifactStore, d graph.SourceDependenciesCache, p latest_v1.Pipeline) (build.PipelineBuilder, error) {
// GetBuilder creates a builder from a given RunContext and build pipeline type.
func GetBuilder(r *runcontext.RunContext, s build.ArtifactStore, d graph.SourceDependenciesCache, p latest_v1.Pipeline) (build.PipelineBuilder, error) {
bCtx := &builderCtx{artifactStore: s, sourceDependenciesCache: d, RunContext: r}
switch {
case p.Build.LocalBuild != nil:
Expand Down
47 changes: 40 additions & 7 deletions pkg/skaffold/runner/changeset.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Skaffold Authors
Copyright 2021 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,11 +31,35 @@ type ChangeSet struct {
needsReload bool
}

// NeedsRebuild gets the value of needsRebuild, which itself is not expected to be changed outside ChangeSet
func (c *ChangeSet) NeedsRebuild() []*latest_v1.Artifact {
return c.needsRebuild
}

// NeedsResync gets the value of needsResync, which itself is not expected to be changed outside ChangeSet
func (c *ChangeSet) NeedsResync() []*sync.Item {
return c.needsResync
}

// NeedsRedeploy gets the value of needsRedeploy, which itself is not expected to be changed outside ChangeSet
func (c *ChangeSet) NeedsRedeploy() bool {
return c.needsRedeploy
}

// NeedsRetest gets the value of needsRetest, which itself is not expected to be changed outside ChangeSet
func (c *ChangeSet) NeedsRetest() map[string]bool {
return c.needsRetest
}

// NeedsReload gets the value of needsReload, which itself is not expected to be changed outside ChangeSet
func (c *ChangeSet) NeedsReload() bool {
return c.needsReload
}

func (c *ChangeSet) AddRebuild(a *latest_v1.Artifact) {
if _, ok := c.rebuildTracker[a.ImageName]; ok {
return
}

if c.rebuildTracker == nil {
c.rebuildTracker = map[string]*latest_v1.Artifact{}
}
Expand All @@ -54,28 +78,37 @@ func (c *ChangeSet) AddResync(s *sync.Item) {
if _, ok := c.resyncTracker[s.Image]; ok {
return
}

if c.resyncTracker == nil {
c.resyncTracker = map[string]*sync.Item{}
}
c.resyncTracker[s.Image] = s
c.needsResync = append(c.needsResync, s)
}

func (c *ChangeSet) resetBuild() {
func (c *ChangeSet) ResetBuild() {
c.rebuildTracker = make(map[string]*latest_v1.Artifact)
c.needsRebuild = nil
}

func (c *ChangeSet) resetSync() {
func (c *ChangeSet) ResetSync() {
c.resyncTracker = make(map[string]*sync.Item)
c.needsResync = nil
}

func (c *ChangeSet) resetDeploy() {
func (c *ChangeSet) ResetDeploy() {
c.needsRedeploy = false
}

func (c *ChangeSet) resetTest() {
// Redeploy marks that deploy is expected to happen.
func (c *ChangeSet) Redeploy() {
c.needsRedeploy = true
}

// Reload marks that reload is expected to happen.
func (c *ChangeSet) Reload() {
c.needsReload = true
}

func (c *ChangeSet) ResetTest() {
c.needsRetest = make(map[string]bool)
}
33 changes: 19 additions & 14 deletions pkg/skaffold/runner/intent.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Intents struct {
lock sync.Mutex
}

func newIntents(autoBuild, autoSync, autoDeploy bool) *Intents {
func NewIntents(autoBuild, autoSync, autoDeploy bool) *Intents {
i := &Intents{
autoBuild: autoBuild,
autoSync: autoSync,
Expand All @@ -39,81 +39,86 @@ func newIntents(autoBuild, autoSync, autoDeploy bool) *Intents {
return i
}

func (i *Intents) reset() {
// GetIntentsAttrs returns the intent attributes for testing only.
func (i *Intents) GetIntentsAttrs() (bool, bool, bool) {
return i.build, i.sync, i.deploy
}

func (i *Intents) Reset() {
i.lock.Lock()
i.build = i.autoBuild
i.sync = i.autoSync
i.deploy = i.autoDeploy
i.lock.Unlock()
}

func (i *Intents) resetBuild() {
func (i *Intents) ResetBuild() {
i.lock.Lock()
i.build = i.autoBuild
i.lock.Unlock()
}

func (i *Intents) resetSync() {
func (i *Intents) ResetSync() {
i.lock.Lock()
i.sync = i.autoSync
i.lock.Unlock()
}

func (i *Intents) resetDeploy() {
func (i *Intents) ResetDeploy() {
i.lock.Lock()
i.deploy = i.autoDeploy
i.lock.Unlock()
}

func (i *Intents) setBuild(val bool) {
func (i *Intents) SetBuild(val bool) {
i.lock.Lock()
i.build = val
i.lock.Unlock()
}

func (i *Intents) setSync(val bool) {
func (i *Intents) SetSync(val bool) {
i.lock.Lock()
i.sync = val
i.lock.Unlock()
}

func (i *Intents) setDeploy(val bool) {
func (i *Intents) SetDeploy(val bool) {
i.lock.Lock()
i.deploy = val
i.lock.Unlock()
}

func (i *Intents) getAutoBuild() bool {
func (i *Intents) GetAutoBuild() bool {
i.lock.Lock()
defer i.lock.Unlock()
return i.autoBuild
}

func (i *Intents) getAutoSync() bool {
func (i *Intents) GetAutoSync() bool {
i.lock.Lock()
defer i.lock.Unlock()
return i.autoSync
}

func (i *Intents) getAutoDeploy() bool {
func (i *Intents) GetAutoDeploy() bool {
i.lock.Lock()
defer i.lock.Unlock()
return i.autoDeploy
}

func (i *Intents) setAutoBuild(val bool) {
func (i *Intents) SetAutoBuild(val bool) {
i.lock.Lock()
i.autoBuild = val
i.lock.Unlock()
}

func (i *Intents) setAutoSync(val bool) {
func (i *Intents) SetAutoSync(val bool) {
i.lock.Lock()
i.autoSync = val
i.lock.Unlock()
}

func (i *Intents) setAutoDeploy(val bool) {
func (i *Intents) SetAutoDeploy(val bool) {
i.lock.Lock()
i.autoDeploy = val
i.lock.Unlock()
Expand Down
10 changes: 10 additions & 0 deletions pkg/skaffold/runner/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/trigger"
)

func NewSkaffoldListener(monitor filemon.Monitor, trigger trigger.Trigger, cache graph.SourceDependenciesCache,
intentChan <-chan bool) *SkaffoldListener {
return &SkaffoldListener{
Monitor: monitor,
Trigger: trigger,
sourceDependenciesCache: cache,
intentChan: intentChan,
}
}

type Listener interface {
WatchForChanges(context.Context, io.Writer, func() error) error
LogWatchToUser(io.Writer)
Expand Down
Loading

0 comments on commit ebcbdd9

Please sign in to comment.