pod.go 2.5 KB
Newer Older
L
liqingping 已提交
1 2 3 4
package testutils

import (
	"context"
L
liqingping 已提交
5 6 7 8
	"fmt"
	"io"
	"strings"
	"time"
L
liqingping 已提交
9 10 11 12

	corev1 "k8s.io/api/core/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/types"
L
liqingping 已提交
13 14
	"k8s.io/apimachinery/pkg/util/wait"
	"k8s.io/client-go/kubernetes"
L
liqingping 已提交
15 16
	"sigs.k8s.io/controller-runtime/pkg/client"

L
liqingping 已提交
17
	dicommon "opendilab.org/di-orchestrator/common"
L
liqingping 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
)

func NewPod(name, jobName string, ownRefer metav1.OwnerReference) *corev1.Pod {
	pod := &corev1.Pod{
		TypeMeta: metav1.TypeMeta{
			APIVersion: "v1",
			Kind:       "Pod",
		},
		ObjectMeta: metav1.ObjectMeta{
			Name:      name,
			Namespace: DIJobNamespace,
		},
		Spec: corev1.PodSpec{
			Containers: []corev1.Container{
				{
L
liqingping 已提交
33
					Name:    dicommon.DefaultContainerName,
L
liqingping 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
					Image:   DIJobImage,
					Command: []string{"/bin/sh", "-c", "sleep", DefaultSleepDuration},
				},
			},
		},
	}
	pod.SetOwnerReferences([]metav1.OwnerReference{ownRefer})
	return pod
}

func UpdatePodPhase(ctx context.Context, k8sClient client.Client, podKey types.NamespacedName, phase corev1.PodPhase) error {
	var pod corev1.Pod
	err := k8sClient.Get(ctx, podKey, &pod)
	if err != nil {
		return err
	}
L
liqingping 已提交
50 51

	containerName := pod.Spec.Containers[0].Name
L
liqingping 已提交
52
	pod.Status.Phase = phase
L
liqingping 已提交
53 54 55 56 57 58 59 60
	if phase == corev1.PodRunning {
		state := corev1.ContainerStateRunning{}
		cstatus := corev1.ContainerStatus{Name: containerName, State: corev1.ContainerState{
			Running: &state,
		}}
		pod.Status.ContainerStatuses = append(pod.Status.ContainerStatuses, cstatus)
	}

L
liqingping 已提交
61 62 63 64 65 66
	err = k8sClient.Status().Update(ctx, &pod, &client.UpdateOptions{})
	if err != nil {
		return err
	}
	return nil
}
L
liqingping 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

func GetPodLogs(clientSet *kubernetes.Clientset,
	namespace string, podName string, containerName string, follow bool, message string, timeout time.Duration) error {
	count := int64(100)
	podLogOptions := corev1.PodLogOptions{
		Container: containerName,
		Follow:    follow,
		TailLines: &count,
	}

	podLogRequest := clientSet.CoreV1().
		Pods(namespace).
		GetLogs(podName, &podLogOptions)
	stream, err := podLogRequest.Stream(context.TODO())
	if err != nil {
		return err
	}
	defer stream.Close()

	err = wait.Poll(50*time.Millisecond, timeout, func() (bool, error) {
		buf := make([]byte, 2000)
		numBytes, err := stream.Read(buf)
		if numBytes == 0 {
			return false, nil
		}
		if err == io.EOF {
			return false, fmt.Errorf("can't find related logs '%s' from pod %s", message, podName)
		}
		if err != nil {
			return false, err
		}
		logs := string(buf[:numBytes])
		if strings.Contains(logs, message) {
			return true, nil
		}
		return false, nil
	})
	if err != nil {
		return err
	}
	return nil
}