diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 00000000..22cd2c8b --- /dev/null +++ b/.drone.yml @@ -0,0 +1,221 @@ +--- +kind: pipeline +name: amd64 + +platform: + os: linux + arch: amd64 + +steps: +- name: build + image: rancher/dapper:v0.4.1 + commands: + - dapper ci + volumes: + - name: docker + path: /var/run/docker.sock + +- name: github_binary_release + image: plugins/github-release + settings: + api_key: + from_secret: github_token + prerelease: true + checksum: + - sha256 + checksum_file: CHECKSUMsum-amd64.txt + checksum_flatten: true + files: + - "dist/artifacts/*" + when: + instance: + - drone-publish.rancher.io + ref: + - refs/head/master + - refs/tags/* + event: + - tag + +- name: docker-publish + image: plugins/docker + settings: + dockerfile: package/Dockerfile + password: + from_secret: docker_password + repo: "galal-hussein/k3k" + tag: "${DRONE_TAG}-amd64" + username: + from_secret: docker_username + when: + instance: + - drone-publish.rancher.io + ref: + - refs/head/master + - refs/tags/* + event: + - tag + +volumes: +- name: docker + host: + path: /var/run/docker.sock + +--- +kind: pipeline +name: arm64 + +platform: + os: linux + arch: arm64 + +steps: +- name: build + image: rancher/dapper:v0.4.1 + commands: + - dapper ci + volumes: + - name: docker + path: /var/run/docker.sock + +- name: github_binary_release + image: plugins/github-release + settings: + api_key: + from_secret: github_token + prerelease: true + checksum: + - sha256 + checksum_file: CHECKSUMsum-arm64.txt + checksum_flatten: true + files: + - "dist/artifacts/*" + when: + instance: + - drone-publish.rancher.io + ref: + - refs/head/master + - refs/tags/* + event: + - tag + +- name: docker-publish + image: plugins/docker + settings: + dockerfile: package/Dockerfile + password: + from_secret: docker_password + repo: "galal-hussein/k3k" + tag: "${DRONE_TAG}-arm64" + username: + from_secret: docker_username + when: + instance: + - drone-publish.rancher.io + ref: + - refs/head/master + - refs/tags/* + event: + - tag + +volumes: +- name: docker + host: + path: /var/run/docker.sock + +--- +kind: pipeline +name: arm + +platform: + os: linux + arch: arm + +steps: +- name: build + image: rancher/dapper:v0.4.1 + commands: + - dapper ci + volumes: + - name: docker + path: /var/run/docker.sock + +- name: github_binary_release + image: plugins/github-release + settings: + api_key: + from_secret: github_token + prerelease: true + checksum: + - sha256 + checksum_file: CHECKSUMsum-arm.txt + checksum_flatten: true + files: + - "dist/artifacts/*" + when: + instance: + - drone-publish.rancher.io + ref: + - refs/head/master + - refs/tags/* + event: + - tag + +- name: docker-publish + image: plugins/docker + settings: + dockerfile: package/Dockerfile + password: + from_secret: docker_password + repo: "galal-hussein/k3k" + tag: "${DRONE_TAG}-arm" + username: + from_secret: docker_username + when: + instance: + - drone-publish.rancher.io + ref: + - refs/head/master + - refs/tags/* + event: + - tag + +volumes: +- name: docker + host: + path: /var/run/docker.sock + +--- +kind: pipeline +name: manifest + +platform: + os: linux + arch: amd64 + +steps: +- name: manifest + image: plugins/manifest:1.0.2 + settings: + username: + from_secret: docker_username + password: + from_secret: docker_password + platforms: + - linux/amd64 + - linux/arm64 + - linux/arm + target: "galal-hussein/k3k:${DRONE_TAG}" + template: "galal-hussein/k3k:${DRONE_TAG}-ARCH" + when: + instance: + - drone-publish.rancher.io + ref: + - refs/head/master + - refs/tags/* + event: + - tag + +depends_on: +- amd64 +- arm64 +- arm diff --git a/.gitignore b/.gitignore index e94d5ac3..04ac6364 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,8 @@ +/.dapper +/.cache +/bin +/dist +*.swp +.idea k3k -kubeconfig.yaml + diff --git a/Dockerfile.dapper b/Dockerfile.dapper new file mode 100644 index 00000000..d11e49c1 --- /dev/null +++ b/Dockerfile.dapper @@ -0,0 +1,21 @@ +ARG GOLANG=golang:1.19.5-alpine3.17 +FROM ${GOLANG} + +ARG DAPPER_HOST_ARCH +ENV ARCH $DAPPER_HOST_ARCH + +RUN apk -U add bash git gcc musl-dev docker vim less file curl wget ca-certificates +RUN if [ "${ARCH}" == "amd64" ]; then \ + curl -sL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.15.0; \ + fi + +ENV GO111MODULE on +ENV DAPPER_ENV REPO TAG DRONE_TAG CROSS +ENV DAPPER_SOURCE /go/src/github.com/galal-hussein/k3k/ +ENV DAPPER_OUTPUT ./bin ./dist +ENV DAPPER_DOCKER_SOCKET true +ENV HOME ${DAPPER_SOURCE} +WORKDIR ${DAPPER_SOURCE} + +ENTRYPOINT ["./scripts/entry"] +CMD ["ci"] diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..5d7b47ef --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +TARGETS := $(shell ls scripts) + +.dapper: + @echo Downloading dapper + @curl -sL https://releases.rancher.com/dapper/latest/dapper-$$(uname -s)-$$(uname -m) > .dapper.tmp + @@chmod +x .dapper.tmp + @./.dapper.tmp -v + @mv .dapper.tmp .dapper + +$(TARGETS): .dapper + ./.dapper $@ + +.DEFAULT_GOAL := default + +.PHONY: $(TARGETS) diff --git a/package/Dockerfile b/package/Dockerfile new file mode 100644 index 00000000..aec50209 --- /dev/null +++ b/package/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +COPY bin/k3k /usr/bin/ +CMD ["k3k"] diff --git a/scripts/boilerplate.go.txt b/scripts/boilerplate.go.txt new file mode 100755 index 00000000..115ad205 --- /dev/null +++ b/scripts/boilerplate.go.txt @@ -0,0 +1,16 @@ +/* +Copyright YEAR Rancher Labs, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + diff --git a/scripts/build b/scripts/build new file mode 100755 index 00000000..1dca29ee --- /dev/null +++ b/scripts/build @@ -0,0 +1,18 @@ +#!/bin/bash +set -e + +source $(dirname $0)/version + +cd $(dirname $0)/.. + +mkdir -p bin +if [ "$(uname)" = "Linux" ]; then + OTHER_LINKFLAGS="-extldflags -static -s" +fi +LINKFLAGS="-X github.com/rancher/k3k.Version=$VERSION" +LINKFLAGS="-X github.com/rancher/k3k.GitCommit=$COMMIT $LINKFLAGS" +CGO_ENABLED=0 go build -ldflags "$LINKFLAGS $OTHER_LINKFLAGS" -o bin/k3k +if [ "$CROSS" = "true" ] && [ "$ARCH" = "amd64" ]; then + GOOS=darwin go build -ldflags "$LINKFLAGS" -o bin/k3k-darwin + GOOS=windows go build -ldflags "$LINKFLAGS" -o bin/k3k-windows +fi diff --git a/scripts/ci b/scripts/ci new file mode 100755 index 00000000..52334105 --- /dev/null +++ b/scripts/ci @@ -0,0 +1,10 @@ +#!/bin/bash +set -e + +cd $(dirname $0) + +./build +./test +./validate +./validate-ci +./package diff --git a/scripts/default b/scripts/default new file mode 100755 index 00000000..af2ad2db --- /dev/null +++ b/scripts/default @@ -0,0 +1,8 @@ +#!/bin/bash +set -e + +cd $(dirname $0) + +./build +./test +./package diff --git a/scripts/entry b/scripts/entry new file mode 100755 index 00000000..78fb5679 --- /dev/null +++ b/scripts/entry @@ -0,0 +1,11 @@ +#!/bin/bash +set -e + +mkdir -p bin dist +if [ -e ./scripts/$1 ]; then + ./scripts/"$@" +else + exec "$@" +fi + +chown -R $DAPPER_UID:$DAPPER_GID . diff --git a/scripts/package b/scripts/package new file mode 100755 index 00000000..a4dcd8cb --- /dev/null +++ b/scripts/package @@ -0,0 +1,18 @@ +#!/bin/bash +set -e + +source $(dirname $0)/version + +cd $(dirname $0)/.. + +mkdir -p dist/artifacts +cp bin/k3k dist/artifacts/k3k${SUFFIX} + +IMAGE=${REPO}/k3k:${TAG} +DOCKERFILE=package/Dockerfile +if [ -e ${DOCKERFILE}.${ARCH} ]; then + DOCKERFILE=${DOCKERFILE}.${ARCH} +fi + +docker build -f ${DOCKERFILE} -t ${IMAGE} . +echo Built ${IMAGE} diff --git a/scripts/release b/scripts/release new file mode 100755 index 00000000..7af0df35 --- /dev/null +++ b/scripts/release @@ -0,0 +1,3 @@ +#!/bin/bash + +exec $(dirname $0)/ci diff --git a/scripts/test b/scripts/test new file mode 100755 index 00000000..08662432 --- /dev/null +++ b/scripts/test @@ -0,0 +1,7 @@ +#!/bin/bash +set -e + +cd $(dirname $0)/.. + +echo Running tests +go test -cover -tags=test ./... diff --git a/scripts/validate b/scripts/validate new file mode 100755 index 00000000..7f98128b --- /dev/null +++ b/scripts/validate @@ -0,0 +1,21 @@ +#!/bin/bash +set -e + +cd $(dirname $0)/.. + +echo Running validation + +PACKAGES="$(go list ./...)" + +if ! command -v golangci-lint; then + echo Skipping validation: no golangci-lint available + exit +fi + +echo Running validation + +echo Running: golangci-lint +golangci-lint run + +echo Running: go fmt +test -z "$(go fmt ${PACKAGES} | tee /dev/stderr)" diff --git a/scripts/validate-ci b/scripts/validate-ci new file mode 100755 index 00000000..e7981cf8 --- /dev/null +++ b/scripts/validate-ci @@ -0,0 +1,15 @@ +#!/bin/bash +set -e + +cd $(dirname $0)/.. + +go generate + +source ./scripts/version + +if [ -n "$DIRTY" ]; then + echo Git is dirty + git status + git diff + exit 1 +fi diff --git a/scripts/version b/scripts/version new file mode 100755 index 00000000..19877dd0 --- /dev/null +++ b/scripts/version @@ -0,0 +1,27 @@ +#!/bin/bash + +if [ -n "$(git status --porcelain --untracked-files=no)" ]; then + DIRTY="-dirty" +fi + +COMMIT=$(git rev-parse --short HEAD) +GIT_TAG=${DRONE_TAG:-$(git tag -l --contains HEAD | head -n 1)} + +if [[ -z "$DIRTY" && -n "$GIT_TAG" ]]; then + VERSION=$GIT_TAG +else + VERSION="${COMMIT}${DIRTY}" +fi + +if [ -z "$ARCH" ]; then + ARCH=$(go env GOHOSTARCH) +fi + +SUFFIX="-${ARCH}" + +TAG=${TAG:-${VERSION}${SUFFIX}} +REPO=${REPO:-galal-hussein} + +if echo $TAG | grep -q dirty; then + TAG=dev +fi