-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor skaffold init for more flexible builder detection (#2274)
- Loading branch information
Showing
6 changed files
with
476 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
Copyright 2019 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 docker | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" | ||
"github.com/moby/buildkit/frontend/dockerfile/command" | ||
"github.com/moby/buildkit/frontend/dockerfile/parser" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// For testing | ||
var ( | ||
ValidateDockerfileFunc = ValidateDockerfile | ||
) | ||
|
||
// Docker is the path to a dockerfile. Implements the InitBuilder interface. | ||
type Docker string | ||
|
||
// Name returns the name of the builder, "Docker" | ||
func (d Docker) Name() string { | ||
return "Docker" | ||
} | ||
|
||
// Describe returns the initBuilder's string representation, used when prompting the user to choose a builder. | ||
func (d Docker) Describe() string { | ||
return fmt.Sprintf("%s (%s)", d.Name(), d) | ||
} | ||
|
||
// CreateArtifact creates an Artifact to be included in the generated Build Config | ||
func (d Docker) CreateArtifact(manifestImage string) *latest.Artifact { | ||
path := string(d) | ||
workspace := filepath.Dir(path) | ||
a := &latest.Artifact{ImageName: manifestImage} | ||
if workspace != "." { | ||
a.Workspace = workspace | ||
} | ||
if filepath.Base(path) != constants.DefaultDockerfilePath { | ||
a.ArtifactType = latest.ArtifactType{ | ||
DockerArtifact: &latest.DockerArtifact{DockerfilePath: path}, | ||
} | ||
} | ||
|
||
return a | ||
} | ||
|
||
// ConfiguredImage returns the target image configured by the builder, or an empty string if no image is configured | ||
func (d Docker) ConfiguredImage() string { | ||
// Target image is not configured in dockerfiles | ||
return "" | ||
} | ||
|
||
// Path returns the path to the dockerfile | ||
func (d Docker) Path() string { | ||
return string(d) | ||
} | ||
|
||
// ValidateDockerfile makes sure the given Dockerfile is existing and valid. | ||
func ValidateDockerfile(path string) bool { | ||
f, err := os.Open(path) | ||
if err != nil { | ||
logrus.Warnf("opening file %s: %s", path, err.Error()) | ||
return false | ||
} | ||
|
||
res, err := parser.Parse(f) | ||
if err != nil || res == nil || len(res.AST.Children) == 0 { | ||
return false | ||
} | ||
|
||
// validate each node contains valid dockerfile directive | ||
for _, child := range res.AST.Children { | ||
_, ok := command.Commands[child.Value] | ||
if !ok { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
Copyright 2019 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 docker | ||
|
||
import ( | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" | ||
"github.com/GoogleContainerTools/skaffold/testutil" | ||
) | ||
|
||
func TestValidateDockerfile(t *testing.T) { | ||
var tests = []struct { | ||
description string | ||
content string | ||
fileToValidate string | ||
expectedValid bool | ||
}{ | ||
{ | ||
description: "valid", | ||
content: "FROM scratch", | ||
fileToValidate: "Dockerfile", | ||
expectedValid: true, | ||
}, | ||
{ | ||
description: "invalid command", | ||
content: "GARBAGE", | ||
fileToValidate: "Dockerfile", | ||
expectedValid: false, | ||
}, | ||
{ | ||
description: "not found", | ||
fileToValidate: "Unknown", | ||
expectedValid: false, | ||
}, | ||
{ | ||
description: "invalid file", | ||
content: "#escape", | ||
fileToValidate: "Dockerfile", | ||
expectedValid: false, | ||
}, | ||
} | ||
for _, test := range tests { | ||
testutil.Run(t, test.description, func(t *testutil.T) { | ||
tmpDir := t.NewTempDir(). | ||
Write("Dockerfile", test.content) | ||
|
||
valid := ValidateDockerfile(tmpDir.Path(test.fileToValidate)) | ||
|
||
t.CheckDeepEqual(test.expectedValid, valid) | ||
}) | ||
} | ||
} | ||
|
||
func TestDescribe(t *testing.T) { | ||
var tests = []struct { | ||
description string | ||
dockerfile Docker | ||
expectedPrompt string | ||
}{ | ||
{ | ||
description: "Dockerfile prompt", | ||
dockerfile: Docker("path/to/Dockerfile"), | ||
expectedPrompt: "Docker (path/to/Dockerfile)", | ||
}, | ||
} | ||
for _, test := range tests { | ||
testutil.Run(t, test.description, func(t *testutil.T) { | ||
t.CheckDeepEqual(test.expectedPrompt, test.dockerfile.Describe()) | ||
}) | ||
} | ||
} | ||
|
||
func TestCreateArtifact(t *testing.T) { | ||
var tests = []struct { | ||
description string | ||
dockerfile Docker | ||
manifestImage string | ||
expectedArtifact latest.Artifact | ||
expectedImage string | ||
}{ | ||
{ | ||
description: "default filename", | ||
dockerfile: Docker(filepath.Join("path", "to", "Dockerfile")), | ||
manifestImage: "image", | ||
expectedArtifact: latest.Artifact{ | ||
ImageName: "image", | ||
Workspace: filepath.Join("path", "to"), | ||
ArtifactType: latest.ArtifactType{}, | ||
}, | ||
}, | ||
{ | ||
description: "non-default filename", | ||
dockerfile: Docker(filepath.Join("path", "to", "Dockerfile1")), | ||
manifestImage: "image", | ||
expectedArtifact: latest.Artifact{ | ||
ImageName: "image", | ||
Workspace: filepath.Join("path", "to"), | ||
ArtifactType: latest.ArtifactType{ | ||
DockerArtifact: &latest.DockerArtifact{DockerfilePath: filepath.Join("path", "to", "Dockerfile1")}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
description: "ignore workspace", | ||
dockerfile: Docker("Dockerfile"), | ||
manifestImage: "image", | ||
expectedArtifact: latest.Artifact{ | ||
ImageName: "image", | ||
ArtifactType: latest.ArtifactType{}, | ||
}, | ||
}, | ||
} | ||
for _, test := range tests { | ||
testutil.Run(t, test.description, func(t *testutil.T) { | ||
artifact := test.dockerfile.CreateArtifact(test.manifestImage) | ||
t.CheckDeepEqual(test.expectedArtifact, *artifact) | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.