-
Notifications
You must be signed in to change notification settings - Fork 8
/
pods.go
208 lines (188 loc) · 4.88 KB
/
pods.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package helpers
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
)
// PodHelper defines helper methods for handling Pods
type PodHelper interface {
// WaitPodRunning waits for the Pod to be running for up to given timeout and returns a boolean indicating
// if the status was reached. If the pod is Failed returns error.
WaitPodRunning(name string, timeout time.Duration) (bool, error)
// Exec executes a non-interactive command described in options and returns the stdout and stderr outputs
Exec(pod string, container string, command []string, stdin []byte) ([]byte, []byte, error)
// AttachEphemeralContainer adds an ephemeral container to a running pod, waiting for up to
// a given timeout until the container is running
AttachEphemeralContainer(podName string, container corev1.EphemeralContainer, timeout time.Duration) error
}
// podConditionChecker defines a function that checks if a pod satisfies a condition
type podConditionChecker func(*corev1.Pod) (bool, error)
// waitForCondition watches a Pod in a namespace until a podConditionChecker is satisfied or a timeout expires
func (h *helpers) waitForCondition(
namespace string,
name string,
timeout time.Duration,
checker podConditionChecker,
) (bool, error) {
selector := fields.Set{
"metadata.name": name,
}.AsSelector()
watcher, err := h.client.CoreV1().Pods(namespace).Watch(
h.ctx,
metav1.ListOptions{
FieldSelector: selector.String(),
},
)
if err != nil {
return false, err
}
defer watcher.Stop()
expired := time.After(timeout)
for {
select {
case <-expired:
return false, nil
case event := <-watcher.ResultChan():
if event.Type == watch.Error {
return false, fmt.Errorf("error watching for pod: %v", event.Object)
}
if event.Type == watch.Modified {
pod, isPod := event.Object.(*corev1.Pod)
if !isPod {
return false, errors.New("received unknown object while watching for pods")
}
condition, err := checker(pod)
if condition || err != nil {
return condition, err
}
}
}
}
}
func (h *helpers) WaitPodRunning(name string, timeout time.Duration) (bool, error) {
return h.waitForCondition(
h.namespace,
name,
timeout,
func(pod *corev1.Pod) (bool, error) {
if pod.Status.Phase == corev1.PodFailed {
return false, errors.New("pod has failed")
}
if pod.Status.Phase == corev1.PodRunning {
return true, nil
}
return false, nil
},
)
}
func (h *helpers) Exec(
pod string,
container string,
command []string,
stdin []byte,
) ([]byte, []byte, error) {
req := h.client.CoreV1().RESTClient().
Post().
Namespace(h.namespace).
Resource("pods").
Name(pod).
SubResource("exec").
VersionedParams(&corev1.PodExecOptions{
Container: container,
Command: command,
Stdin: true,
Stdout: true,
Stderr: true,
TTY: false,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(h.config, "POST", req.URL())
if err != nil {
return nil, nil, err
}
var stdout, stderr bytes.Buffer
err = exec.Stream(remotecommand.StreamOptions{
Stdin: bytes.NewReader(stdin),
Stdout: &stdout,
Stderr: &stderr,
Tty: true,
})
if err != nil {
return nil, nil, err
}
return stdout.Bytes(), stderr.Bytes(), nil
}
func (h *helpers) AttachEphemeralContainer(
podName string,
container corev1.EphemeralContainer,
timeout time.Duration,
) error {
pod, err := h.client.CoreV1().Pods(h.namespace).Get(
h.ctx,
podName,
metav1.GetOptions{},
)
if err != nil {
return err
}
podJSON, err := json.Marshal(pod)
if err != nil {
return err
}
updatedPod := pod.DeepCopy()
updatedPod.Spec.EphemeralContainers = append(updatedPod.Spec.EphemeralContainers, container)
updateJSON, err := json.Marshal(updatedPod)
if err != nil {
return err
}
patch, err := strategicpatch.CreateTwoWayMergePatch(podJSON, updateJSON, pod)
if err != nil {
return err
}
_, err = h.client.CoreV1().Pods(h.namespace).Patch(
h.ctx,
pod.Name,
types.StrategicMergePatchType,
patch,
metav1.PatchOptions{},
"ephemeralcontainers",
)
if err != nil {
return err
}
if timeout == 0 {
return nil
}
running, err := h.waitForCondition(
h.namespace,
podName,
timeout,
checkEphemeralContainerState,
)
if err != nil {
return err
}
if !running {
return fmt.Errorf("ephemeral container has not started after %fs", timeout.Seconds())
}
return nil
}
func checkEphemeralContainerState(pod *corev1.Pod) (bool, error) {
if pod.Status.EphemeralContainerStatuses != nil {
for _, cs := range pod.Status.EphemeralContainerStatuses {
if cs.State.Running != nil {
return true, nil
}
}
}
return false, nil
}