Choosing between etcd CAS and startup‑script idempotency for retry‑safe writes in k3os
0 reputation · 15 Oct 2025, 13:40 UTC
0 reputation · 15 Oct 2025, 13:40 UTC
In k3os the immutable OS forces most state changes through the Kubernetes API, so preventing duplicate writes on retry is a design decision rather than an operational afterthought.
Etcd’s compare‑and‑swap (CAS) using resource versions gives a built‑in idempotent write path: a repeated payload should return a conflict instead of creating a second record.
Startup scripts can also be written to check existing resources before applying, but that adds logic, introduces races with concurrent controller updates, and only offers best‑effort guarantees.
The trade‑off is whether to rely on CAS for all critical updates and reserve script‑level checks for non‑critical configuration, or to combine both layers for defense‑in‑depth.
Which approach should be the primary guard for critical resources? What verification steps confirm that a duplicate write returns a conflict rather than succeeding? Are there version‑specific CAS behavior changes in k3s that affect this decision?
Use etcd-backed compare-and-swap (CAS) through the Kubernetes API as the primary guard for all critical resources. The API server enforces atomic conflict detection via resourceVersion preconditions on every mutating request. Startup-script existence checks should be reserved for non-critical configuration or as a secondary defense-in-depth layer only.
resourceVersion inside the same etcd transaction that applies the write. No window exists for a concurrent controller to modify the object between a script's read and write.kubectl get followed by kubectl apply inevitably races with other controllers (kubelet, k3s internal controllers, operators). CAS removes that race entirely.resourceVersion returns HTTP 409 Conflict with a body indicating the current version. The client can then re-read, recompute, and retry safely.Capture a current resourceVersion
RV=$(kubectl get configmap critical-config -n kube-system -o jsonpath='{.metadata.resourceVersion}')Mutate the object through another path (simulate a concurrent controller)
kubectl patch configmap critical-config -n kube-system --patch '{"data":{"timestamp":"'$(date -Iseconds)'"}}'Retry the original update with the stale resourceVersion
kubectl replace --raw "/api/v1/namespaces/kube-system/configmaps/critical-config"
-H "Content-Type: application/json"
-d "$(kubectl get configmap critical-config -n kube-system -o json | jq '.metadata.resourceVersion = "'$RV'"')"Expect HTTP 409 Conflict and a response body containing the current resourceVersion. The write is rejected; no duplicate record is created.
Confirm idempotent create semantics
kubectl create configmap test-dup --from-literal=key=val
kubectl create configmap test-dup --from-literal=key=val # second callThe second call returns AlreadyExists (HTTP 409), not a new resource.
resourceVersion conflict handling.What k3os version (and its embedded k3s/Kubernetes/etcd versions) are you deploying? If you are on a version before Kubernetes 1.25 or etcd 3.5, verify that resourceVersion semantics match current upstream behavior before relying on CAS as the sole guard.
Use comments to ask for clarification. Post a solution as an answer.
36,775 reputation · 15 Oct 2025, 14:04 UTC
The answer correctly identifies etcd CAS via resourceVersion as the primary guard, but it's worth noting that kubectl apply --server-side (enabled by default in Kubernetes 1.25+, which k3s v1.25+ embeds) switches conflict detection from whole-object resourceVersion to field-level fieldManager ownership.
This means:
kubectl replace or PUT with If-Match still returns 409 Conflict on stale resourceVersion.kubectl apply --server-side may succeed concurrently if field sets don't overlap, without a 409.For k3os clusters on k3s < v1.25, CAS is purely resourceVersion-based. On ≥ v1.25, verify which code paths use server-side apply (many controllers now do) and test both conflict modes. A practical check: run the verification steps twice—once with kubectl replace --raw (legacy) and once with kubectl apply --server-side --field-manager=test—to confirm expected 409 behavior for your critical resources.