-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpod.go
143 lines (128 loc) · 3.46 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
package kubernetes
import (
"io"
"net/http"
"git.containerum.net/ch/kube-api/pkg/kubeerrors"
log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/kubernetes/pkg/api/legacyscheme"
)
//TODO: Imp struct to GetPodLogs func
type LogOptions struct {
Container string
Tail int64
Follow bool
Previous bool
}
type ExecOptions struct {
Container string
Command []string
Stdin io.Reader
Stdout, Stderr io.Writer
TTY bool
TerminalSizeQueue remotecommand.TerminalSizeQueue
}
//GetPodList returns pods list
func (k *Kube) GetPodList(ns string, owner string) (interface{}, error) {
pods, err := k.CoreV1().Pods(ns).List(meta_v1.ListOptions{
LabelSelector: getOwnerLabel(owner),
})
if err != nil {
log.WithFields(log.Fields{
"Namespace": ns,
"Owner": owner,
}).Error(err)
return nil, err
}
return pods, nil
}
//GetPod returns pod
func (k *Kube) GetPod(ns string, po string) (interface{}, error) {
pod, err := k.CoreV1().Pods(ns).Get(po, meta_v1.GetOptions{})
if err != nil {
log.WithFields(log.Fields{
"Namespace": ns,
"Pod": po,
}).Error(err)
return nil, err
}
return pod, nil
}
func (k *Kube) GetPodListByDeployment(ns string, deploy string) (interface{}, error) {
pods, err := k.CoreV1().Pods(ns).List(meta_v1.ListOptions{
LabelSelector: getDeploymentLabel(deploy),
})
if err != nil {
log.WithFields(log.Fields{
"Namespace": ns,
"Deployment": deploy,
}).Error(err)
return nil, err
}
return pods, nil
}
//DeletePod deletes pod
func (k *Kube) DeletePod(ns string, po string) error {
err := k.CoreV1().Pods(ns).Delete(po, &meta_v1.DeleteOptions{})
if err != nil {
log.WithFields(log.Fields{
"Namespace": ns,
"Pod": po,
}).Error(err)
return err
}
return nil
}
//GetPodLogs attaches client to pod log
func (k *Kube) GetPodLogs(ns string, po string, opt *LogOptions) (io.ReadCloser, error) {
req := k.CoreV1().Pods(ns).GetLogs(po, &v1.PodLogOptions{
TailLines: &opt.Tail,
Follow: opt.Follow,
Previous: opt.Previous,
Container: opt.Container,
})
return req.Stream()
}
func (k *Kube) Exec(ns string, po string, opt *ExecOptions) error {
// logic taken from "kubectl exec" command
pod, err := k.CoreV1().Pods(ns).Get(po, meta_v1.GetOptions{})
if err != nil {
return err
}
if pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed {
return kubeerrors.ErrRequestValidationFailed().
AddDetailF("cannot exec into a container in a completed pod; current phase is %s", pod.Status.Phase)
}
containerName := opt.Container
if len(containerName) == 0 {
containerName = pod.Spec.Containers[0].Name
}
req := k.RESTClient().
Post().
Resource("pods").
Name(pod.Name).
Namespace(pod.Namespace).
SubResource("exec").
Param("container", containerName)
req.VersionedParams(&v1.PodExecOptions{
Container: containerName,
Command: opt.Command,
Stdin: opt.Stdin != nil,
Stdout: opt.Stdout != nil,
Stderr: opt.Stderr != nil,
TTY: opt.TTY,
}, legacyscheme.ParameterCodec)
executor, err := remotecommand.NewSPDYExecutor(k.config, http.MethodPost, req.URL())
if err != nil {
return err
}
return executor.Stream(remotecommand.StreamOptions{
Stdin: opt.Stdin,
Stdout: opt.Stdout,
Stderr: opt.Stderr,
Tty: opt.TTY,
TerminalSizeQueue: opt.TerminalSizeQueue,
})
}