Ignore fields an autoscaler owns
Keep the replica count out of drift when a HorizontalPodAutoscaler sets it, and know exactly when that needs no rule and when it needs an ignore rule.
The situation
web runs under a HorizontalPodAutoscaler that keeps between 2 and 6 replicas. The autoscaler changes spec.replicas all day. If Wardn reported each change as drift, the Drift tab would be full of noise, and the one real change, someone running kubectl set image, would be lost in it (Drift).
Wardn only reports fields it owns. Whether it owns spec.replicas depends on your manifest, so the answer is not the same for every setup.
When is the replica count drift?
| Your setup | Is the replica count drift? | What to do |
|---|---|---|
The manifest leaves spec.replicas out. | No. Wardn never set the field, so there is nothing to compare. | Nothing. This is the shape to aim for. |
The manifest sets spec.replicas, and a HorizontalPodAutoscaler in the same namespace targets the workload. | Not reported for a Deployment or StatefulSet, as long as the deployment's service account may list HorizontalPodAutoscalers. | Remove the field from the manifest anyway; see below. |
| The same, but the service account may not list HorizontalPodAutoscalers. | Yes, once the autoscaler moves the count. | Grant the right, label the workload, or add an ignore rule. |
The manifest sets spec.replicas, and the scaler is not a HorizontalPodAutoscaler, or targets a kind other than a Deployment or StatefulSet. | Yes. | Add an ignore rule. |
Someone runs kubectl scale on a workload with no autoscaler. | Yes, and it should be. | Revert or adopt. |
Wardn's operator looks at the HorizontalPodAutoscalers in each namespace it compares and skips /spec/replicas on a Deployment or StatefulSet that one of them targets by name. It does this with the rights of the deployment's service account, wardn-deployer. If listing is refused, the operator carries on without the autoscaler rule and the field is compared like any other, so a narrow role is the usual reason an "ignored" field is reported anyway.
An ignore rule is the explicit answer: for one object and one JSON pointer, drift is not reported, whatever the reason. A rule hides the report only. It does not change what an apply writes, which is why the manifest comes first.
Leave replicas out of the manifest
An ignore rule stops Wardn reporting the field. It does not stop Wardn applying it. If the manifest says replicas: 2, every apply of that Deployment, a new commit or Sync now, sets the count to 2 again, and the autoscaler then moves it back. Pods are stopped and started for nothing. The Kubernetes guidance for autoscaled workloads is to remove spec.replicas from the manifest, and that applies here too.
Repository layout
Settings
The manifests
The Deployment has no replicas line. The autoscaler decides the count, and the Deployment's default of 1 is only what it starts at.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: ghcr.io/example/shop-web:1.4.2
ports:
- containerPort: 8080
resources:
requests:
cpu: 100m
memory: 128MiapiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 6
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 80The cluster needs a metrics server for CPU targets; Wardn does not install one. A HorizontalPodAutoscaler is namespaced and is applied like any other object.
Removing replicas from a Deployment that already has it
Kubernetes resets the count to its default of 1 when spec.replicas disappears from the applied manifest, until the autoscaler scales it up again. Pods are stopped for that moment. Make the change in a quiet period, and expect a short dip. A new Deployment that never had the field does not see it.
Let the operator see the autoscaler
The automatic rule needs one right in the namespace. The built-in admin role has it. With a narrower role for wardn-deployer, add it (Connect a cluster):
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: wardn-read-autoscalers
namespace: shop
rules:
- apiGroups: ["autoscaling"]
resources: ["horizontalpodautoscalers"]
verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: wardn-read-autoscalers
namespace: shop
roleRef: { apiGroup: rbac.authorization.k8s.io, kind: Role, name: wardn-read-autoscalers }
subjects:
- { kind: ServiceAccount, name: wardn-deployer, namespace: shop }Mark a workload as autoscaled
If you cannot grant the right, or the autoscaler is created later, label the workload. The operator skips /spec/replicas on a Deployment or StatefulSet carrying this label without looking for an autoscaler:
metadata:
name: web
labels:
drift.wardn.dev/hpa-managed: "true"Add an ignore rule
For any other case, such as a scaler that is not a HorizontalPodAutoscaler, a rule names the object and the field. In the web app, open the deployment's Drift tab and choose Ignore field for the drifted object, then apply your choices. The same through the API, which also lets you write the rule before any drift exists:
api() { curl -fsS -H "Authorization: Bearer $WARDN_TOKEN" -H 'Content-Type: application/json' "$@"; }
api -X POST "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/drift/ignores" -d '{
"object": {"group": "apps", "version": "v1", "kind": "Deployment", "namespace": "shop", "name": "web"},
"pointer": "/spec/replicas",
"reason": "managed by the autoscaler"
}'The pointer is an RFC 6901 JSON pointer and covers everything below it. The drift already on record is filtered at once. Adding or removing a rule needs drift:ignore, which the owners of a deployment, cluster, environment or app hold (Drift).
Check that it worked
The Deployment starts at 1 replica and the autoscaler raises it to minReplicas, 2, so the live count is not the Deployment's default. Look at the counts, then ask for the drift:
kubectl --context kind-prod -n shop get hpa web # the autoscaler's current and wanted counts
kubectl --context kind-prod -n shop get deploy web -o jsonpath='{.spec.replicas}'
api "$WARDN_API_URL/api/v1/deployments/$DEPLOYMENT_ID/drift" | jq '.drift, [.clusters[].objects[] | {kind, name, fields: [.fields[].path]}]'With the setup above, .drift stays {"status": "none", "count": 0} while the count changes. In the web app the deployment's Overview shows no Drift count, and the Drift tab says "The cluster matches the snapshot the deployment runs."
Then check that real drift is still caught. Change something Wardn owns:
kubectl --context kind-prod -n shop set image deploy/web web=ghcr.io/example/shop-web:0.0.1After the operator's next check, the Drift tab lists Deployment web with one field, /spec/template/spec/containers/0/image, and no replica count. Revert it, which applies the snapshot again; this is the check that the rule narrowed the report rather than silencing the Deployment.
If /spec/replicas is reported, work down the table at the top: is the field in the manifest, may wardn-deployer list HorizontalPodAutoscalers (kubectl auth can-i list horizontalpodautoscalers -n shop --as system:serviceaccount:shop:wardn-deployer), and is the autoscaler in the same namespace as the workload. Delete a rule you no longer want from Ignored fields, or with DELETE .../drift/ignores/{ruleId}; reporting turns back on.
Next
Drift
Detect, revert, adopt and ignore, and let Wardn revert after a delay.
Recover from a bad change
Drift in the cluster and a restore, step by step.
Health rule for a custom resource
Tell Wardn what a custom resource's status means.
Related: Drift concept, Compare snapshots, Roles and grants.
Health rule for a custom resource
Tell Wardn when a cert-manager Certificate is healthy and when it has failed, with a CEL health rule on the deployment.
External Secrets Operator with Wardn
Let External Secrets Operator create the Secret from your secret store, keep the values out of git and out of Wardn, and still see in Wardn when a secret is missing, stale or has changed.