英文:
Rollback the deployment using client-go api
问题
我想使用k8s的client-go库将部署回滚到特定的修订版本(回滚历史记录)。但是到目前为止,我还没有找到解决方案。我只能获取资源修订版本,而无法获取使用kubectl命令获取的“部署修订版本”。
以下是使用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"
提前谢谢。
英文:
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
答案1
得分: 1
假设你已经查看了更新示例?
无论如何,这里的dp
变量包含了你所需的所有内容:
dp, err := clientset.ExtensionsV1beta1Client.Deployments("default").Get("nginx-deployment", metav1.GetOptions{})
所以,dp
的类型是v1beta1.Deployment
,它包含了一个类型为metav1.ObjectMeta
的变量,其中包含了ResourceVersion
。
英文:
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
.
答案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"]
英文:
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
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
}
英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论