Skip to content

Commit

Permalink
Merge pull request #15 from r2d4/watch
Browse files Browse the repository at this point in the history
Add Watch API and `skaffold dev` command
  • Loading branch information
r2d4 committed Feb 7, 2018
2 parents 7e7389b + 2677194 commit 15a418c
Show file tree
Hide file tree
Showing 79 changed files with 9,238 additions and 102 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@
[[constraint]]
name = "github.com/spf13/afero"
version = "1.0.2"

[[constraint]]
branch = "master"
name = "github.com/rjeczalik/notify"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ $(BUILD_DIR)/$(PROJECT): $(BUILD_DIR)/$(PROJECT)-$(GOOS)-$(GOARCH)
cp $(BUILD_DIR)/$(PROJECT)-$(GOOS)-$(GOARCH) $@

$(BUILD_DIR)/$(PROJECT)-%-$(GOARCH): $(GO_FILES) $(BUILD_DIR)
GOOS=$* GOARCH=$(GOARCH) CGO_ENABLED=0 go build -ldflags $(GO_LDFLAGS) -o $@ $(BUILD_PACKAGE)
GOOS=$* GOARCH=$(GOARCH) CGO_ENABLED=1 go build -ldflags $(GO_LDFLAGS) -o $@ $(BUILD_PACKAGE)

%.sha256: %
shasum -a 256 $< &> $@
Expand Down
36 changes: 33 additions & 3 deletions cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ package cmd

import (
"io"
"os"

"github.com/GoogleCloudPlatform/skaffold/pkg/skaffold/version"

"github.com/GoogleCloudPlatform/skaffold/pkg/skaffold/config"
"github.com/GoogleCloudPlatform/skaffold/pkg/skaffold/constants"
"github.com/GoogleCloudPlatform/skaffold/pkg/skaffold/runner"
"github.com/GoogleCloudPlatform/skaffold/pkg/skaffold/version"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var v string
var (
v string
filename string
)

func NewSkaffoldCommand(out, err io.Writer) *cobra.Command {
c := &cobra.Command{
Expand All @@ -44,6 +49,7 @@ func NewSkaffoldCommand(out, err io.Writer) *cobra.Command {

c.AddCommand(NewCmdVersion(out))
c.AddCommand(NewCmdRun(out))
c.AddCommand(NewCmdDev(out))
c.AddCommand(NewCmdDocker(out))

c.PersistentFlags().StringVarP(&v, "verbosity", "v", constants.DefaultLogLevel.String(), "Log level (debug, info, warn, error, fatal, panic")
Expand All @@ -59,3 +65,27 @@ func SetUpLogs(out io.Writer, level string) error {
logrus.SetLevel(lvl)
return nil
}

func runSkaffold(out io.Writer, dev bool, filename string) error {
f, err := os.Open(filename)
if err != nil {
return errors.Wrap(err, "opening skaffold config")
}
defer f.Close()

cfg, err := config.Parse(f)
if err != nil {
return errors.Wrap(err, "parsing skaffold config")
}

r, err := runner.NewForConfig(out, dev, cfg)
if err != nil {
return errors.Wrap(err, "getting skaffold config")
}

if err := r.Run(); err != nil {
return errors.Wrap(err, "running skaffold steps")
}

return nil
}
38 changes: 38 additions & 0 deletions cmd/skaffold/app/cmd/dev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2018 Google LLC
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 cmd

import (
"io"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func NewCmdDev(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "dev",
Short: "runs a pipeline file in development mode",
Run: func(cmd *cobra.Command, args []string) {
if err := runSkaffold(out, true, filename); err != nil {
logrus.Errorf("run: %s", err)
}
},
}
cmd.Flags().StringVarP(&filename, "filename", "f", "skaffold.yaml", "Filename of pipeline file")
return cmd
}
34 changes: 1 addition & 33 deletions cmd/skaffold/app/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,21 @@ package cmd

import (
"io"
"os"

"github.com/GoogleCloudPlatform/skaffold/pkg/skaffold/config"
"github.com/GoogleCloudPlatform/skaffold/pkg/skaffold/runner"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
filename string
)

func NewCmdRun(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "runs a pipeline file",
Run: func(cmd *cobra.Command, args []string) {
if err := runSkaffold(out, filename); err != nil {
if err := runSkaffold(out, false, filename); err != nil {
logrus.Errorf("run: %s", err)
}
},
}
cmd.Flags().StringVarP(&filename, "filename", "f", "skaffold.yaml", "Filename of pipeline file")
return cmd
}

func runSkaffold(out io.Writer, filename string) error {
f, err := os.Open(filename)
if err != nil {
return errors.Wrap(err, "opening skaffold config")
}
defer f.Close()

cfg, err := config.Parse(nil, f)
if err != nil {
return errors.Wrap(err, "parsing skaffold config")
}

r, err := runner.NewForConfig(out, cfg)
if err != nil {
return errors.Wrap(err, "getting skaffold config")
}

if err := r.Run(); err != nil {
return errors.Wrap(err, "running skaffold steps")
}

return nil
}
16 changes: 8 additions & 8 deletions pkg/skaffold/build/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestLocalRun(t *testing.T) {
description: "single build",
out: &bytes.Buffer{},
config: &config.BuildConfig{
Artifacts: []config.Artifact{
Artifacts: []*config.Artifact{
{
ImageName: "test",
Workspace: ".",
Expand All @@ -74,7 +74,7 @@ func TestLocalRun(t *testing.T) {
description: "error image build",
out: &bytes.Buffer{},
config: &config.BuildConfig{
Artifacts: []config.Artifact{
Artifacts: []*config.Artifact{
{
ImageName: "test",
Workspace: ".",
Expand All @@ -89,7 +89,7 @@ func TestLocalRun(t *testing.T) {
description: "error image tag",
out: &bytes.Buffer{},
config: &config.BuildConfig{
Artifacts: []config.Artifact{
Artifacts: []*config.Artifact{
{
ImageName: "test",
Workspace: ".",
Expand All @@ -104,7 +104,7 @@ func TestLocalRun(t *testing.T) {
description: "error api client",
out: &bytes.Buffer{},
config: &config.BuildConfig{
Artifacts: []config.Artifact{
Artifacts: []*config.Artifact{
{
ImageName: "test",
Workspace: ".",
Expand All @@ -119,7 +119,7 @@ func TestLocalRun(t *testing.T) {
description: "bad writer",
out: &testutil.BadWriter{},
config: &config.BuildConfig{
Artifacts: []config.Artifact{
Artifacts: []*config.Artifact{
{
ImageName: "test",
Workspace: ".",
Expand All @@ -134,7 +134,7 @@ func TestLocalRun(t *testing.T) {
description: "error image list",
out: &testutil.BadWriter{},
config: &config.BuildConfig{
Artifacts: []config.Artifact{
Artifacts: []*config.Artifact{
{
ImageName: "test",
Workspace: ".",
Expand All @@ -148,7 +148,7 @@ func TestLocalRun(t *testing.T) {
{
description: "error tagger",
config: &config.BuildConfig{
Artifacts: []config.Artifact{
Artifacts: []*config.Artifact{
{
ImageName: "test",
Workspace: ".",
Expand All @@ -175,7 +175,7 @@ func TestLocalRun(t *testing.T) {

func TestNewLocalBuilder(t *testing.T) {
_, err := NewLocalBuilder(&config.BuildConfig{
Artifacts: []config.Artifact{
Artifacts: []*config.Artifact{
{
ImageName: "test",
Workspace: ".",
Expand Down
11 changes: 3 additions & 8 deletions pkg/skaffold/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,14 @@ type SkaffoldConfig struct {
APIVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`

Watch bool `yaml:"watch"`

Build BuildConfig `yaml:"build"`
Deploy DeployConfig `yaml:"deploy"`
}

// BuildConfig contains all the configuration for the build steps
type BuildConfig struct {
Artifacts []Artifact `yaml:"artifacts"`
TagPolicy string `yaml:"tagPolicy"`
Artifacts []*Artifact `yaml:"artifacts"`
TagPolicy string `yaml:"tagPolicy"`
BuildType `yaml:",inline"`
}

Expand Down Expand Up @@ -87,16 +85,13 @@ type Artifact struct {
// Parse reads from an io.Reader and unmarshals the result into a SkaffoldConfig.
// The default config argument provides default values for the config,
// which can be overridden if present in the config file.
func Parse(defaultConfig *SkaffoldConfig, config io.Reader) (*SkaffoldConfig, error) {
func Parse(config io.Reader) (*SkaffoldConfig, error) {
var b bytes.Buffer
if _, err := b.ReadFrom(config); err != nil {
return nil, errors.Wrap(err, "reading config")
}

var cfg SkaffoldConfig
if defaultConfig != nil {
cfg = *defaultConfig
}
if err := yaml.Unmarshal(b.Bytes(), &cfg); err != nil {
return nil, err
}
Expand Down
41 changes: 7 additions & 34 deletions pkg/skaffold/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,7 @@ var configA = &SkaffoldConfig{
APIVersion: "skaffold/v1",
Kind: "Config",
Build: BuildConfig{
Artifacts: []Artifact{
{
ImageName: "example",
Workspace: "./examples/app",
},
},
},
Deploy: DeployConfig{
Name: "example",
Parameters: map[string]string{
"key": "value",
},
},
}

var configB = &SkaffoldConfig{
APIVersion: "skaffold/v1",
Kind: "Config",
Watch: true,
Build: BuildConfig{
Artifacts: []Artifact{
Artifacts: []*Artifact{
{
ImageName: "example",
Workspace: "./examples/app",
Expand All @@ -81,12 +61,11 @@ var configB = &SkaffoldConfig{

func TestParseConfig(t *testing.T) {
var tests = []struct {
description string
config string
defaultConfig *SkaffoldConfig
expected *SkaffoldConfig
badReader bool
shouldErr bool
description string
config string
expected *SkaffoldConfig
badReader bool
shouldErr bool
}{
{
description: "Parse config",
Expand All @@ -98,12 +77,6 @@ func TestParseConfig(t *testing.T) {
config: badConfigA,
shouldErr: true,
},
{
description: "default config",
defaultConfig: &SkaffoldConfig{Watch: true},
config: rawConfigA,
expected: configB,
},
{
description: "bad reader",
badReader: true,
Expand All @@ -118,7 +91,7 @@ func TestParseConfig(t *testing.T) {
if test.badReader {
r = testutil.BadReader{}
}
cfg, err := Parse(test.defaultConfig, r)
cfg, err := Parse(r)
testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expected, cfg)
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/docker/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func GetDockerfileDependencies(workspace string, r io.Reader) ([]string, error)
for dep := range depMap {
deps = append(deps, dep)
}

logrus.Infof("Found dependencies for dockerfile %s", deps)
expandedDeps, err := expandDeps(workspace, deps)
if err != nil {
return nil, errors.Wrap(err, "expanding dockerfile paths")
Expand Down
Loading

0 comments on commit 15a418c

Please sign in to comment.