Skip to content

Implements jib.GetDependenciesMaven/GetDependenciesGradle.#1058

Merged
balopat merged 34 commits intoGoogleContainerTools:jib_skaffoldfrom
coollog:jib-GetDependenciesMaven
Oct 3, 2018
Merged

Implements jib.GetDependenciesMaven/GetDependenciesGradle.#1058
balopat merged 34 commits intoGoogleContainerTools:jib_skaffoldfrom
coollog:jib-GetDependenciesMaven

Conversation

@coollog
Copy link
Copy Markdown
Contributor

@coollog coollog commented Sep 27, 2018

This PR builds off of #1043 and should merge that before merging this.

Implements jib/jib.go with GetDependenciesMaven and GetDependenciesGradle.

For Maven, calls out to mvn jib:_skaffold-files -q. Uses the wrapper mvnw if present. Uses Windows wrapper mvnw.cmd if present and OS is Windows.

For Gradle, calls out to gradle _jibSkaffoldFiles -q. Uses the wrapper gradlew if present. Uses Windows wrapper gradlew.bat if present and OS is Windows.

Note that this only covers the single-module use case and multi-module use case will be added in future PRs.

@GoogleContainerTools/java-tools

return err == nil
}

func getDependencies(workspace string, buildFile string, defaultExecutable string, windowsExecutable string, isWindows bool, subCommand []string, artifactName string) ([]string, error) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Would a struct over a long param list be preferred?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could isWindows be a package variable? I created some helpers for execution that looks for the maven/gradle wrappers with different extensions. (https://github.com/briandealwis/skaffold/tree/jib-build)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Linking the commit for reference: briandealwis@1ec213d

I'll try to change that and see if the testing works well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm, not sure if package variables are good for readability? @GoogleContainerTools/container-tools

if isWindows {
if exists(workspace, windowsExecutable) {
executable = "call"
subCommand = append([]string{windowsExecutable}, subCommand...)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we need to use call from within Go?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, I actually need to use cmd.exe /C ... instead. Also, @GoogleContainerTools/container-tools is there a style preference for using back-ticks for string constants vs quotes?

Copy link
Copy Markdown

@tstromberg tstromberg Sep 28, 2018

Choose a reason for hiding this comment

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

Not that I'm aware of: we generally follow standard Go & Kubernetes Coding Conventions: https://github.com/kubernetes/community/blob/master/contributors/guide/coding-conventions.md

In practice, raw string literals (back-ticks) in Go are only used for strings containing a back-slash or double quotes. Other string declarations should probably use the interpreted string literal form (double quotes) unless there is a special requirement to do so.

For specific discussion of string constants (ones declared with const), see the "String constants" section on https://blog.golang.org/constants

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for clarifying!

return getDependencies(workspace, "build.gradle", "gradle", "gradlew.bat", isWindows, []string{"_jibSkaffoldFiles", "-q"}, "jib-gradle")
}

func exists(workspace string, filename string) bool {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It would be worth making this an isDir() that also tests that the !info.IsDir()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure what the directory check is for?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Right now this will return true for a directory.

return err == nil
}

func getDependencies(workspace string, buildFile string, defaultExecutable string, windowsExecutable string, isWindows bool, subCommand []string, artifactName string) ([]string, error) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could isWindows be a package variable? I created some helpers for execution that looks for the maven/gradle wrappers with different extensions. (https://github.com/briandealwis/skaffold/tree/jib-build)

@coollog coollog changed the base branch from master to jib_skaffold September 28, 2018 17:32
// All paths are absolute.
// TODO(coollog): Add support for multi-module projects.
func GetDependenciesMaven(workspace string, _ /*a*/ *v1alpha3.JibMavenArtifact, isWindows bool) ([]string, error) {
return getDependencies(workspace, "pom.xml", "mvn", "mvnw.cmd", isWindows, []string{"jib:_skaffold-files", "-q"}, "jib-maven")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This might need to specify the profile in use.

type JibMavenArtifact struct {
// Only multi-module
Module string `yaml:"module"`
Profile string `yaml:"profile"`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Turns out profile is not multimodule only.

@coollog coollog force-pushed the jib-GetDependenciesMaven branch from 125e03b to 6895843 Compare September 28, 2018 17:42
Copy link
Copy Markdown
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

Operating system related code should go to *_GOOS suffixed files instead of explicit runtime.GOOS checks. Unit tests that are testing platform dependent code can be also separated out to run/be built only on the given OS using build tags.

docs: https://golang.org/pkg/go/build/
See this minikube package as an example: https://github.com/kubernetes/minikube/tree/master/pkg/minikube/cluster

@coollog
Copy link
Copy Markdown
Contributor Author

coollog commented Sep 28, 2018

@balopat Will change into that format.

gradleWindowsWrapper = "gradlew.bat"
)

func getCommandMaven(workspace string, a *v1alpha3.JibMavenArtifact, isWindows bool) (executable string, subCommand []string) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we generalize this and getCommandGradle to take the subcommand as an argument?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

getCommand does take subCommand (defaultSubCommand) as an argument

@coollog
Copy link
Copy Markdown
Contributor Author

coollog commented Sep 28, 2018

Changed to use *_GOOS filename format - LMK if this looks okay.

return getDependencies(workspace, "build.gradle", "gradle", "gradlew.bat", isWindows, []string{"_jibSkaffoldFiles", "-q"}, "jib-gradle")
}

func exists(workspace string, filename string) bool {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Right now this will return true for a directory.

package jib

func getWrapper() string {
return "gradlew"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This doesn't seem right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh is this missing something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh oops. Hmm, how did this even pass the tests.


wrapperExecutable := getWrapper()
if exists(workspace, wrapperExecutable) {
executable = "./" + wrapperExecutable
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it's worth making the path absolute just in case the command-execution sets a different working directory

package jib

func getWrapper() string {
return "gradlew"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Doesn't seem right?


wrapperExecutable := getWrapper()
if exists(workspace, wrapperExecutable) {
executable = "./" + wrapperExecutable
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ditto returning an absolute path

return "gradlew"
}

func getCommand(workspace string, defaultExecutable string, defaultSubCommand []string) (executable string, subCommand []string) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you capitalize this so that it can be used elsewhere?

Or provide InvokeMaven and InvokeGradle variants that hide the wrapper bit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think I'll leave that to another PR - one that would use this method from another package - it's probably good to keep visibility as low as possible until it needs to be exposed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I want to replace what I have :-)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Would making getCommandMaven and getCommandGradle exposed be good? I'll change it to include a custom subcommand to use.

@coollog
Copy link
Copy Markdown
Contributor Author

coollog commented Sep 28, 2018

Hmm, it seems if I name a test like jib_non_windows_test.go, it doesn't execute when I do make test on my linux machine. @GoogleContainerTools/container-tools

@coollog
Copy link
Copy Markdown
Contributor Author

coollog commented Oct 2, 2018

Alright, finally got Windows to pass.

@@ -0,0 +1,38 @@
/*
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

do we really need both jib_linux and jib_darwin? @balopat ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

no just call it jib.go and put // +build linux darwin at the top

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

you can call it jib_unix.go if you really want

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay, will go with jib_unix.go since jib.go has common logic shared with jib_windows.go

@codecov-io
Copy link
Copy Markdown

codecov-io commented Oct 3, 2018

Codecov Report

Merging #1058 into jib_skaffold will increase coverage by 0.82%.
The diff coverage is 100%.

Impacted file tree graph

@@               Coverage Diff                @@
##           jib_skaffold    #1058      +/-   ##
================================================
+ Coverage         40.72%   41.55%   +0.82%     
================================================
  Files                72       77       +5     
  Lines              3104     3148      +44     
================================================
+ Hits               1264     1308      +44     
  Misses             1715     1715              
  Partials            125      125
Impacted Files Coverage Δ
pkg/skaffold/jib/jib_unix.go 100% <100%> (ø)
pkg/skaffold/util/util.go 51.42% <100%> (+4%) ⬆️
pkg/skaffold/jib/jib_gradle.go 100% <100%> (ø)
pkg/skaffold/jib/jib_gradle_unix.go 100% <100%> (ø)
pkg/skaffold/jib/jib_maven.go 100% <100%> (ø)
pkg/skaffold/jib/jib_maven_unix.go 100% <100%> (ø)
pkg/skaffold/jib/jib.go 100% <100%> (ø) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3bd47ae...39fb94b. Read the comment docs.

Copy link
Copy Markdown
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

left some more reorganizational comments and nits


func getDependencies(workspace string, buildFile string, executable string, subCommand []string, artifactName string) ([]string, error) {
if _, err := resolveFile(workspace, buildFile); err != nil {
return nil, errors.Errorf("no %s found", buildFile)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: use errors.Wrap instead with err

}

// resolveFile resolves the absolute path of the file named filename in directory workspace, erroring if it is not a file
func resolveFile(workspace string, filename string) (string, error) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

  • this could live in pkg/skaffold/util/util.go
  • I would rename it to just AbsFile.
  • workspace -> dir

package jib

func getWrapper(defaultExecutable string) string {
switch defaultExecutable {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this smells a little bit like we are trying to decide between maven/gradle logic using arbitrary consts (lack of enums I guess?), while in other cases we are using "Maven/Gradle" postfix. It's starting to get confusing what logic belongs to maven/gradle and what's common. I'd like to see if introducing the subpackages under jib/maven, jib/gradle would help or not.

@coollog
Copy link
Copy Markdown
Contributor Author

coollog commented Oct 3, 2018

Rearranged the structure into separate jib_maven.go/jib_gradle.go and their respective OS-specific code. Let me know if this structure looks okay and I'll migrate the tests over as well.

@balopat
Copy link
Copy Markdown
Contributor

balopat commented Oct 3, 2018

It looks good to me, go ahead with the tests!

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)

func getCommand(workspace string, defaultExecutable string, wrapperExecutable string, defaultSubCommand []string) *exec.Cmd {
Copy link
Copy Markdown
Member

@loosebazooka loosebazooka Oct 3, 2018

Choose a reason for hiding this comment

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

if there's no alternative to the subCommand, do we need to specify default?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nope, I'll rename to just subCommand

@loosebazooka
Copy link
Copy Markdown
Member

We don't need to cover expanding directories in this pr, right?

@coollog
Copy link
Copy Markdown
Contributor Author

coollog commented Oct 3, 2018

Tests are updated as well and added TODO for expanding directories in follow-up PR.

Copy link
Copy Markdown
Contributor

@balopat balopat left a comment

Choose a reason for hiding this comment

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

LGTM

@balopat balopat merged commit 412101b into GoogleContainerTools:jib_skaffold Oct 3, 2018
@coollog coollog deleted the jib-GetDependenciesMaven branch October 3, 2018 22:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants