From f67e1b64aacaeead115add243c7b103e455037e3 Mon Sep 17 00:00:00 2001 From: Emily McMullan Date: Thu, 9 May 2024 10:33:07 -0400 Subject: [PATCH] Cherry-pick: Use build args for image (#226) use build args for image Signed-off-by: Emily McMullan --- Dockerfile | 18 +++++++++++++++--- cmd/settings.go | 23 ++++++++++++++++++----- cmd/version.go | 8 ++++++-- hack/update-settings.sh | 14 -------------- 4 files changed, 39 insertions(+), 24 deletions(-) delete mode 100755 hack/update-settings.sh diff --git a/Dockerfile b/Dockerfile index 123be2b..90c0c4a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,9 +26,21 @@ COPY cmd/ cmd/ # Build ARG VERSION ARG BUILD_COMMIT -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' -X 'github.com/konveyor-ecosystem/kantra/cmd.BuildCommit=$BUILD_COMMIT'" -a -o kantra main.go -RUN CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' -X 'github.com/konveyor-ecosystem/kantra/cmd.BuildCommit=$BUILD_COMMIT'" -a -o darwin-kantra main.go -RUN CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' -X 'github.com/konveyor-ecosystem/kantra/cmd.BuildCommit=$BUILD_COMMIT'" -a -o windows-kantra main.go +ARG IMAGE=quay.io/konveyor/kantra +ARG NAME=kantra +ARG JAVA_BUNDLE=/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar + +RUN CGO_ENABLED=0 GOOS=linux go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' \ +-X 'github.com/konveyor-ecosystem/kantra/cmd.RunnerImage=$IMAGE' -X 'github.com/konveyor-ecosystem/kantra/cmd.BuildCommit=$BUILD_COMMIT' \ +-X 'github.com/konveyor-ecosystem/kantra/cmd.JavaBundlesLocation=$JAVA_BUNDLE' -X 'github.com/konveyor-ecosystem/kantra/cmd.RootCommandName=$NAME'" -a -o kantra main.go + +RUN CGO_ENABLED=0 GOOS=darwin go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' \ +-X 'github.com/konveyor-ecosystem/kantra/cmd.RunnerImage=$IMAGE' -X 'github.com/konveyor-ecosystem/kantra/cmd.BuildCommit=$BUILD_COMMIT' \ +-X 'github.com/konveyor-ecosystem/kantra/cmd.JavaBundlesLocation=$JAVA_BUNDLE' -X 'github.com/konveyor-ecosystem/kantra/cmd.RootCommandName=$NAME'" -a -o darwin-kantra main.go + +RUN CGO_ENABLED=0 GOOS=windows go build --ldflags="-X 'github.com/konveyor-ecosystem/kantra/cmd.Version=$VERSION' \ +-X 'github.com/konveyor-ecosystem/kantra/cmd.RunnerImage=$IMAGE' -X 'github.com/konveyor-ecosystem/kantra/cmd.BuildCommit=$BUILD_COMMIT' \ +-X 'github.com/konveyor-ecosystem/kantra/cmd.JavaBundlesLocation=$JAVA_BUNDLE' -X 'github.com/konveyor-ecosystem/kantra/cmd.RootCommandName=$NAME'" -a -o windows-kantra main.go FROM quay.io/konveyor/analyzer-lsp:release-0.3 diff --git a/cmd/settings.go b/cmd/settings.go index ffc2a8b..2516bf6 100644 --- a/cmd/settings.go +++ b/cmd/settings.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "os/exec" + "strings" "github.com/codingconcepts/env" ) @@ -19,7 +20,6 @@ const ( XMLRulePath = "/opt/xmlrules" ShimOutputPath = "/opt/shimoutput" CustomRulePath = "/opt/input/rules" - JavaBundlesLocation = "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" ) type Config struct { @@ -43,7 +43,9 @@ func (c *Config) Load() error { if err := c.loadRunnerImg(); err != nil { return err } - + if err := c.loadCommandName(); err != nil { + return err + } err := env.Set(c) if err != nil { return err @@ -52,9 +54,20 @@ func (c *Config) Load() error { } func (c *Config) loadRunnerImg() error { - if Version != "v99.0.0" { - updatedImg := fmt.Sprintf("quay.io/konveyor/kantra:%v", Version) - err := os.Setenv("RUNNER_IMG", updatedImg) + // if version tag is given in image + img := strings.TrimSuffix(RunnerImage, fmt.Sprintf(":%v", Version)) + updatedImg := fmt.Sprintf("%v:%v", img, Version) + err := os.Setenv("RUNNER_IMG", updatedImg) + if err != nil { + return err + } + + return nil +} + +func (c *Config) loadCommandName() error { + if RootCommandName != "kantra" { + err := os.Setenv("CMD_NAME", RootCommandName) if err != nil { return err } diff --git a/cmd/version.go b/cmd/version.go index f553ca3..4a5e5ef 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -7,8 +7,11 @@ import ( ) var ( - BuildCommit = "" - Version = "v99.0.0" + BuildCommit = "" + Version = "release-0.3" + RunnerImage = "quay.io/konveyor/kantra" + RootCommandName = "kantra" + JavaBundlesLocation = "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" ) // Use build flags to set correct Version and BuildCommit @@ -22,6 +25,7 @@ func NewVersionCommand() *cobra.Command { Run: func(cmd *cobra.Command, args []string) { fmt.Printf("version: %s\n", Version) fmt.Printf("SHA: %s\n", BuildCommit) + fmt.Printf("image: %s\n", RunnerImage) }, } return versionCmd diff --git a/hack/update-settings.sh b/hack/update-settings.sh deleted file mode 100755 index 22bc226..0000000 --- a/hack/update-settings.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -# Set IMAGE and NAME environment variables, and run the script in root dir: -# $ IMAGE=quay.io/konveyor/kantra NAME=kantra ./hack/update-settings.sh - -image="${IMAGE:-quay.io/konveyor/kantra}" -name="${NAME:-kantra}" -bundle="${BUNDLE:-/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar}" - -sed -i \ - -e "s,\(RootCommandName *string.*default:\"\)[^\"]*\",\1$name\"," \ - -e "s,\(RunnerImage *string.*default:\"\)[^\"]*\",\1$image\"," \ - -e "s,\(JavaBundlesLocation *= *\"\)[^\"]*\",\1$bundle\"," \ - ./cmd/settings.go