This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
watch.go
219 lines (191 loc) · 5.27 KB
/
watch.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
package k8s
import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
metav1 "github.com/ericchiang/k8s/apis/meta/v1"
"github.com/ericchiang/k8s/runtime"
"github.com/golang/protobuf/proto"
)
// Decode events from a watch stream.
//
// See: https://github.com/kubernetes/community/blob/master/contributors/design-proposals/protobuf.md#streaming-wire-format
// Watcher receives a stream of events tracking a particular resource within
// a namespace or across all namespaces.
//
// Watcher does not automatically reconnect. If a watch fails, a new watch must
// be initialized.
type Watcher struct {
watcher interface {
Next(Resource) (string, error)
Close() error
}
}
// Next decodes the next event from the watch stream. Errors are fatal, and
// indicate that the watcher should no longer be used, and must be recreated.
func (w *Watcher) Next(r Resource) (string, error) {
return w.watcher.Next(r)
}
// Close closes the active connection with the API server being used for
// the watch.
func (w *Watcher) Close() error {
return w.watcher.Close()
}
type watcherJSON struct {
d *json.Decoder
c io.Closer
}
func (w *watcherJSON) Close() error {
return w.c.Close()
}
func (w *watcherJSON) Next(r Resource) (string, error) {
var event struct {
Type string `json:"type"`
Object json.RawMessage `json:"object"`
}
if err := w.d.Decode(&event); err != nil {
return "", fmt.Errorf("decode event: %v", err)
}
if event.Type == "" {
return "", errors.New("watch event had no type field")
}
if event.Type == EventError {
status := &metav1.Status{}
if err := json.Unmarshal([]byte(event.Object), status); err != nil {
return "", fmt.Errorf("decoding event error: %v", err)
}
return event.Type, &APIError{
Status: status,
Code: int(*status.Code),
}
}
if err := json.Unmarshal([]byte(event.Object), r); err != nil {
return "", fmt.Errorf("decode resource: %v", err)
}
return event.Type, nil
}
type watcherPB struct {
r io.ReadCloser
}
func (w *watcherPB) Next(r Resource) (string, error) {
msg, ok := r.(proto.Message)
if !ok {
return "", errors.New("object was not a protobuf message")
}
event, unknown, err := w.next()
if err != nil {
return "", err
}
if event.Type == nil || *event.Type == "" {
return "", errors.New("watch event had no type field")
}
if *event.Type == EventError {
status := &metav1.Status{}
if err := proto.Unmarshal(unknown.Raw, status); err != nil {
return "", fmt.Errorf("decoding event error: %v", err)
}
return *event.Type, &APIError{
Status: status,
Code: int(*status.Code),
}
}
if err := proto.Unmarshal(unknown.Raw, msg); err != nil {
return "", err
}
return *event.Type, nil
}
func (w *watcherPB) Close() error {
return w.r.Close()
}
func (w *watcherPB) next() (*metav1.WatchEvent, *runtime.Unknown, error) {
length := make([]byte, 4)
if _, err := io.ReadFull(w.r, length); err != nil {
return nil, nil, err
}
body := make([]byte, int(binary.BigEndian.Uint32(length)))
if _, err := io.ReadFull(w.r, body); err != nil {
return nil, nil, fmt.Errorf("read frame body: %v", err)
}
var event metav1.WatchEvent
if err := proto.Unmarshal(body, &event); err != nil {
return nil, nil, err
}
if event.Object == nil {
return nil, nil, fmt.Errorf("event had no underlying object")
}
unknown, err := parseUnknown(event.Object.Raw)
if err != nil {
return nil, nil, err
}
return &event, unknown, nil
}
var unknownPrefix = []byte{0x6b, 0x38, 0x73, 0x00}
func parseUnknown(b []byte) (*runtime.Unknown, error) {
if !bytes.HasPrefix(b, unknownPrefix) {
return nil, errors.New("bytes did not start with expected prefix")
}
var u runtime.Unknown
if err := proto.Unmarshal(b[len(unknownPrefix):], &u); err != nil {
return nil, err
}
return &u, nil
}
// Watch creates a watch on a resource. It takes an example Resource to
// determine what endpoint to watch.
//
// Watch does not automatically reconnect. If a watch fails, a new watch must
// be initialized.
//
// // Watch configmaps in the "kube-system" namespace
// var configMap corev1.ConfigMap
// watcher, err := client.Watch(ctx, "kube-system", &configMap)
// if err != nil {
// // handle error
// }
// defer watcher.Close() // Always close the returned watcher.
//
// for {
// cm := new(corev1.ConfigMap)
// eventType, err := watcher.Next(cm)
// if err != nil {
// // watcher encountered and error, exit or create a new watcher
// }
// fmt.Println(eventType, *cm.Metadata.Name)
// }
//
func (c *Client) Watch(ctx context.Context, namespace string, r Resource, options ...Option) (*Watcher, error) {
url, err := resourceWatchURL(c.Endpoint, namespace, r, options...)
if err != nil {
return nil, err
}
ct := contentTypeFor(r)
req, err := c.newRequest(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", ct)
resp, err := c.client().Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode/100 != 2 {
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return nil, err
}
return nil, newAPIError(resp.Header.Get("Content-Type"), resp.StatusCode, body)
}
if ct == contentTypePB {
return &Watcher{&watcherPB{r: resp.Body}}, nil
}
return &Watcher{&watcherJSON{
d: json.NewDecoder(resp.Body),
c: resp.Body,
}}, nil
}