fix: normalize Pimox registry ssh and containerd state
This commit is contained in:
parent
e8109e25a2
commit
e849b75d25
|
|
@ -1416,6 +1416,41 @@ pimox_scp() {
|
|||
scp -i "${key_path}" -o BatchMode=yes -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new "${source_path}" "${target}"
|
||||
}
|
||||
|
||||
refresh_lab_ssh_known_host() {
|
||||
local host="$1"
|
||||
local enabled="${LAB_REFRESH_REBUILT_VM_HOST_KEYS:-true}"
|
||||
local known_hosts_file="${LAB_SSH_KNOWN_HOSTS_FILE:-${HOME}/.ssh/known_hosts}"
|
||||
local scan_output
|
||||
|
||||
if disabled_value "${enabled}"; then
|
||||
return 0
|
||||
fi
|
||||
if ! truthy "${enabled}"; then
|
||||
echo "LAB_REFRESH_REBUILT_VM_HOST_KEYS must be true or false." >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ -z "${host}" || ! "${host}" =~ ^[A-Za-z0-9_.:-]+$ ]]; then
|
||||
echo "Invalid SSH known-host target '${host}'." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "$(dirname "${known_hosts_file}")"
|
||||
touch "${known_hosts_file}"
|
||||
chmod 0600 "${known_hosts_file}"
|
||||
|
||||
if ! scan_output="$(ssh-keyscan -T 5 -t ed25519 "${host}" 2>/dev/null)"; then
|
||||
echo "Could not scan SSH host key for ${host}." >&2
|
||||
exit 1
|
||||
fi
|
||||
if [[ -z "${scan_output}" ]]; then
|
||||
echo "ssh-keyscan returned no host key for ${host}." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
ssh-keygen -R "${host}" -f "${known_hosts_file}" >/dev/null 2>&1 || true
|
||||
printf '%s\n' "${scan_output}" >>"${known_hosts_file}"
|
||||
}
|
||||
|
||||
pimox_guest_ipv4() {
|
||||
local guest_json
|
||||
local host="$1"
|
||||
|
|
@ -6914,6 +6949,7 @@ ensure_pimox_registry_storage_path() {
|
|||
IFS=$'\t' read -r worker_host worker_user worker_node worker_key_path <<<"${inventory}"
|
||||
|
||||
echo "Ensuring local registry storage exists on ${worker_node} (${worker_user}@${worker_host}:${registry_path})..."
|
||||
refresh_lab_ssh_known_host "${worker_host}"
|
||||
ssh -i "${worker_key_path}" -o BatchMode=yes -o ConnectTimeout=10 -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new "${worker_user}@${worker_host}" "set -eu
|
||||
sudo mkdir -p '${registry_path}'
|
||||
sudo chown 1000:1000 '${registry_path}'
|
||||
|
|
@ -7095,12 +7131,16 @@ ensure_pimox_containerd_registry_refresh() {
|
|||
for node_entry in "${node_entries[@]}"; do
|
||||
IFS=$'\t' read -r node_host node_user node_name node_key_path <<<"${node_entry}"
|
||||
echo "Refreshing containerd local-registry config on ${node_name} (${node_user}@${node_host})..."
|
||||
refresh_lab_ssh_known_host "${node_host}"
|
||||
ssh -i "${node_key_path}" -o BatchMode=yes -o ConnectTimeout=10 -o IdentitiesOnly=yes -o StrictHostKeyChecking=accept-new "${node_user}@${node_host}" \
|
||||
"REGISTRY_ENDPOINT='${registry_endpoint}' bash -s" <<'PIMOX_CONTAINERD_EOF'
|
||||
set -eu
|
||||
|
||||
registry_endpoint="${REGISTRY_ENDPOINT:?}"
|
||||
changed=0
|
||||
config_bootstrapped=0
|
||||
hosts_changed=0
|
||||
original_config_tmp=""
|
||||
|
||||
containerd_config_version() {
|
||||
awk -F= '
|
||||
|
|
@ -7112,6 +7152,27 @@ containerd_config_version() {
|
|||
' /etc/containerd/config.toml
|
||||
}
|
||||
|
||||
reset_containerd_registry_tables() {
|
||||
sudo awk '
|
||||
function is_registry_table(line) {
|
||||
return line ~ /^[[:space:]]*\[plugins\./ &&
|
||||
line ~ /[.]registry([.\]]|$)/ &&
|
||||
(line ~ /io[.]containerd[.]grpc[.]v1[.]cri/ ||
|
||||
line ~ /io[.]containerd[.]cri[.]v1[.]images/)
|
||||
}
|
||||
is_registry_table($0) { skip = 1; next }
|
||||
skip && /^[[:space:]]*\[/ { skip = 0 }
|
||||
!skip { print }
|
||||
' /etc/containerd/config.toml | sudo tee /etc/containerd/config.toml.homelab >/dev/null
|
||||
|
||||
if ! sudo cmp -s /etc/containerd/config.toml.homelab /etc/containerd/config.toml; then
|
||||
sudo mv /etc/containerd/config.toml.homelab /etc/containerd/config.toml
|
||||
changed=1
|
||||
else
|
||||
sudo rm -f /etc/containerd/config.toml.homelab
|
||||
fi
|
||||
}
|
||||
|
||||
ensure_containerd_registry_config_path() {
|
||||
plugin="$1"
|
||||
append_section="$2"
|
||||
|
|
@ -7176,8 +7237,11 @@ ensure_containerd_registry_config_path() {
|
|||
sudo mkdir -p /etc/containerd "/etc/containerd/certs.d/${registry_endpoint}"
|
||||
if ! sudo test -s /etc/containerd/config.toml; then
|
||||
sudo containerd config default | sudo tee /etc/containerd/config.toml >/dev/null
|
||||
changed=1
|
||||
config_bootstrapped=1
|
||||
fi
|
||||
original_config_tmp="$(mktemp)"
|
||||
sudo cp /etc/containerd/config.toml "${original_config_tmp}"
|
||||
reset_containerd_registry_tables
|
||||
|
||||
hosts_tmp="$(mktemp)"
|
||||
cat >"${hosts_tmp}" <<REGISTRY_HOSTS_EOF
|
||||
|
|
@ -7191,17 +7255,15 @@ REGISTRY_HOSTS_EOF
|
|||
if ! sudo test -f "/etc/containerd/certs.d/${registry_endpoint}/hosts.toml" ||
|
||||
! sudo cmp -s "${hosts_tmp}" "/etc/containerd/certs.d/${registry_endpoint}/hosts.toml"; then
|
||||
sudo cp "${hosts_tmp}" "/etc/containerd/certs.d/${registry_endpoint}/hosts.toml"
|
||||
changed=1
|
||||
hosts_changed=1
|
||||
fi
|
||||
rm -f "${hosts_tmp}"
|
||||
|
||||
if ! sudo grep -Eq 'config_path[[:space:]]*=[[:space:]]*"/etc/containerd/certs.d"' /etc/containerd/config.toml; then
|
||||
config_version="$(containerd_config_version)"
|
||||
if [ "${config_version}" = "3" ]; then
|
||||
ensure_containerd_registry_config_path "io.containerd.cri.v1.images" '[plugins."io.containerd.cri.v1.images".registry]'
|
||||
else
|
||||
ensure_containerd_registry_config_path "io.containerd.grpc.v1.cri" '[plugins."io.containerd.grpc.v1.cri".registry]'
|
||||
fi
|
||||
config_version="$(containerd_config_version)"
|
||||
if [ "${config_version}" = "3" ]; then
|
||||
ensure_containerd_registry_config_path "io.containerd.cri.v1.images" '[plugins."io.containerd.cri.v1.images".registry]'
|
||||
else
|
||||
ensure_containerd_registry_config_path "io.containerd.grpc.v1.cri" '[plugins."io.containerd.grpc.v1.cri".registry]'
|
||||
fi
|
||||
|
||||
sudo tee /etc/crictl.yaml >/dev/null <<'CRICTL_EOF'
|
||||
|
|
@ -7212,6 +7274,13 @@ debug: false
|
|||
CRICTL_EOF
|
||||
|
||||
sudo containerd config dump >/dev/null
|
||||
changed=0
|
||||
if [ "${config_bootstrapped}" = "1" ] ||
|
||||
[ "${hosts_changed}" = "1" ] ||
|
||||
! sudo cmp -s "${original_config_tmp}" /etc/containerd/config.toml; then
|
||||
changed=1
|
||||
fi
|
||||
rm -f "${original_config_tmp}"
|
||||
if [ "${changed}" = "1" ]; then
|
||||
sudo systemctl restart containerd
|
||||
sudo systemctl restart kubelet
|
||||
|
|
|
|||
Loading…
Reference in New Issue