Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated cherry pick of #1431: fix(service): list specify pod for status #1495

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion modules/scheduler/executor/plugins/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion modules/scheduler/executor/plugins/k8s/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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:
Expand Down
96 changes: 96 additions & 0 deletions modules/scheduler/executor/plugins/k8s/pod/pod_test.go
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.

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\"",
},
})

}