[WIP] K8s update and runtime package changes (#895)
* First commit: outline of K8s runtime package * Added poller. Added auto-updater into default runtime * Added build and updated Poller interface * Added comments and NewRuntime that accepts Options * DefaultPoller; Runtime options * First commit to add Kubernetes cruft * Add comments * Add micro- prefix to K8s runtime service names * Get rid of import cycles. Move K8s runtime into main runtime package * Major refactoring: Poller replaced by Notifier POller has been replaced by Notifier which returns a channel of events that can be consumed and acted upon. * Added runtime configuration options * K8s runtime is now Kubernetes runtime in dedicated pkg. Naming kung-fu. * Fix typo in command. * Fixed typo * Dont Delete service when runtime stops. runtime.Stop stops services; no need to double-stop * Track runtime services * Parse Unix timestamps properly * Added deployments into K8s client. Debug logging
This commit is contained in:
92
runtime/kubernetes/client/watch/body.go
Normal file
92
runtime/kubernetes/client/watch/body.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package watch
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// bodyWatcher scans the body of a request for chunks
|
||||
type bodyWatcher struct {
|
||||
results chan Event
|
||||
stop chan struct{}
|
||||
res *http.Response
|
||||
req *http.Request
|
||||
}
|
||||
|
||||
// Changes returns the results channel
|
||||
func (wr *bodyWatcher) ResultChan() <-chan Event {
|
||||
return wr.results
|
||||
}
|
||||
|
||||
// Stop cancels the request
|
||||
func (wr *bodyWatcher) Stop() {
|
||||
select {
|
||||
case <-wr.stop:
|
||||
return
|
||||
default:
|
||||
close(wr.stop)
|
||||
close(wr.results)
|
||||
}
|
||||
}
|
||||
|
||||
func (wr *bodyWatcher) stream() {
|
||||
reader := bufio.NewReader(wr.res.Body)
|
||||
|
||||
// ignore first few messages from stream,
|
||||
// as they are usually old.
|
||||
ignore := true
|
||||
|
||||
go func() {
|
||||
<-time.After(time.Second)
|
||||
ignore = false
|
||||
}()
|
||||
|
||||
go func() {
|
||||
// stop the watcher
|
||||
defer wr.Stop()
|
||||
|
||||
for {
|
||||
// read a line
|
||||
b, err := reader.ReadBytes('\n')
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// ignore for the first second
|
||||
if ignore {
|
||||
continue
|
||||
}
|
||||
|
||||
// send the event
|
||||
var event Event
|
||||
if err := json.Unmarshal(b, &event); err != nil {
|
||||
continue
|
||||
}
|
||||
wr.results <- event
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// NewBodyWatcher creates a k8s body watcher for
|
||||
// a given http request
|
||||
func NewBodyWatcher(req *http.Request, client *http.Client) (Watch, error) {
|
||||
stop := make(chan struct{})
|
||||
req.Cancel = stop
|
||||
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
wr := &bodyWatcher{
|
||||
results: make(chan Event),
|
||||
stop: stop,
|
||||
req: req,
|
||||
res: res,
|
||||
}
|
||||
|
||||
go wr.stream()
|
||||
return wr, nil
|
||||
}
|
26
runtime/kubernetes/client/watch/watch.go
Normal file
26
runtime/kubernetes/client/watch/watch.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package watch
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
// Watch ...
|
||||
type Watch interface {
|
||||
Stop()
|
||||
ResultChan() <-chan Event
|
||||
}
|
||||
|
||||
// EventType defines the possible types of events.
|
||||
type EventType string
|
||||
|
||||
// EventTypes used
|
||||
const (
|
||||
Added EventType = "ADDED"
|
||||
Modified EventType = "MODIFIED"
|
||||
Deleted EventType = "DELETED"
|
||||
Error EventType = "ERROR"
|
||||
)
|
||||
|
||||
// Event represents a single event to a watched resource.
|
||||
type Event struct {
|
||||
Type EventType `json:"type"`
|
||||
Object json.RawMessage `json:"object"`
|
||||
}
|
71
runtime/kubernetes/client/watch/watch_test.go
Normal file
71
runtime/kubernetes/client/watch/watch_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package watch
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var actions = []string{
|
||||
`{"type": "create", "object":{"foo": "bar"}}`,
|
||||
`{"type": "delete", INVALID}`,
|
||||
`{"type": "update", "object":{"foo": {"foo": "bar"}}}`,
|
||||
`{"type": "delete", "object":null}`,
|
||||
}
|
||||
|
||||
func TestBodyWatcher(t *testing.T) {
|
||||
// set up server with handler to flush strings from ch.
|
||||
ch := make(chan string)
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
flusher, ok := w.(http.Flusher)
|
||||
if !ok {
|
||||
t.Fatal("expected ResponseWriter to be a flusher")
|
||||
}
|
||||
|
||||
fmt.Fprintf(w, "\n")
|
||||
flusher.Flush()
|
||||
|
||||
for v := range ch {
|
||||
fmt.Fprintf(w, "%s\n", v)
|
||||
flusher.Flush()
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
req, err := http.NewRequest("GET", ts.URL, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("did not expect NewRequest to return err: %v", err)
|
||||
}
|
||||
|
||||
// setup body watcher
|
||||
w, err := NewBodyWatcher(req, http.DefaultClient)
|
||||
if err != nil {
|
||||
t.Fatalf("did not expect NewBodyWatcher to return %v", err)
|
||||
}
|
||||
|
||||
<-time.After(time.Second)
|
||||
|
||||
// send action strings in, and expect result back
|
||||
ch <- actions[0]
|
||||
if r := <-w.ResultChan(); r.Type != "create" {
|
||||
t.Fatalf("expected result to be create")
|
||||
}
|
||||
|
||||
ch <- actions[1] // should be ignored as its invalid json
|
||||
ch <- actions[2]
|
||||
if r := <-w.ResultChan(); r.Type != "update" {
|
||||
t.Fatalf("expected result to be update")
|
||||
}
|
||||
|
||||
ch <- actions[3]
|
||||
if r := <-w.ResultChan(); r.Type != "delete" {
|
||||
t.Fatalf("expected result to be delete")
|
||||
}
|
||||
|
||||
// stop should clean up all channels.
|
||||
w.Stop()
|
||||
close(ch)
|
||||
}
|
Reference in New Issue
Block a user