Skip to content

Conversation

yuwenma
Copy link
Contributor

@yuwenma yuwenma commented Nov 7, 2022

This PR extends the kfn build to use ko as the default builder.

It now supports the following cases

  • kfn build:
    if ko is not installed, kfn build will run go install github.com/google/ko@latest to install ko; default repo is ko.local.
    user can specify two flags --repo for KO_DOCKER_REPO and --tag to tag the ko image

  • kfn build --builder docker:
    if docker is not installed, this execution will fail with user message kfn requires that "docker" is installed and on the PATH
    users can specify to use docker as builder via --builder=docker flag
    users can specify two flags to control the docker build --dockerfile=<PATH TO DOCKERFILE> and --image=<image:tag> which is the same as docker build --tag flag.

I have a separate PR to hide the Dockerfile unless user requests it to be exposed. #643

Unittest coverage on kfn build.

Copy link
Contributor

@justinsb justinsb 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 suggestions for the future, but lgtm

r.Command.Flags().StringVarP(&r.BuilderType, "builder", "b", Ko,
"the image builder. `ko` is the default builder, which requires `go build`; `docker` is accepted, and "+
" requires you to have docker installed and running")
r.Command.Flags().StringVarP(&r.Docker.Image, "image", "i", Image,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: does r.Docker.Image this mean that this can't configure the ko image?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I had a todo to better align the -image and -tag flags. It would require several corner case handling which are not quite the focus of this PR.

r.Command.Flags().StringVarP(&r.Ko.Tag, "tag", "t", "latest",
"the ko image tag")
// TODO: Docker CLI uses `--tag` flag to refer to "image:tag", which could be confusing but broadly accepted.
// We should better guide users on how to use "tag" and "image" flags for kfn.
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I think this is OK for now but I should be able to specify the image name (with tag) and have it work for both docker and ko. I guess we also want consistent behaviour if we specify only the tag or image, but that's less important.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally agree.
I want to make it more user friendly so would like to support cases if image contains tag, or tag contains image (which docker build does).

}
color.Green(out.String())
color.Green("Image %v builds successfully. Now you can publish the image", r.Tag)
fmt.Println(out)
Copy link
Contributor

Choose a reason for hiding this comment

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

If you're going to print it anyway, might be better to stream it (by setting cmd.Stdout = os.Stdout and cmd.Stderr = os.Stderr) ... TBD.

Copy link
Contributor Author

@yuwenma yuwenma Nov 7, 2022

Choose a reason for hiding this comment

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

Using cmd.Stdout = os.Stdout will printout all the logs, while the out only shows the last line (empty in many cases). I'm sort of hesitating between concise user messages and debugging modes with detailed logs.
changed to stdout.

}

func (r *BuildRunner) dockerfileExist() bool {
func (r *DockerBuilder) fileExist() bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: dockerfileExists?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! changed back to dockerfileExists.
Because I changed the object from BuildRunner to DockerBuilder, I thought it is verbose to repeat docker. similar to docker build --file.

Image = "function:latest"
DockerfilePath = "Dockerfile"
builtinDockerfilePath = "embed/Dockerfile"
BuiltinDockerfilePath = "embed/Dockerfile"
Copy link
Contributor

Choose a reason for hiding this comment

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

Try the go:embed stuff ... it's really cool

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, I'm using go:embed. https://github.com/GoogleContainerTools/kpt-functions-sdk/blob/master/go/cli/commands/build.go#L31
this is just to give the embedded dockerfile a const var.

@yuwenma yuwenma force-pushed the kfn-ko-build branch 2 times, most recently from 5d8cfb0 to c19f84a Compare November 7, 2022 21:34
@yuwenma yuwenma merged commit af40447 into kptdev:master Nov 7, 2022
@yuwenma yuwenma deleted the kfn-ko-build branch November 7, 2022 23:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants