From e86ea6c7143582471177dc6cbb08124a5e45f21e Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Tue, 1 Oct 2024 16:45:30 -0400 Subject: [PATCH 1/3] Prefer UBI and go download over library/golang - speed up build since there is less to download - reduce external dependencies - build and run on the same base image REPOSITORY TAG IMAGE ID CREATED SIZE my test 422e3c099772 10 seconds ago 366 MB docker.io/library/golang 1.23 361d8b5c9aa5 3 weeks ago 862 MB CP4AIOPS-7146 --- manageiq-operator/Dockerfile | 11 +++++++++-- spec/manageiq-operator/go_version_spec.rb | 8 +++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/manageiq-operator/Dockerfile b/manageiq-operator/Dockerfile index 6721871d0..73c80e4fc 100644 --- a/manageiq-operator/Dockerfile +++ b/manageiq-operator/Dockerfile @@ -1,5 +1,10 @@ # Build the manager binary -FROM docker.io/library/golang:1.23 as builder +FROM registry.access.redhat.com/ubi9/ubi-minimal as builder + +RUN microdnf -y install gzip tar && \ + curl -f -L https://go.dev/dl/go1.23.1.linux-amd64.tar.gz | tar -C / -xzf - + +ENV PATH=/go/bin:/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin WORKDIR /workspace # Copy the Go Modules manifests @@ -14,9 +19,11 @@ COPY cmd/main.go cmd/main.go COPY api/ api/ COPY internal/controller/ internal/controller/ -# Build +# Build go executable RUN CGO_ENABLED=0 go build -a -o manager cmd/main.go + +# Build operator image FROM registry.access.redhat.com/ubi9/ubi-minimal:latest LABEL name="ManageIQ Operator" \ diff --git a/spec/manageiq-operator/go_version_spec.rb b/spec/manageiq-operator/go_version_spec.rb index d4ca7873d..733f679cd 100644 --- a/spec/manageiq-operator/go_version_spec.rb +++ b/spec/manageiq-operator/go_version_spec.rb @@ -1,11 +1,9 @@ describe "Go Version" do it "matches go.mod" do require 'awesome_spawn' - dockerfile_version = File.read(ROOT.join("manageiq-operator", "Dockerfile")).match(/^FROM.+golang:(\d+\.\d+).+/)[1] - mod_version = File.read(ROOT.join("manageiq-operator", "go.mod")).match(/^go\s(\d+\.\d+)/)[1] - running_version = AwesomeSpawn.run!("go version", :chdir => ROOT.join("manageiq-operator")).output.match(/.*\sgo(\d+\.\d+).*/)[1] + mod_version = File.read(ROOT.join("manageiq-operator", "go.mod")).match(/^go\s(\d+\.\d+)/)[1] + running_version = AwesomeSpawn.run!("go version", :chdir => ROOT.join("manageiq-operator")).output.match(/.*\sgo(\d+\.\d+).*/)[1] - expect(dockerfile_version).to eq(mod_version) - expect(running_version).to eq(mod_version) + expect(running_version).to eq(mod_version) end end From 50f143a12091f84ef9d6759a811279a268523ed4 Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Wed, 2 Oct 2024 12:10:25 -0400 Subject: [PATCH 2/3] Dynamically find the arch and latest desired go version --- manageiq-operator/Dockerfile | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/manageiq-operator/Dockerfile b/manageiq-operator/Dockerfile index 73c80e4fc..fdab2a35d 100644 --- a/manageiq-operator/Dockerfile +++ b/manageiq-operator/Dockerfile @@ -1,8 +1,16 @@ # Build the manager binary FROM registry.access.redhat.com/ubi9/ubi-minimal as builder -RUN microdnf -y install gzip tar && \ - curl -f -L https://go.dev/dl/go1.23.1.linux-amd64.tar.gz | tar -C / -xzf - +COPY go.mod /go.mod + +RUN microdnf -y install gzip jq tar && \ + arch=$(uname -m) && \ + if [ ${arch} == "x86_64" ] ; then \ + arch="amd64" \ + ; fi && \ + goversion=$(sed -nr 's/^go\s+(.*)/go\1/p' /go.mod) && \ + file=$(curl -s 'https://go.dev/dl/?mode=json&include=all' | jq -r --arg GOVERSION "${goversion}" --arg ARCH "${arch}" 'map(select(.version | startswith($GOVERSION)))[0].files[] | select(.arch == $ARCH and .os == "linux").filename') && \ + curl -s -f -L https://go.dev/dl/${file} | tar -C / -xzf - ENV PATH=/go/bin:/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin From 596a30501c1ed95ff05f9e6f0771fec330d97a78 Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Wed, 2 Oct 2024 16:14:43 -0400 Subject: [PATCH 3/3] review feedback --- manageiq-operator/Dockerfile | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/manageiq-operator/Dockerfile b/manageiq-operator/Dockerfile index fdab2a35d..e8e076f58 100644 --- a/manageiq-operator/Dockerfile +++ b/manageiq-operator/Dockerfile @@ -4,12 +4,9 @@ FROM registry.access.redhat.com/ubi9/ubi-minimal as builder COPY go.mod /go.mod RUN microdnf -y install gzip jq tar && \ - arch=$(uname -m) && \ - if [ ${arch} == "x86_64" ] ; then \ - arch="amd64" \ - ; fi && \ + arch=$(uname -m | sed 's/x86_64/amd64/') && \ goversion=$(sed -nr 's/^go\s+(.*)/go\1/p' /go.mod) && \ - file=$(curl -s 'https://go.dev/dl/?mode=json&include=all' | jq -r --arg GOVERSION "${goversion}" --arg ARCH "${arch}" 'map(select(.version | startswith($GOVERSION)))[0].files[] | select(.arch == $ARCH and .os == "linux").filename') && \ + file=$(curl -s 'https://go.dev/dl/?mode=json&include=all' | jq -r --arg GOVERSION "${goversion}" --arg ARCH "${arch}" 'first(.[] | select(.version | startswith($GOVERSION))).files[] | select(.arch == $ARCH and .os == "linux").filename') && \ curl -s -f -L https://go.dev/dl/${file} | tar -C / -xzf - ENV PATH=/go/bin:/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin