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

Add a Build command #476

Merged
merged 5 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 49 additions & 0 deletions cmd/skaffold/app/cmd/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
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 (
"context"
"io"

"github.com/spf13/cobra"
)

// NewCmdBuild describes the CLI command to build artifacts.
func NewCmdBuild(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "build",
Short: "Builds the artifacts",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return build(out, filename)
},
}
AddRunDevFlags(cmd)
return cmd
}

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

runner, err := NewRunner(out, filename)
if err != nil {
return err
}

return runner.Build(ctx)
}
45 changes: 23 additions & 22 deletions cmd/skaffold/app/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package cmd

import (
"context"
"io"

yaml "gopkg.in/yaml.v2"
Expand Down Expand Up @@ -45,7 +44,6 @@ var rootCmd = &cobra.Command{
}

func NewSkaffoldCommand(out, err io.Writer) *cobra.Command {

rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if err := SetUpLogs(err, v); err != nil {
return err
Expand All @@ -58,6 +56,7 @@ func NewSkaffoldCommand(out, err io.Writer) *cobra.Command {
rootCmd.AddCommand(NewCmdVersion(out))
rootCmd.AddCommand(NewCmdRun(out))
rootCmd.AddCommand(NewCmdDev(out))
rootCmd.AddCommand(NewCmdBuild(out))
rootCmd.AddCommand(NewCmdFix(out))
rootCmd.AddCommand(NewCmdDocker(out))

Expand Down Expand Up @@ -90,26 +89,39 @@ func SetUpLogs(out io.Writer, level string) error {
return nil
}

func runSkaffold(out io.Writer, dev bool, filename string) error {
ctx := context.Background()
func NewRunner(out io.Writer, filename string) (*runner.SkaffoldRunner, error) {
config, err := readConfiguration(filename)
if err != nil {
return nil, errors.Wrap(err, "reading configuration")
}

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

return r, nil
}

func readConfiguration(filename string) (*config.SkaffoldConfig, error) {
buf, err := util.ReadConfiguration(filename)
if err != nil {
return errors.Wrap(err, "read skaffold config")
return nil, errors.Wrap(err, "read skaffold config")
}

apiVersion := &config.ApiVersion{}
if err := yaml.Unmarshal(buf, apiVersion); err != nil {
return errors.Wrap(err, "parsing api version")
return nil, errors.Wrap(err, "parsing api version")
}

if apiVersion.Version != config.LatestVersion {
return errors.New("Config version out of date: run `skaffold fix`")
return nil, errors.New("Config version out of date: run `skaffold fix`")
}

cfg, err := config.GetConfig(buf, true, dev)
cfg, err := config.GetConfig(buf, true)
if err != nil {
return errors.Wrap(err, "parsing skaffold config")
return nil, errors.Wrap(err, "parsing skaffold config")
}

// we already ensured that the versions match in the previous block,
Expand All @@ -118,19 +130,8 @@ func runSkaffold(out io.Writer, dev bool, filename string) error {

err = latestConfig.ApplyProfiles(opts.Profiles)
if err != nil {
return errors.Wrap(err, "applying profiles")
}

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

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

return nil
return latestConfig, nil
}
15 changes: 14 additions & 1 deletion cmd/skaffold/app/cmd/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,34 @@ limitations under the License.
package cmd

import (
"context"
"io"

"github.com/spf13/cobra"
)

// NewCmdDev describes the CLI command to run a pipeline in development mode.
func NewCmdDev(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "dev",
Short: "Runs a pipeline file in development mode",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runSkaffold(out, true, filename)
return dev(out, filename)
},
}
AddRunDevFlags(cmd)
AddDevFlags(cmd)
return cmd
}

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

runner, err := NewRunner(out, filename)
if err != nil {
return err
}

return runner.Build(ctx)
}
2 changes: 1 addition & 1 deletion cmd/skaffold/app/cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewCmdFix(out io.Writer) *cobra.Command {
if err != nil {
logrus.Errorf("fix: %s", err)
}
cfg, err := config.GetConfig(contents, false, true)
cfg, err := config.GetConfig(contents, false)
if err != nil {
logrus.Error(err)
return
Expand Down
15 changes: 14 additions & 1 deletion cmd/skaffold/app/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,35 @@ limitations under the License.
package cmd

import (
"context"
"io"

"github.com/spf13/cobra"
)

// NewCmdRun describes the CLI command to run a pipeline.
func NewCmdRun(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "run",
Short: "Runs a pipeline file",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return runSkaffold(out, false, filename)
return run(out, filename)
},
}
AddRunDevFlags(cmd)

cmd.Flags().StringVarP(&opts.CustomTag, "tag", "t", "", "The optional custom tag to use for images which overrides the current Tagger configuration")
return cmd
}

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

runner, err := NewRunner(out, filename)
if err != nil {
return err
}

return runner.Run(ctx)
}
47 changes: 4 additions & 43 deletions pkg/skaffold/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,35 +67,22 @@ func TestParseConfig(t *testing.T) {
var tests = []struct {
description string
config string
dev bool
expected util.VersionedConfig
badReader bool
shouldErr bool
}{
{
description: "Minimal config for dev",
description: "Minimal config",
config: minimalConfig,
dev: true,
expected: config(
withLocalBuild(
withTagPolicy(v1alpha2.TagPolicy{ShaTagger: &v1alpha2.ShaTagger{}}),
),
),
},
{
description: "Minimal config for run",
config: minimalConfig,
dev: false,
expected: config(
withLocalBuild(
withTagPolicy(v1alpha2.TagPolicy{GitTagger: &v1alpha2.GitTagger{}}),
),
),
},
{
description: "Simple config for dev",
description: "Simple config",
config: simpleConfig,
dev: true,
expected: config(
withLocalBuild(
withTagPolicy(v1alpha2.TagPolicy{GitTagger: &v1alpha2.GitTagger{}}),
Expand All @@ -105,34 +92,8 @@ func TestParseConfig(t *testing.T) {
),
},
{
description: "Simple config for run",
config: simpleConfig,
dev: false,
expected: config(
withLocalBuild(
withTagPolicy(v1alpha2.TagPolicy{GitTagger: &v1alpha2.GitTagger{}}),
withDockerArtifact("example", ".", "Dockerfile"),
),
withDeploy("example"),
),
},
{
description: "Complete config for dev",
config: completeConfig,
dev: true,
expected: config(
withGCBBuild("ID",
withTagPolicy(v1alpha2.TagPolicy{ShaTagger: &v1alpha2.ShaTagger{}}),
withDockerArtifact("image1", "./examples/app1", "Dockerfile.dev"),
withBazelArtifact("image2", "./examples/app2", "//:example.tar"),
),
withDeploy("example"),
),
},
{
description: "Complete config for run",
description: "Complete config",
config: completeConfig,
dev: false,
expected: config(
withGCBBuild("ID",
withTagPolicy(v1alpha2.TagPolicy{ShaTagger: &v1alpha2.ShaTagger{}}),
Expand All @@ -151,7 +112,7 @@ func TestParseConfig(t *testing.T) {

for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
cfg, err := GetConfig([]byte(test.config), true, test.dev)
cfg, err := GetConfig([]byte(test.config), true)
testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expected, cfg)
})
}
Expand Down
1 change: 0 additions & 1 deletion pkg/skaffold/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import "io"
// SkaffoldOptions are options that are set by command line arguments not included
// in the config file itself
type SkaffoldOptions struct {
DevMode bool
Cleanup bool
Notification bool
Profiles []string
Expand Down
14 changes: 7 additions & 7 deletions pkg/skaffold/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ import (
// Ordered list of all schema versions
var Versions = []string{v1alpha1.Version, v1alpha2.Version}

var schemaVersions map[string]func([]byte, bool, bool) (util.VersionedConfig, error) = map[string]func([]byte, bool, bool) (util.VersionedConfig, error){
v1alpha1.Version: func(contents []byte, useDefault bool, dev bool) (util.VersionedConfig, error) {
var schemaVersions = map[string]func([]byte, bool) (util.VersionedConfig, error){
v1alpha1.Version: func(contents []byte, useDefault bool) (util.VersionedConfig, error) {
config := new(v1alpha1.SkaffoldConfig)
err := config.Parse(contents, useDefault, dev)
err := config.Parse(contents, useDefault)
return config, err
},
v1alpha2.Version: func(contents []byte, useDefault bool, dev bool) (util.VersionedConfig, error) {
v1alpha2.Version: func(contents []byte, useDefault bool) (util.VersionedConfig, error) {
config := new(v1alpha2.SkaffoldConfig)
err := config.Parse(contents, useDefault, dev)
err := config.Parse(contents, useDefault)
return config, err
},
}

func GetConfig(contents []byte, useDefault bool, dev bool) (util.VersionedConfig, error) {
func GetConfig(contents []byte, useDefault bool) (util.VersionedConfig, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this also mean that we no longer have default differences between dev/run? I'm ok with that if thats the case, but it should be noted.

for _, version := range Versions {
if cfg, err := schemaVersions[version](contents, useDefault, dev); err == nil {
if cfg, err := schemaVersions[version](contents, useDefault); err == nil {
// successfully parsed, but make sure versions match
if cfg.GetVersion() == version {
return cfg, err
Expand Down
Loading