I have the following code snippet:
// patchDeploymentReplicas updates the number of replicas of a given deployment
func patchDeploymentReplicas(ctx context.Context, cs *kubernetes.Clientset, ns, d string, repl int) error {
err := retry.RetryOnConflict(retry.DefaultRetry, func() error {
result, err := cs.AppsV1().Deployments(ns).Get(context.TODO(), d, metav1.GetOptions{})
if err != nil {
return err
}
fmt.Println(*result.Spec.Replicas) // debug
result.Spec.Replicas = flip(int32(repl))
_, err = cs.AppsV1().Deployments(ns).Update(context.TODO(), result, metav1.UpdateOptions{})
fmt.Println(*result.Spec.Replicas) // debug
return err
})
if err != nil {
return err
}
return nil
}
func flip(i int32) *int32 {
return &i
}
which is mostly from the Official client-go documentation.
However, when I run this code in (Minikube) cluster, the following happens:
{"level":"debug","routine":"suspender","namespace":"plop","deployment":"misc-depl","time":"2021-10-05T15:26:19Z","message":"scaling misc-depl from 1 to 0 replicas"}
1
1
{"level":"debug","routine":"suspender","namespace":"plop","deployment":"misc-depl","time":"2021-10-05T15:26:19Z","message":"scaling misc-depl from 1 to 0 replicas"}
1
1
(the JSON log line comes from another function).
We can see that the deployment object is not updated, which is confirmed when I use kubectl
:
❯ k get deployments.apps -n plop -w
NAME READY UP-TO-DATE AVAILABLE AGE
kube-ns-suspender-depl 0/0 0 0 3h2m
misc-depl 1/1 1 1 9m29s
^C
However, when I edit the deployment manually (kubectl edit -n plop deployments.apps misc-depl
), it scales to 0 without any problem.
I have tried both the answers proposed here and none of them worked for me.
I guess the object I'm editing in my code is in reality a copy of the one I would like to edit, but I'm not sure how to debug and fix this.
EDIT
It seems that the issue comes from the fact that I want to scale to 0. I tried the reversed operation (scale from 0 to 1 replica) and it worked like a charm.
SECOND AND LAST EDIT
It was indeed a PEBCAK :(
Solved!
CodePudding user response:
It was a logical issue elsewhere in the code.