Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
redo of BUILDKIT caching PR (#3972)
Browse files Browse the repository at this point in the history
* [DO NOT MERGE] WIP:redo of BUILDKIT caching PR

* WIP: seeing if i can get tkr-source to compile

* updated caches to use the old way

* Cleanup and remove dbeugging

* revert poller.go change, irrelevant

* woudl typo fix
  • Loading branch information
jayunit100 authored and Guo XiaoShuang committed Mar 27, 2023
1 parent c90fdcd commit 1a06603
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 29 deletions.
10 changes: 6 additions & 4 deletions addons/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
# Copyright 2021 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# Enable Buildkit
# syntax=docker/dockerfile:1.4

# Build from publicly reachable source by default, but allow people to re-build images on
# top of their own trusted images.
ARG BUILDER_BASE_IMAGE=golang:1.18

# Build the manager binary
FROM $BUILDER_BASE_IMAGE as builder

WORKDIR /workspace

# Copy the go source
COPY ./ ./


# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod go mod download

# Build
ARG LD_FLAGS
ENV LD_FLAGS="$LD_FLAGS "'-extldflags "-static"'
RUN cd addons && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o bin/manager main.go
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod cd addons && CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -ldflags "$LD_FLAGS" -o bin/manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
3 changes: 3 additions & 0 deletions addons/manifests/samples/simple-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Copyright 2021 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# Enable Buildkit
# syntax=docker/dockerfile:1.4

FROM scratch
COPY templates/* ./
4 changes: 2 additions & 2 deletions capabilities/controller/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ COPY apis/ apis/
COPY cli/runtime cli/runtime

WORKDIR capabilities/controller
RUN go mod download
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod go mod download

# Build
ARG LD_FLAGS
ENV LD_FLAGS="$LD_FLAGS "'-extldflags "-static"'
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o manager main.go
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
5 changes: 5 additions & 0 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ ROOT_DIR := $(shell git rev-parse --show-toplevel)
RELATIVE_ROOT ?= .
CONTROLLER_GEN_SRC ?= "./..."

# Framework has lots of components, and many build steps that are disparate.
# Use docker buildkit so that we get faster image builds and skip redundant work.
# TODO: We need to measure what this speeds up, and what it doesn't (and why).
export DOCKER_BUILDKIT := 1

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
Expand Down
8 changes: 5 additions & 3 deletions featuregates/controller/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Copyright 2021 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# Enable Buildkit

# syntax=docker/dockerfile:1.4

# Build from publicly reachable source by default, but allow people to re-build images on
# top of their own trusted images.
Expand All @@ -19,7 +21,7 @@ COPY cli/runtime cli/runtime
COPY capabilities/client capabilities/client

WORKDIR featuregates/controller
RUN go mod download
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod go mod download
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer

Expand All @@ -31,7 +33,7 @@ COPY ./featuregates/controller/main.go ./main.go
ARG LD_FLAGS
ENV LD_FLAGS="$LD_FLAGS "'-extldflags "-static"'

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags "$LD_FLAGS" -o manager ./main.go
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -ldflags "$LD_FLAGS" -o manager ./main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
9 changes: 6 additions & 3 deletions object-propagation/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Copyright 2022 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# Enable Buildkit
# syntax=docker/dockerfile:1.4

# Build from publicly reachable source by default, but allow people to re-build images on
# top of their own trusted images.
ARG BUILDER_BASE_IMAGE=golang:1.18
Expand All @@ -18,7 +21,7 @@ COPY object-propagation/go.mod object-propagation/go.mod
COPY object-propagation/go.sum object-propagation/go.sum

WORKDIR /workspace/object-propagation
RUN go mod download
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod go mod download
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer

Expand All @@ -28,7 +31,7 @@ COPY object-propagation/ ./
# Build
ARG LD_FLAGS
ENV LD_FLAGS="$LD_FLAGS "'-extldflags "-static"'
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager main.go
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand All @@ -37,4 +40,4 @@ WORKDIR /
COPY --from=builder /workspace/object-propagation/manager .
USER nonroot:nonroot

ENTRYPOINT ["/manager"]
ENTRYPOINT ["/manager"]
8 changes: 5 additions & 3 deletions pinniped-components/post-deploy/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2021 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# syntax=docker.io/docker/dockerfile:1.3.0

# Build from publicly reachable source by default, but allow people to re-build images on
# top of their own trusted images.
Expand All @@ -14,15 +15,16 @@ WORKDIR /workspace
COPY tanzu-auth-controller-manager tanzu-auth-controller-manager
COPY post-deploy/go.mod post-deploy/go.mod
COPY post-deploy/go.sum post-deploy/go.sum
RUN cd post-deploy && go mod download

RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod cd post-deploy && go mod download

# Copy the source
COPY post-deploy/cmd/ post-deploy/cmd/
COPY post-deploy/pkg/ post-deploy/pkg/
COPY post-deploy/Makefile post-deploy/Makefile
#COPY .git/ .git/

RUN make native -C post-deploy
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod make native -C post-deploy

# Support older deployment YAMLs by providing symlink for the historic job name without -job.
# The subdirectory here is to ensure that the docker COPY command does not follow the symlink
Expand All @@ -36,4 +38,4 @@ WORKDIR /
COPY --from=builder /tmp/links/ .
COPY --from=builder /workspace/post-deploy/tkg-pinniped-post-deploy-job .
COPY --from=builder /workspace/post-deploy/tkg-pinniped-post-deploy-controller .
CMD ["/tkg-pinniped-post-deploy-job"]
CMD ["/tkg-pinniped-post-deploy-job"]
7 changes: 5 additions & 2 deletions pinniped-components/tanzu-auth-controller-manager/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Copyright 2022 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

# Enable Buildkit
# syntax=docker/dockerfile:1.4

# Build from publicly reachable source by default, but allow people to re-build images on
# top of their own trusted images.
ARG BUILDER_BASE_IMAGE=golang:1.18
Expand All @@ -12,7 +15,7 @@ WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
RUN go mod download
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod go mod download

# Copy the source
COPY main.go main.go
Expand All @@ -21,7 +24,7 @@ COPY controllers/ controllers/
# Build
ARG LD_FLAGS
ENV LD_FLAGS="$LD_FLAGS "'-extldflags "-static"'
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o tanzu-auth-controller-manager .
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o tanzu-auth-controller-manager .

FROM gcr.io/distroless/static:nonroot
WORKDIR /
Expand Down
5 changes: 3 additions & 2 deletions pkg/v1/tkr/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2022 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# syntax=docker.io/docker/dockerfile:1.3.0

# Build from publicly reachable source by default, but allow people to re-build images on
# top of their own trusted images.
Expand All @@ -18,7 +19,7 @@ COPY pkg/v1/tkr/go.mod pkg/v1/tkr/go.mod
COPY pkg/v1/tkr/go.sum pkg/v1/tkr/go.sum

WORKDIR /workspace/pkg/v1/tkr
RUN go mod download
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod go mod download
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer

Expand All @@ -28,7 +29,7 @@ COPY pkg/v1/tkr/ ./
# Build
ARG LD_FLAGS
ENV LD_FLAGS="$LD_FLAGS "'-extldflags "-static"'
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager ./main.go
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager ./main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
5 changes: 3 additions & 2 deletions tkg/vsphere-template-resolver/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2022 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# syntax=docker/dockerfile:1.4

# Build from publicly reachable source by default, but allow people to re-build images on
# top of their own trusted images.
Expand All @@ -26,7 +27,7 @@ COPY tkg/go.mod tkg/go.mod
COPY tkg/go.sum tkg/go.sum

WORKDIR /workspace/tkg
RUN go mod download
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod go mod download
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer

Expand All @@ -36,7 +37,7 @@ COPY tkg/ ./
# Build
ARG LD_FLAGS
ENV LD_FLAGS="$LD_FLAGS "'-extldflags "-static"'
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager vsphere-template-resolver/main.go
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager vsphere-template-resolver/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
17 changes: 15 additions & 2 deletions tkr/controller/tkr-source/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2022 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# syntax=docker/dockerfile:1.4

# Build from publicly reachable source by default, but allow people to re-build images on
# top of their own trusted images.
Expand All @@ -17,8 +18,18 @@ COPY util/ util/
COPY tkr/go.mod tkr/go.mod
COPY tkr/go.sum tkr/go.sum

# TODO: @jayunit100 ~ I Tried this but it didnt work, filed an upstream issue https://github.com/moby/buildkit/issues/3301
# ENV CACHE_GOBUILD='--mount="type=cache,target=/root/.cache/go-build"'
# ENV CACHE_GOLANG='--mount="type=cache,target=/root/.local/share/golang"'
# ENV CACHE_GOMOD='--mount="type=cache,target=/go/pkg/mod"'
# ENV CACHE="$CACHE_GOMOD $CACHE_GOLANG $CACHE_GOBUILD"
# RUN $CACHE go mod download
# Since this failed, we copy paste 3 --mount commands explicitly on line 31 below. Someday would be nice to generalize all of that into a interpolated call.

WORKDIR /workspace/tkr
RUN go mod download

RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod go mod download

# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer

Expand All @@ -28,7 +39,9 @@ COPY tkr/ ./
# Build
ARG LD_FLAGS
ENV LD_FLAGS="$LD_FLAGS "'-extldflags "-static"'
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager controller/tkr-source/main.go

# RUN --mount=type=cache,target=/root/.cache/go-build \
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager controller/tkr-source/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
5 changes: 3 additions & 2 deletions tkr/controller/tkr-status/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2022 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# syntax=docker/dockerfile:1.4

# Build from publicly reachable source by default, but allow people to re-build images on
# top of their own trusted images.
Expand All @@ -18,7 +19,7 @@ COPY tkr/go.mod tkr/go.mod
COPY tkr/go.sum tkr/go.sum

WORKDIR /workspace/tkr
RUN go mod download
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod go mod download
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer

Expand All @@ -28,7 +29,7 @@ COPY tkr/ ./
# Build
ARG LD_FLAGS
ENV LD_FLAGS="$LD_FLAGS "'-extldflags "-static"'
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager controller/tkr-status/main.go
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager controller/tkr-status/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
6 changes: 4 additions & 2 deletions tkr/webhook/cluster/tkr-resolver/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Copyright 2022 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# Enable Buildkit
# syntax=docker/dockerfile:1.4

# Build from publicly reachable source by default, but allow people to re-build images on
# top of their own trusted images.
Expand All @@ -18,7 +20,7 @@ COPY tkr/go.mod tkr/go.mod
COPY tkr/go.sum tkr/go.sum

WORKDIR /workspace/tkr
RUN go mod download
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod go mod download
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer

Expand All @@ -28,7 +30,7 @@ COPY tkr/ ./
# Build
ARG LD_FLAGS
ENV LD_FLAGS="$LD_FLAGS "'-extldflags "-static"'
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager webhook/cluster/tkr-resolver/main.go
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager webhook/cluster/tkr-resolver/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
7 changes: 5 additions & 2 deletions tkr/webhook/tkr-conversion/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Copyright 2022 VMware, Inc. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# syntax=docker/dockerfile:1.4

# Copyright 2021 VMware, Inc. All Rights Reserved.

# Build from publicly reachable source by default, but allow people to re-build images on
# top of their own trusted images.
Expand All @@ -18,7 +21,7 @@ COPY tkr/go.mod tkr/go.mod
COPY tkr/go.sum tkr/go.sum

WORKDIR /workspace/tkr
RUN go mod download
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod go mod download
# cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer

Expand All @@ -28,7 +31,7 @@ COPY tkr/ ./
# Build
ARG LD_FLAGS
ENV LD_FLAGS="$LD_FLAGS "'-extldflags "-static"'
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager webhook/tkr-conversion/main.go
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.local/share/golang --mount=type=cache,target=/go/pkg/mod CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -ldflags "$LD_FLAGS" -o manager webhook/tkr-conversion/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down

0 comments on commit 1a06603

Please # to comment.