forked from coreos/krud
/
main.go
312 lines (286 loc) · 7.13 KB
/
main.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"encoding/json"
"flag"
"fmt"
"html/template"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
"time"
"github.com/coreos/pkg/flagutil"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client"
"k8s.io/kubernetes/pkg/kubectl"
)
func main() {
fs := flag.NewFlagSet("krud", flag.ExitOnError)
listen := fs.String("listen", ":9500", "")
deploymentKey := fs.String("deployment-key", "deployment", "Key to use to differentiate between two different controllers.")
controllerName := fs.String("controller-name", "", "Name of the replication controller to update.")
namespace := fs.String("namespace", api.NamespaceDefault, "Namespace the replication controller.")
k8sEndpoint := fs.String("k8s-endpoint", "http://localhost:8080", "URL of the Kubernetes API server")
if err := fs.Parse(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
if err := flagutil.SetFlagsFromEnv(fs, "KRUD"); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
if *deploymentKey == "" || *controllerName == "" {
panic("missing deployment key or controller name")
}
k := &Krud{
DeploymentKey: *deploymentKey,
ControllerName: *controllerName,
Endpoint: *k8sEndpoint,
Namespace: *namespace,
}
http.HandleFunc("/push", k.push)
http.HandleFunc("/", k.view)
log.Fatal(k.listen(*listen))
}
type Krud struct {
DeploymentKey string
ControllerName string
Namespace string
Endpoint string
// Hooks contains all incoming webhooks
Hooks []*Webhook
// Next is the next-to-update webhook, nil for none. Multiple attempts will
// use the most recently received hook.
Next chan *Webhook
sync.Mutex
}
func (k *Krud) listen(listen string) error {
k.Next = make(chan *Webhook)
go k.start()
log.Println("serving on", listen)
return http.ListenAndServe(listen, nil)
}
func (k *Krud) start() {
for {
h := <-k.Next
Loop:
for {
select {
case c := <-k.Next:
if h.Received.Before(c.Received) {
h = c
}
default:
break Loop
}
}
if err := k.update(h); err != nil {
k.Lock()
h.UpdateError = err
k.Unlock()
}
}
}
type Webhook struct {
Data interface{}
Kind string
Source string
Received time.Time
// UpdateAttempt is set if an update was started for this hook.
UpdateAttempt bool
// UpdateID is the ID of this update, which is what the value of the deployment
// key is set to.
UpdateID string
// UpdateSuccess is true if the attempted update was successful.
UpdateSuccess bool
UpdateStart time.Time
UpdateEnd time.Time
UpdateStatus string
UpdateError error
}
func serveError(w http.ResponseWriter, err error) {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
type QuayWebhook struct {
DockerURL string `json:"docker_url"`
Homepage string
Name string
Namespace string
PrunedImageCount int `json:"pruned_image_count"`
PushedImageCount int `json:"pushed_image_count"`
Repository string
UpdatedTags struct {
Latest string
} `json:"updated_tags"`
Visibility string
}
type DockerWebhook struct {
CallbackURL string `json:"callback_url"`
PushData struct {
Images interface{} `json:"images"`
PushedAt int `json:"pushed_at"`
Pusher string `json:"pusher"`
} `json:"push_data"`
Repository struct {
CommentCount int `json:"comment_count"`
DateCreated int `json:"date_created"`
Description string `json:"description"`
FullDescription string `json:"full_description"`
IsOfficial bool `json:"is_official"`
IsPrivate bool `json:"is_private"`
IsTrusted bool `json:"is_trusted"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Owner string `json:"owner"`
RepoName string `json:"repo_name"`
RepoURL string `json:"repo_url"`
StarCount int `json:"star_count"`
Status string `json:"status"`
} `json:"repository"`
}
var (
viewFuncs = template.FuncMap{
"json": func(v interface{}) (string, error) {
b, err := json.MarshalIndent(v, "", " ")
return string(b), err
},
}
viewTemplate = template.Must(template.New("").Funcs(viewFuncs).Parse(indexHTML))
)
func (k *Krud) view(w http.ResponseWriter, r *http.Request) {
k.Lock()
defer k.Unlock()
err := viewTemplate.Execute(w, k)
if err != nil {
fmt.Println(err)
serveError(w, err)
}
}
func parseWebhook(r io.Reader) (v interface{}, kind string, err error) {
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, "", err
}
{
var d QuayWebhook
if err := json.Unmarshal(b, &d); err == nil {
return d, "quay.io", nil
}
}
{
var d DockerWebhook
if err := json.Unmarshal(b, &d); err == nil {
return d, "docker hub", nil
}
}
return nil, "", fmt.Errorf("unrecognized webhook")
}
func (k *Krud) push(w http.ResponseWriter, r *http.Request) {
d, kind, err := parseWebhook(r.Body)
if err != nil {
serveError(w, err)
return
}
k.Lock()
defer k.Unlock()
wh := &Webhook{
Data: &d,
Kind: kind,
Source: r.RemoteAddr,
Received: time.Now(),
}
k.Hooks = append(k.Hooks, wh)
go func() {
k.Next <- wh
}()
}
func (k *Krud) update(h *Webhook) error {
h.UpdateAttempt = true
h.UpdateStart = time.Now()
defer func() {
h.UpdateEnd = time.Now()
}()
conf := &client.Config{
Host: k.Endpoint,
}
client, err := client.New(conf)
if err != nil {
return err
}
rcs := client.ReplicationControllers(k.Namespace)
oldRc, err := rcs.Get(k.ControllerName)
if err != nil {
return err
}
newRc, err := rcs.Get(k.ControllerName)
if err != nil {
return err
}
hash, err := api.HashObject(oldRc, client.Codec)
if err != nil {
return err
}
h.UpdateID = hash
newRc.Name = fmt.Sprintf("%s-%s", k.ControllerName, hash)
newRc.ResourceVersion = ""
apply := func(key, value string, ms ...map[string]string) {
for _, m := range ms {
m[key] = value
}
}
apply(k.DeploymentKey, hash, newRc.Spec.Selector, newRc.Spec.Template.Labels)
apply("run", k.ControllerName, newRc.Spec.Selector, newRc.Spec.Template.Labels)
ruconf := kubectl.RollingUpdaterConfig{
Out: &lockBuffer{
k: k,
h: h,
},
OldRc: oldRc,
NewRc: newRc,
UpdatePeriod: time.Second * 3, // todo: change to time.Minute
Timeout: time.Minute * 5,
Interval: time.Second * 3,
UpdateAcceptor: kubectl.DefaultUpdateAcceptor,
CleanupPolicy: kubectl.RenameRollingUpdateCleanupPolicy,
}
ruc := kubectl.NewRollingUpdaterClient(client)
println("doing rolling update")
err = kubectl.NewRollingUpdater(k.Namespace, ruc).Update(&ruconf)
println("done")
k.Lock()
h.UpdateSuccess = err == nil
k.Unlock()
return err
}
type lockBuffer struct {
k *Krud
h *Webhook
}
func (l *lockBuffer) Write(p []byte) (n int, err error) {
l.k.Lock()
defer l.k.Unlock()
l.h.UpdateStatus += string(p)
fmt.Println("WRITE", string(p))
return len(p), nil
}
const indexHTML = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>krud</title>
</head>
<body>
{{range .Hooks}}
<div>
Err: {{.UpdateError}}
<br>Status: <pre>{{.UpdateStatus}}</pre>
<br>Value: <pre>{{. | json}}</pre>
</div>
<hr>
{{end}}
</body>
</html>
`