Skip to content

Commit

Permalink
Merge pull request #833 from dgageot/watch
Browse files Browse the repository at this point in the history
Watch mode 4th edition
  • Loading branch information
dgageot authored Aug 6, 2018
2 parents 04ace7a + 1aa2086 commit a011cbe
Show file tree
Hide file tree
Showing 18 changed files with 326 additions and 622 deletions.
6 changes: 3 additions & 3 deletions cmd/skaffold/app/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewCmdBuild(out io.Writer) *cobra.Command {
Short: "Builds the artifacts",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runBuild(out, filename)
return runBuild(out)
},
}
AddRunDevFlags(cmd)
Expand All @@ -53,10 +53,10 @@ type BuildOutput struct {
Builds []build.Artifact
}

func runBuild(out io.Writer, filename string) error {
func runBuild(out io.Writer) error {
ctx := context.Background()

runner, config, err := newRunner(filename)
runner, config, err := newRunner(opts)
if err != nil {
return errors.Wrap(err, "creating runner")
}
Expand Down
9 changes: 4 additions & 5 deletions cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
var (
opts = &config.SkaffoldOptions{}
v string
filename string
overwrite bool

updateMsg = make(chan string)
Expand Down Expand Up @@ -106,14 +105,14 @@ func AddDevFlags(cmd *cobra.Command) {
}

func AddRunDevFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&filename, "filename", "f", "skaffold.yaml", "Filename or URL to the pipeline file")
cmd.Flags().StringVarP(&opts.ConfigurationFile, "filename", "f", "skaffold.yaml", "Filename or URL to the pipeline file")
cmd.Flags().BoolVar(&opts.Notification, "toot", false, "Emit a terminal beep after the deploy is complete")
cmd.Flags().StringArrayVarP(&opts.Profiles, "profile", "p", nil, "Activate profiles by name")
cmd.Flags().StringVarP(&opts.Namespace, "namespace", "n", "", "Run Helm deployments in the specified namespace")
}

func AddFixFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&filename, "filename", "f", "skaffold.yaml", "Filename or URL to the pipeline file")
cmd.Flags().StringVarP(&opts.ConfigurationFile, "filename", "f", "skaffold.yaml", "Filename or URL to the pipeline file")
cmd.Flags().BoolVar(&overwrite, "overwrite", false, "Overwrite original config with fixed config")
}

Expand All @@ -127,8 +126,8 @@ func SetUpLogs(out io.Writer, level string) error {
return nil
}

func readConfiguration(filename string) (*config.SkaffoldConfig, error) {
config, err := cmdutil.ParseConfig(filename)
func readConfiguration(opts *config.SkaffoldOptions) (*config.SkaffoldConfig, error) {
config, err := cmdutil.ParseConfig(opts.ConfigurationFile)
if err != nil {
return nil, errors.Wrap(err, "parsing skaffold config")
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/skaffold/app/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ func NewCmdDelete(out io.Writer) *cobra.Command {
Short: "Delete the deployed resources",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return delete(out, filename)
return delete(out)
},
}
AddRunDevFlags(cmd)
return cmd
}

func delete(out io.Writer, filename string) error {
func delete(out io.Writer) error {
ctx := context.Background()

runner, _, err := newRunner(filename)
runner, _, err := newRunner(opts)
if err != nil {
return errors.Wrap(err, "creating runner")
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/skaffold/app/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewCmdDeploy(out io.Writer) *cobra.Command {
Short: "Deploys the artifacts",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runDeploy(out, filename)
return runDeploy(out)
},
}
AddRunDevFlags(cmd)
Expand All @@ -47,10 +47,10 @@ func NewCmdDeploy(out io.Writer) *cobra.Command {
return cmd
}

func runDeploy(out io.Writer, filename string) error {
func runDeploy(out io.Writer) error {
ctx := context.Background()

r, _, err := newRunner(filename)
r, _, err := newRunner(opts)
if err != nil {
return errors.Wrap(err, "creating runner")
}
Expand Down
91 changes: 20 additions & 71 deletions cmd/skaffold/app/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@ import (
"io"
"os"
"os/signal"
"sync"
"syscall"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/watch"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -38,99 +36,50 @@ func NewCmdDev(out io.Writer) *cobra.Command {
Short: "Runs a pipeline file in development mode",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return dev(out, filename)
return dev(out)
},
}
AddRunDevFlags(cmd)
AddDevFlags(cmd)
return cmd
}

func dev(out io.Writer, filename string) error {
func dev(out io.Writer) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if opts.Cleanup {
catchCtrlC(cancel)
}

errDev := devLoop(ctx, cancel, out, filename)

if opts.Cleanup {
if err := delete(out, filename); err != nil {
logrus.Warnln("cleanup:", err)
}
}

return errDev
}

func devLoop(ctx context.Context, cancelMainLoop context.CancelFunc, out io.Writer, filename string) error {
watcher, err := watch.NewFileWatcher([]string{filename}, runner.PollInterval)
if err != nil {
return errors.Wrap(err, "watching configuration")
defer func() {
if err := delete(out); err != nil {
logrus.Warnln("cleanup:", err)
}
}()
}

c := make(chan context.CancelFunc, 1)
var devLoop sync.WaitGroup
devLoop.Add(1)
for {
select {
case <-ctx.Done():
return nil
default:
r, config, err := newRunner(opts)
if err != nil {
return errors.Wrap(err, "creating runner")
}

go func() {
for {
select {
case <-ctx.Done():
devLoop.Done()
return
default:
ctxDev, cancelDev := context.WithCancel(ctx)
c <- cancelDev
if err := runDev(ctxDev, out, filename); err != nil {
logrus.Errorln("dev:", err)
cancelMainLoop()
devLoop.Done()
return
if _, err := r.Dev(ctx, out, config.Build.Artifacts); err != nil {
if errors.Cause(err) != runner.ErrorConfigurationChanged {
return err
}
}
}
}()

errRun := watcher.Run(ctx, func([]string) error {
cancelDev := <-c
cancelDev()
return nil
})

// Drain c to make sure the dev loop is not waiting for it
go func() {
for range c {
}
}()
devLoop.Wait()

return errRun
}

func runDev(ctx context.Context, out io.Writer, filename string) error {
runner, config, err := newRunner(filename)
if err != nil {
return errors.Wrap(err, "creating runner")
}

_, err = runner.Dev(ctx, out, config.Build.Artifacts)
if err != nil {
return errors.Wrap(err, "dev step")
}

return nil
}

func catchCtrlC(cancel context.CancelFunc) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, os.Interrupt,
syscall.SIGTERM,
syscall.SIGINT,
syscall.SIGPIPE,
)
signal.Notify(signals, os.Interrupt, syscall.SIGTERM, syscall.SIGINT, syscall.SIGPIPE)

go func() {
<-signals
Expand Down
16 changes: 7 additions & 9 deletions cmd/skaffold/app/cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,23 @@ import (
"io"
"io/ioutil"

yaml "gopkg.in/yaml.v2"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/color"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema"
schemautil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
)

func NewCmdFix(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "fix",
Short: "Converts old skaffold.yaml to newest schema version",
Run: func(cmd *cobra.Command, args []string) {
contents, err := util.ReadConfiguration(filename)
contents, err := util.ReadConfiguration(opts.ConfigurationFile)
if err != nil {
logrus.Errorf("fix: %s", err)
}
Expand Down Expand Up @@ -71,10 +69,10 @@ func runFix(out io.Writer, cfg schemautil.VersionedConfig) error {
return errors.Wrap(err, "marshaling new config")
}
if overwrite {
if err := ioutil.WriteFile(filename, newCfg, 0644); err != nil {
if err := ioutil.WriteFile(opts.ConfigurationFile, newCfg, 0644); err != nil {
return errors.Wrap(err, "writing config file")
}
color.Default.Fprintf(out, "New config at version %s generated and written to %s\n", cfg.GetVersion(), filename)
color.Default.Fprintf(out, "New config at version %s generated and written to %s\n", cfg.GetVersion(), opts.ConfigurationFile)
} else {
out.Write(newCfg)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/skaffold/app/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewCmdRun(out io.Writer) *cobra.Command {
Short: "Runs a pipeline file",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return run(out, filename)
return run(out)
},
}
AddRunDevFlags(cmd)
Expand All @@ -40,10 +40,10 @@ func NewCmdRun(out io.Writer) *cobra.Command {
return cmd
}

func run(out io.Writer, filename string) error {
func run(out io.Writer) error {
ctx := context.Background()

runner, config, err := newRunner(filename)
runner, config, err := newRunner(opts)
if err != nil {
return errors.Wrap(err, "creating runner")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/skaffold/app/cmd/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
)

// newRunner creates a SkaffoldRunner and returns the SkaffoldConfig associated with it.
func newRunner(filename string) (*runner.SkaffoldRunner, *config.SkaffoldConfig, error) {
config, err := readConfiguration(filename)
func newRunner(opts *config.SkaffoldOptions) (*runner.SkaffoldRunner, *config.SkaffoldConfig, error) {
config, err := readConfiguration(opts)
if err != nil {
return nil, nil, errors.Wrap(err, "reading configuration")
}
Expand Down
1 change: 1 addition & 0 deletions examples/getting-started/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
func main() {
for {
fmt.Println("Hello world!")

time.Sleep(time.Second * 1)
}
}
11 changes: 6 additions & 5 deletions pkg/skaffold/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ import (
// SkaffoldOptions are options that are set by command line arguments not included
// in the config file itself
type SkaffoldOptions struct {
Cleanup bool
Notification bool
Profiles []string
CustomTag string
Namespace string
ConfigurationFile string
Cleanup bool
Notification bool
Profiles []string
CustomTag string
Namespace string
}

// Labels returns a map of labels to be applied to all deployed
Expand Down
37 changes: 37 additions & 0 deletions pkg/skaffold/runner/changes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2018 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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package runner

import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
)

type changes struct {
diryArtifacts []*v1alpha2.Artifact
needsRedeploy bool
needsReload bool
}

func (c *changes) Add(a *v1alpha2.Artifact) {
c.diryArtifacts = append(c.diryArtifacts, a)
}

func (c *changes) reset() {
c.diryArtifacts = nil
c.needsRedeploy = false
c.needsReload = false
}
Loading

0 comments on commit a011cbe

Please sign in to comment.