Skip to content

Commit

Permalink
Provide Dapper free build alternative (#254)
Browse files Browse the repository at this point in the history
* Bump dapper version

Signed-off-by: Derek Nola <[email protected]>

* Consolidate validation

Signed-off-by: Derek Nola <[email protected]>

* Support for regular multistage docker

Signed-off-by: Derek Nola <[email protected]>

* Dirty determination outside docker

Signed-off-by: Derek Nola <[email protected]>

* Optimize caching for code change and tagging

Signed-off-by: Derek Nola <[email protected]>

* Nit: codespell

Signed-off-by: Derek Nola <[email protected]>

* Arm build fix

Signed-off-by: Derek Nola <[email protected]>

* Typo

Signed-off-by: Derek Nola <[email protected]>

---------

Signed-off-by: Derek Nola <[email protected]>
  • Loading branch information
dereknola authored Dec 20, 2023
1 parent 9ce97dc commit f7ae7ce
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 39 deletions.
18 changes: 9 additions & 9 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ platform:

steps:
- name: build
image: rancher/dapper:v0.5.0
image: rancher/dapper:v0.6.0
commands:
- dapper ci
environment:
Expand Down Expand Up @@ -68,7 +68,7 @@ steps:
- refs/tags/*

- name: test-image
image: rancher/dapper:v0.5.0
image: rancher/dapper:v0.6.0
commands:
- docker build -t kine:test-${DRONE_COMMIT} -f Dockerfile.test .
environment:
Expand All @@ -81,7 +81,7 @@ steps:
- pull_request

- name: test-sqlite
image: rancher/dapper:v0.5.0
image: rancher/dapper:v0.6.0
depends_on:
- build
- test-image
Expand All @@ -98,7 +98,7 @@ steps:
- pull_request

- name: test-mysql
image: rancher/dapper:v0.5.0
image: rancher/dapper:v0.6.0
depends_on:
- build
- test-image
Expand All @@ -115,7 +115,7 @@ steps:
- pull_request

- name: test-postgres
image: rancher/dapper:v0.5.0
image: rancher/dapper:v0.6.0
depends_on:
- build
- test-image
Expand All @@ -132,7 +132,7 @@ steps:
- pull_request

- name: test-cockroachdb
image: rancher/dapper:v0.5.0
image: rancher/dapper:v0.6.0
depends_on:
- build
- test-image
Expand All @@ -149,7 +149,7 @@ steps:
- pull_request

- name: test-nats
image: rancher/dapper:v0.5.0
image: rancher/dapper:v0.6.0
depends_on:
- build
- test-image
Expand Down Expand Up @@ -189,7 +189,7 @@ platform:

steps:
- name: build
image: rancher/dapper:v0.5.0
image: rancher/dapper:v0.6.0
commands:
- dapper ci
environment:
Expand Down Expand Up @@ -263,7 +263,7 @@ platform:

steps:
- name: build
image: rancher/dapper:v0.5.0
image: rancher/dapper:v0.5.0 # Fixed to v0.5.0 as newer versions of dapper don't work on arm
commands:
- dapper ci
environment:
Expand Down
50 changes: 50 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

FROM golang:1.21-alpine3.18 AS infra
ARG ARCH=amd64

RUN apk -U add bash coreutils git gcc musl-dev docker-cli vim less file curl wget ca-certificates
# go imports version gopls/v0.14.1
# https://github.com/golang/tools/releases/latest
RUN go install golang.org/x/tools/cmd/goimports@e985f842fa05caad2f3486f0711512aedffbcda8
RUN rm -rf /go/src /go/pkg
RUN if [ "${ARCH}" == "amd64" ]; then \
curl -sL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s; \
fi

ENV SRC_DIR=/go/src/github.com/k3s-io/kine
WORKDIR ${SRC_DIR}/

# Validate needs everything in the project, so we separate it out for better caching
FROM infra as validate
ARG SKIP_VALIDATE
ENV SKIP_VALIDATE=${SKIP_VALIDATE}
COPY . .
RUN --mount=type=cache,id=gomod,target=/go/pkg/mod \
--mount=type=cache,id=gobuild,target=/root/.cache/go-build \
--mount=type=cache,id=lint,target=/root/.cache/golangci-lint \
./scripts/validate

FROM infra AS build
ARG CROSS
ARG TAG
ARG DRONE_TAG
ARG ARCH=amd64
ENV ARCH=${ARCH}

COPY ./scripts/build ./scripts/version ./scripts/
COPY ./go.mod ./go.sum ./main.go ./
COPY ./pkg ./pkg
COPY ./.git ./.git
COPY ./.golangci.json ./.golangci.json

RUN --mount=type=cache,id=gomod,target=/go/pkg/mod \
--mount=type=cache,id=gobuild,target=/root/.cache/go-build \
./scripts/build

COPY ./scripts/package ./scripts/entry ./scripts/
COPY ./package ./package
CMD ./scripts/entry package

FROM scratch as binary
ENV SRC_DIR=/go/src/github.com/k3s-io/kine
COPY --from=build ${SRC_DIR}/bin /bin
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,25 @@ $(TARGETS): .dapper
./.dapper $@

.DEFAULT_GOAL := ci

ARCH ?= amd64
REPO ?= rancher
DEFAULT_BUILD_ARGS=--build-arg="REPO=$(REPO)" --build-arg="TAG=$(TAG)" --build-arg="ARCH=$(ARCH)" --build-arg="DIRTY=$(DIRTY)"
DIRTY := $(shell git status --porcelain --untracked-files=no)
ifneq ($(DIRTY),)
DIRTY="-dirty"
endif

.PHONY: no-dapper
no-dapper:
DOCKER_BUILDKIT=1 docker build \
$(DEFAULT_BUILD_ARGS) --build-arg="SKIP_VALIDATE=$(SKIP_VALIDATE)" \
--target=validate -f Dockerfile .
DOCKER_BUILDKIT=1 docker build \
$(DEFAULT_BUILD_ARGS) --build-arg="DRONE_TAG=$(DRONE_TAG)" --build-arg="CROSS=$(CROSS)" \
-f Dockerfile --target=build -t kine-build .
DOCKER_BUILDKIT=1 docker build \
$(DEFAULT_BUILD_ARGS) --build-arg="DRONE_TAG=$(DRONE_TAG)" --build-arg="CROSS=$(CROSS)" \
-f Dockerfile --target=binary --output=. .
DOCKER_BUILDKIT=1 docker run -v /var/run/docker.sock:/var/run/docker.sock -v ./dist:/go/src/github.com/k3s-io/kine/dist \
-e DAPPER_UID=1000 -e DAPPER_GID=1000 -e IMAGE_NAME -e DRONE_TAG -e DIRTY=$(DIRTY) kine-build
2 changes: 1 addition & 1 deletion pkg/drivers/generic/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (d *Generic) Insert(ctx context.Context, key string, create, delete bool, c
}

// Drivers without LastInsertID support may conflict on the serial id key when inserting rows,
// as the ID is reserved at the begining of the implicit transaction, but does not become
// as the ID is reserved at the beginning of the implicit transaction, but does not become
// visible until the transaction completes, at which point we may have already created a gap fill record.
// Retry the insert if the driver indicates a retriable insert error, to avoid presenting a spurious
// duplicate key error to the client.
Expand Down
2 changes: 1 addition & 1 deletion pkg/drivers/nats/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func parseConnection(dsn string, tlsInfo tls.Config) (*Config, error) {

// command line options provided to kine will override the file
// https://github.com/nats-io/jsm.go/blob/v0.0.29/natscontext/context.go#L257
// allows for user, creds, nke, token, certifcate, ca, inboxprefix from the context.json
// allows for user, creds, nke, token, certificate, ca, inboxprefix from the context.json
natsClientOpts, err := natsContext.NATSOptions(config.clientOptions...)
if err != nil {
return nil, err
Expand Down
6 changes: 1 addition & 5 deletions scripts/ci
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ set -e

cd $(dirname $0)

if [ -z "$SKIP_VALIDATE" ]; then
./validate
./validate-ci
fi

./validate
./build
./package
25 changes: 19 additions & 6 deletions scripts/validate
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
#!/bin/bash
set -e

if [ -n "$SKIP_VALIDATE" ]; then
echo Skipping validation
exit 0
fi

cd $(dirname $0)/..

echo Running validation

PACKAGES="$(go list ./...)"

if ! command -v golangci-lint; then
echo Skipping validation: no golangci-lint available
exit
if command -v golangci-lint; then
echo Running: golangci-lint
golangci-lint run
fi

echo Running: golangci-lint
golangci-lint run

echo Running: go fmt
test -z "$(go fmt ${PACKAGES} | tee /dev/stderr)"

go generate

source ./scripts/version

if [ -n "$DIRTY" ]; then
echo Git is dirty
git status
git diff
exit 1
fi
15 changes: 0 additions & 15 deletions scripts/validate-ci

This file was deleted.

3 changes: 2 additions & 1 deletion scripts/version
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash

if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
# Without Dapper, DIRTY is determined in the Makefile and passed in as a build arg/env var
if [ -z "$DIRTY" ] && [ -n "$(git status --porcelain --untracked-files=no)" ]; then
DIRTY="-dirty"
fi

Expand Down
2 changes: 1 addition & 1 deletion updatecli/updatecli.d/updatecli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# To test this:
# have "UPDATECLI_GITHUB_ACTOR" env set to your github username
# have "UPDATECLI_GITHUB_TOKEN" env set to your github token
# have the latest verison of updatecli installed
# have the latest version of updatecli installed
# 'updatecli diff -v updatecli/values.yaml -c updatecli/updatecli.d/updatecli.yml'
# In the future, more useful files should be added to this directory.
---
Expand Down

0 comments on commit f7ae7ce

Please sign in to comment.