Skip to content

Commit a314b3e

Browse files
authored
Merge pull request #1343 from dgageot/fix-812
Cleanup only if something was actually deployed
2 parents 04431e2 + 1c7c212 commit a314b3e

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

cmd/skaffold/app/cmd/dev.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ func dev(out io.Writer) error {
5252
defer cancel()
5353
catchCtrlC(cancel)
5454

55+
cleanup := func() {}
5556
if opts.Cleanup {
5657
defer func() {
57-
if err := delete(out); err != nil {
58-
logrus.Warnln("cleanup:", err)
59-
}
58+
cleanup()
6059
}()
6160
}
6261

@@ -70,7 +69,15 @@ func dev(out io.Writer) error {
7069
return errors.Wrap(err, "creating runner")
7170
}
7271

73-
if _, err := r.Dev(ctx, out, config.Build.Artifacts); err != nil {
72+
err = r.Dev(ctx, out, config.Build.Artifacts)
73+
if r.HasDeployed() {
74+
cleanup = func() {
75+
if err := r.Cleanup(context.Background(), out); err != nil {
76+
logrus.Warnln("cleanup:", err)
77+
}
78+
}
79+
}
80+
if err != nil {
7481
if errors.Cause(err) != runner.ErrorConfigurationChanged {
7582
return err
7683
}

pkg/skaffold/runner/runner.go

+22-9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type SkaffoldRunner struct {
5959
opts *config.SkaffoldOptions
6060
watchFactory watch.Factory
6161
builds []build.Artifact
62+
hasDeployed bool
6263
imageList *kubernetes.ImageList
6364
}
6465

@@ -207,6 +208,11 @@ func (r *SkaffoldRunner) newLogger(out io.Writer, artifacts []*latest.Artifact)
207208
return kubernetes.NewLogAggregator(out, imageNames, r.imageList)
208209
}
209210

211+
// HasDeployed returns true if this runner has deployed something.
212+
func (r *SkaffoldRunner) HasDeployed() bool {
213+
return r.hasDeployed
214+
}
215+
210216
func (r *SkaffoldRunner) buildTestDeploy(ctx context.Context, out io.Writer, artifacts []*latest.Artifact) error {
211217
bRes, err := r.BuildAndTest(ctx, out, artifacts)
212218
if err != nil {
@@ -260,6 +266,13 @@ func (r *SkaffoldRunner) BuildAndTest(ctx context.Context, out io.Writer, artifa
260266
return bRes, err
261267
}
262268

269+
// Deploy deploys the given artifacts
270+
func (r *SkaffoldRunner) Deploy(ctx context.Context, out io.Writer, artifacts []build.Artifact) ([]deploy.Artifact, error) {
271+
dRes, err := r.Deployer.Deploy(ctx, out, artifacts)
272+
r.hasDeployed = true
273+
return dRes, err
274+
}
275+
263276
// TailLogs prints the logs for deployed artifacts.
264277
func (r *SkaffoldRunner) TailLogs(ctx context.Context, out io.Writer, artifacts []*latest.Artifact, bRes []build.Artifact) error {
265278
if !r.opts.Tail {
@@ -281,7 +294,7 @@ func (r *SkaffoldRunner) TailLogs(ctx context.Context, out io.Writer, artifacts
281294

282295
// Dev watches for changes and runs the skaffold build and deploy
283296
// pipeline until interrrupted by the user.
284-
func (r *SkaffoldRunner) Dev(ctx context.Context, out io.Writer, artifacts []*latest.Artifact) ([]build.Artifact, error) {
297+
func (r *SkaffoldRunner) Dev(ctx context.Context, out io.Writer, artifacts []*latest.Artifact) error {
285298
logger := r.newLogger(out, artifacts)
286299

287300
// Create watcher and register artifacts to build current state of files.
@@ -349,7 +362,7 @@ func (r *SkaffoldRunner) Dev(ctx context.Context, out io.Writer, artifacts []*la
349362
func() ([]string, error) { return DependenciesForArtifact(ctx, artifact) },
350363
func(e watch.Events) { changed.AddDirtyArtifact(artifact, e) },
351364
); err != nil {
352-
return nil, errors.Wrapf(err, "watching files for artifact %s", artifact.ImageName)
365+
return errors.Wrapf(err, "watching files for artifact %s", artifact.ImageName)
353366
}
354367
}
355368

@@ -358,47 +371,47 @@ func (r *SkaffoldRunner) Dev(ctx context.Context, out io.Writer, artifacts []*la
358371
func() ([]string, error) { return r.TestDependencies() },
359372
func(watch.Events) { changed.needsRedeploy = true },
360373
); err != nil {
361-
return nil, errors.Wrap(err, "watching test files")
374+
return errors.Wrap(err, "watching test files")
362375
}
363376

364377
// Watch deployment configuration
365378
if err := watcher.Register(
366379
func() ([]string, error) { return r.Dependencies() },
367380
func(watch.Events) { changed.needsRedeploy = true },
368381
); err != nil {
369-
return nil, errors.Wrap(err, "watching files for deployer")
382+
return errors.Wrap(err, "watching files for deployer")
370383
}
371384

372385
// Watch Skaffold configuration
373386
if err := watcher.Register(
374387
func() ([]string, error) { return []string{r.opts.ConfigurationFile}, nil },
375388
func(watch.Events) { changed.needsReload = true },
376389
); err != nil {
377-
return nil, errors.Wrapf(err, "watching skaffold configuration %s", r.opts.ConfigurationFile)
390+
return errors.Wrapf(err, "watching skaffold configuration %s", r.opts.ConfigurationFile)
378391
}
379392

380393
// First run
381394
if err := r.buildTestDeploy(ctx, out, artifacts); err != nil {
382-
return nil, errors.Wrap(err, "exiting dev mode because first run failed")
395+
return errors.Wrap(err, "exiting dev mode because first run failed")
383396
}
384397

385398
// Start logs
386399
if r.opts.TailDev {
387400
if err := logger.Start(ctx); err != nil {
388-
return nil, errors.Wrap(err, "starting logger")
401+
return errors.Wrap(err, "starting logger")
389402
}
390403
}
391404

392405
if r.opts.PortForward {
393406
portForwarder := kubernetes.NewPortForwarder(out, r.imageList)
394407

395408
if err := portForwarder.Start(ctx); err != nil {
396-
return nil, errors.Wrap(err, "starting port-forwarder")
409+
return errors.Wrap(err, "starting port-forwarder")
397410
}
398411
}
399412

400413
r.Trigger.WatchForChanges(out)
401-
return nil, watcher.Run(ctx, r.Trigger, onChange)
414+
return watcher.Run(ctx, r.Trigger, onChange)
402415
}
403416

404417
func (r *SkaffoldRunner) shouldWatch(artifact *latest.Artifact) bool {

pkg/skaffold/runner/runner_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func TestDev(t *testing.T) {
394394
runner.Deployer = test.deployer
395395
runner.watchFactory = test.watcherFactory
396396

397-
_, err := runner.Dev(context.Background(), ioutil.Discard, nil)
397+
err := runner.Dev(context.Background(), ioutil.Discard, nil)
398398

399399
testutil.CheckError(t, test.shouldErr, err)
400400
})
@@ -417,7 +417,7 @@ func TestBuildAndDeployAllArtifacts(t *testing.T) {
417417

418418
// Both artifacts are changed
419419
runner.watchFactory = NewWatcherFactory(nil, nil, []int{0, 1})
420-
_, err := runner.Dev(ctx, ioutil.Discard, artifacts)
420+
err := runner.Dev(ctx, ioutil.Discard, artifacts)
421421

422422
if err != nil {
423423
t.Errorf("Didn't expect an error. Got %s", err)
@@ -431,7 +431,7 @@ func TestBuildAndDeployAllArtifacts(t *testing.T) {
431431

432432
// Only one is changed
433433
runner.watchFactory = NewWatcherFactory(nil, nil, []int{1})
434-
_, err = runner.Dev(ctx, ioutil.Discard, artifacts)
434+
err = runner.Dev(ctx, ioutil.Discard, artifacts)
435435

436436
if err != nil {
437437
t.Errorf("Didn't expect an error. Got %s", err)

0 commit comments

Comments
 (0)