/
pod.go
149 lines (127 loc) · 2.9 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package k8s
import (
"bytes"
"io"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func GetPodList() (*apiv1.PodList, error) {
cli, err := GetClientSet()
if err != nil {
return nil, err
}
config, err := cli.CoreV1().Pods("").List(metav1.ListOptions{})
return config, err
}
func GetPodListByNamespace(namespace string) (*apiv1.PodList, error) {
cli, err := GetClientSet()
if err != nil {
return nil, err
}
config, err := cli.CoreV1().Pods(namespace).List(metav1.ListOptions{})
return config, err
}
func GetPodListByLabels(namespace, label string) (*apiv1.PodList, error) {
cli, err := GetClientSet()
if err != nil {
return nil, err
}
listoptions := metav1.ListOptions{
LabelSelector: label,
}
config, err := cli.CoreV1().Pods(namespace).List(listoptions)
return config, err
}
func GetPodByName(namespace, name string) (*apiv1.Pod, error) {
cli, err := GetClientSet()
if err != nil {
return nil, err
}
config, err := cli.CoreV1().Pods(namespace).Get(name, metav1.GetOptions{})
return config, err
}
func DeletePod(namespace, name string) error {
cli, err := GetClientSet()
if err != nil {
return err
}
err = cli.CoreV1().Pods(namespace).Delete(name, &metav1.DeleteOptions{})
return err
}
func GetPodLogByPodId(namespace, podid string) (string, error) {
var result string
f := func(s int64) *int64 {
return &s
}
cli, err := GetClientSet()
if err != nil {
return result, err
}
options := apiv1.PodLogOptions{
Follow: false,
TailLines: f(1000),
}
req := cli.CoreV1().Pods(namespace).GetLogs(podid, &options)
readCloser, err := req.Stream()
if err != nil {
return result, err
}
defer readCloser.Close()
var out bytes.Buffer
_, err = io.Copy(&out, readCloser)
if err != nil {
return result, err
}
result = out.String()
return result, nil
}
func GetPodLogByPodIdByNum(namespace, podid string, num int64) (string, error) {
var result string
f := func(s int64) *int64 {
return &s
}
cli, err := GetClientSet()
if err != nil {
return result, err
}
options := apiv1.PodLogOptions{
Follow: false,
SinceSeconds: f(num),
}
req := cli.CoreV1().Pods(namespace).GetLogs(podid, &options)
readCloser, err := req.Stream()
if err != nil {
return result, err
}
defer readCloser.Close()
var out bytes.Buffer
_, err = io.Copy(&out, readCloser)
if err != nil {
return result, err
}
result = out.String()
return result, nil
}
func GetPodLogByPodIdAll(namespace, podid string) (string, error) {
var result string
cli, err := GetClientSet()
if err != nil {
return result, err
}
options := apiv1.PodLogOptions{
Follow: false,
}
req := cli.CoreV1().Pods(namespace).GetLogs(podid, &options)
readCloser, err := req.Stream()
if err != nil {
return result, err
}
defer readCloser.Close()
var out bytes.Buffer
_, err = io.Copy(&out, readCloser)
if err != nil {
return result, err
}
result = out.String()
return result, nil
}