-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpod.go
50 lines (44 loc) · 1.35 KB
/
pod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package libs
import (
"context"
"io"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
)
// WaitForPodCondition waits for the specified condition type with the given status.
func WaitForPodCondition(client *kubernetes.Clientset, namespace, name string, conditionType corev1.PodConditionType, status corev1.ConditionStatus, timeout time.Duration) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return wait.PollImmediate(1*time.Second, timeout, func() (bool, error) {
pod, err := client.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return false, err
}
for _, condition := range pod.Status.Conditions {
if condition.Type == conditionType && condition.Status == status {
return true, nil
}
}
return false, nil
})
}
func GetPodLog(clientset *kubernetes.Clientset, namespace, podName, containerName string) (string, error) {
req := clientset.CoreV1().Pods(namespace).GetLogs(podName, &corev1.PodLogOptions{
Container: containerName,
})
logs, err := req.Stream(context.Background())
if err != nil {
return "", err
}
defer logs.Close()
buf := new(strings.Builder)
_, err = io.Copy(buf, logs)
if err != nil {
return "", err
}
return buf.String(), nil
}