使用client-go api回滚部署

时间:2017-08-11 12:04:10

标签: go kubernetes kubectl kubernetes-go-client

我想使用k8s的client-go库将部署回滚到某个修订版(推出历史记录)。但到目前为止,我还没有找到解决方案。我可以onyl获取资源修订版但不能部署修订版'我使用kebctl

kubectl rollout history deployment/nginx_dep

以下是使用client-go api的代码:

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

clientset, err := kubernetes.NewForConfig(config)

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

使用client-go api: 如何获取给定部署的现有修订。我想回滚部署以使用此修订版。谁能告诉我应该怎么做?

以下是我项目中的家属列表:

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

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

提前谢谢

4 个答案:

答案 0 :(得分:1)

假设您已经查看了update example

在任何情况下,dp变量都包含您需要的所有内容:

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

因此dp属于v1beta1.Deployment类型,其中包含metav1.ObjectMeta类型的变量,其中包含ResourceVersion

答案 1 :(得分:0)

现在拥有最新版本的部署,我们的想法是在触发应用之前保存此部署版本。 是否可以在申请前保存?

答案 2 :(得分:0)

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"]

答案 3 :(得分:0)

尝试 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
}