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

add ko support for building and pushing container images #2564

Closed
wants to merge 0 commits into from
Closed

add ko support for building and pushing container images #2564

wants to merge 0 commits into from

Conversation

developer-guy
Copy link
Member

@developer-guy developer-guy commented Oct 6, 2021

Signed-off-by: Batuhan Apaydin batuhan.apaydin@trendyol.com
Co-authored-by: Furkan furkan.turkal@trendyol.com (@Dentrax)
Co-authored-by: Erkan Zileli erkan.zileli@trendyol.com (@erkanzileli)

fix #2556

to add ko support for building and pushing container images

Sample .goreleaser.yml
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
  hooks:
    # You may remove this if you don't use go modules.
    - go mod tidy
builds:
  - env:
      - CGO_ENABLED=0
    goos:
      - linux
      - windows
      - darwin
    ldflags:
      - -s -w -X github.com/GoTurkiye/hello-world-cli/cmd.GitCommit={{ .Commit }} -X github.com/GoTurkiye/hello-world-cli/cmd.Version={{ .Version }}
archives:
  - replacements:
      darwin: Darwin
      linux: Linux
      windows: Windows
      386: i386
      amd64: x86_64
checksum:
  name_template: 'checksums.txt'
snapshot:
  name_template: "{{ incpatch .Version }}-next"
changelog:
  sort: asc
  filters:
    exclude:
      - '^docs:'
      - '^test:'
dockers:
  - image_templates: [ "ghcr.io/goturkiye/{{ .ProjectName }}"]
    goos: linux
    goarch: amd64
    use: ko  ## notice here

Screen Shot 2021-10-07 at 00 05 18
Screen Shot 2021-10-07 at 00 04 09

@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Oct 6, 2021
Copy link
Member

@caarlos0 caarlos0 left a comment

Choose a reason for hiding this comment

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

thanks for the PR!

made some comments here.

would also be nice to add tests for this 🙏

internal/pipe/docker/api_ko.go Outdated Show resolved Hide resolved
internal/pipe/docker/api_ko.go Outdated Show resolved Hide resolved
internal/pipe/docker/api_ko.go Outdated Show resolved Hide resolved
internal/pipe/docker/api_ko.go Outdated Show resolved Hide resolved
@erkanzileli erkanzileli mentioned this pull request Oct 7, 2021
4 tasks
@caarlos0
Copy link
Member

caarlos0 commented Oct 7, 2021

btw, with this we can also easily create multi-arch docker images, just need to pass --platform=all to docker args 🤝

go.mod Outdated Show resolved Hide resolved
@caarlos0
Copy link
Member

caarlos0 commented Oct 7, 2021

thinking more about this, I got more questions:

  • ko has a lot of config that the docker pipe wont support... what should we do about that?
  • in the same line, we are inferring the module from ./go.mod, but what happens if the user has many go.mod files?

everything would likely work for "simple" projects, but not sure about more complex ones...

@developer-guy
Copy link
Member Author

I completed all the comments, here is the result when I tried this integration with my own CLI and it uses the following .goreleaser.yaml:

$ export GITHUB_TOKEN=xxx
$ export KO_DOCKER_REPO=docker.io/devopps
$ goreleaser release --rm-dist
$ crane ls devopps/hello-world-cli
0.3.3
latest
v0
.goreleaser
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
  hooks:
    # You may remove this if you don't use go modules.
    - go mod tidy

builds:
  - env:
      - CGO_ENABLED=0
    goos:
      - linux
      - windows
      - darwin
    ldflags:
      - -s -w -X github.com/GoTurkiye/hello-world-cli/cmd.GitCommit={{ .Commit }} -X github.com/GoTurkiye/hello-world-cli/cmd.Version={{ .Version }}
archives:
  - replacements:
      darwin: Darwin
      linux: Linux
      windows: Windows
      386: i386
      amd64: x86_64
checksum:
  name_template: 'checksums.txt'
snapshot:
  name_template: "{{ incpatch .Version }}-next"
changelog:
  sort: asc
  filters:
    exclude:
      - '^docs:'
      - '^test:'
dockers:
  - image_templates:
    - "{{ .ProjectName }}"
    - "{{ .ProjectName }}:{{ .Tag }}"
    - "{{ .ProjectName }}:v{{ .Major }}"
    goos: linux
    goarch: amd64
    use: ko

PTAL @caarlos0 @imjasonh @Dentrax @erkanzileli

@developer-guy
Copy link
Member Author

thinking more about this, I got more questions:

  • ko has a lot of config that the docker pipe wont support... what should we do about that?
  • in the same line, we are inferring the module from ./go.mod, but what happens if the user has many go.mod files?

everything would likely work for "simple" projects, but not sure about more complex ones...

@imjasonh maybe can help us here.

@imjasonh
Copy link
Contributor

imjasonh commented Nov 1, 2021

Sorry for the delayed response.

I'm a bit worried that ko-in-goreleaser will be confusing since ko itself reuses some of goreleaser's config types in .ko.yaml, and it may be confusing to decide where to describe certain things between goreleaser (how the binary gets built) and .ko.yaml (how the image(s) get built)

For the question of "how to configure ko", could we have a special ko: section in the goreleaser config? This would let users describe their module without having to inspect go.mod(s), and could also help with unsupported fields in the docker: field, since we won't need to use it.

wdyt?

cc @jonjohnsonjr

@developer-guy
Copy link
Member Author

developer-guy commented Nov 2, 2021

Sorry for the delayed response.

I'm a bit worried that ko-in-goreleaser will be confusing since ko itself reuses some of goreleaser's config types in .ko.yaml, and it may be confusing to decide where to describe certain things between goreleaser (how the binary gets built) and .ko.yaml (how the image(s) get built)

For the question of "how to configure ko", could we have a special ko: section in the goreleaser config? This would let users describe their module without having to inspect go.mod(s), and could also help with unsupported fields in the docker: field, since we won't need to use it.

wdyt?

cc @jonjohnsonjr

thank you for such a great explanation, I think both methods (current solution and your suggestion) would work fine but yes I agree with your concerns about configuring ko while using it in GoReleaser because ko also builds the binary first which means that fields defined in build: section will be ignored in this case, so we have to use something like the following (moving the build section to .ko.yaml)

👀 https://github.com/google/ko#overriding-go-build-settings

WDYT @caarlos0?

@caarlos0
Copy link
Member

caarlos0 commented Nov 3, 2021

I'm thinking maybe we could instead use ko as a dependency and skip the go build part (as we already do that), and just build the images... would that be possible (haven't investigate ko's codebase yet)? if so, wdyt?

@Dentrax
Copy link
Member

Dentrax commented Nov 3, 2021

I'm thinking maybe we could instead use ko as a dependency and skip the go build part (as we already do that), and just build the images... would that be possible (haven't investigate ko's codebase yet)? if so, wdyt?

I'm not so sure whether instead of using the binary, and importing ko code as a library is an anti-pattern or not. Is ko intended to be used as a library? We can use it as a library ofc, but that is a side-effect of how ko code is structured, which is subject to change. To clarify aforementioned concerns and the getting feedback from "ko as a dependency", cc'ing @imjasonh 🙃

@imjasonh
Copy link
Contributor

imjasonh commented Nov 3, 2021

ko can be used as a library, some folks on Skaffold are also looking into integrating ko using its library methods.

The main functions you'd probably care about are build.NewGo to create a new builder, and publish.NewDefault to push the image.

@caarlos0
Copy link
Member

caarlos0 commented Nov 3, 2021

cc/ @cpanato

@caarlos0
Copy link
Member

closing for now, as we'll discuss this in our community meeting

@caarlos0 caarlos0 closed this Jan 13, 2022
@jonjohnsonjr
Copy link
Contributor

@caarlos0 tried searching briefly for docs around a community meeting and couldn't find anything -- have a link handy?

@caarlos0
Copy link
Member

sure, here you go: goreleaser/community#2

PS: we don't have a proper date yet, but likely later january or early february

@developer-guy developer-guy reopened this Oct 22, 2022
@developer-guy
Copy link
Member Author

developer-guy commented Oct 22, 2022

let's kick this off and rewrite it by using ko as a library instead of executing it directly through the binary.

This repo will help us with that: https://github.com/imjasonh/miniko.

/cc @imjasonh @caarlos0

@pull-request-size pull-request-size bot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 22, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Dec 5, 2022

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request]: add google/ko as brand new builder engine for building and pushing container images
6 participants