-
Notifications
You must be signed in to change notification settings - Fork 5
/
tools.go
288 lines (271 loc) · 7.92 KB
/
tools.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package replicationcontroller
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
)
// IsReady check if the replicationcontroller is ready
func (h *Handler) IsReady(name string) bool {
checkGeneration := func(rc *corev1.ReplicationController) bool {
if rc.Generation != rc.Status.ObservedGeneration {
return false
}
return true
}
checkReplicas := func(rc *corev1.ReplicationController) bool {
if rc.Spec.Replicas == nil {
return false
}
if *rc.Spec.Replicas != rc.Status.ReadyReplicas {
return false
}
if *rc.Spec.Replicas != rc.Status.AvailableReplicas {
return false
}
if *rc.Spec.Replicas != rc.Status.Replicas {
return false
}
if *rc.Spec.Replicas != rc.Status.FullyLabeledReplicas {
return false
}
return true
}
rc, err := h.Get(name)
if err != nil {
return false
}
if checkGeneration(rc) && checkReplicas(rc) {
return true
}
return false
}
// WaitReady waiting for the replicationcontroller to be in the ready status.
func (h *Handler) WaitReady(name string) error {
if h.IsReady(name) {
return nil
}
errCh := make(chan error, 1)
chkCh := make(chan struct{}, 1)
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGQUIT)
ctxCheck, cancelCheck := context.WithCancel(h.ctx)
ctxWatch, cancelWatch := context.WithCancel(h.ctx)
defer cancelCheck()
defer cancelWatch()
// this goroutine used to check whether replicationcontroller is ready and exists.
// if replicationcontroller already ready, return nil.
// if replicationcontroller does not exist, return error.
// if replicationcontroller exists but not ready, return nothing.
go func(context.Context) {
<-chkCh
for i := 0; i < 6; i++ {
// replicationcontroller is already ready, return nil
if h.IsReady(name) {
errCh <- nil
return
}
// replicationcontroller no longer exists, return err
_, err := h.Get(name)
if k8serrors.IsNotFound(err) {
errCh <- err
return
}
// replicationcontroller exists but not ready, return nothing.
if err == nil {
return
}
log.Error(err)
time.Sleep(time.Second * 10)
}
}(ctxCheck)
// this goroutine used to watch replicationcontroller.
go func(ctx context.Context) {
for {
timeout := int64(0)
listOptions := metav1.SingleObject(metav1.ObjectMeta{Name: name, Namespace: h.namespace})
listOptions.TimeoutSeconds = &timeout
watcher, err := h.clientset.CoreV1().ReplicationControllers(h.namespace).Watch(h.ctx, listOptions)
if err != nil {
errCh <- err
return
}
chkCh <- struct{}{}
for event := range watcher.ResultChan() {
switch event.Type {
case watch.Modified:
if h.IsReady(name) {
watcher.Stop()
errCh <- nil
return
}
case watch.Deleted:
watcher.Stop()
errCh <- fmt.Errorf("replicationcontroller/%s was deleted", name)
return
case watch.Bookmark:
log.Debug("watch replicationcontroller: bookmark")
case watch.Error:
log.Debug("watch replicationcontroller: error")
}
}
// If event channel is closed, it means the kube-apiserver has closed the connection.
log.Debug("watch replicationcontroller: reconnect to kubernetes")
watcher.Stop()
}
}(ctxWatch)
select {
case sig := <-sigCh:
return fmt.Errorf("cancelled by signal: %s", sig.String())
case err := <-errCh:
return err
}
}
//// WaitReady wait for the replicationcontroller to be in the ready status
//func (h *Handler) WaitReady(name string, check bool) (err error) {
// var (
// watcher watch.Interface
// timeout = int64(0)
// )
// // 在 watch 之前就先判断 replicationcontroller 是否就绪, 如果就绪了就没必要 watch 了
// if h.IsReady(name) {
// return
// }
// // 是否判断 replicationcontroller 是否存在
// if check {
// if _, err = h.Get(name); err != nil {
// return
// }
// }
// for {
// // replicationcontroller 没有就绪, 那么就开始监听 replicationcontroller 的事件
// listOptions := metav1.SingleObject(metav1.ObjectMeta{Name: name, Namespace: h.namespace})
// listOptions.TimeoutSeconds = &timeout
// watcher, err = h.clientset.CoreV1().ReplicationControllers(h.namespace).Watch(h.ctx, listOptions)
// for event := range watcher.ResultChan() {
// switch event.Type {
// case watch.Modified:
// if h.IsReady(name) {
// watcher.Stop()
// return
// }
// case watch.Deleted:
// watcher.Stop()
// return fmt.Errorf("%s deleted", name)
// case watch.Bookmark:
// log.Debug("watch replicationcontroller: bookmark")
// case watch.Error:
// log.Debug("watch replicationcontroller: error")
// }
// }
// // watcher 因为 keepalive 超时断开了连接, 关闭了 channel
// log.Debug("watch replicationcontroller: reconnect to kubernetes")
// watcher.Stop()
// }
//}
// GetPods
// GetPVC
// GetPV
// GetAge
// GetPods get replicationcontroller all pods
func (h *Handler) GetPods(object interface{}) ([]*corev1.Pod, error) {
switch val := object.(type) {
case string:
rc, err := h.Get(val)
if err != nil {
return nil, err
}
return h.getPods(rc)
case *corev1.ReplicationController:
return h.getPods(val)
case corev1.ReplicationController:
return h.getPods(&val)
default:
return nil, ErrInvalidToolsType
}
}
func (h *Handler) getPods(rc *corev1.ReplicationController) ([]*corev1.Pod, error) {
listOptions := h.Options.ListOptions.DeepCopy()
listOptions.LabelSelector = ""
podList, err := h.clientset.CoreV1().Pods(h.namespace).List(h.ctx, *listOptions)
if err != nil {
return nil, err
}
var pl []*corev1.Pod
for i := range podList.Items {
for _, or := range podList.Items[i].OwnerReferences {
if or.Name == rc.Name {
pl = append(pl, &podList.Items[i])
}
}
}
return pl, nil
}
// GetPVC get all persistentvolumeclaims mounted by the replicationcontroller.
func (h *Handler) GetPVC(object interface{}) ([]string, error) {
switch val := object.(type) {
case string:
rc, err := h.Get(val)
if err != nil {
return nil, err
}
return h.getPVC(rc), nil
case *corev1.ReplicationController:
return h.getPVC(val), nil
case corev1.ReplicationController:
return h.getPVC(&val), nil
default:
return nil, ErrInvalidToolsType
}
}
func (h *Handler) getPVC(rc *corev1.ReplicationController) []string {
var pl []string
for _, volume := range rc.Spec.Template.Spec.Volumes {
if volume.PersistentVolumeClaim != nil {
pl = append(pl, volume.PersistentVolumeClaim.ClaimName)
}
}
return pl
}
// GetPV get all persistentvolumes mounted by the replicationcontroller.
func (h *Handler) GetPV(object interface{}) ([]string, error) {
// GetPV does not need to check whether replicationcontroller is exists.
// GetPVC will do it.
pvcList, err := h.GetPVC(object)
if err != nil {
return nil, err
}
var pl []string
for _, pvc := range pvcList {
pvcObj, err := h.clientset.CoreV1().
PersistentVolumeClaims(h.namespace).Get(h.ctx, pvc, h.Options.GetOptions)
if err == nil {
pl = append(pl, pvcObj.Spec.VolumeName)
}
}
return pl, nil
}
// GetAge returns replicationcontroller age.
func (h *Handler) GetAge(object interface{}) (time.Duration, error) {
switch val := object.(type) {
case string:
rc, err := h.Get(val)
if err != nil {
return time.Duration(0), err
}
return time.Now().Sub(rc.CreationTimestamp.Time), nil
case *corev1.ReplicationController:
return time.Now().Sub(val.CreationTimestamp.Time), nil
case corev1.ReplicationController:
return time.Now().Sub(val.CreationTimestamp.Time), nil
default:
return time.Duration(0), ErrInvalidToolsType
}
}