Skip to content

Commit

Permalink
hack: use tools build constraint for generated-files deps
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Nov 17, 2022
1 parent f771330 commit 764327d
Show file tree
Hide file tree
Showing 55 changed files with 19,872 additions and 76 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ require (
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8
google.golang.org/genproto v0.0.0-20220502173005-c8bf987b8c21
google.golang.org/grpc v1.50.1
google.golang.org/protobuf v1.28.1
)

require (
Expand Down Expand Up @@ -149,7 +150,6 @@ require (
go.opentelemetry.io/otel/internal/metric v0.27.0 // indirect
go.opentelemetry.io/otel/metric v0.27.0 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
93 changes: 51 additions & 42 deletions hack/dockerfiles/generated-files.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,48 +1,57 @@
# syntax=docker/dockerfile-upstream:master

# protoc is dynamically linked to glibc to can't use golang:1.10-alpine
FROM golang:1.19-buster AS gobuild-base

RUN apt-get update && apt-get --no-install-recommends install -y \
unzip \
&& true

# https://github.com/golang/protobuf/blob/v1.3.5/.travis.yml#L15
ARG PROTOC_VERSION=3.11.4
ARG TARGETOS TARGETARCH
RUN set -e; \
arch=$(echo $TARGETARCH | sed -e s/amd64/x86_64/ -e s/arm64/aarch_64/); \
wget -q https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-${TARGETOS}-${arch}.zip && unzip protoc-${PROTOC_VERSION}-${TARGETOS}-${arch}.zip -d /usr/local

ARG GOGO_VERSION=v1.3.2
RUN --mount=target=/root/.cache,type=cache GO111MODULE=on go install \
github.com/gogo/protobuf/protoc-gen-gogo@${GOGO_VERSION} \
github.com/gogo/protobuf/protoc-gen-gogofaster@${GOGO_VERSION} \
github.com/gogo/protobuf/protoc-gen-gogoslick@${GOGO_VERSION}

ARG PROTOBUF_VERSION=v1.3.5
RUN --mount=target=/root/.cache,type=cache GO111MODULE=on go install \
github.com/golang/protobuf/protoc-gen-go@${PROTOBUF_VERSION}

ARG GO_VERSION="1.19"
ARG PROTOC_VERSION="3.11.4"

# protoc is dynamically linked to glibc so can't use alpine base
FROM golang:${GO_VERSION}-buster AS base
RUN apt-get update && apt-get --no-install-recommends install -y git unzip
ARG PROTOC_VERSION
ARG TARGETOS
ARG TARGETARCH
RUN <<EOT
set -e
arch=$(echo $TARGETARCH | sed -e s/amd64/x86_64/ -e s/arm64/aarch_64/)
wget -q https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-${TARGETOS}-${arch}.zip
unzip protoc-${PROTOC_VERSION}-${TARGETOS}-${arch}.zip -d /usr/local
EOT
WORKDIR /go/src/github.com/moby/buildkit

# Generate into a subdirectory because if it is in the root then the
# extraction with `docker export` ends up putting `.dockerenv`, `dev`,
# `sys` and `proc` into the source directory. With this we can use
# `tar --strip-components=1 generated-files` on the output of `docker
# export`.
FROM gobuild-base AS generated
RUN mkdir /generated-files
RUN --mount=target=/tmp/src \
cp -r /tmp/src/. . && \
git add -A && \
go generate -mod=vendor -v ./... && \
git ls-files -m --others -- **/*.pb.go | tar -cf - --files-from - | tar -C /generated-files -xf -
FROM base AS tools
RUN --mount=type=bind,target=.,rw \
--mount=type=cache,target=/root/.cache \
--mount=type=cache,target=/go/pkg/mod \
go install \
github.com/gogo/protobuf/protoc-gen-gogo \
github.com/gogo/protobuf/protoc-gen-gogofaster \
github.com/gogo/protobuf/protoc-gen-gogoslick && \
# FIXME: Should be inferred from go.mod (1.5.2 atm)
go install \
github.com/golang/protobuf/[email protected]

FROM tools AS generated
RUN --mount=type=bind,target=.,rw <<EOT
set -ex
go generate -mod=vendor -v ./...
mkdir /out
git ls-files -m --others -- ':!vendor' '**/*.pb.go' | tar -cf - --files-from - | tar -C /out -xf -
EOT

FROM scratch AS update
COPY --from=generated /generated-files /generated-files

FROM gobuild-base AS validate
RUN --mount=target=/tmp/src \
cp -r /tmp/src/. . && \
go generate -mod=vendor -v ./... && git diff && ./hack/validate-generated-files check
COPY --from=generated /out /

FROM base AS validate
RUN --mount=type=bind,target=.,rw \
--mount=type=bind,from=generated,source=/out,target=/generated-files <<EOT
set -e
git add -A
if [ "$(ls -A /generated-files)" ]; then
cp -rf /generated-files/* .
fi
diff=$(git status --porcelain -- ':!vendor' '**/*.pb.go')
if [ -n "$diff" ]; then
echo >&2 'ERROR: The result of "go generate" differs. Please update with "make generated-files"'
echo "$diff"
exit 1
fi
EOT
7 changes: 1 addition & 6 deletions hack/update-generated-files
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
. $(dirname $0)/util
set -eu

gogo_version=$(awk '$1 == "github.com/gogo/protobuf" { print $2 }' go.mod)
# protobuf_version=$(awk '$3 == "github.com/golang/protobuf" { print $4 }' go.mod)
output=$(mktemp -d -t buildctl-output.XXXXXXXXXX)

buildxCmd build \
--target "update" \
--build-arg "GOGO_VERSION=$gogo_version" \
--output "type=local,dest=$output" \
--file "./hack/dockerfiles/generated-files.Dockerfile" \
.

# --build-arg "PROTOBUF_VERSION=$protobuf_version" \

cp -R "$output/generated-files/." .
cp -R "$output/." .
rm -rf $output
34 changes: 7 additions & 27 deletions hack/validate-generated-files
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
#!/usr/bin/env bash

. $(dirname $0)/util
set -eu

case ${1:-} in
'')
. $(dirname $0)/util
gogo_version=$(awk '$1 == "github.com/gogo/protobuf" { print $2 }' go.mod)
buildxCmd build \
--target validate \
--build-arg "GOGO_VERSION=$gogo_version" \
--file ./hack/dockerfiles/generated-files.Dockerfile \
.
;;
check)
diffs="$(git status --porcelain -- **/*.pb.go 2>/dev/null)"
set +x
if [ "$diffs" ]; then
{
echo 'The result of "go generate" differs'
echo
echo "$diffs"
echo
echo 'Please update with "make generated-files"'
echo
} >&2
exit 1
fi
echo 'Congratulations! All auto generated files are correct.'
;;
esac
buildxCmd build \
--target "validate" \
--output "type=cacheonly" \
--file "./hack/dockerfiles/generated-files.Dockerfile" \
.
15 changes: 15 additions & 0 deletions tools/tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build tools
// +build tools

// Package tools tracks dependencies on binaries not referenced in this codebase.
// https://github.com/golang/go/wiki/Modules#how-can-i-track-tool-dependencies-for-a-module
// Disclaimer: Avoid adding tools that don't need to be inferred from go.mod
// like golangci-lint and check they don't import too many dependencies.
package tools

import (
_ "github.com/gogo/protobuf/protoc-gen-gogo"
_ "github.com/gogo/protobuf/protoc-gen-gogofaster"
_ "github.com/gogo/protobuf/protoc-gen-gogoslick"
_ "github.com/golang/protobuf/protoc-gen-go"
)
Loading

0 comments on commit 764327d

Please sign in to comment.