#!/usr/bin/env bash # Write the notAfter time of the k3s API server (6443) and kubelet (10250) # serving certificates as Prometheus textfile metrics. No root needed. # k3s renews these only when it starts and they are within 120 days of expiry # (docs.k3s.io cli/certificate), and the kubelet exposes no certificate metric, # so nothing else in the cluster watches them. set -Eeuo pipefail # shellcheck source=../lib/host-backup.sh source "$(dirname -- "${BASH_SOURCE[0]}")/../lib/host-backup.sh" lines="" for target in apiserver:6443 kubelet:10250; do name="${target%%:*}" port="${target##*:}" end="$(echo | timeout 10 openssl s_client -connect "127.0.0.1:$port" 2>/dev/null | openssl x509 -noout -enddate 2>/dev/null | sed 's/^notAfter=//')" || true [[ -n "$end" ]] || hb_fail "could not read the $name certificate on 127.0.0.1:$port" lines+="platform_k3s_certificate_not_after_seconds{certificate=\"$name\"} $(date -d "$end" +%s)"$'\n' hb_log "$name certificate notAfter $end" done hb_write_metrics platform_k3s_certificates <