Day 4: Find deprecated APIs before they disappear
Goal: Find manifests, Helm releases and clients that use API versions which go away with the next upgrade, and migrate them · Duration: approx. 45 min · Prerequisite: Day 0
Briefing
- Kubernetes removes API versions according to a fixed rule: a beta version is announced as deprecated and removed a few releases later.
- After the upgrade, the API server rejects removed versions. Running
objects are kept, but every
kubectl applyfrom an old Git repository, every old Helm chart version and every old client fails. - You have to check three places: files (Git, Helm charts), the cluster and the clients that talk to the API server.
| Tool | Checks |
|---|---|
pluto detect-files |
Manifests in a directory |
pluto detect-helm |
Stored Helm releases |
Metric apiserver_requested_deprecated_apis |
Who calls deprecated APIs at runtime |
kubectl convert |
Migrates manifests to the new version |
Drill 1: Install pluto
mkdir -p ~/day2/tag04 && cd ~/day2/tag04
ARCH=$(dpkg --print-architecture)
curl -fsSLo /tmp/pluto.tgz \
https://github.com/FairwindsOps/pluto/releases/download/v5.24.5/pluto_5.24.5_linux_$ARCH.tar.gz
tar -xzf /tmp/pluto.tgz -C /tmp pluto
sudo install /tmp/pluto /usr/local/bin/pluto
pluto versionExpected:
Version:5.24.5 Commit:d7d4f3f312f5b65c989a5a4eb92eb6b4ef86e29f
pluto ships with a list of all known deprecation dates. What affects your upgrade to v1.36 and v1.37?
pluto list-versions | awk 'NR==1 || /v1\.3[3-7]/' | cut -c1-100Expected (abridged, truncated on the right):
KIND NAME DEPRECATED IN REMOVED IN
ResourceSlice resource.k8s.io/v1beta1 v1.33.0 v1.36.0
ResourceClaim resource.k8s.io/v1beta1 v1.33.0 v1.36.0
DeviceClass resource.k8s.io/v1beta1 v1.33.0 v1.36.0
ResourceClaimTemplate resource.k8s.io/v1beta1 v1.33.0 v1.36.0
VolumeAttributesClass storage.k8s.io/v1beta1 v1.34.0 n/a
StorageVersionMigration storagemigration.k8s.io/v1alpha1 v1.35.0 v1.35.0
With v1.36, resource.k8s.io/v1beta1 (Dynamic Resource
Allocation) goes away. Does your cluster use this API at all?
kubectl api-versions | grep '^resource'Expected:
resource.k8s.io/v1
Save your progress? Sign up for free
Drill 2: A Git repository with legacy leftovers
A typical repository: manifests from a few years ago that nobody has touched because they “work, after all”.
mkdir -p ~/day2/tag04/repo && cd ~/day2/tag04/repo
cat > pdb.yaml <<'EOF'
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata: {name: alt, namespace: demo}
spec:
maxUnavailable: 1
selector: {matchLabels: {app: web}}
EOF
cat > hpa.yaml <<'EOF'
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata: {name: web, namespace: demo}
spec:
scaleTargetRef: {apiVersion: apps/v1, kind: Deployment, name: web}
minReplicas: 3
maxReplicas: 6
metrics:
- type: Resource
resource: {name: cpu, target: {type: Utilization, averageUtilization: 80}}
EOF
cat > cronjob.yaml <<'EOF'
apiVersion: batch/v1beta1
kind: CronJob
metadata: {name: aufraeumen, namespace: demo}
spec:
schedule: "0 3 * * *"
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- {name: job, image: busybox:1.37, command: [sh, -c, "echo aufgeraeumt"]}
EOF
cp ~/day2/tag00/web.yaml web.yaml
cd ..Save your progress? Sign up for free
Drill 3: What happens on apply?
kubectl apply -f repo/
echo "Exit-Code: $?"Expected (lines wrapped):
deployment.apps/web unchanged
service/web unchanged
resource mapping not found for name: "aufraeumen" namespace: "demo" from
"repo/cronjob.yaml": no matches for kind "CronJob" in version "batch/v1beta1"
ensure CRDs are installed first
resource mapping not found for name: "web" namespace: "demo" from "repo/hpa.yaml":
no matches for kind "HorizontalPodAutoscaler" in version "autoscaling/v2beta2"
ensure CRDs are installed first
resource mapping not found for name: "alt" namespace: "demo" from "repo/pdb.yaml":
no matches for kind "PodDisruptionBudget" in version "policy/v1beta1"
ensure CRDs are installed first
Exit-Code: 1
The dangerous part is not the error message but the state afterwards:
web was applied, the other three were not. A pipeline that
applies such a repository after every commit leaves a half-updated
namespace behind. The hint “ensure CRDs are installed first” is
misleading: no CRD is missing, what is missing is an API version that no
longer exists.
Save your progress? Sign up for free
Drill 4: pluto finds it beforehand
pluto detect-files -d repo -o wide | cut -c1-120Expected:
NAME NAMESPACE KIND VERSION REPLACEMENT
aufraeumen demo CronJob batch/v1beta1 batch/v1
web demo HorizontalPodAutoscaler autoscaling/v2beta2 autoscaling/v2
alt demo PodDisruptionBudget policy/v1beta1 policy/v1
To the right of this are the columns DEPRECATED IN,
REMOVED IN and REMOVED. Careful: without a
target version, pluto checks against an old default version and reports
REMOVED false for the HPA. Always check against the version
you want to reach:
pluto detect-files -d repo --target-versions k8s=v1.37.0
echo "Exit-Code: $?"Expected:
NAME KIND VERSION REPLACEMENT REMOVED DEPRECATED REPL AVAIL
aufraeumen CronJob batch/v1beta1 batch/v1 true true true
web HorizontalPodAutoscaler autoscaling/v2beta2 autoscaling/v2 true true false
alt PodDisruptionBudget policy/v1beta1 policy/v1 true true true
Exit-Code: 4
The exit code is not 0. This makes pluto suitable as a step in a CI pipeline: a pull request with removed APIs fails before it reaches the cluster.
Save your progress? Sign up for free
Drill 5: Migrate with kubectl convert
kubectl convert is a separate plugin that matches the
kubectl version:
ARCH=$(dpkg --print-architecture)
curl -fsSLo /tmp/kubectl-convert \
https://dl.k8s.io/release/v1.35.10/bin/linux/$ARCH/kubectl-convert
sudo install /tmp/kubectl-convert /usr/local/bin/kubectl-convert
kubectl convert -f repo/pdb.yaml --output-version policy/v1 > repo/pdb.neu.yaml
kubectl convert -f repo/hpa.yaml --output-version autoscaling/v2 > repo/hpa.neu.yaml
kubectl convert -f repo/cronjob.yaml --output-version batch/v1 > repo/cronjob.neu.yaml
mv repo/pdb.neu.yaml repo/pdb.yaml
mv repo/hpa.neu.yaml repo/hpa.yaml
mv repo/cronjob.neu.yaml repo/cronjob.yaml
cat repo/pdb.yamlExpected:
apiVersion: policy/v1
kind: PodDisruptionBudget
metadata:
name: alt
namespace: demo
spec:
maxUnavailable: 1
selector:
matchLabels:
app: web
status:
currentHealthy: 0
desiredHealthy: 0
disruptionsAllowed: 0
expectedPods: 0
kubectl convert adds an empty status block.
It does no harm, but it does not belong in the Git repository; delete it
before committing. Verify:
pluto detect-files -d repo --target-versions k8s=v1.37.0
kubectl apply -f repo/Expected:
There were no resources found with known deprecated apiVersions.
cronjob.batch/aufraeumen created
horizontalpodautoscaler.autoscaling/web created
poddisruptionbudget.policy/alt created
deployment.apps/web unchanged
service/web unchanged
Save your progress? Sign up for free
Drill 6: Helm releases and the cluster itself
Helm stores every release with its rendered manifests as a Secret. A release that was installed with an API that was later removed can neither be upgraded nor cleanly deleted after the upgrade.
pluto detect-helm --target-versions k8s=v1.37.0 -o wide
pluto detect-all-in-cluster --target-versions k8s=v1.37.0Expected:
There were no resources found with known deprecated apiVersions.
There were no resources found with known deprecated apiVersions.
In this lab everything is clean. If pluto finds an affected Helm release in your cluster, the way forward is: first install a chart version with the new APIs while the old API still exists. Only then upgrade.
Save your progress? Sign up for free
Drill 7: Who calls deprecated APIs at runtime?
Files are only half the truth. Controllers, operators, scripts and tools call the API directly. The API server warns about this and keeps count:
kubectl -n demo get endpoints shopExpected:
Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice
NAME ENDPOINTS AGE
shop 10.244.0.21:8080,10.244.1.75:8080 15m
kubectl get --raw /metrics | grep '^apiserver_requested_deprecated_apis'Expected (lines wrapped):
apiserver_requested_deprecated_apis{group="",removed_release="",
resource="componentstatuses",subresource="",version="v1"} 1
apiserver_requested_deprecated_apis{group="",removed_release="",
resource="endpoints",subresource="",version="v1"} 1
Two hits. endpoints comes from your
kubectl get endpoints. And componentstatuses?
pluto itself queried that in Drill 6, an API that has been deprecated
since v1.19. This is exactly what the metric is for: it also shows the
callers you did not think of. removed_release is empty;
neither API has a removal date yet.
Two limitations: each API server counts on its own, and behind the virtual address you reach a different one on every call. And the counter starts from zero on every restart of the API server. In production, Prometheus collects the metric from all instances.
Save your progress? Sign up for free
Exercise: Speed Drills
- Check a single manifest against v1.36. Target time: 1 min
- List all API versions of the
autoscalinggroup that your cluster offers. Target time: 1 min - Show only the metric lines in which
removed_releaseis set. Target time: 1 min
Solution
pluto detect-files -d repo --target-versions k8s=v1.36.0
kubectl api-versions | grep '^autoscaling'
kubectl get --raw /metrics | grep '^apiserver_requested_deprecated_apis' \
| grep -v 'removed_release=""'autoscaling/v1
autoscaling/v2
The last line returns nothing in this lab: no caller uses an API with a fixed removal date.
Save your progress? Sign up for free
Checkpoint
pluto detect-files -d ~/day2/tag04/repo --target-versions k8s=v1.37.0; echo $?Expected:
There were no resources found with known deprecated apiVersions.
and 0.
kubectl -n demo get cronjob,hpa,pdbExpected: aufraeumen, the HPA
web and the PDBs alt and
shop.
Challenge
Turn pluto into a gate before every upgrade: a single command that checks all three sources against v1.37 and only exits with 0 if nothing is found anywhere.
Hint: && chains commands and
stops at the first error.
Solution
pluto detect-files -d ~/day2/tag04/repo --target-versions k8s=v1.37.0 \
&& pluto detect-helm --target-versions k8s=v1.37.0 \
&& pluto detect-all-in-cluster --target-versions k8s=v1.37.0 \
&& echo "bereit für v1.37"Expected:
There were no resources found with known deprecated apiVersions.
There were no resources found with known deprecated apiVersions.
There were no resources found with known deprecated apiVersions.
bereit für v1.37
Save your progress? Sign up for free
Cleanup
You no longer need the legacy objects. web and the PDB
shop stay.
kubectl -n demo delete cronjob aufraeumen
kubectl -n demo delete hpa web
kubectl -n demo delete pdb altInterview Note
- What happens to running objects when an API version is removed? Nothing. etcd stores them in the new version. Only clients and manifests that request the old version fail.
- How do you find callers of deprecated APIs? Via the
metric
apiserver_requested_deprecated_apisor the audit logs, in which the API server marks such requests. - Why is a Helm release with a removed API a problem? During an upgrade, Helm compares with the stored manifest. If the API server no longer knows its version, the comparison fails.
Enterprise Note
- pluto or comparable checks run in CI for every repository that contains manifests or Helm charts, with the target version of the next upgrade.
- Before every upgrade, platform teams publish a list of affected teams and set a deadline, instead of running into surprises on upgrade day.
- For Helm releases with APIs that have already been removed, there is
the plugin
helm mapkubeapis, which rewrites the stored manifests after the fact.