Cluster information:
Kubernetes version:v.124.11
Installation method:kubeadm
Host OS: rocky8.6
Scenes
There is only one application in the cluster that is updating the deployment. There are no other applications, so there will be no conflict between applications!
[image]
example code
firstGet, getErr := deploymentsClient.Get(context.TODO(), "my-deployment", metav1.GetOptions{})
if getErr != nil {
panic(fmt.Errorf("Failed to get latest version of Deployment: %v", getErr))
}
fmt.Println("ResourceVersion before first update:", firstGet.ObjectMeta.ResourceVersion)
firstGet.Spec.Template.Spec.Containers[0].Image = "nginx:1.13"
firstGet.Spec.Replicas = int32Ptr(3)
afterFirstUpdate, updateErr := deploymentsClient.Update(context.TODO(), firstGet, metav1.UpdateOptions{})
if updateErr != nil {
fmt.Println("first update error:", updateErr)
}
fmt.Printf("after first update: %s, %d\n", afterFirstUpdate.Spec.Template.Spec.Containers[0].Image, *afterFirstUpdate.Spec.Replicas)
fmt.Println("after first update:", afterFirstUpdate.ObjectMeta.ResourceVersion)
secondGet, getErr := deploymentsClient.Get(context.TODO(), "my-deployment", metav1.GetOptions{})
if getErr != nil {
panic(fmt.Errorf("Failed to get latest version of Deployment: %v", getErr))
}
fmt.Println("before second update:", secondGet.ObjectMeta.ResourceVersion)
secondGet.Spec.Template.Spec.Containers[0].Image = "nginx:1.14"
afterSecondUpdate, updateErr := deploymentsClient.Update(context.TODO(), secondGet, metav1.UpdateOptions{})
if updateErr != nil {
fmt.Println("second update error:", updateErr)
afterSecondUpdateFaild, getErr := deploymentsClient.Get(context.TODO(), "my-deployment", metav1.GetOptions{})
if getErr != nil {
panic(fmt.Errorf("Failed to get latest version of Deployment: %v", getErr))
}
fmt.Println("after second update failed:", afterSecondUpdateFaild.ObjectMeta.ResourceVersion)
} else {
fmt.Printf("after second update: %s %d\n", afterSecondUpdate.Spec.Template.Spec.Containers[0].Image, *afterSecondUpdate.Spec.Replicas)
fmt.Println("after second update:", afterSecondUpdate.ObjectMeta.ResourceVersion)
}
abnormal behavior
1.The update is successful but the resourceVerison has not changed.(18727702 to 18727702)
ResourceVersion before first update: 18727702
after first update: nginx:1.13, 3
after first update: 18727702
before second update: 18727702
after second update: nginx:1.14 3
after second update: 18728288
2.Update fails but resourceVerison changes(18728419 to 18728421)
ResourceVersion before first update: 18728373
after first update: nginx:1.13, 3
after first update: 18728419
before second update: 18728419
second update error: Operation cannot be fulfilled on deployments.apps "my-deployment": the object has been modified; please apply your changes to the latest version and try again
after second update failed: 18728421