yannh / kubeconform

A FAST Kubernetes manifests validator, with support for Custom Resources!
Apache License 2.0
2.15k stars 121 forks source link

Version not returned when using go installer #200

Closed nvuillam closed 1 year ago

nvuillam commented 1 year ago

Hi, thanks for your work on your great linter , that we embedded within MegaLinter !

In our Dockerfile, we have the following line to get the latest version of KubeConform

RUN go install github.com/yannh/kubeconform/cmd/kubeconform@latest

We have an automated CI job that upgrades linters and writes their version in online documentation, and kubeconform always returns "development".

Is there some workaround, like maybe a different installation command for Alpine linux, or maybe it is a bug ?

To reproduce:

$ go install github.com/yannh/kubeconform/cmd/kubeconform@latest
$ kubeconform -v
development

Thanks & best regards !

yannh commented 1 year ago

Hi @nvuillam ! The version is injected at build time by overriding this variable https://github.com/yannh/kubeconform/blob/ce2f6de185c210d5237e57261b93bd33b7ff0bc6/cmd/kubeconform/main.go#L18 , in goreleaser, here: https://github.com/yannh/kubeconform/blob/ce2f6de185c210d5237e57261b93bd33b7ff0bc6/.goreleaser.yml#L24

This is passed to go build as a -ldflags parameter. I am not sure if you can pass that parameter using go install as well? :thinking:

EDIT: Yup

yann@yann-thinkpad:~$ go install -ldflags "-X 'main.version=foo'" github.com/yannh/kubeconform/cmd/kubeconform@v0.4.13 
yann@yann-thinkpad:~$ /home/yann/go/bin/kubeconform -v
foo

Does this help? Best, Yann

nvuillam commented 1 year ago

Thanks for the quick reply :)

I'm unfortunately not expert in go, I'm just the linter guy ^^

I need a command that will always install the latest version of kubeconform on an alpine linux base dockerfile (without knowing the number of the latest version), then be able to retrieve the version number by calling kubeconform -v , is there a way to do it ? :)

It can also work with a COPY from another alpine-based docker image that would be up to date with latest kubeconform version

yannh commented 1 year ago

When you install like this "go install github.com/yannh/kubeconform/cmd/kubeconform@latest" you install the latest master, which is the development version, so this is behaving as expected :bow: If you want to install a specific version, you would need to go install at a particular tag, eg:

go install -ldflags "-X 'main.version=v0.4.13'" github.com/yannh/kubeconform/cmd/kubeconform@v0.4.13

You might need a bit of bash to figure out what that tag might be. Docker images are published here: https://github.com/yannh/kubeconform/pkgs/container/kubeconform , latest here would point to the latest tagged version.

nvuillam commented 1 year ago

I see you publish a latest-alpine, it seems to be exactly what I'm looking for :)

https://github.com/yannh/kubeconform/pkgs/container/kubeconform/73554889?tag=latest-alpine

FROM ghcr.io/yannh/kubeconform:latest-alpine as kubeconform
COPY --link --from=kubeconform /bin/kubeconform /usr/bin/

Would that work ? :)

yannh commented 1 year ago

That should work yes :+1:

nvuillam commented 1 year ago

@yannh I have the following error... maybe you could direct me to the appropriate folder(s) to copy, as it seems it is not /bin/kubeconform ?

 > [stage-18 15/50] COPY --link --from=kubeconform /bin/kubeconform /usr/bin/:
------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref y758aaqmsou52b3qygl48tttz::lixznodorvzlxb2z9ovar6lgl: "/bin/kubeconform": not found
yannh commented 1 year ago

Just /kubeconform 👍

nvuillam commented 1 year ago

Works like a charm, many thanks for your fast and efficient support @yannh :)

nvuillam commented 1 year ago

(If you are interested, to thank you I can configure MegaLinter for you on kubeconform repo someday :) )