Keeping record of the change cause using the --record flag

3 min read | by Jordi Prats

On Kubernetes, when we update objects such as a deployment or a daemonset we can check it's rollout history using kubectl rollout history:

$ kubectl rollout history deploy pet2cattle
deployment.apps/pet2cattle 
REVISION  CHANGE-CAUSE
21        <none>
22        <none>
23        <none>
24        <none>
26        <none>
28        <none>
29        <none>
30        <none>
32        <none>
33        <none>
34        <none>

By default we won't be able to see a change cause, but we can fill this gap by setting the command that triggered the update adding the --record flag as follows:

$ kubectl scale deployment/pet2cattle --replicas 2 --record
deployment.apps/pet2cattle scaled

If we describe the object we will see that this is adding an annotation with the change cause (kubernetes.io/change-cause):

$ kubectl describe deployment/pet2cattle
Name:                   pet2cattle
Namespace:              default
CreationTimestamp:      Thu, 25 Dec 2020 10:07:38 +0100
Labels:                 app=pet2cattle
Annotations:            deployment.kubernetes.io/revision: 1
                        kubernetes.io/change-cause: kubectl scale deployment/pet2cattle --replicas=2 --record=true
Selector:               app=pet2cattle
Replicas:               2 desired | 2 updated | 2 total | 1 available | 1 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=pet2cattle
  Containers:
   nginx:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      False   MinimumReplicasUnavailable
OldReplicaSets:  <none>
NewReplicaSet:   pet2cattle-997c454df (2/2 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  68s   deployment-controller  Scaled up replica set pet2cattle-997c454df to 1
  Normal  ScalingReplicaSet  9s    deployment-controller  Scaled up replica set pet2cattle-997c454df to 2

Running again kubectl rollout history we will be able to see the command on the CHANGE-CAUSE column

$ kubectl rollout history deploy pet2cattle
deployment.apps/pet2cattle 
REVISION  CHANGE-CAUSE
22        <none>
23        <none>
24        <none>
26        <none>
28        <none>
29        <none>
30        <none>
32        <none>
33        <none>
34        <none>
35        kubectl scale deployment/pet2cattle --replicas=2 --record=true

We can also add any annotation we like:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kubernetes.io/change-cause: ps auxf

And it will also show up on the CHANGE-CAUSE column:

$ kubectl rollout history deploy pet2cattle
deployment.apps/pet2cattle 
REVISION  CHANGE-CAUSE
99        <none>
100       <none>
101       <none>
102       <none>
103       <none>
104       <none>
105       <none>
106       <none>
107       <none>
109       kubectl scale deployment/pet2cattle --replicas=2 --record=true
110       ps auxf

Posted on 18/05/2021