%global _dwz_low_mem_die_limit 0
%global provider github
%global provider_tld  com
%global project kubernetes
%global repo kubernetes
# https://github.com/kubernetes/kubernetes
%global provider_prefix %{provider}.%{provider_tld}/%{project}/%{repo}
%global import_path k8s.io/kubernetes
%global _buildshell  /bin/bash
%global _checkshell  /bin/bash

Name:           kubernetes
Version:        1.18.8
%global commit  9f2892aab98fe339f3bd70e3c470144299398ace
Release:        1%{?dist}
Summary:        Container cluster management
License:        ASL 2.0
URL:            https://%{import_path}
ExclusiveArch:  ppc64le ppc64 x86_64 aarch64 s390x
Source0:        https://%{provider_prefix}/archive/%{commit}/%{repo}-%{shortcommit}.tar.gz
Source1:	kubernetes-env-apiserver
Source2:	kube-proxy.service
Source3:        kubernetes-accounting.conf
Source4:        kubeadm.conf
Source5:	kubelet.service
Source6:	kube-controller-manager.service
Source7:	kube-apiserver.service
Source8:	kubernetes-env-control-manager
Source9:	kubernetes-env-scheduler
Source10:	kubernetes-tmpfiles
Source11:	kube-scheduler.service
Source12:	kubernetes-env-config
Source13:	kubernetes-env-kubelet
Source14:	kubernetes-env-proxy
Source33:       genmanpages.sh
# It obsoletes cadvisor but needs its source code (literally integrated)
Obsoletes:      cadvisor
# kubernetes is decomposed into master and node subpackages
# require both of them for updates
Requires: kubernetes-master = %{version}-%{release}
Requires: kubernetes-node = %{version}-%{release}
%global shortcommit %(c=%{commit}; echo ${c:0:7})


%description
%{summary}


%package master
Summary: Kubernetes services for master host
BuildRequires: golang >= 1.2-7
BuildRequires: systemd
BuildRequires: rsync
BuildRequires: go-md2man
BuildRequires: go-bindata
Requires(pre): shadow-utils
Requires: kubernetes-client = %{version}-%{release}
# if node is installed with node, version and release must be the same
Conflicts: kubernetes-node < %{version}-%{release}
Conflicts: kubernetes-node > %{version}-%{release}
%description master
Kubernetes services for master host


%package node
Summary: Kubernetes services for node host
Requires: conntrack-tools
BuildRequires: golang >= 1.2-7
BuildRequires: systemd
BuildRequires: rsync
BuildRequires: go-md2man
BuildRequires: go-bindata
Requires(pre): shadow-utils
Requires:      socat
Requires:      kubernetes-client = %{version}-%{release}
# if master is installed with node, version and release must be the same
Conflicts: kubernetes-master < %{version}-%{release}
Conflicts: kubernetes-master > %{version}-%{release}
%description node
Kubernetes services for node host


%package  kubeadm
Summary:  Kubernetes tool for standing up clusters
Requires: kubernetes-node = %{version}-%{release}
Requires: containernetworking-plugins
%description kubeadm
Kubernetes tool for standing up clusters


%package client
Summary: Kubernetes client tools
BuildRequires: golang >= 1.2-7
BuildRequires: go-bindata
%description client
Kubernetes client tools like kubectl


%prep
%setup -q -n %{repo}-%{commit}
mkdir -p src/k8s.io/kubernetes
mv $(ls | grep -v "^src$") src/k8s.io/kubernetes/.
%ifarch ppc64
sed 's/ppc64le/ppc64/' -i src/k8s.io/kubernetes/cluster/clientbin.sh
sed 's/ppc64le/ppc64/' -i src/k8s.io/kubernetes/cluster/gce/upgrade.sh
sed 's/ppc64le/ppc64/' -i src/k8s.io/kubernetes/hack/lib/util.sh
sed 's/ppc64le/ppc64/' -i src/k8s.io/kubernetes/hack/local-up-cluster.sh
sed 's/OUT_DIR ?= _output/OUT_DIR ?= _output\/local\/go/' -i src/k8s.io/kubernetes/Makefile
%endif


%build
pushd src/k8s.io/kubernetes/
export KUBE_GIT_TREE_STATE="clean"
export KUBE_GIT_COMMIT=%{commit}
export KUBE_GIT_VERSION=v%{version}
export KUBE_EXTRA_GOPATH=$(pwd)/Godeps/_workspace
# https://bugzilla.redhat.com/show_bug.cgi?id=1392922#c1
#%$%ifarch ppc64le
#%$export GOLDFLAGS='-linkmode=external'
#%$%endif
# Build each binary separately to generate a unique build-id.
make WHAT="cmd/kube-proxy"
make WHAT="cmd/kube-apiserver"
make WHAT="cmd/kubelet"
make WHAT="cmd/kubeadm"
make WHAT="cmd/kube-scheduler"
make WHAT="cmd/kubectl"
make WHAT="cmd/kubemark"
make WHAT="cmd/kube-controller-manager"
# convert md to man
./hack/generate-docs.sh || true
pushd docs
pushd admin
cp kube-apiserver.md kube-controller-manager.md kube-proxy.md kube-scheduler.md kubelet.md ..
popd
cp %{SOURCE33} genmanpages.sh
bash genmanpages.sh
popd
popd


%install
pushd src/k8s.io/kubernetes/
. hack/lib/init.sh
kube::golang::setup_env
#%ifarch ppc64le
output_path="_output/local/go/bin"
#%else
#$%output_path="${KUBE_OUTPUT_BINPATH}/$(kube::golang::host_platform)"
#%endif
install -m 0755 -d %{buildroot}%{_bindir}
install -p -m 0754 -t %{buildroot}%{_bindir} ${output_path}/kube-proxy
install -p -m 0754 -t %{buildroot}%{_bindir} ${output_path}/kube-apiserver
install -p -m 0754 -t %{buildroot}%{_bindir} ${output_path}/kubelet
install -p -m 0754 -t %{buildroot}%{_bindir} ${output_path}/kubeadm
install -p -m 0754 -t %{buildroot}%{_bindir} ${output_path}/kube-scheduler
install -p -m 0754 -t %{buildroot}%{_bindir} ${output_path}/kubectl
install -p -m 0754 -t %{buildroot}%{_bindir} ${output_path}/kubemark
install -p -m 0754 -t %{buildroot}%{_bindir} ${output_path}/kube-controller-manager
install -d -m 0755 %{buildroot}/%{_sysconfdir}/systemd/system/kubelet.service.d
install -p -m 0644 -t %{buildroot}/%{_sysconfdir}/systemd/system/kubelet.service.d %{SOURCE4}
install -d -m 0755 %{buildroot}%{_datadir}/bash-completion/completions/
%{buildroot}%{_bindir}/kubectl completion bash > %{buildroot}%{_datadir}/bash-completion/completions/kubectl
install -d -m 0755 %{buildroot}%{_sysconfdir}/%{name}
install -p -m 0644 %{SOURCE12} %{buildroot}%{_sysconfdir}/%{name}/config
install -p -m 0644 %{SOURCE1} %{buildroot}%{_sysconfdir}/%{name}/apiserver
install -p -m 0644 %{SOURCE9} %{buildroot}%{_sysconfdir}/%{name}/scheduler
install -p -m 0644 %{SOURCE8} %{buildroot}%{_sysconfdir}/%{name}/controller-manager
install -p -m 0644 %{SOURCE13} %{buildroot}%{_sysconfdir}/%{name}/kubelet
install -p -m 0644 %{SOURCE14} %{buildroot}%{_sysconfdir}/%{name}/proxy
install -d -m 0755 %{buildroot}%{_unitdir}
install -p -m 0644 -t %{buildroot}%{_unitdir} %{SOURCE2}
install -p -m 0644 -t %{buildroot}%{_unitdir} %{SOURCE5}
install -p -m 0644 -t %{buildroot}%{_unitdir} %{SOURCE6}
install -p -m 0644 -t %{buildroot}%{_unitdir} %{SOURCE7}
install -p -m 0644 -t %{buildroot}%{_unitdir} %{SOURCE11}
install -d %{buildroot}%{_mandir}/man1
install -p -m 0644 docs/man/man1/* %{buildroot}%{_mandir}/man1
#rm %{buildroot}%{_mandir}/man1/cloud-controller-manager.*
install -d %{buildroot}%{_sharedstatedir}/kubelet
install -d -m 0755 %{buildroot}%{_tmpfilesdir}
install -p -m 0644 %{SOURCE10} %{buildroot}/%{_tmpfilesdir}/kubernetes.conf
mkdir -p %{buildroot}/run
install -d -m 0755 %{buildroot}/run/%{name}/
install -d -m 0755 %{buildroot}/%{_sysconfdir}/systemd/system.conf.d
install -p -m 0644 -t %{buildroot}/%{_sysconfdir}/systemd/system.conf.d %{SOURCE3}
popd
mv src/k8s.io/kubernetes/*.md .
mv src/k8s.io/kubernetes/LICENSE .


%files
# empty as it depends on master and node


%files master
%license LICENSE
%doc *.md
%{_mandir}/man1/kube-apiserver.1*
%{_mandir}/man1/kube-controller-manager.1*
%{_mandir}/man1/kube-scheduler.1*
%attr(754, -, kube) %caps(cap_net_bind_service=ep) %{_bindir}/kube-apiserver
%{_bindir}/kube-controller-manager
%{_bindir}/kube-scheduler
%{_unitdir}/kube-apiserver.service
%{_unitdir}/kube-controller-manager.service
%{_unitdir}/kube-scheduler.service
%dir %{_sysconfdir}/%{name}
%config(noreplace) %{_sysconfdir}/%{name}/apiserver
%config(noreplace) %{_sysconfdir}/%{name}/scheduler
%config(noreplace) %{_sysconfdir}/%{name}/config
%config(noreplace) %{_sysconfdir}/%{name}/controller-manager
%{_tmpfilesdir}/kubernetes.conf
%verify(not size mtime md5) %attr(755, kube,kube) %dir /run/%{name}


%files node
%license LICENSE
%doc *.md
%{_mandir}/man1/kubelet.1*
%{_mandir}/man1/kube-proxy.1*
%{_bindir}/kubelet
%{_bindir}/kube-proxy
%{_unitdir}/kube-proxy.service
%{_unitdir}/kubelet.service
%dir %{_sharedstatedir}/kubelet
%dir %{_sysconfdir}/%{name}
%config(noreplace) %{_sysconfdir}/%{name}/config
%config(noreplace) %{_sysconfdir}/%{name}/kubelet
%config(noreplace) %{_sysconfdir}/%{name}/proxy
%config(noreplace) %{_sysconfdir}/systemd/system.conf.d/kubernetes-accounting.conf
%{_tmpfilesdir}/kubernetes.conf
%verify(not size mtime md5) %attr(755, kube,kube) %dir /run/%{name}


%files kubeadm
%license LICENSE
%doc *.md
%{_mandir}/man1/kubeadm.1*
%{_mandir}/man1/kubeadm-*
%{_bindir}/kubeadm
%{_bindir}/kubemark
%dir %{_sysconfdir}/systemd/system/kubelet.service.d
%config(noreplace) %{_sysconfdir}/systemd/system/kubelet.service.d/kubeadm.conf


%files client
%license LICENSE
%doc *.md
%{_mandir}/man1/kubectl.1*
%{_mandir}/man1/kubectl-*
%{_bindir}/kubectl
%{_datadir}/bash-completion/completions/kubectl


%pre master
getent group kube >/dev/null || groupadd -r kube
getent passwd kube >/dev/null || useradd -r -g kube -d / -s /sbin/nologin -c "Kubernetes user" kube
%post master
%systemd_post kube-apiserver kube-scheduler kube-controller-manager
%preun master
%systemd_preun kube-apiserver kube-scheduler kube-controller-manager
%postun master
%systemd_postun kube-apiserver kube-scheduler kube-controller-manager


%pre node
getent group kube >/dev/null || groupadd -r kube
getent passwd kube >/dev/null || useradd -r -g kube -d / -s /sbin/nologin -c "Kubernetes user" kube
%post node
%systemd_post kubelet kube-proxy
# If accounting is not currently enabled systemd reexec
if [[ `systemctl show docker kubelet | grep -q -e CPUAccounting=no -e MemoryAccounting=no; echo $?` -eq 0 ]]; then
  systemctl daemon-reexec
fi


%preun node
%systemd_preun kubelet kube-proxy
%postun node
%systemd_postun kubelet kube-proxy


%changelog
