Changing the Deployment Strategy from RollingUpdate to Recreate using Golang

6/1/2021

I have a very big issue. We have a large number of deployments for a component which up until now were using the RollingUpdate deployment strategy type.

spec:
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate

Now a new design decision needs us to move this to a Recreate strategy, which would be fairly easy if we could just write a shell script to call kubectl patch and use the $retainKeys method described in the docs.

However, we use a custom tool written in Golang to manage our gazillion deployments and we'd like to integrate this into our regular update cycle and avoid this hack. Therefore, I've changed the code to look like this:

deploy := &appsv1.Deployment{
		Spec: appsv1.DeploymentSpec{
			Strategy: appsv1.DeploymentStrategy{
				Type: appsv1.RecreateDeploymentStrategyType,
			},

Obviously, when we run the update procedure, the tool fails with the following error:

Error updating *v1.Deployment Deployment.apps "cluster-agent" is invalid: spec.strategy.rollingUpdate: Forbidden: may not be specified when strategy `type` is 'Recreate'

According to the above link, the retainKeys trick has to be used but I have not managed to find out how that can be done from within Golang.

I can see in the k8s api sources that the patch strategy supports the retainKeys method:

Strategy DeploymentStrategy `json:"strategy,omitempty" patchStrategy:"retainKeys"

But would someone be so kind as to let me know how can I specify the retainKeys list within my Golang structs/code? Thank you very much!

-- user10592125
deployment
go
kubernetes
patch

0 Answers