File kcp-cli.spec of Package kcp-cli

#
# spec file for package kcp-cli
#
# Copyright (c) 2025 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/
#


%define server_package_name kcp-server
%define server_executable_name kcp
%define kubectl_kcp_plugin_name kubectl-kcp
%define kubectl_ws_plugin_name kubectl-ws
%define kubectl_create_workspace_plugin_name kubectl-create-workspace
%define kubectl_create_workspace_plugin_executable_name kubectl-create_workspace

Name:           kcp-cli
Version:        0.27.1
Release:        0
Summary:        CLI for the KCP control plane
License:        Apache-2.0
URL:            https://github.com/kcp-dev/kcp
Source:         %{name}-%{version}.tar.gz
Source1:        vendor.tar.gz
Source2:        vendor-cli.tar.gz
BuildRequires:  bash-completion
BuildRequires:  fish
BuildRequires:  go >= 1.23
BuildRequires:  zsh
BuildRequires:  make
BuildRequires:  jq
BuildRequires:  git-core

%description
kcp is a Kubernetes-like control plane focusing on:

- A control plane for many independent, isolated “clusters” known as workspaces
- Enabling API service providers to offer APIs centrally using multi-tenant
  operators
- Easy API consumption for users in their workspaces

kcp can be a building block for SaaS service providers who need a massively
multi-tenant platform to offer services to a large number of fully isolated
tenants using Kubernetes-native APIs. The goal is to be useful to cloud
providers as well as enterprise IT departments offering APIs within their
company.

%package -n %{name}-bash-completion
Summary:        Bash Completion for %{name}
Group:          System/Shells
Requires:       %{name} = %{version}
Requires:       bash-completion
Supplements:    (%{name} and bash-completion)
BuildArch:      noarch

%description -n %{name}-bash-completion
Bash command line completion support for %{name}.

%package -n %{name}-fish-completion
Summary:        Fish Completion for %{name}
Group:          System/Shells
Requires:       %{name} = %{version}
Supplements:    (%{name} and fish)
BuildArch:      noarch

%description -n %{name}-fish-completion
Fish command line completion support for %{name}.

%package -n %{name}-zsh-completion
Summary:        Zsh Completion for %{name}
Group:          System/Shells
Requires:       %{name} = %{version}
Supplements:    (%{name} and zsh)
BuildArch:      noarch

%description -n %{name}-zsh-completion
zsh command line completion support for %{name}.

%package -n %{server_package_name}
Summary:        The kcp server component

%description -n %{server_package_name}
This package contains the server component of kcp, the Kubernetes-like control
planes for form-factors and use-cases beyond Kubernetes and container
workloads.

%package -n %{server_package_name}-bash-completion
Summary:        Bash Completion for %{server_package_name}
Group:          System/Shells
Requires:       %{server_package_name} = %{version}
Requires:       bash-completion
Supplements:    (%{server_package_name} and bash-completion)
BuildArch:      noarch

%description -n %{server_package_name}-bash-completion
Bash command line completion support for %{server_package_name}.

%package -n %{server_package_name}-fish-completion
Summary:        Fish Completion for %{server_package_name}
Group:          System/Shells
Requires:       %{server_package_name} = %{version}
Supplements:    (%{server_package_name} and fish)
BuildArch:      noarch

%description -n %{server_package_name}-fish-completion
Fish command line completion support for %{server_package_name}.

%package -n %{server_package_name}-zsh-completion
Summary:        Zsh Completion for %{server_package_name}
Group:          System/Shells
Requires:       %{server_package_name} = %{version}
Supplements:    (%{server_package_name} and zsh)
BuildArch:      noarch

%description -n %{server_package_name}-zsh-completion
zsh command line completion support for %{server_package_name}.

%prep
%autosetup -p 1 -a 1
tar xvf %{SOURCE2} -C cli/

%build
# hash will be shortened by COMMIT_HASH:0:8 later
COMMIT_HASH="$(sed -n 's/commit: \(.*\)/\1/p' %_sourcedir/%{name}.obsinfo)"

DATE_FMT="+%%Y-%%m-%%dT%%H:%%M:%%SZ"
BUILD_DATE=$(date -u -d "@${SOURCE_DATE_EPOCH}" "${DATE_FMT}" 2>/dev/null || date -u -r "${SOURCE_DATE_EPOCH}" "${DATE_FMT}" 2>/dev/null || date -u "${DATE_FMT}")

KUBE_MAJOR_VERSION="$(go mod edit -json | jq '.Require[] | select(.Path == "k8s.io/kubernetes") | .Version' --raw-output | sed 's/v\([0-9]*\).*/\1/')"
KUBE_MINOR_VERSION="$(go mod edit -json | jq '.Require[] | select(.Path == "k8s.io/kubernetes") | .Version' --raw-output | sed 's/v[0-9]*\.\([0-9]*\).*/\1/')"
KUBE_FULL_VERSION="$(go mod edit -json | jq '.Require[] | select(.Path == "k8s.io/kubernetes") | .Version' --raw-output)"

export GOLDFLAGS="-X k8s.io/client-go/pkg/version.gitCommit=${COMMIT_HASH:0:8}
 -X k8s.io/client-go/pkg/version.gitTreeState=clean \
 -X k8s.io/client-go/pkg/version.gitVersion=${KUBE_FULL_VERSION}+kcp-v%{version} \
 -X k8s.io/client-go/pkg/version.gitMajor=${KUBE_MAJOR_VERSION} \
 -X k8s.io/client-go/pkg/version.gitMinor=${KUBE_MINOR_VERSION} \
 -X k8s.io/client-go/pkg/version.buildDate=${BUILD_DATE} \
 -X k8s.io/component-base/version.gitCommit=${COMMIT_HASH:0:8} \
 -X k8s.io/component-base/version.gitTreeState=clean \
 -X k8s.io/component-base/version.gitVersion=${KUBE_FULL_VERSION}+kcp-v%{version} \
 -X k8s.io/component-base/version.gitMajor=${KUBE_MAJOR_VERSION} \
 -X k8s.io/component-base/version.gitMinor=${KUBE_MINOR_VERSION} \
 -X k8s.io/component-base/version.buildDate=${BUILD_DATE}"

# kcp binary
go build \
   -mod=vendor \
   -buildmode=pie \
   -ldflags="$GOLDFLAGS" \
   -o bin/%{server_executable_name} ./cmd/%{server_executable_name}/

cd cli

# kubectl-kcp binary
go build \
   -mod=vendor \
   -buildmode=pie \
   -ldflags="$GOLDFLAGS" \
   -o ../bin/%{kubectl_kcp_plugin_name} ./cmd/%{kubectl_kcp_plugin_name}/

# kubectl-ws binary
go build \
   -mod=vendor \
   -buildmode=pie \
   -ldflags="$GOLDFLAGS" \
   -o ../bin/%{kubectl_ws_plugin_name} ./cmd/%{kubectl_ws_plugin_name}/

# kubectl-create-workspace binary
go build \
   -mod=vendor \
   -buildmode=pie \
   -ldflags="$GOLDFLAGS" \
   -o ../bin/%{kubectl_create_workspace_plugin_executable_name} ./cmd/%{kubectl_create_workspace_plugin_name}/

%install
# Install the binaries
install -D -m 0755 bin/%{server_executable_name} %{buildroot}/%{_bindir}/%{server_executable_name}
install -D -m 0755 bin/%{kubectl_kcp_plugin_name} %{buildroot}/%{_bindir}/%{kubectl_kcp_plugin_name}
install -D -m 0755 bin/%{kubectl_ws_plugin_name} %{buildroot}/%{_bindir}/%{kubectl_ws_plugin_name}
install -D -m 0755 bin/%{kubectl_create_workspace_plugin_executable_name} %{buildroot}/%{_bindir}/%{kubectl_create_workspace_plugin_executable_name}

#
# server
#

# create the bash completion file
mkdir -p %{buildroot}%{_datarootdir}/bash-completion/completions/
%{buildroot}/%{_bindir}/%{server_executable_name} completion bash > %{buildroot}%{_datarootdir}/bash-completion/completions/%{server_executable_name}

# create the fish completion file
mkdir -p %{buildroot}%{_datarootdir}/fish/vendor_completions.d/
%{buildroot}/%{_bindir}/%{server_executable_name} completion fish > %{buildroot}%{_datarootdir}/fish/vendor_completions.d/%{server_executable_name}.fish

# create the zsh completion file
mkdir -p %{buildroot}%{_datarootdir}/zsh/site-functions/
%{buildroot}/%{_bindir}/%{server_executable_name} completion zsh > %{buildroot}%{_datarootdir}/zsh/site-functions/_%{server_executable_name}

#
# kubectl plugins
#

# create the bash completion file
mkdir -p %{buildroot}%{_datarootdir}/bash-completion/completions/
%{buildroot}/%{_bindir}/%{kubectl_kcp_plugin_name} completion bash > %{buildroot}%{_datarootdir}/bash-completion/completions/%{kubectl_kcp_plugin_name}
%{buildroot}/%{_bindir}/%{kubectl_ws_plugin_name} completion bash > %{buildroot}%{_datarootdir}/bash-completion/completions/%{kubectl_ws_plugin_name}
# kubectl-create-workspace does not allow creating completions

# create the fish completion file
mkdir -p %{buildroot}%{_datarootdir}/fish/vendor_completions.d/
%{buildroot}/%{_bindir}/%{kubectl_kcp_plugin_name} completion fish > %{buildroot}%{_datarootdir}/fish/vendor_completions.d/%{kubectl_kcp_plugin_name}.fish
%{buildroot}/%{_bindir}/%{kubectl_ws_plugin_name} completion fish > %{buildroot}%{_datarootdir}/fish/vendor_completions.d/%{kubectl_ws_plugin_name}.fish
# kubectl-create-workspace does not allow creating completions

# create the zsh completion file
mkdir -p %{buildroot}%{_datarootdir}/zsh/site-functions/
%{buildroot}/%{_bindir}/%{kubectl_kcp_plugin_name} completion zsh > %{buildroot}%{_datarootdir}/zsh/site-functions/_%{kubectl_kcp_plugin_name}
%{buildroot}/%{_bindir}/%{kubectl_ws_plugin_name} completion zsh > %{buildroot}%{_datarootdir}/zsh/site-functions/_%{kubectl_ws_plugin_name}
# kubectl-create-workspace does not allow creating completions

%check
%{buildroot}/%{_bindir}/%{server_executable_name} --version | grep v%{version}
%{buildroot}/%{_bindir}/%{kubectl_kcp_plugin_name} --version | grep v%{version}
%{buildroot}/%{_bindir}/%{kubectl_ws_plugin_name} --version | grep v%{version}
# kubectl-create-workspace does not have a --version command...

%files
%doc README.md
%license LICENSE
%{_bindir}/%{kubectl_kcp_plugin_name}
%{_bindir}/%{kubectl_ws_plugin_name}
%{_bindir}/%{kubectl_create_workspace_plugin_executable_name}

%files bash-completion
%{_datarootdir}/bash-completion/completions/%{kubectl_kcp_plugin_name}
%{_datarootdir}/bash-completion/completions/%{kubectl_ws_plugin_name}

%files fish-completion
%{_datarootdir}/fish/vendor_completions.d/%{kubectl_kcp_plugin_name}.fish
%{_datarootdir}/fish/vendor_completions.d/%{kubectl_ws_plugin_name}.fish

%files zsh-completion
%{_datarootdir}/zsh/site-functions/_%{kubectl_kcp_plugin_name}
%{_datarootdir}/zsh/site-functions/_%{kubectl_ws_plugin_name}

%files -n %{server_package_name}
%doc README.md
%license LICENSE
%{_bindir}/%{server_executable_name}

%files -n %{server_package_name}-bash-completion
%{_datarootdir}/bash-completion/completions/%{server_executable_name}

%files -n %{server_package_name}-fish-completion
%{_datarootdir}/fish/vendor_completions.d/%{server_executable_name}.fish

%files -n %{server_package_name}-zsh-completion
%{_datarootdir}/zsh/site-functions/_%{server_executable_name}

%changelog
openSUSE Build Service is sponsored by