File k3s.spec of Package k3s

#
# spec file for package k3s
#
# Copyright (c) 2022 SUSE LLC
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An "Open Source License" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.

# Please submit bugfixes or comments via https://bugs.opensuse.org/
#

%global _buildshell /bin/bash

# To workaround https://github.com/rancher/k3s/issues/231, build kubectl
%define build_kubectl 1

# baseversion - version of kubernetes for this package
%define baseversion 1.30

%define runc_version %(grep version: %{_sourcedir}/runc.obsinfo|cut -d ' ' -f2)
%define containerd_version %(grep version: %{_sourcedir}/containerd.obsinfo|cut -d ' ' -f2)
%define cniplugins_version %(grep version: %{_sourcedir}/cniplugins.obsinfo|cut -d ' ' -f2)
%define flannelplugin_version %(grep version: %{_sourcedir}/flannelplugin.obsinfo|cut -d ' ' -f2)

Name:           k3s
Version:        1.28.11+k3s2
Release:        0
Summary:        A container orchestration system based on a reduced Kubernetes feature set
License:        Apache-2.0
Group:          System/Management
URL:            https://k3s.io
Source0:        %{name}-%{version}.tar.gz
Source1:        vendor.tar.gz
Source2:        k3s-server.service.in
Source3:        k3s-agent.service.in
Source4:        server.conf
Source5:        agent.conf
Source6:	k3s.obsinfo

Source100:	runc-%{runc_version}.tar.gz
Source101:	vendor-runc.tar.gz
Source102:	runc.obsinfo

Source110:	containerd-%{containerd_version}.tar.gz
Source111:	vendor-containerd.tar.gz
Source112:	containerd.obsinfo

Source120:	cniplugins-%{cniplugins_version}.tar.gz
Source121:	vendor-cniplugins.tar.gz
Source122:	cniplugins.obsinfo

Source130:	flannelplugin-%{flannelplugin_version}.tar.gz
Source132:	flannelplugin.obsinfo

Source200: 	https://k3s.io/k3s-charts/assets/traefik-crd/traefik-crd-25.0.3+up25.0.0.tgz
Source201: 	https://k3s.io/k3s-charts/assets/traefik/traefik-25.0.3+up25.0.0.tgz
Patch:          s390x.patch
Patch1:		k3s-hardcode-go-version.diff
Patch2:		k3s-stable-timestamp.diff
Patch3:		local-only-build.diff
BuildRequires:  c_compiler
BuildRequires:  bash
BuildRequires:  golang-packaging
BuildRequires:  pkgconfig
BuildRequires:  systemd-rpm-macros
BuildRequires:  go1.22
BuildRequires:  pkgconfig(sqlite3)
BuildRequires:  yq
BuildRequires:  pkgconfig(libseccomp)

Requires:       iptables
Requires:       conntrack-tools
Requires:       iptables
Conflicts:      cri-tools
Conflicts:      kubectl
Conflicts:      kubernetes-client
Conflicts:      kubernetes-client-provider
# /var/lib/kubelet is also packaged in kubernetes1.XX-kubelet-common
# this in turn Requires kubernetes-kubelet-common
# by conflicting with kubernetes-kubelet-common we conflict with all
# kubernetes-version packages
Conflicts:      kubernetes-kubelet-common
Conflicts:      rke2
Requires(post): update-alternatives
Requires(postun):update-alternatives
%{?systemd_requires}

%description
k3s is a container orchestration system for automating application
deployment, scaling, and management. It is a Kubernetes-compliant
distribution that differs from the original Kubernetes (colloquially
"k8s") in that:

  * Legacy, alpha, or non-default features are removed.
  * Most in-tree plugins (cloud providers and storage plugins) were
    removed, since they can be replaced with out-of-tree addons.
  * sqlite3 is the default storage mechanism.
    etcd3 is still available, but not the default.
  * There is a new launcher that handles a lot of the complexity of
    TLS and options.

%package cni
Summary: cni built for k3s

%description cni

This is the container network infrastructure built for k3s.

%prep
%autosetup -n %{name}-%{version} -a 1 -p1

rm -rf build/static/charts
mkdir -p build/static/charts
missing=false
for CHART_FILE in $(grep -rlF HelmChart manifests/ | xargs yq eval --no-doc .spec.chart | xargs -n1 basename); do
	if test -f "%{_sourcedir}/$CHART_FILE"; then
		ln -s %{_sourcedir}/$CHART_FILE build/static/charts/
	else
		echo "Missing chart file: $CHART_FILE" >&2
		missing=true
	fi
done
if $missing; then
	exit 1
fi


mkdir deps
pushd deps
DEPS=$PWD
%{__tar} zxf %{SOURCE100}
%{__tar} zxf %{SOURCE110}
%{__tar} zxf %{SOURCE120}
%{__tar} zxf %{SOURCE130}
popd

mkdir -p build/src/github.com/opencontainers
pushd build/src/github.com/opencontainers
ln -s ${DEPS}/runc-%{runc_version} runc
cd runc
%{__tar} xzf %{SOURCE101}
popd

mkdir -p build/src/github.com/containerd
pushd build/src/github.com/containerd
ln -s ${DEPS}/containerd-%{containerd_version} containerd
cd containerd
%{__tar} xzf %{SOURCE111}
popd

mkdir -p build/src/github.com/containernetworking
pushd  build/src/github.com/containernetworking
ln -s ${DEPS}/cniplugins-%{cniplugins_version} plugins
cd plugins
%{__tar} xzf %{SOURCE121}

rm -rf plugins/meta/flannel
cd plugins/meta
ln -s ${DEPS}/flannelplugin-%{flannelplugin_version} flannel
popd

#sed -i 's#exec.LookPath("host-local")#exec.LookPath("%{_libexecdir}/cni/host-local")#' \
#       pkg/agent/config/config.go


. scripts/version.sh

check_dependency() {
    local name=$1
    local expected=$2
    local actual=$3
    expected=${expected/-/}
    actual=${actual/-/}
    if test "$expected" != "$actual"; then
	echo "Packaging error: Incorrect $name version.  Expected ${expected}; got ${actual}." >&2
	exit 1
    fi
}

check_dependency runc $VERSION_RUNC %{runc_version}
check_dependency containerd $VERSION_CONTAINERD %{containerd_version}
check_dependency cniplugins $VERSION_CNIPLUGINS %{cniplugins_version}
check_dependency flannelplugin $VERSION_FLANNEL_PLUGIN %{flannelplugin_version}

%build

export K3S_BUILD_TIMESTAMP="-d @$(grep mtime: %{SOURCE6}|cut -d ' ' -f 2)"
export DRONE_COMMIT=$(grep commit: %{SOURCE6}|cut -d ' ' -f 2)
export DRONE_TAG=v%{version}
### Versions are set in scripts/version.sh, k3s will not start without VERSION_GOLANG
export VERSION_GOLANG="$(go version | cut -d" " -f3)"
scripts/build

%install
export K3S_BUILD_TIMESTAMP="-d @$(grep mtime: %{SOURCE6}|cut -d ' ' -f 2)"
export DRONE_COMMIT=$(grep commit: %{SOURCE6}|cut -d ' ' -f 2)
export DRONE_TAG=v%{version}
export VERSION_GOLANG="$(go version | cut -d" " -f3)"
scripts/package-cli

:> files.links
# Install symlinks
%{__install} -D bin/k3s %{buildroot}%{_bindir}/k3s
%{__install} -D bin/runc %{buildroot}%{_libexecdir}/k3s/runc
%{__install} -D bin/cni %{buildroot}%{_libexecdir}/k3s/cni
%{__install} -D bin/containerd-shim-runc-v2 %{buildroot}%{_libexecdir}/k3s/containerd-shim-runc-v2
for f in bin/*; do
	name="$(basename "$f")"
	if test -L "$f"; then
		link="$(readlink "$f")"
		if test "$link" = "k3s"; then
			ln -s k3s %{buildroot}%{_bindir}/$name
			echo "%%{_bindir}/$name" >> files.links
			case "$name" in
			kubectl|crictl|ctr)
				ln -s k3s %{buildroot}%{_bindir}/${name}%{baseversion}
				echo "%%{_bindir}/${name}%{baseversion}" >> files.links
				echo "%ghost %_sysconfdir/alternatives/${name}" >> files.links ;;
			esac
		elif test "$link" = "cni"; then
			ln -s cni %{buildroot}%{_libexecdir}/k3s/$name
			echo "%%{_libexecdir}/k3s/$name" >> files.links

		fi
	fi
done
mkdir -p %{buildroot}%{_localstatedir}/lib/rancher/k3s
mkdir -p %{buildroot}%{_localstatedir}/lib/rancher/k3s/server/manifests

mkdir -p %{buildroot}%{_sysconfdir}/rancher/k3s
install -D -m 644 %{SOURCE4} %{buildroot}%{_sysconfdir}/rancher/k3s/server.conf
install -D -m 644 %{SOURCE5} %{buildroot}%{_sysconfdir}/rancher/k3s/agent.conf

mkdir -p %{buildroot}%{_unitdir}
sed -e 's;@LIBEXEC@;%{_libexecdir};' %{SOURCE2} > %{buildroot}%{_unitdir}/k3s-server.service
sed -e 's;@LIBEXEC@;%{_libexecdir};' %{SOURCE3} > %{buildroot}%{_unitdir}/k3s-agent.service

mkdir -p %{buildroot}%{_sbindir}
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rck3s-server
ln -s %{_sbindir}/service %{buildroot}%{_sbindir}/rck3s-agent

# alternatives
ln -s -f %{_sysconfdir}/alternatives/kubectl %{buildroot}%{_bindir}/kubectl

%pre
%service_add_pre k3s-server.service k3s-agent.service

%post
export baseversion="%{baseversion}"
%{_sbindir}/update-alternatives \
  --install %{_bindir}/kubectl kubectl %{_bindir}/kubectl%{baseversion} ${baseversion/./} \
  --slave %{_bindir}/crictl crictl %{_bindir}/crictl%{baseversion} \
  --slave %{_bindir}/ctr ctr %{_bindir}/ctr%{baseversion}

%service_add_post k3s-server.service k3s-agent.service

%preun
%service_del_preun k3s-server.service k3s-agent.service

%postun
if [ ! -f %{_bindir}/kubectl%{baseversion} ] ; then
  update-alternatives --remove kubectl %{_bindir}/kubectl%{baseversion}
fi

%service_del_postun k3s-server.service k3s-agent.service

%files -f files.links
%license LICENSE
%doc README.md
%{_bindir}/k3s
%{_localstatedir}/lib/rancher
%config %{_sysconfdir}/rancher
%config(noreplace) %{_sysconfdir}/rancher/k3s/agent.conf
%config(noreplace) %{_sysconfdir}/rancher/k3s/server.conf
%{_unitdir}/k3s-agent.service
%{_unitdir}/k3s-server.service
%{_sbindir}/rck3s-agent
%{_sbindir}/rck3s-server
%dir %{_libexecdir}/k3s
%{_libexecdir}/k3s/cni
%{_libexecdir}/k3s/runc
%{_libexecdir}/k3s/containerd-shim-runc-v2

%changelog
openSUSE Build Service is sponsored by