diff --git a/cmd/skaffold/app/cmd/build.go b/cmd/skaffold/app/cmd/build.go index 7295dc31359..22c6aecf101 100644 --- a/cmd/skaffold/app/cmd/build.go +++ b/cmd/skaffold/app/cmd/build.go @@ -23,6 +23,7 @@ import ( "github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/flags" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -35,11 +36,11 @@ var ( // NewCmdBuild describes the CLI command to build artifacts. func NewCmdBuild(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "build", + Use: constants.CommandBuild, Short: "Builds the artifacts", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - opts.Command = "build" + opts.Command = constants.CommandBuild return runBuild(out) }, } diff --git a/cmd/skaffold/app/cmd/delete.go b/cmd/skaffold/app/cmd/delete.go index 25e75475f7b..0fce6468891 100644 --- a/cmd/skaffold/app/cmd/delete.go +++ b/cmd/skaffold/app/cmd/delete.go @@ -20,6 +20,7 @@ import ( "context" "io" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -27,11 +28,11 @@ import ( // NewCmdDelete describes the CLI command to delete deployed resources. func NewCmdDelete(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "delete", + Use: constants.CommandDelete, Short: "Delete the deployed resources", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - opts.Command = "delete" + opts.Command = constants.CommandDelete return delete(out) }, } diff --git a/cmd/skaffold/app/cmd/deploy.go b/cmd/skaffold/app/cmd/deploy.go index b34f41af14a..0fd17e6c98a 100644 --- a/cmd/skaffold/app/cmd/deploy.go +++ b/cmd/skaffold/app/cmd/deploy.go @@ -19,18 +19,19 @@ package cmd import ( "io" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/spf13/cobra" ) // NewCmdDeploy describes the CLI command to deploy artifacts. func NewCmdDeploy(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "deploy", + Use: constants.CommandDeploy, Short: "Deploys the artifacts", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { // Same actions as `skaffold run`, but with pre-built images. - opts.Command = "deploy" + opts.Command = constants.CommandDeploy return run(out) }, } diff --git a/cmd/skaffold/app/cmd/dev.go b/cmd/skaffold/app/cmd/dev.go index ecdea1af560..3c5c67b7046 100644 --- a/cmd/skaffold/app/cmd/dev.go +++ b/cmd/skaffold/app/cmd/dev.go @@ -20,6 +20,7 @@ import ( "context" "io" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -29,11 +30,11 @@ import ( // NewCmdDev describes the CLI command to run a pipeline in development mode. func NewCmdDev(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "dev", + Use: constants.CommandDev, Short: "Runs a pipeline file in development mode", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - opts.Command = "dev" + opts.Command = constants.CommandDev return dev(out) }, } diff --git a/cmd/skaffold/app/cmd/run.go b/cmd/skaffold/app/cmd/run.go index bb3b4caac3d..22a4cff3da6 100644 --- a/cmd/skaffold/app/cmd/run.go +++ b/cmd/skaffold/app/cmd/run.go @@ -21,6 +21,7 @@ import ( "io" "github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/tips" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -28,11 +29,11 @@ import ( // NewCmdRun describes the CLI command to run a pipeline. func NewCmdRun(out io.Writer) *cobra.Command { cmd := &cobra.Command{ - Use: "run", + Use: constants.CommandRun, Short: "Runs a pipeline file", Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { - opts.Command = "run" + opts.Command = constants.CommandRun err := run(out) if err == nil { tips.PrintForRun(out, opts) diff --git a/integration/run_test.go b/integration/run_test.go index d083cc1837b..dd184173adc 100644 --- a/integration/run_test.go +++ b/integration/run_test.go @@ -32,6 +32,7 @@ import ( "time" "github.com/GoogleContainerTools/skaffold/cmd/skaffold/app/cmd/config" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" kubernetesutil "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" "github.com/GoogleContainerTools/skaffold/testutil" @@ -146,7 +147,7 @@ func TestRun(t *testing.T) { ns, deleteNs := setupNamespace(t) defer deleteNs() - runSkaffold(t, "run", test.dir, ns.Name, test.filename, test.env) + runSkaffold(t, constants.CommandRun, test.dir, ns.Name, test.filename, test.env) for _, p := range test.pods { if err := kubernetesutil.WaitForPodReady(context.Background(), client.CoreV1().Pods(ns.Name), p); err != nil { @@ -160,7 +161,7 @@ func TestRun(t *testing.T) { } } - runSkaffold(t, "delete", test.dir, ns.Name, test.filename, test.env) + runSkaffold(t, constants.CommandDelete, test.dir, ns.Name, test.filename, test.env) }) } } @@ -169,7 +170,7 @@ func TestDeploy(t *testing.T) { ns, deleteNs := setupNamespace(t) defer deleteNs() - runSkaffold(t, "deploy", "examples/kustomize", ns.Name, "", nil, "--images", "index.docker.io/library/busybox:1") + runSkaffold(t, constants.CommandDeploy, "examples/kustomize", ns.Name, "", nil, "--images", "index.docker.io/library/busybox:1") depName := "kustomize-test" if err := kubernetesutil.WaitForDeploymentToStabilize(context.Background(), client, ns.Name, depName, 10*time.Minute); err != nil { @@ -185,7 +186,7 @@ func TestDeploy(t *testing.T) { t.Fatalf("Wrong image name in kustomized deployment: %s", dep.Spec.Template.Spec.Containers[0].Image) } - runSkaffold(t, "delete", "examples/kustomize", ns.Name, "", nil) + runSkaffold(t, constants.CommandDelete, "examples/kustomize", ns.Name, "", nil) } func TestDev(t *testing.T) { @@ -195,7 +196,7 @@ func TestDev(t *testing.T) { run(t, "examples/test-dev-job", "touch", "foo") defer run(t, "examples/test-dev-job", "rm", "foo") - go runSkaffoldNoFail("dev", "examples/test-dev-job", ns.Name, "", nil) + go runSkaffoldNoFail(constants.CommandDev, "examples/test-dev-job", ns.Name, "", nil) jobName := "test-dev-job" if err := kubernetesutil.WaitForJobToStabilize(context.Background(), client, ns.Name, jobName, 10*time.Minute); err != nil { @@ -227,7 +228,7 @@ func TestDevSync(t *testing.T) { ns, deleteNs := setupNamespace(t) defer deleteNs() - go runSkaffoldNoFail("dev", "examples/test-file-sync", ns.Name, "", nil) + go runSkaffoldNoFail(constants.CommandDev, "examples/test-file-sync", ns.Name, "", nil) if err := kubernetesutil.WaitForPodReady(context.Background(), client.CoreV1().Pods(ns.Name), "test-file-sync"); err != nil { t.Fatalf("Timed out waiting for pod ready") @@ -300,7 +301,7 @@ func TestFix(t *testing.T) { t.Fatalf("testing error: %v", err) } - runCmd := exec.Command("skaffold", "run", "--namespace", ns.Name, "-f", "-") + runCmd := exec.Command("skaffold", constants.CommandRun, "--namespace", ns.Name, "-f", "-") runCmd.Dir = "testdata" runCmd.Stdin = bytes.NewReader(out) err = util.RunCmd(runCmd) @@ -429,7 +430,7 @@ func TestInit(t *testing.T) { t.Fatalf("running init: %v, output: %s", err, out) } - runCmd := exec.Command("skaffold", "run", "-f", generatedYaml) + runCmd := exec.Command("skaffold", constants.CommandRun, "-f", generatedYaml) runCmd.Dir = test.dir out, err = util.RunCmdOut(runCmd) if err != nil { diff --git a/pkg/skaffold/constants/constants.go b/pkg/skaffold/constants/constants.go index 3f15650bc49..396494f5bf8 100644 --- a/pkg/skaffold/constants/constants.go +++ b/pkg/skaffold/constants/constants.go @@ -66,6 +66,12 @@ const ( // A regex matching valid repository names (https://github.com/docker/distribution/blob/master/reference/reference.go) RepositoryComponentRegex string = `^[a-z\d]+(?:(?:[_.]|__|-+)[a-z\d]+)*$` + + CommandBuild = "build" + CommandDelete = "delete" + CommandDeploy = "deploy" + CommandDev = "dev" + CommandRun = "run" ) var DefaultKubectlManifests = []string{"k8s/*.yaml"} diff --git a/pkg/skaffold/runner/runner.go b/pkg/skaffold/runner/runner.go index 4e21ca85f9d..a8ea2e1f810 100644 --- a/pkg/skaffold/runner/runner.go +++ b/pkg/skaffold/runner/runner.go @@ -32,6 +32,7 @@ import ( "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/local" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes" kubectx "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/context" @@ -92,7 +93,7 @@ func NewForConfig(opts *config.SkaffoldOptions, cfg *latest.SkaffoldPipeline) (* return nil, errors.Wrap(err, "parsing test config") } - forceDeploy := opts.Command == "dev" || opts.ForceDeploy + forceDeploy := opts.Command == constants.CommandDev || opts.ForceDeploy deployer, err := getDeployer(&cfg.Deploy, kubeContext, opts.Namespace, forceDeploy, defaultRepo) if err != nil { return nil, errors.Wrap(err, "parsing deploy config") diff --git a/pkg/skaffold/schema/profiles_test.go b/pkg/skaffold/schema/profiles_test.go index 4afcb0809bb..5ceeed2b44f 100644 --- a/pkg/skaffold/schema/profiles_test.go +++ b/pkg/skaffold/schema/profiles_test.go @@ -21,6 +21,7 @@ import ( "testing" cfg "github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" "github.com/GoogleContainerTools/skaffold/testutil" yamlpatch "github.com/krishicks/yaml-patch" @@ -228,7 +229,7 @@ func TestActivatedProfiles(t *testing.T) { { description: "Selected on the command line", opts: &cfg.SkaffoldOptions{ - Command: "dev", + Command: constants.CommandDev, Profiles: []string{"activated", "also-activated"}, }, profiles: []latest.Profile{ @@ -240,12 +241,12 @@ func TestActivatedProfiles(t *testing.T) { }, { description: "Auto-activated by command", opts: &cfg.SkaffoldOptions{ - Command: "dev", + Command: constants.CommandDev, }, profiles: []latest.Profile{ - {Name: "run-profile", Activation: []latest.Activation{{Command: "run"}}}, - {Name: "dev-profile", Activation: []latest.Activation{{Command: "dev"}}}, - {Name: "non-run-profile", Activation: []latest.Activation{{Command: "!run"}}}, + {Name: "run-profile", Activation: []latest.Activation{{Command: constants.CommandRun}}}, + {Name: "dev-profile", Activation: []latest.Activation{{Command: constants.CommandDev}}}, + {Name: "non-run-profile", Activation: []latest.Activation{{Command: "!" + constants.CommandRun}}}, }, expected: []string{"dev-profile", "non-run-profile"}, }, { @@ -276,21 +277,21 @@ func TestActivatedProfiles(t *testing.T) { }, { description: "AND between activation criteria", opts: &cfg.SkaffoldOptions{ - Command: "dev", + Command: constants.CommandDev, }, profiles: []latest.Profile{ { Name: "activated", Activation: []latest.Activation{{ Env: "KEY=VALUE", KubeContext: "prod-context", - Command: "dev", + Command: constants.CommandDev, }}, }, { Name: "not-activated", Activation: []latest.Activation{{ Env: "KEY=VALUE", KubeContext: "prod-context", - Command: "build", + Command: constants.CommandBuild, }}, }, }, @@ -298,14 +299,14 @@ func TestActivatedProfiles(t *testing.T) { }, { description: "OR between activations", opts: &cfg.SkaffoldOptions{ - Command: "dev", + Command: constants.CommandDev, }, profiles: []latest.Profile{ { Name: "activated", Activation: []latest.Activation{{ - Command: "run", + Command: constants.CommandRun, }, { - Command: "dev", + Command: constants.CommandDev, }}, }, },