Search code examples
gokuberneteskubectlkubernetes-go-client

Rollback the deployment using client-go api


I would like to roll back the deployment to a certain revision( rollout history) using client-go library of k8s. But so far I havent found a solution. I could onyl fetch resource revision but not 'deployment revision' that I get using kebctl

kubectl rollout history deployment/nginx_dep

Here is the code using client-go api :

config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)

clientset, err := kubernetes.NewForConfig(config)

dp, err := clientset.ExtensionsV1beta1Client.Deployments("default").Get("nginx-deployment", metav1.GetOptions{})

Using client-go api: How do I get the existing revision for the given deployment.? I want to roll back the deployment to use this revision. Can anyone tell me how I should do that??

Here is the list of dependecies in my project:

[[constraint]]
  name = "k8s.io/client-go"
  version = "3.0.0"

[[override]]
  name = "k8s.io/apimachinery"
  branch = "release-1.6"

Thank you in advance


Solution

  • Assuming you already had a look at the update example?

    In any case, the dp variable here contains all you need:

    dp, err := clientset.ExtensionsV1beta1Client.Deployments("default").Get("nginx-deployment", metav1.GetOptions{})
    

    So dp is of type v1beta1.Deployment which contains a variable of type metav1.ObjectMeta which has the ResourceVersion.