github.com/alibaba/sealer@v0.8.6-0.20220430115802-37a2bdaa8173/pkg/checker/pod_checker.go (about) 1 // Copyright © 2021 Alibaba Group Holding Ltd. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package checker 16 17 import ( 18 "text/template" 19 20 corev1 "k8s.io/api/core/v1" 21 22 "github.com/alibaba/sealer/common" 23 "github.com/alibaba/sealer/logger" 24 "github.com/alibaba/sealer/pkg/client/k8s" 25 v2 "github.com/alibaba/sealer/types/api/v2" 26 ) 27 28 type PodChecker struct { 29 client *k8s.Client 30 } 31 32 type PodNamespaceStatus struct { 33 NamespaceName string 34 RunningCount uint32 35 NotRunningCount uint32 36 PodCount uint32 37 NotRunningPodList []*corev1.Pod 38 } 39 40 var PodNamespaceStatusList []PodNamespaceStatus 41 42 func (n *PodChecker) Check(cluster *v2.Cluster, phase string) error { 43 if phase != PhasePost { 44 return nil 45 } 46 c, err := k8s.Newk8sClient() 47 if err != nil { 48 return err 49 } 50 n.client = c 51 52 namespacePodList, err := n.client.ListAllNamespacesPods() 53 if err != nil { 54 return err 55 } 56 for _, podNamespace := range namespacePodList { 57 var runningCount uint32 = 0 58 var notRunningCount uint32 = 0 59 var podCount uint32 60 var notRunningPodList []*corev1.Pod 61 for _, pod := range podNamespace.PodList.Items { 62 if err := getPodReadyStatus(pod); err != nil { 63 notRunningCount++ 64 newPod := pod 65 notRunningPodList = append(notRunningPodList, &newPod) 66 } else { 67 runningCount++ 68 } 69 } 70 podCount = runningCount + notRunningCount 71 podNamespaceStatus := PodNamespaceStatus{ 72 NamespaceName: podNamespace.Namespace.Name, 73 RunningCount: runningCount, 74 NotRunningCount: notRunningCount, 75 PodCount: podCount, 76 NotRunningPodList: notRunningPodList, 77 } 78 PodNamespaceStatusList = append(PodNamespaceStatusList, podNamespaceStatus) 79 } 80 err = n.Output(PodNamespaceStatusList) 81 if err != nil { 82 return err 83 } 84 return nil 85 } 86 87 func (n *PodChecker) Output(podNamespaceStatusList []PodNamespaceStatus) error { 88 t := template.New("pod_checker") 89 t, err := t.Parse( 90 `Cluster Pod Status 91 {{ range . -}} 92 Namespace: {{ .NamespaceName }} 93 RunningPod: {{ .RunningCount }}/{{ .PodCount }} 94 {{ if (gt .NotRunningCount 0) -}} 95 Not Running Pod List: 96 {{- range .NotRunningPodList }} 97 PodName: {{ .Name }} 98 {{- end }} 99 {{ end }} 100 {{- end }} 101 `) 102 if err != nil { 103 panic(err) 104 } 105 t = template.Must(t, err) 106 err = t.Execute(common.StdOut, podNamespaceStatusList) 107 if err != nil { 108 logger.Error("pod checkers template can not excute %s", err) 109 return err 110 } 111 return nil 112 } 113 114 func getPodReadyStatus(pod corev1.Pod) error { 115 for _, condition := range pod.Status.Conditions { 116 if condition.Type == "Ready" { 117 if condition.Status == "True" { 118 return nil 119 } 120 } 121 } 122 return &NotFindReadyTypeError{} 123 } 124 125 func NewPodChecker() Interface { 126 return &PodChecker{} 127 }