# Contributor: Oleg Titov <oleg.titov@gmail.com>
# Maintainer: Oleg Titov <oleg.titov@gmail.com>
pkgname=k3s
_pkgver=1.31.0+k3s1
pkgver=${_pkgver/+k3s/.}
pkgrel=0
pkgdesc="Lightweight Kubernetes. 5 less than k8s"
url="https://k3s.io"
arch="all !riscv64 !loongarch64" # https://github.com/marten-seemann/tcp/pull/1
license="Apache-2.0"
options="!check" # No test suite from upstream
depends="
	cni-plugins
	cni-plugin-flannel
	conntrack-tools
	containerd
	coreutils
	dbus
	findutils
	ipset
	iptables
	"
makedepends="
	bash
	go
	linux-headers
	sqlite-dev
	sqlite-static
	yq
	zlib-dev
	zlib-static
	btrfs-progs-dev
	btrfs-progs-static
	libseccomp-dev
	libseccomp-static
	"
install="$pkgname.post-install $pkgname.pre-deinstall"
subpackages="$pkgname-doc $pkgname-openrc"
source="$pkgname-$pkgver.tar.gz::https://github.com/k3s-io/k3s/archive/v$_pkgver.tar.gz
	k3s.confd
	k3s.initd
	k3s.logrotate
	k3s.modules-load
	"
builddir="$srcdir/src/github.com/k3s-io/$pkgname"

# secfixes:
#   1.29.3.1-r0:
#     - CVE-2023-45142
#     - CVE-2023-48795
#   1.27.5.1-r0:
#     - CVE-2023-32187
#   1.27.3.1-r0:
#     - CVE-2023-2728
#   1.21.3.1-r0:
#     - CVE-2021-32001
#   1.21.1.1-r0:
#     - CVE-2021-30465
#   1.20.6.1-r0:
#     - CVE-2021-25735
#   1.20.5.1-r0:
#     - CVE-2021-21334
#   1.19.4.2-r0:
#     - CVE-2020-15257
#   1.18.6.1-r0:
#     - CVE-2020-8557
#     - CVE-2020-8559
#   1.18.3.1-r0:
#     - CVE-2020-10749
#     - CVE-2020-8555
#   0.8.1-r0:
#     - CVE-2019-9512
#     - CVE-2019-9514
#   0.8.0-r0:
#     - CVE-2019-11247
#     - CVE-2019-11249

export GOBIN="$srcdir/bin"
export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"
export GOFLAGS="$GOFLAGS -modcacherw"

prepare() {
	export GOPATH="$srcdir"

	mkdir -p "$(dirname $builddir)"
	mv "$srcdir"/$pkgname-${_pkgver/+/-} "$builddir"/

	default_prepare
}

build() {
	mkdir -p build/data && ./scripts/download && go generate

	VERSION_GOLANG="$(go version | cut -d" " -f3)"

	TAGS="apparmor seccomp netcgo osusergo providerless static_build libsqlite3"
	STATIC_SQLITE="-extldflags '-static -lm -ldl -lz -lpthread'"
	VERSIONFLAGS="
	-X github.com/k3s-io/k3s/pkg/version.Version=v$_pkgver
	-X github.com/k3s-io/k3s/pkg/version.GitCommit=AlpineLinux
	-X github.com/k3s-io/k3s/pkg/version.UpstreamGolang=$VERSION_GOLANG
	"

	go build -o k3s -tags "$TAGS" -ldflags "$VERSIONFLAGS $STATIC_SQLITE" ./cmd/server/main.go
}

package() {
	install -m755 -D k3s \
		"$pkgdir"/usr/bin/k3s

	install -m644 -D -t "$pkgdir"/usr/share/doc/$pkgname README.md

	install -m755 -D "$srcdir"/k3s.initd "$pkgdir"/etc/init.d/k3s
	install -m644 -D "$srcdir"/k3s.confd "$pkgdir"/etc/conf.d/k3s
	install -m644 -D "$srcdir"/k3s.logrotate "$pkgdir"/etc/logrotate.d/k3s
	install -m644 -D "$srcdir"/k3s.modules-load "$pkgdir"/usr/lib/modules-load.d/k3s.conf
}

sha512sums="
b185069ac7a17d640b342135ed9e5dd3bc1ab6c76884d42c1909151397a1aef5d545b46e97970f8bea7fca8ebb2f1726b9d918339589d453220317f933815328  k3s-1.31.0.1.tar.gz
f03221efceb4ce2305c41c4c9e6d02ee5b799ed0cdfb1fc5018f8696e4d05575ae63b7c87596d765c5aa76c4a3bacf7c205e3eb61465e26886081a5d0da013ea  k3s.confd
1015ee6ce5c69595df3150d7bbdfe528cf20305dac299831faa9cce00a454daf5548e78b1db79dcb8da300edc54553dfda0b95aed5e7bee27c1c726aef640350  k3s.initd
018a5e9b417a937c17f0a4a9e08eed434f06186207626ad038aec22ee667aba4cefa6e9e2a222e2c430d2cbb88c8663648f5bab0e76926a0edd13b8bdfd2673a  k3s.logrotate
85ee1310cb36c85c42b4068a9549a3ef72b856cd61b2c1036c3e871ef43a69ed80b43599ad94ce5b069ddd823e730596bb3d3875d4ba8cd77c4cc1985335ffff  k3s.modules-load
"
