mirror of
https://github.com/kubernetes-sigs/kubespray.git
synced 2026-10-29 22:03:22 -02:30
ci: run the Ansible controller as an unprivileged user (#13527)
* ci: run the Ansible controller as an unprivileged user * ci: clarify non-root controller check * ci: make the sudo privilege check fail when sudo succeeds Bash does not apply errexit to the negated sudo command. The old check could not fail the job even when passwordless sudo worked. Exit explicitly when sudo succeeds. Remove --break-system-packages from both upgrade pip installs because they run inside the controller virtual environment.
This commit is contained in:
+9
-1
@@ -8,11 +8,19 @@ stages:
|
|||||||
variables:
|
variables:
|
||||||
FAILFASTCI_NAMESPACE: 'kargo-ci'
|
FAILFASTCI_NAMESPACE: 'kargo-ci'
|
||||||
GITLAB_REPOSITORY: 'kargo-ci/kubernetes-sigs-kubespray'
|
GITLAB_REPOSITORY: 'kargo-ci/kubernetes-sigs-kubespray'
|
||||||
GIT_CONFIG_COUNT: 2
|
# Let the helper assign checkout ownership to the build user.
|
||||||
|
FF_DISABLE_UMASK_FOR_DOCKER_EXECUTOR: "true"
|
||||||
|
FF_DISABLE_UMASK_FOR_KUBERNETES_EXECUTOR: "true"
|
||||||
|
GIT_CONFIG_COUNT: 3
|
||||||
GIT_CONFIG_KEY_0: user.email
|
GIT_CONFIG_KEY_0: user.email
|
||||||
GIT_CONFIG_VALUE_0: "ci@kubespray.io"
|
GIT_CONFIG_VALUE_0: "ci@kubespray.io"
|
||||||
GIT_CONFIG_KEY_1: user.name
|
GIT_CONFIG_KEY_1: user.name
|
||||||
GIT_CONFIG_VALUE_1: "Kubespray CI"
|
GIT_CONFIG_VALUE_1: "Kubespray CI"
|
||||||
|
# TODO: Remove this safe.directory workaround after fixing the inconsistent GitLab Runner versions.
|
||||||
|
# Runners older than 17.11 may leave checkout ownership incorrect despite the FF_DISABLE_UMASK flags.
|
||||||
|
# https://github.com/kubernetes-sigs/kubespray/pull/13527#discussion_r4198076964
|
||||||
|
GIT_CONFIG_KEY_2: safe.directory
|
||||||
|
GIT_CONFIG_VALUE_2: "$CI_PROJECT_DIR"
|
||||||
ANSIBLE_FORCE_COLOR: "true"
|
ANSIBLE_FORCE_COLOR: "true"
|
||||||
MAGIC: "ci check this"
|
MAGIC: "ci check this"
|
||||||
GS_ACCESS_KEY_ID: $GS_KEY
|
GS_ACCESS_KEY_ID: $GS_KEY
|
||||||
|
|||||||
+15
-2
@@ -16,11 +16,24 @@ pre-commit:
|
|||||||
when: 'always'
|
when: 'always'
|
||||||
needs: []
|
needs: []
|
||||||
|
|
||||||
|
# Verify non-root Git setup, CI file writes, and local Ansible execution.
|
||||||
|
non-root-controller:
|
||||||
|
extends: .job
|
||||||
|
stage: test
|
||||||
|
script:
|
||||||
|
- test "$(id -u)" -ne 0
|
||||||
|
- if sudo -n true; then exit 1; fi
|
||||||
|
- test "$HOME" = /home/kubespray
|
||||||
|
- |
|
||||||
|
for directory in "$HOME" "$CI_PROJECT_DIR" "$CI_PROJECT_DIR/.git" "$CI_PROJECT_DIR"/contrib/terraform/*/; do
|
||||||
|
probe=$(mktemp "$directory/.write-check.XXXXXX")
|
||||||
|
rm "$probe"
|
||||||
|
done
|
||||||
|
- ansible localhost --connection=local -m setup -a 'filter=ansible_user_uid'
|
||||||
|
|
||||||
vagrant-validate:
|
vagrant-validate:
|
||||||
extends: .job
|
extends: .job
|
||||||
stage: test
|
stage: test
|
||||||
tags: [ffci]
|
tags: [ffci]
|
||||||
variables:
|
|
||||||
VAGRANT_VERSION: 2.3.7
|
|
||||||
script:
|
script:
|
||||||
- ./tests/scripts/vagrant-validate.sh
|
- ./tests/scripts/vagrant-validate.sh
|
||||||
|
|||||||
+11
-4
@@ -16,6 +16,7 @@ RUN apt update -q \
|
|||||||
libssl-dev \
|
libssl-dev \
|
||||||
python3-dev \
|
python3-dev \
|
||||||
python3-pip \
|
python3-pip \
|
||||||
|
python3-venv \
|
||||||
sshpass \
|
sshpass \
|
||||||
apt-transport-https \
|
apt-transport-https \
|
||||||
jq \
|
jq \
|
||||||
@@ -44,16 +45,22 @@ ADD ./requirements.txt /kubespray/requirements.txt
|
|||||||
ADD ./tests/requirements.txt /kubespray/tests/requirements.txt
|
ADD ./tests/requirements.txt /kubespray/tests/requirements.txt
|
||||||
|
|
||||||
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 1 \
|
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 1 \
|
||||||
&& pip install --break-system-packages --ignore-installed --no-compile --no-cache-dir pip -U \
|
|
||||||
&& pip install --break-system-packages --no-compile --no-cache-dir -r tests/requirements.txt \
|
|
||||||
&& curl -L https://dl.k8s.io/release/v1.37.1/bin/linux/$(dpkg --print-architecture)/kubectl -o /usr/local/bin/kubectl \
|
&& curl -L https://dl.k8s.io/release/v1.37.1/bin/linux/$(dpkg --print-architecture)/kubectl -o /usr/local/bin/kubectl \
|
||||||
&& echo $(curl -L https://dl.k8s.io/release/v1.37.1/bin/linux/$(dpkg --print-architecture)/kubectl.sha256) /usr/local/bin/kubectl | sha256sum --check \
|
&& echo $(curl -L https://dl.k8s.io/release/v1.37.1/bin/linux/$(dpkg --print-architecture)/kubectl.sha256) /usr/local/bin/kubectl | sha256sum --check \
|
||||||
&& chmod a+x /usr/local/bin/kubectl \
|
&& chmod a+x /usr/local/bin/kubectl \
|
||||||
# Install Vagrant
|
# Install Vagrant
|
||||||
&& curl -LO https://releases.hashicorp.com/vagrant/${VAGRANT_VERSION}/vagrant_${VAGRANT_VERSION}-1_$(dpkg --print-architecture).deb \
|
&& curl -LO https://releases.hashicorp.com/vagrant/${VAGRANT_VERSION}/vagrant_${VAGRANT_VERSION}-1_$(dpkg --print-architecture).deb \
|
||||||
&& dpkg -i vagrant_${VAGRANT_VERSION}-1_$(dpkg --print-architecture).deb \
|
&& dpkg -i vagrant_${VAGRANT_VERSION}-1_$(dpkg --print-architecture).deb \
|
||||||
&& rm vagrant_${VAGRANT_VERSION}-1_$(dpkg --print-architecture).deb \
|
&& rm vagrant_${VAGRANT_VERSION}-1_$(dpkg --print-architecture).deb
|
||||||
|
|
||||||
|
RUN useradd --create-home --uid 1001 kubespray
|
||||||
|
ENV HOME=/home/kubespray \
|
||||||
|
PATH="/home/kubespray/.local/bin:/home/kubespray/venv/bin:${PATH}"
|
||||||
|
USER kubespray
|
||||||
|
|
||||||
|
RUN python3 -m venv /home/kubespray/venv \
|
||||||
|
&& pip install --no-compile --no-cache-dir -r tests/requirements.txt \
|
||||||
&& vagrant plugin install vagrant-libvirt \
|
&& vagrant plugin install vagrant-libvirt \
|
||||||
# Install Kubernetes collections
|
# Install Kubernetes collections
|
||||||
&& pip install --break-system-packages --no-compile --no-cache-dir kubernetes==35.0.0 \
|
&& pip install --no-compile --no-cache-dir kubernetes==35.0.0 \
|
||||||
&& ansible-galaxy collection install kubernetes.core:==6.4.0
|
&& ansible-galaxy collection install kubernetes.core:==6.4.0
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ RUN apt update -q \
|
|||||||
libssl-dev \
|
libssl-dev \
|
||||||
python3-dev \
|
python3-dev \
|
||||||
python3-pip \
|
python3-pip \
|
||||||
|
python3-venv \
|
||||||
sshpass \
|
sshpass \
|
||||||
apt-transport-https \
|
apt-transport-https \
|
||||||
jq \
|
jq \
|
||||||
@@ -44,16 +45,22 @@ ADD ./requirements.txt /kubespray/requirements.txt
|
|||||||
ADD ./tests/requirements.txt /kubespray/tests/requirements.txt
|
ADD ./tests/requirements.txt /kubespray/tests/requirements.txt
|
||||||
|
|
||||||
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 1 \
|
RUN update-alternatives --install /usr/bin/python python /usr/bin/python3 1 \
|
||||||
&& pip install --break-system-packages --ignore-installed --no-compile --no-cache-dir pip -U \
|
|
||||||
&& pip install --break-system-packages --no-compile --no-cache-dir -r tests/requirements.txt \
|
|
||||||
&& curl -L https://dl.k8s.io/release/v{{ kube_version }}/bin/linux/$(dpkg --print-architecture)/kubectl -o /usr/local/bin/kubectl \
|
&& curl -L https://dl.k8s.io/release/v{{ kube_version }}/bin/linux/$(dpkg --print-architecture)/kubectl -o /usr/local/bin/kubectl \
|
||||||
&& echo $(curl -L https://dl.k8s.io/release/v{{ kube_version }}/bin/linux/$(dpkg --print-architecture)/kubectl.sha256) /usr/local/bin/kubectl | sha256sum --check \
|
&& echo $(curl -L https://dl.k8s.io/release/v{{ kube_version }}/bin/linux/$(dpkg --print-architecture)/kubectl.sha256) /usr/local/bin/kubectl | sha256sum --check \
|
||||||
&& chmod a+x /usr/local/bin/kubectl \
|
&& chmod a+x /usr/local/bin/kubectl \
|
||||||
# Install Vagrant
|
# Install Vagrant
|
||||||
&& curl -LO https://releases.hashicorp.com/vagrant/${VAGRANT_VERSION}/vagrant_${VAGRANT_VERSION}-1_$(dpkg --print-architecture).deb \
|
&& curl -LO https://releases.hashicorp.com/vagrant/${VAGRANT_VERSION}/vagrant_${VAGRANT_VERSION}-1_$(dpkg --print-architecture).deb \
|
||||||
&& dpkg -i vagrant_${VAGRANT_VERSION}-1_$(dpkg --print-architecture).deb \
|
&& dpkg -i vagrant_${VAGRANT_VERSION}-1_$(dpkg --print-architecture).deb \
|
||||||
&& rm vagrant_${VAGRANT_VERSION}-1_$(dpkg --print-architecture).deb \
|
&& rm vagrant_${VAGRANT_VERSION}-1_$(dpkg --print-architecture).deb
|
||||||
|
|
||||||
|
RUN useradd --create-home --uid 1001 kubespray
|
||||||
|
ENV HOME=/home/kubespray \
|
||||||
|
PATH="/home/kubespray/.local/bin:/home/kubespray/venv/bin:${PATH}"
|
||||||
|
USER kubespray
|
||||||
|
|
||||||
|
RUN python3 -m venv /home/kubespray/venv \
|
||||||
|
&& pip install --no-compile --no-cache-dir -r tests/requirements.txt \
|
||||||
&& vagrant plugin install vagrant-libvirt \
|
&& vagrant plugin install vagrant-libvirt \
|
||||||
# Install Kubernetes collections
|
# Install Kubernetes collections
|
||||||
&& pip install --break-system-packages --no-compile --no-cache-dir kubernetes==35.0.0 \
|
&& pip install --no-compile --no-cache-dir kubernetes==35.0.0 \
|
||||||
&& ansible-galaxy collection install kubernetes.core:==6.4.0
|
&& ansible-galaxy collection install kubernetes.core:==6.4.0
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
- name: Provision Packet VMs
|
- name: Provision Packet VMs
|
||||||
hosts: localhost
|
hosts: localhost
|
||||||
gather_facts: false
|
gather_facts: false
|
||||||
become: true
|
become: false
|
||||||
tasks:
|
tasks:
|
||||||
- name: Create Kubevirt VMs
|
- name: Create Kubevirt VMs
|
||||||
import_role:
|
import_role:
|
||||||
|
|||||||
@@ -3,6 +3,6 @@ set -euxo pipefail
|
|||||||
|
|
||||||
curl --proto '=https' --tlsv1.2 -fsSL https://get.opentofu.org/install-opentofu.sh -o install-opentofu.sh
|
curl --proto '=https' --tlsv1.2 -fsSL https://get.opentofu.org/install-opentofu.sh -o install-opentofu.sh
|
||||||
chmod +x install-opentofu.sh
|
chmod +x install-opentofu.sh
|
||||||
./install-opentofu.sh --install-method standalone
|
./install-opentofu.sh --install-method standalone --install-path "${HOME}/.local/bin" --symlink-path - --root-method none
|
||||||
rm -f install-opentofu.sh
|
rm -f install-opentofu.sh
|
||||||
tofu --version
|
tofu --version
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ if [ "${UPGRADE_TEST}" != "false" ]; then
|
|||||||
# Checkout the current tests/ directory ; even when testing old version,
|
# Checkout the current tests/ directory ; even when testing old version,
|
||||||
# we want the up-to-date test setup/provisionning
|
# we want the up-to-date test setup/provisionning
|
||||||
git checkout "${CI_COMMIT_SHA}" -- tests/
|
git checkout "${CI_COMMIT_SHA}" -- tests/
|
||||||
pip install --break-system-packages --no-compile --no-cache-dir -r requirements.txt
|
pip install --no-compile --no-cache-dir -r requirements.txt
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export ANSIBLE_BECOME=true
|
export ANSIBLE_BECOME=true
|
||||||
@@ -58,7 +58,7 @@ fi
|
|||||||
if [ "${UPGRADE_TEST}" != "false" ]; then
|
if [ "${UPGRADE_TEST}" != "false" ]; then
|
||||||
git checkout "${CI_COMMIT_SHA}"
|
git checkout "${CI_COMMIT_SHA}"
|
||||||
|
|
||||||
pip install --break-system-packages --no-compile --no-cache-dir -r requirements.txt
|
pip install --no-compile --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
case "${UPGRADE_TEST}" in
|
case "${UPGRADE_TEST}" in
|
||||||
"basic")
|
"basic")
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -euxo pipefail
|
set -euxo pipefail
|
||||||
|
|
||||||
curl -sL "https://releases.hashicorp.com/vagrant/${VAGRANT_VERSION}/vagrant_${VAGRANT_VERSION}-1_amd64.deb" -o "/tmp/vagrant_${VAGRANT_VERSION}-1_amd64.deb"
|
|
||||||
dpkg -i "/tmp/vagrant_${VAGRANT_VERSION}-1_amd64.deb"
|
|
||||||
vagrant validate --ignore-provider
|
vagrant validate --ignore-provider
|
||||||
|
|||||||
Reference in New Issue
Block a user