diff --git a/modules/scheduler/executor/plugins/k8s/k8s.go b/modules/scheduler/executor/plugins/k8s/k8s.go index ebfd86a909e..34941dab2aa 100644 --- a/modules/scheduler/executor/plugins/k8s/k8s.go +++ b/modules/scheduler/executor/plugins/k8s/k8s.go @@ -986,7 +986,7 @@ func (k *Kubernetes) getStatelessStatus(ctx context.Context, sg *apistructs.Serv isReady = false resultStatus.Status = apistructs.StatusProgressing sg.Services[i].Status = apistructs.StatusProgressing - podstatuses, err := k.pod.GetNamespacedPodsStatus(pods.Items) + podstatuses, err := k.pod.GetNamespacedPodsStatus(pods.Items, sg.Services[i].Name) if err != nil { logrus.Errorf("failed to get pod unready reasons, namespace: %v, name: %s, %v", sg.Services[i].Namespace, diff --git a/modules/scheduler/executor/plugins/k8s/pod/pod.go b/modules/scheduler/executor/plugins/k8s/pod/pod.go index 87b1fba76ef..abed370a066 100644 --- a/modules/scheduler/executor/plugins/k8s/pod/pod.go +++ b/modules/scheduler/executor/plugins/k8s/pod/pod.go @@ -20,6 +20,7 @@ import ( "encoding/json" "fmt" "io/ioutil" + "strings" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -369,10 +370,13 @@ type PodStatus struct { Message string } -func (p *Pod) GetNamespacedPodsStatus(pods []apiv1.Pod) ([]PodStatus, error) { +func (p *Pod) GetNamespacedPodsStatus(pods []apiv1.Pod, serviceName string) ([]PodStatus, error) { r := []PodStatus{} for _, pod := range pods { + if serviceName == "" || !strings.Contains(pod.Name, serviceName) { + continue + } reason, message := p.UnreadyPodReason(&pod) switch reason { case None: diff --git a/modules/scheduler/executor/plugins/k8s/pod/pod_test.go b/modules/scheduler/executor/plugins/k8s/pod/pod_test.go new file mode 100644 index 00000000000..1305522a644 --- /dev/null +++ b/modules/scheduler/executor/plugins/k8s/pod/pod_test.go @@ -0,0 +1,96 @@ +// Copyright (c) 2021 Terminus, Inc. +// +// This program is free software: you can use, redistribute, and/or modify +// it under the terms of the GNU Affero General Public License, version 3 +// or later ("AGPL"), as published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package pod + +import ( + "testing" + + "github.com/sirupsen/logrus" + "gotest.tools/assert" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestPod_GetNamespacedPodsStatus(t *testing.T) { + pod := Pod{} + pods := []corev1.Pod{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "test-pod", + }, + Spec: corev1.PodSpec{}, + Status: corev1.PodStatus{ + Phase: corev1.PodPending, + Conditions: []corev1.PodCondition{ + { + Type: corev1.PodInitialized, + Status: "True", + LastTransitionTime: metav1.Time{}, + }, + { + Type: corev1.ContainersReady, + Status: "False", + LastTransitionTime: metav1.Time{}, + Reason: "ContainersNotReady", + Message: "containers with unready status: [test-pod]", + }, + { + Type: corev1.PodScheduled, + Status: "True", + LastTransitionTime: metav1.Time{}, + }, + }, + Message: "containers with unready status: [test-pod]", + Reason: "ContainersNotReady", + HostIP: "10.1.1.1", + PodIP: "9.8.7.6", + PodIPs: []corev1.PodIP{ + { + IP: "9.8.7.6", + }, + }, + ContainerStatuses: []corev1.ContainerStatus{ + { + Image: "test-pod:v1", + Name: "test-pod", + State: corev1.ContainerState{ + Waiting: &corev1.ContainerStateWaiting{ + Reason: "ImagePullBackOff", + Message: "Back-off pulling image \"test:v1\"", + }, + }, + }, + }, + StartTime: &metav1.Time{}, + }, + }, + } + podStatus, err := pod.GetNamespacedPodsStatus(pods, "") + if err != nil { + logrus.Fatal(err) + } + assert.DeepEqual(t, podStatus, []PodStatus{}) + + podStatus, err = pod.GetNamespacedPodsStatus(pods, "test-pod") + if err != nil { + logrus.Fatal(err) + } + assert.DeepEqual(t, podStatus, []PodStatus{ + { + Reason: ImagePullFailed, + Message: "Back-off pulling image \"test:v1\"", + }, + }) + +}