From 0211c2c7a5deaf2c5f14ae6b4e3581bb72a054d6 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Fri, 15 Mar 2019 15:07:48 -0700 Subject: [PATCH 1/2] Error out if no Dockerfiles are found This should fix #1795; now, if no Dockerfiles are found in the workspace then skaffold will error out. If no k8s manifests are found it will error out (already implemented). --- pkg/skaffold/initializer/init.go | 3 +++ pkg/skaffold/initializer/init_test.go | 18 +++++++----------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/pkg/skaffold/initializer/init.go b/pkg/skaffold/initializer/init.go index 5189572ba54..914b99c7434 100644 --- a/pkg/skaffold/initializer/init.go +++ b/pkg/skaffold/initializer/init.go @@ -290,6 +290,9 @@ func generateSkaffoldPipeline(k Initializer, dockerfilePairs []dockerfilePair) ( } func printAnalyzeJSON(out io.Writer, dockerfiles, images []string) error { + if len(dockerfiles) == 0 { + return errors.New("one or more valid Dockerfiles must be present to run skaffold; please provide at least one Dockerfile and try again") + } a := struct { Dockerfiles []string `json:"dockerfiles,omitempty"` Images []string `json:"images,omitempty"` diff --git a/pkg/skaffold/initializer/init_test.go b/pkg/skaffold/initializer/init_test.go index 9e0c5440c7e..2caa2f1cf16 100644 --- a/pkg/skaffold/initializer/init_test.go +++ b/pkg/skaffold/initializer/init_test.go @@ -28,6 +28,7 @@ func TestPrintAnalyzeJSON(t *testing.T) { name string dockerfiles []string images []string + shouldErr bool expected string }{ { @@ -37,25 +38,20 @@ func TestPrintAnalyzeJSON(t *testing.T) { expected: "{\"dockerfiles\":[\"Dockerfile\",\"Dockerfile_2\"],\"images\":[\"image1\",\"image2\"]}", }, { - name: "no dockerfile", - images: []string{"image1", "image2"}, - expected: "{\"images\":[\"image1\",\"image2\"]}", + name: "no dockerfile", + images: []string{"image1", "image2"}, + shouldErr: true, }, { - name: "no images", - dockerfiles: []string{"Dockerfile", "Dockerfile_2"}, - expected: "{\"dockerfiles\":[\"Dockerfile\",\"Dockerfile_2\"]}", - }, - { - name: "no dockerfiles or images", - expected: "{}", + name: "no dockerfiles or images", + shouldErr: true, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { out := bytes.NewBuffer([]byte{}) err := printAnalyzeJSON(out, test.dockerfiles, test.images) - testutil.CheckErrorAndDeepEqual(t, false, err, test.expected, out.String()) + testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expected, out.String()) }) } } From 9d652dd58e001aa833b931c5cd7930982507fe60 Mon Sep 17 00:00:00 2001 From: Priya Wadhwa Date: Mon, 18 Mar 2019 14:37:01 -0700 Subject: [PATCH 2/2] updated error message, don't error if skipping build --- pkg/skaffold/initializer/init.go | 10 +++++----- pkg/skaffold/initializer/init_test.go | 8 +++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/skaffold/initializer/init.go b/pkg/skaffold/initializer/init.go index 914b99c7434..4f2701096e7 100644 --- a/pkg/skaffold/initializer/init.go +++ b/pkg/skaffold/initializer/init.go @@ -116,13 +116,13 @@ func DoInit(out io.Writer, c Config) error { } images := k.GetImages() if c.Analyze { - return printAnalyzeJSON(out, dockerfiles, images) + return printAnalyzeJSON(out, c.SkipBuild, dockerfiles, images) } var pairs []dockerfilePair // conditionally generate build artifacts if !c.SkipBuild { if len(dockerfiles) == 0 { - return errors.New("one or more valid Dockerfiles must be present to run skaffold; please provide at least one Dockerfile and try again") + return errors.New("one or more valid Dockerfiles must be present to build images with skaffold; please provide at least one Dockerfile and try again or run `skaffold init --skip-build`") } if c.CliArtifacts != nil { @@ -289,9 +289,9 @@ func generateSkaffoldPipeline(k Initializer, dockerfilePairs []dockerfilePair) ( return pipelineStr, nil } -func printAnalyzeJSON(out io.Writer, dockerfiles, images []string) error { - if len(dockerfiles) == 0 { - return errors.New("one or more valid Dockerfiles must be present to run skaffold; please provide at least one Dockerfile and try again") +func printAnalyzeJSON(out io.Writer, skipBuild bool, dockerfiles, images []string) error { + if !skipBuild && len(dockerfiles) == 0 { + return errors.New("one or more valid Dockerfiles must be present to build images with skaffold; please provide at least one Dockerfile and try again or run `skaffold init --skip-build`") } a := struct { Dockerfiles []string `json:"dockerfiles,omitempty"` diff --git a/pkg/skaffold/initializer/init_test.go b/pkg/skaffold/initializer/init_test.go index 2caa2f1cf16..12c12ed2a2d 100644 --- a/pkg/skaffold/initializer/init_test.go +++ b/pkg/skaffold/initializer/init_test.go @@ -28,6 +28,7 @@ func TestPrintAnalyzeJSON(t *testing.T) { name string dockerfiles []string images []string + skipBuild bool shouldErr bool expected string }{ @@ -37,6 +38,11 @@ func TestPrintAnalyzeJSON(t *testing.T) { images: []string{"image1", "image2"}, expected: "{\"dockerfiles\":[\"Dockerfile\",\"Dockerfile_2\"],\"images\":[\"image1\",\"image2\"]}", }, + { + name: "no dockerfile, skip build", + images: []string{"image1", "image2"}, + skipBuild: true, + expected: "{\"images\":[\"image1\",\"image2\"]}"}, { name: "no dockerfile", images: []string{"image1", "image2"}, @@ -50,7 +56,7 @@ func TestPrintAnalyzeJSON(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { out := bytes.NewBuffer([]byte{}) - err := printAnalyzeJSON(out, test.dockerfiles, test.images) + err := printAnalyzeJSON(out, test.skipBuild, test.dockerfiles, test.images) testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expected, out.String()) }) }