SHELL := /bin/bash
# Build targets
TARGETS ?= darwin/amd64 linux/amd64 linux/386 linux/arm linux/arm64 windows/amd64
TARGET_OBJS ?= darwin-amd64.tar.gz darwin-amd64.tar.gz.sha256 linux-amd64.tar.gz linux-amd64.tar.gz.sha256 linux-386.tar.gz linux-386.tar.gz.sha256 linux-arm.tar.gz linux-arm.tar.gz.sha256 linux-arm64.tar.gz linux-arm64.tar.gz.sha256 windows-amd64.zip windows-amd64.zip.sha256
# get git tag
GIT_TAG := $( shell git describe --tags)
i f e q ( $( GIT_TAG ) , )
GIT_TAG := $( shell git describe --always)
e n d i f
# get latest k3s version: grep the tag and replace + with - (difference between git and dockerhub tags)
K3S_TAG := $( shell curl --silent "https://update.k3s.io/v1-release/channels/stable" | egrep -o '/v[^ ]+"' | sed -E 's/\/|\"//g' | sed -E 's/\+/\-/' )
i f e q ( $( K 3S_TAG ) , )
$(warning K3S_TAG undefined : couldn 't get latest k 3s image tag !)
$(warning Output of curl : $( shell curl --silent "https ://update .k 3s .io /v 1-release /channels /stable ") )
$( error exiting )
e n d i f
# determine if make is being executed from interactive terminal
INTERACTIVE := $( shell [ -t 0 ] && echo 1)
# E2E test settings
E2E_LOG_LEVEL ?= WARN
E2E_SKIP ?=
# Go options
GO ?= go
PKG := $( shell go mod vendor)
TAGS :=
TESTS := .
TESTFLAGS :=
LDFLAGS := -w -s -X github.com/rancher/k3d/version.Version= ${ GIT_TAG } -X github.com/rancher/k3d/version.K3sVersion= ${ K3S_TAG }
GOFLAGS :=
BINDIR := $( CURDIR) /bin
BINARIES := k3d
K3D_IMAGE_TAG := $( GIT_TAG)
# Go Package required
PKG_GOX := github.com/mitchellh/gox@v1.0.1
PKG_GOLANGCI_LINT_VERSION := 1.25.0
PKG_GOLANGCI_LINT_SCRIPT := https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh
PKG_GOLANGCI_LINT := github.com/golangci/golangci-lint/cmd/golangci-lint@v${ PKG_GOLANGCI_LINT_VERSION }
# configuration adjustments for golangci-lint
GOLANGCI_LINT_DISABLED_LINTERS := "" # disabling typecheck, because it currently (06.09.2019) fails with Go 1.13
# Use Go Modules for everything
export GO111MODULE = on
# go source directories.
# DIRS defines a single level directly, we only look at *.go in this directory.
# REC_DIRS defines a source code tree. All go files are analyzed recursively.
DIRS := .
REC_DIRS := cmd
# Rules for finding all go source files using 'DIRS' and 'REC_DIRS'
GO_SRC := $( foreach dir,$( DIRS) ,$( wildcard $( dir) /*.go) )
GO_SRC += $( foreach dir,$( REC_DIRS) ,$( shell find $( dir) -name "*.go" ) )
# Rules for directory list as input for the golangci-lint program
LINT_DIRS := $( DIRS) $( foreach dir,$( REC_DIRS) ,$( dir) /...)
.PHONY : all build build -cross clean fmt check -fmt lint check extra -clean install -tools
all : clean fmt check build
build :
CGO_ENABLED = 0 $( GO) build $( GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o '$(BINDIR)/$(BINARIES)'
build-cross : LDFLAGS += -extldflags "-static "
build-cross :
CGO_ENABLED = 0 gox -parallel= 3 -output= " _dist/ $( BINARIES) -{{.OS}}-{{.Arch}} " -osarch= '$(TARGETS)' $( GOFLAGS) $( if $( TAGS) ,-tags '$(TAGS)' ,) -ldflags '$(LDFLAGS)'
build-docker : Dockerfile
@echo " Building Docker image k3d: $( K3D_IMAGE_TAG) "
docker build -t k3d:$( K3D_IMAGE_TAG) .
clean :
@rm -rf $( BINDIR) _dist/
extra-clean : clean
$( GO) clean -i $( PKG_GOX)
$( GO) clean -i $( PKG_GOLANGCI_LINT)
# fmt will fix the golang source style in place.
fmt :
@gofmt -s -l -w $( GO_SRC)
e2e : build -docker
@echo " Running e2e tests in k3d: $( K3D_IMAGE_TAG) "
LOG_LEVEL = " $( E2E_LOG_LEVEL) " E2E_SKIP = " $( E2E_SKIP) " tests/dind.sh " ${ K3D_IMAGE_TAG } "
# check-fmt returns an error code if any source code contains format error.
check-fmt :
@test -z $( shell gofmt -s -l $( GO_SRC) | tee /dev/stderr) || echo "[WARN] Fix formatting issues with 'make fmt'"
lint :
@golangci-lint run -D $( GOLANGCI_LINT_DISABLED_LINTERS) $( LINT_DIRS)
check : check -fmt lint
# Check for required executables
HAS_GOX := $( shell command -v gox 2> /dev/null)
HAS_GOLANGCI := $( shell command -v golangci-lint)
HAS_GOLANGCI_VERSION := $( shell golangci-lint --version | grep " version $( PKG_GOLANGCI_LINT_VERSION) " 2>& 1)
install-tools :
i f n d e f H A S _ G O X
( $( GO) get $( PKG_GOX) )
e n d i f
i f n d e f H A S _ G O L A N G C I
( curl -sfL $( PKG_GOLANGCI_LINT_SCRIPT) | sh -s -- -b ${ GOPATH } /bin v${ PKG_GOLANGCI_LINT_VERSION } )
e n d i f
i f d e f H A S _ G O L A N G C I
i f e q ( $( HAS_GOLANGCI_VERSION ) , )
i f d e f I N T E R A C T I V E
@echo " Warning: Your installed version of golangci-lint (interactive: ${ INTERACTIVE } ) differs from what we'd like to use. Switch to v ${ PKG_GOLANGCI_LINT_VERSION } ? [Y/n] "
@read line; if [ $$ line = = "y" ] ; then ( curl -sfL $( PKG_GOLANGCI_LINT_SCRIPT) | sh -s -- -b ${ GOPATH } /bin v${ PKG_GOLANGCI_LINT_VERSION } ) ; fi
e l s e
@echo " Warning: you're not using the same version of golangci-lint as us (v ${ PKG_GOLANGCI_LINT_VERSION } ) "
e n d i f
e n d i f
e n d i f
ci-setup :
@echo "Installing Go tools..."
curl -sfL $( PKG_GOLANGCI_LINT_SCRIPT) | sh -s -- -b ${ GOPATH } /bin v$( PKG_GOLANGCI_LINT_VERSION)
go get $( PKG_GOX)
@echo "Installing kubectl..."
curl -LO https://storage.googleapis.com/kubernetes-release/release/` curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt` /bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
ci-tests : fmt check e 2e