主要的问题是,如果有一种方法可以从client-go sdk中完成一个pod,我并不想删除一个pod,我只想用一个阶段状态来完成它:Completed。
在代码中,我试图更新pod阶段,但它不工作,它不会返回错误或恐慌,但pod没有完成。我的代码:
func main() {
// creates the in-cluster config
config, err := rest.InClusterConfig()
if err != nil {
panic(err.Error())
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
for {
pods, err := clientset.CoreV1().Pods("ns").List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
for _, pod := range pods.Items {
podName:= pod.Name
if strings.Contains(strings.ToLower(podName), "single-condition") {
fmt.Println("get pods metadatada")
fmt.Println(pod.Name)
fmt.Printf("pod.Name %s \n", pod.Name)
fmt.Printf("Status.Phase %s \n", pod.Status.Phase)
fmt.Printf("PodIP %s \n", pod.Status.PodIP)
containers := pod.Status.ContainerStatuses
if len(containers) > 0 {
for _ ,c := range containers {
fmt.Printf("c.Name %s \n", c.Name)
fmt.Printf("c.State %s \n", c.State)
fmt.Printf("c.State.Terminated %s \n", c.State.Terminated)
stateTerminated := c.State.Terminated
stateRunning := c.State.Running
if stateTerminated == nil && stateRunning != nil {
fmt.Printf("c.State.Terminated %s \n", c.State.Terminated)
fmt.Printf("stateRunning Reason: %s\n", reflect.TypeOf(c.State.Running))
getPod, getErr := clientset.CoreV1().Pods("ns").Get(context.TODO(), "single-condition-pipeline-9rqrs-1224102659" , metav1.GetOptions{})
if getErr != nil {
fmt.Println("error1")
panic(fmt.Errorf("Failed to get: %v", getErr))
}
fmt.Println("update values")
fmt.Printf(" getPodName %d \n", getPod.Name)
getPod.Status.Phase = "Succeeded"
fmt.Println("updated status phase")
getContainers := getPod.Status.ContainerStatuses
fmt.Printf("len get container %d \n", len(getContainers))
_, updateErr := clientset.CoreV1().Pods("argo-workflows").Update(context.TODO(), getPod, metav1.UpdateOptions{})
fmt.Println("commit update")
if updateErr != nil {
fmt.Println("error updated")
panic(fmt.Errorf("Failed to update: %v", updateErr))
}
} else {
fmt.Printf("c.State.Terminated %s \n", c.State.Terminated.Reason)
//fmt.Println("Not finished ready!!!")
//fmt.Printf("c.State.Running %s \n", c.State.Running)
//fmt.Printf("c.State.Waiting %s \n", c.State.Waiting)
}
}
}
}
}
time.Sleep(10 * time.Second)
}
}还有一些原木:
single-condition-pipeline-9rqrs-1224102659
pod.Name single-condition-pipeline-9rqrs-1224102659
Status.Phase Running
PodIP XXXXXXXXXXXX
c.Name main
---------------------------------------------------------------------------------------------
c.State {nil &ContainerStateRunning{StartedAt:2021-10-29 04:41:51 +0000 UTC,} nil}
c.State.Terminated nil
c.State.Terminated nil
stateRunning Reason: *v1.ContainerStateRunning
update values
getPodName %!d(string=single-condition-pipeline-9rqrs-1224102659)
updated status phase
len get container 2
commit update
c.Name wait
c.State {nil &ContainerStateRunning{StartedAt:2021-10-29 04:41:51 +0000 UTC,} nil}
c.State.Terminated nil
c.State.Terminated nil
stateRunning Reason: *v1.ContainerStateRunning
update values
getPodName %!d(string=single-condition-pipeline-9rqrs-1224102659)
updated status phase
len get container 2
---------------------------------------------------------------------------------------------
commit update
---------------------------------------------------------------------------------------------
get pods metadatada
single-condition-pipeline-9rqrs-1224102659
pod.Name single-condition-pipeline-9rqrs-1224102659
Status.Phase Running
PodIP XXXXXXXXXX
c.Name main
c.State {nil &ContainerStateRunning{StartedAt:2021-10-29 04:41:51 +0000 UTC,} nil}
c.State.Terminated nil
c.State.Terminated nil
stateRunning Reason: *v1.ContainerStateRunning
update values
getPodName %!d(string=single-condition-pipeline-9rqrs-1224102659)
updated status phase
len get container 2
commit update
c.Name wait
c.State {nil &ContainerStateRunning{StartedAt:2021-10-29 04:41:51 +0000 UTC,} nil}
c.State.Terminated nil
c.State.Terminated nil
stateRunning Reason: *v1.ContainerStateRunning
update values
getPodName %!d(string=single-condition-pipeline-9rqrs-1224102659)
updated status phase
len get container 2
commit update所以在这里:https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#pod-readiness-status,它提到了一个修补程序,但是我不知道如何使用它,所以如果有人可以帮助我,或者有其他方法完成它。
发布于 2021-10-29 12:01:08
您不能在Pod status字段中设置status或其他任何东西,这就是只读。根据吊舱生命周期文档,在“pod中的所有容器都已成功终止,并且不会重新启动”之后,Pod将有一个阶段的Succeeded。因此,只有当您可以使用状态代码0使所有pod容器退出时才会发生这种情况,如果pod restartPolicy设置为onFailure或Never,如果它被设置为Always (默认值),那么容器最终将重新启动,您的吊舱将最终返回到Running阶段。
总之,您不能通过Kube直接做您想要做的事情。你必须:
Succeeded阶段的Succeeded。SIGINT或SIGTERM,也可能是通过自己的API命令它。https://stackoverflow.com/questions/69765180
复制相似问题