# Build the manager binary
FROM golang:1.16 as builder

ARG GO_LDFLAGS="-X github.com/tidbcloud/infra-api/pkg/version.Version=Unknown"

WORKDIR /workspace

# we does not have the permission to download private go module in the container
#
# 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
# COPY go.mod go.mod
# COPY go.sum go.sum
# COPY api/go.mod api/go.mod
# COPY api/go.sum api/go.sum
# RUN go mod download

# Copy the go source
COPY . .

# grab manifests need to deplop network/cluster
RUN ./hack/grab-manifest.sh ./grab-manifests
RUN cp -r ./manifests/* ./grab-manifests/manifests/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -mod=vendor -a -ldflags "${GO_LDFLAGS}" -o manager cmd/infra-provider/main.go

FROM gcr.io/pingcap-public/pingcap/alpine-glibc:alpine-3.14.3
WORKDIR /
COPY --from=builder /workspace/manager .

# Copy manifests to the default path used by infra-provider
COPY --from=builder /workspace/grab-manifests/manifests /manifests
COPY --from=builder /workspace/grab-manifests/charts /charts
COPY --from=builder /workspace/grab-manifests/bin /bin

ENTRYPOINT ["/manager"]
