Using kubectl rollout restart equivalent with k8s python client

6/22/2020

I am trying to develop a AWS lambda to make a rollout restart deployment using the python client. I cannot find any implementation in the github repo or references. Using the -v in the kubectl rollout restart is not giving me enough hints to continue with the development.

Anyways, it is more related to the python client:

https://github.com/kubernetes-client/python

Any ideas? perhaps I could be missing something

-- Vauluu
aws-lambda
kubernetes
python

2 Answers

6/22/2020

The python client interacts directly with the Kubernetes API. Similar to what kubectl does. However, kubectl added some utility commands which contain logic that is not contained in the Kubernetes API. Rollout is one of those utilities.

In this case that means you have two approaches. You could reverse engineer the API calls the kubectl rollout restart makes. Pro tip: With go, you can actually import internal Kubectl behaviour and libraries, making this quite easy. So consider writing your lambda in golang.

Alternatively, you can have your Lambda call the Kubectl binary (using the process exec libraries in python). However, this does mean you need to include the binary in your lambda in some way (either by uploading it with your lambda or by building a lambda layer containing kubectl).

-- Blokje5
Source: StackOverflow

11/22/2021

@Andre Pires, it can be done like this way :

data := fmt.Sprintf(`{"spec":{"template":{"metadata":{"annotations":{"kubectl.kubernetes.io/restartedAt":"%s"}}}},"strategy":{"type":"RollingUpdate","rollingUpdate":{"maxUnavailable":"%s","maxSurge": "%s"}}}`, time.Now().String(), "25%", "25%")
newDeployment, err := clientImpl.ClientSet.AppsV1().Deployments(item.Pod.Namespace).Patch(context.Background(), deployment.Name, types.StrategicMergePatchType, []byte(data), metav1.PatchOptions{FieldManager: "kubectl-rollout"})
-- Navneet Pathak
Source: StackOverflow