-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Refactor skaffold init for more flexible builder detection #2274
Merged
TadCordle
merged 12 commits into
GoogleContainerTools:master
from
TadCordle:skaffold-init-refactor
Jun 25, 2019
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
48209c5
Refactor skaffold init for additional builder detection
TadCordle a9c2bef
Previous PR feedback
TadCordle 3813e2f
Some feedback
TadCordle 8336e0c
Feedback
TadCordle de9049a
Don't use Get in interface function names
TadCordle 9b15a9a
Add 'name' field to analyze json output
TadCordle cc88af4
Fix comment
TadCordle 6ec2cae
Feedback
TadCordle e920efe
Revert unfinalized --analyze changes
TadCordle 6f468b9
Merge remote-tracking branch 'upstream/master' into skaffold-init-ref…
TadCordle 51e4a39
Nits
TadCordle 1dca84a
Merge remote-tracking branch 'upstream/master' into skaffold-init-ref…
TadCordle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: moved from
docker/validate_test.go
.