简体   繁体   中英

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

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 .

clientset, err := kubernetes.NewForConfig(config)
deploymentsClient := clientset.AppsV1().Deployments("yournamespace")
result, err := deploymentsClient.Get("yourdeployment", metav1.GetOptions{})
version := result.GetObjectMeta().GetAnnotations()["deployment.kubernetes.io/revision"]

try try https://github.com/kubernetes/kubernetes/blob/17fec00b8915dbffac40b9eb481516a66092ef3e/pkg/controller/deployment/rollback.go#L30-L69


import (
    "fmt"

    "github.com/golang/glog"

    "k8s.io/api/core/v1"
    extensions "k8s.io/api/extensions/v1beta1"
    "k8s.io/apimachinery/pkg/types"
    deploymentutil "k8s.io/kubernetes/pkg/controller/deployment/util"
)



func (un *Uninject) rollbackToTemplate(d *v1.Deployment, rs []v1.ReplicaSet) (bool, error) {

    sort.Slice(rs, func(i, j int) bool {
        return rs[i].CreationTimestamp.UnixNano() > rs[j].CreationTimestamp.UnixNano()

    })
    performedRollback := false
    replicaSet := v1.ReplicaSet{}
    for _, r := range rs {
        for _, initContainer := range r.Spec.Template.Spec.InitContainers {
            if initContainer.Name == "istio-init" {
                performedRollback = true
                break
            } else {
                klog.V(4).Infof("Rolling back to a revision that contains the same template as current deployment %q, skipping rollback...", d.Name)
            }
        }
        if len(r.Spec.Template.Spec.InitContainers) == 0 || !performedRollback {
            replicaSet = r
            performedRollback = true
            break
        }
    }
    if performedRollback {
        klog.V(4).Infof("Rolling back deployment %q to template spec %+v", d.Name, replicaSet.Spec.Template.Spec)
        deploymentutil.SetFromReplicaSetTemplate(d, replicaSet.Spec.Template)
        deploymentutil.SetDeploymentAnnotationsTo(d, &replicaSet)
    }
    return performedRollback, un.updateDeploymentAndClearRollbackTo(d)
}

// updateDeploymentAndClearRollbackTo sets .spec.rollbackTo to nil and update the input deployment
// It is assumed that the caller will have updated the deployment template appropriately (in case
// we want to rollback).
func (un *Uninject) updateDeploymentAndClearRollbackTo(d *v1.Deployment) error {
    klog.Infof("Cleans up rollbackTo of deployment %q", d.Name)
    _, err := un.clientSet.AppsV1().Deployments(d.Namespace).Update(context.TODO(), d, metav1.UpdateOptions{})
    return err
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM