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

Refactor skaffold init for more flexible builder detection #2274

Merged
merged 12 commits into from
Jun 25, 2019
Merged

Refactor skaffold init for more flexible builder detection #2274

merged 12 commits into from
Jun 25, 2019

Conversation

TadCordle
Copy link
Contributor

@TadCordle TadCordle commented Jun 17, 2019

This PR introduces an InitBuilder interface so that skaffold init can more easily detect non-Docker builders in the future. The interface allows us to:

  • Name(): Get the name of the builder that was detected
  • Describe(): Use a specialized prompt string in the interactive CLI while choosing builder/image pairs
  • CreateArtifact(): Generate the corresponding build config artifact for the builder
  • GetConfiguredImage(): Detect a builder's configured target image to skip choosing it from the CLI
  • GetPath(): Get the path to the builder's config file

Follow-up for updating --analyze: #2327

Follow-up for Jib support: #2276

}

// ValidateDockerfile makes sure the given Dockerfile is existing and valid.
var ValidateDockerfile = func(path string) bool {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: this function was moved from docker/validate.go, to keep all the init-related docker stuff in one file. If it makes more sense to leave the InitBuilder implementation by itself, I can bring back the original file and move this back.

Copy link
Member

Choose a reason for hiding this comment

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

Curious: why the var name = func() syntax instead of directly defining a function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, this is another thing that happens in the followup, I end up mocking this function in a test.

"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestValidateDockerfile(t *testing.T) {
Copy link
Contributor Author

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.

// autoSelectBuilders takes a list of builders and images, checks if any of the builders' configured target
// images match an image in the image list, and returns a list of the matching builder/image pairs. Also
// returns the images from the original image list that didn't match any build configurations.
func autoSelectBuilders(buildConfigs []InitBuilder, images []string) ([]BuilderImagePair, []string) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: This function is only really useful for builders that can configure a target image themselves, so there will be a test for this function in the Jib follow-up PR, since Jib makes better use of this than Dockerfiles.

}

// ValidateDockerfile makes sure the given Dockerfile is existing and valid.
var ValidateDockerfile = func(path string) bool {
Copy link
Member

Choose a reason for hiding this comment

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

Curious: why the var name = func() syntax instead of directly defining a function?

pkg/skaffold/initializer/init.go Show resolved Hide resolved
pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
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`")
func printAnalyzeJSON(out io.Writer, skipBuild bool, buildConfigs []InitBuilder, images []string) error {
Copy link
Member

Choose a reason for hiding this comment

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

Is this only for printing debugging output? Does changing the JSON output format not matter?

Copy link
Contributor Author

@TadCordle TadCordle Jun 17, 2019

Choose a reason for hiding this comment

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

Hmm that's a good point, I'm not sure. It's not used from anywhere within skaffold, but might be used externally.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed this a bit so the new output is more consistent across builders/more easily digested by IDEs, but I'm going to double check with the vs code team since I think they use this.

pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
@chanseokoh
Copy link
Member

chanseokoh commented Jun 18, 2019

The logic looks good to me (except for the breaking schema change we talked about.)

Others with Go expertise will have a chance to give feedback on style, naming, effective code, etc.

pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
pkg/skaffold/initializer/init.go Show resolved Hide resolved
pkg/skaffold/initializer/init.go Show resolved Hide resolved
pkg/skaffold/initializer/init.go Show resolved Hide resolved
@codecov-io
Copy link

codecov-io commented Jun 19, 2019

Codecov Report

Merging #2274 into master will increase coverage by 0.33%.
The diff coverage is 59.82%.

Impacted Files Coverage Δ
pkg/skaffold/initializer/init.go 38.82% <47.67%> (+17.22%) ⬆️
pkg/skaffold/docker/docker_init.go 93.54% <93.54%> (ø)

pkg/skaffold/docker/docker_init.go Outdated Show resolved Hide resolved
pkg/skaffold/docker/docker_init.go Outdated Show resolved Hide resolved
pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
@TadCordle
Copy link
Contributor Author

I think I'm going to revert the --analyze changes and make another PR for those, since I don't want this to get too much bigger.

@chanseokoh
Copy link
Member

The last commit to revert --analyze looks OK. That can be done later. Other code remains same, so LGTM for the overall logic.

Copy link
Contributor

@dgageot dgageot left a comment

Choose a reason for hiding this comment

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

a few nits

pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
pkg/skaffold/initializer/init.go Outdated Show resolved Hide resolved
Copy link
Contributor

@nkubala nkubala left a comment

Choose a reason for hiding this comment

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

lgtm

@TadCordle TadCordle merged commit 0cb12ce into GoogleContainerTools:master Jun 25, 2019
@TadCordle TadCordle deleted the skaffold-init-refactor branch June 25, 2019 17:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants