Refactor subscribe

This commit is contained in:
Darren Shepherd
2019-08-12 12:42:37 -07:00
parent e71d8fb0df
commit 01395a0ace
19 changed files with 882 additions and 184 deletions

1
go.sum
View File

@@ -68,6 +68,7 @@ github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c h1:eSfnfIuwhxZyULg1NNuZycJcYkjYVGYe7FczwQReM6U=
github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA=
github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

24
vendor/github.com/pkg/errors/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,24 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof

15
vendor/github.com/pkg/errors/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,15 @@
language: go
go_import_path: github.com/pkg/errors
go:
- 1.4.x
- 1.5.x
- 1.6.x
- 1.7.x
- 1.8.x
- 1.9.x
- 1.10.x
- 1.11.x
- tip
script:
- go test -v ./...

23
vendor/github.com/pkg/errors/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,23 @@
Copyright (c) 2015, Dave Cheney <dave@cheney.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

52
vendor/github.com/pkg/errors/README.md generated vendored Normal file
View File

@@ -0,0 +1,52 @@
# errors [![Travis-CI](https://travis-ci.org/pkg/errors.svg)](https://travis-ci.org/pkg/errors) [![AppVeyor](https://ci.appveyor.com/api/projects/status/b98mptawhudj53ep/branch/master?svg=true)](https://ci.appveyor.com/project/davecheney/errors/branch/master) [![GoDoc](https://godoc.org/github.com/pkg/errors?status.svg)](http://godoc.org/github.com/pkg/errors) [![Report card](https://goreportcard.com/badge/github.com/pkg/errors)](https://goreportcard.com/report/github.com/pkg/errors) [![Sourcegraph](https://sourcegraph.com/github.com/pkg/errors/-/badge.svg)](https://sourcegraph.com/github.com/pkg/errors?badge)
Package errors provides simple error handling primitives.
`go get github.com/pkg/errors`
The traditional error handling idiom in Go is roughly akin to
```go
if err != nil {
return err
}
```
which applied recursively up the call stack results in error reports without context or debugging information. The errors package allows programmers to add context to the failure path in their code in a way that does not destroy the original value of the error.
## Adding context to an error
The errors.Wrap function returns a new error that adds context to the original error. For example
```go
_, err := ioutil.ReadAll(r)
if err != nil {
return errors.Wrap(err, "read failed")
}
```
## Retrieving the cause of an error
Using `errors.Wrap` constructs a stack of errors, adding context to the preceding error. Depending on the nature of the error it may be necessary to reverse the operation of errors.Wrap to retrieve the original error for inspection. Any error value which implements this interface can be inspected by `errors.Cause`.
```go
type causer interface {
Cause() error
}
```
`errors.Cause` will recursively retrieve the topmost error which does not implement `causer`, which is assumed to be the original cause. For example:
```go
switch err := errors.Cause(err).(type) {
case *MyError:
// handle specifically
default:
// unknown error
}
```
[Read the package documentation for more information](https://godoc.org/github.com/pkg/errors).
## Contributing
We welcome pull requests, bug fixes and issue reports. With that said, the bar for adding new symbols to this package is intentionally set high.
Before proposing a change, please discuss your change by raising an issue.
## License
BSD-2-Clause

32
vendor/github.com/pkg/errors/appveyor.yml generated vendored Normal file
View File

@@ -0,0 +1,32 @@
version: build-{build}.{branch}
clone_folder: C:\gopath\src\github.com\pkg\errors
shallow_clone: true # for startup speed
environment:
GOPATH: C:\gopath
platform:
- x64
# http://www.appveyor.com/docs/installed-software
install:
# some helpful output for debugging builds
- go version
- go env
# pre-installed MinGW at C:\MinGW is 32bit only
# but MSYS2 at C:\msys64 has mingw64
- set PATH=C:\msys64\mingw64\bin;%PATH%
- gcc --version
- g++ --version
build_script:
- go install -v ./...
test_script:
- set PATH=C:\gopath\bin;%PATH%
- go test -v ./...
#artifacts:
# - path: '%GOPATH%\bin\*.exe'
deploy: off

282
vendor/github.com/pkg/errors/errors.go generated vendored Normal file
View File

@@ -0,0 +1,282 @@
// Package errors provides simple error handling primitives.
//
// The traditional error handling idiom in Go is roughly akin to
//
// if err != nil {
// return err
// }
//
// which when applied recursively up the call stack results in error reports
// without context or debugging information. The errors package allows
// programmers to add context to the failure path in their code in a way
// that does not destroy the original value of the error.
//
// Adding context to an error
//
// The errors.Wrap function returns a new error that adds context to the
// original error by recording a stack trace at the point Wrap is called,
// together with the supplied message. For example
//
// _, err := ioutil.ReadAll(r)
// if err != nil {
// return errors.Wrap(err, "read failed")
// }
//
// If additional control is required, the errors.WithStack and
// errors.WithMessage functions destructure errors.Wrap into its component
// operations: annotating an error with a stack trace and with a message,
// respectively.
//
// Retrieving the cause of an error
//
// Using errors.Wrap constructs a stack of errors, adding context to the
// preceding error. Depending on the nature of the error it may be necessary
// to reverse the operation of errors.Wrap to retrieve the original error
// for inspection. Any error value which implements this interface
//
// type causer interface {
// Cause() error
// }
//
// can be inspected by errors.Cause. errors.Cause will recursively retrieve
// the topmost error that does not implement causer, which is assumed to be
// the original cause. For example:
//
// switch err := errors.Cause(err).(type) {
// case *MyError:
// // handle specifically
// default:
// // unknown error
// }
//
// Although the causer interface is not exported by this package, it is
// considered a part of its stable public interface.
//
// Formatted printing of errors
//
// All error values returned from this package implement fmt.Formatter and can
// be formatted by the fmt package. The following verbs are supported:
//
// %s print the error. If the error has a Cause it will be
// printed recursively.
// %v see %s
// %+v extended format. Each Frame of the error's StackTrace will
// be printed in detail.
//
// Retrieving the stack trace of an error or wrapper
//
// New, Errorf, Wrap, and Wrapf record a stack trace at the point they are
// invoked. This information can be retrieved with the following interface:
//
// type stackTracer interface {
// StackTrace() errors.StackTrace
// }
//
// The returned errors.StackTrace type is defined as
//
// type StackTrace []Frame
//
// The Frame type represents a call site in the stack trace. Frame supports
// the fmt.Formatter interface that can be used for printing information about
// the stack trace of this error. For example:
//
// if err, ok := err.(stackTracer); ok {
// for _, f := range err.StackTrace() {
// fmt.Printf("%+s:%d", f)
// }
// }
//
// Although the stackTracer interface is not exported by this package, it is
// considered a part of its stable public interface.
//
// See the documentation for Frame.Format for more details.
package errors
import (
"fmt"
"io"
)
// New returns an error with the supplied message.
// New also records the stack trace at the point it was called.
func New(message string) error {
return &fundamental{
msg: message,
stack: callers(),
}
}
// Errorf formats according to a format specifier and returns the string
// as a value that satisfies error.
// Errorf also records the stack trace at the point it was called.
func Errorf(format string, args ...interface{}) error {
return &fundamental{
msg: fmt.Sprintf(format, args...),
stack: callers(),
}
}
// fundamental is an error that has a message and a stack, but no caller.
type fundamental struct {
msg string
*stack
}
func (f *fundamental) Error() string { return f.msg }
func (f *fundamental) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
io.WriteString(s, f.msg)
f.stack.Format(s, verb)
return
}
fallthrough
case 's':
io.WriteString(s, f.msg)
case 'q':
fmt.Fprintf(s, "%q", f.msg)
}
}
// WithStack annotates err with a stack trace at the point WithStack was called.
// If err is nil, WithStack returns nil.
func WithStack(err error) error {
if err == nil {
return nil
}
return &withStack{
err,
callers(),
}
}
type withStack struct {
error
*stack
}
func (w *withStack) Cause() error { return w.error }
func (w *withStack) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v", w.Cause())
w.stack.Format(s, verb)
return
}
fallthrough
case 's':
io.WriteString(s, w.Error())
case 'q':
fmt.Fprintf(s, "%q", w.Error())
}
}
// Wrap returns an error annotating err with a stack trace
// at the point Wrap is called, and the supplied message.
// If err is nil, Wrap returns nil.
func Wrap(err error, message string) error {
if err == nil {
return nil
}
err = &withMessage{
cause: err,
msg: message,
}
return &withStack{
err,
callers(),
}
}
// Wrapf returns an error annotating err with a stack trace
// at the point Wrapf is called, and the format specifier.
// If err is nil, Wrapf returns nil.
func Wrapf(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
err = &withMessage{
cause: err,
msg: fmt.Sprintf(format, args...),
}
return &withStack{
err,
callers(),
}
}
// WithMessage annotates err with a new message.
// If err is nil, WithMessage returns nil.
func WithMessage(err error, message string) error {
if err == nil {
return nil
}
return &withMessage{
cause: err,
msg: message,
}
}
// WithMessagef annotates err with the format specifier.
// If err is nil, WithMessagef returns nil.
func WithMessagef(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return &withMessage{
cause: err,
msg: fmt.Sprintf(format, args...),
}
}
type withMessage struct {
cause error
msg string
}
func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() }
func (w *withMessage) Cause() error { return w.cause }
func (w *withMessage) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "%+v\n", w.Cause())
io.WriteString(s, w.msg)
return
}
fallthrough
case 's', 'q':
io.WriteString(s, w.Error())
}
}
// Cause returns the underlying cause of the error, if possible.
// An error value has a cause if it implements the following
// interface:
//
// type causer interface {
// Cause() error
// }
//
// If the error does not implement Cause, the original error will
// be returned. If the error is nil, nil will be returned without further
// investigation.
func Cause(err error) error {
type causer interface {
Cause() error
}
for err != nil {
cause, ok := err.(causer)
if !ok {
break
}
err = cause.Cause()
}
return err
}

147
vendor/github.com/pkg/errors/stack.go generated vendored Normal file
View File

@@ -0,0 +1,147 @@
package errors
import (
"fmt"
"io"
"path"
"runtime"
"strings"
)
// Frame represents a program counter inside a stack frame.
type Frame uintptr
// pc returns the program counter for this frame;
// multiple frames may have the same PC value.
func (f Frame) pc() uintptr { return uintptr(f) - 1 }
// file returns the full path to the file that contains the
// function for this Frame's pc.
func (f Frame) file() string {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return "unknown"
}
file, _ := fn.FileLine(f.pc())
return file
}
// line returns the line number of source code of the
// function for this Frame's pc.
func (f Frame) line() int {
fn := runtime.FuncForPC(f.pc())
if fn == nil {
return 0
}
_, line := fn.FileLine(f.pc())
return line
}
// Format formats the frame according to the fmt.Formatter interface.
//
// %s source file
// %d source line
// %n function name
// %v equivalent to %s:%d
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+s function name and path of source file relative to the compile time
// GOPATH separated by \n\t (<funcname>\n\t<path>)
// %+v equivalent to %+s:%d
func (f Frame) Format(s fmt.State, verb rune) {
switch verb {
case 's':
switch {
case s.Flag('+'):
pc := f.pc()
fn := runtime.FuncForPC(pc)
if fn == nil {
io.WriteString(s, "unknown")
} else {
file, _ := fn.FileLine(pc)
fmt.Fprintf(s, "%s\n\t%s", fn.Name(), file)
}
default:
io.WriteString(s, path.Base(f.file()))
}
case 'd':
fmt.Fprintf(s, "%d", f.line())
case 'n':
name := runtime.FuncForPC(f.pc()).Name()
io.WriteString(s, funcname(name))
case 'v':
f.Format(s, 's')
io.WriteString(s, ":")
f.Format(s, 'd')
}
}
// StackTrace is stack of Frames from innermost (newest) to outermost (oldest).
type StackTrace []Frame
// Format formats the stack of Frames according to the fmt.Formatter interface.
//
// %s lists source files for each Frame in the stack
// %v lists the source file and line number for each Frame in the stack
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+v Prints filename, function, and line number for each Frame in the stack.
func (st StackTrace) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
switch {
case s.Flag('+'):
for _, f := range st {
fmt.Fprintf(s, "\n%+v", f)
}
case s.Flag('#'):
fmt.Fprintf(s, "%#v", []Frame(st))
default:
fmt.Fprintf(s, "%v", []Frame(st))
}
case 's':
fmt.Fprintf(s, "%s", []Frame(st))
}
}
// stack represents a stack of program counters.
type stack []uintptr
func (s *stack) Format(st fmt.State, verb rune) {
switch verb {
case 'v':
switch {
case st.Flag('+'):
for _, pc := range *s {
f := Frame(pc)
fmt.Fprintf(st, "\n%+v", f)
}
}
}
}
func (s *stack) StackTrace() StackTrace {
f := make([]Frame, len(*s))
for i := 0; i < len(f); i++ {
f[i] = Frame((*s)[i])
}
return f
}
func callers() *stack {
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(3, pcs[:])
var st stack = pcs[0:n]
return &st
}
// funcname removes the path prefix component of a function's name reported by func.Name().
func funcname(name string) string {
i := strings.LastIndex(name, "/")
name = name[i+1:]
i = strings.Index(name, ".")
return name[i+1:]
}

View File

@@ -19,7 +19,9 @@ func QueryOptions(apiOp *types.APIRequest, schema *types.Schema) types.QueryOpti
return types.QueryOptions{}
}
result := &types.QueryOptions{}
result := &types.QueryOptions{
Options: map[string]string{},
}
result.Sort = parseSort(schema, apiOp)
result.Pagination = parsePagination(apiOp)

View File

@@ -27,6 +27,6 @@ func (e *Store) Update(apiOp *types.APIRequest, schema *types.Schema, data types
return types.APIObject{}, nil
}
func (e *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, opt *types.QueryOptions) (chan types.APIEvent, error) {
func (e *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, wr types.WatchRequest) (chan types.APIEvent, error) {
return nil, nil
}

View File

@@ -38,8 +38,8 @@ func (e *errorStore) Delete(apiOp *types.APIRequest, schema *types.Schema, id st
}
func (e *errorStore) Watch(apiOp *types.APIRequest, schema *types.Schema, opt *types.QueryOptions) (chan types.APIEvent, error) {
data, err := e.Store.Watch(apiOp, schema, opt)
func (e *errorStore) Watch(apiOp *types.APIRequest, schema *types.Schema, wr types.WatchRequest) (chan types.APIEvent, error) {
data, err := e.Store.Watch(apiOp, schema, wr)
return data, translateError(err)
}

View File

@@ -3,6 +3,8 @@ package proxy
import (
"sync"
errors2 "github.com/pkg/errors"
"github.com/rancher/norman/pkg/types"
"github.com/rancher/norman/pkg/types/convert/merge"
"github.com/rancher/norman/pkg/types/values"
@@ -107,61 +109,78 @@ func (s *Store) listNamespace(namespace string, apiOp types.APIRequest, schema *
return k8sClient.List(metav1.ListOptions{})
}
func (s *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, opt *types.QueryOptions) (chan types.APIEvent, error) {
k8sClient, err := s.clientGetter.Client(apiOp, schema)
if err != nil {
return nil, err
func returnErr(err error, c chan types.APIEvent) {
c <- types.APIEvent{
Name: "resource.error",
Error: err,
}
}
func (s *Store) listAndWatch(apiOp *types.APIRequest, k8sClient dynamic.ResourceInterface, schema *types.Schema, w types.WatchRequest, result chan types.APIEvent) {
rev := w.Revision
if rev == "" {
list, err := k8sClient.List(metav1.ListOptions{
Limit: 1,
})
if err != nil {
returnErr(errors2.Wrapf(err, "failed to list %s", schema.ID), result)
return
}
rev = list.GetResourceVersion()
}
list, err := k8sClient.List(metav1.ListOptions{})
timeout := int64(60 * 30)
watcher, err := k8sClient.Watch(metav1.ListOptions{
Watch: true,
TimeoutSeconds: &timeout,
ResourceVersion: rev,
})
if err != nil {
returnErr(errors2.Wrapf(err, "stopping watch for %s: %v", schema.ID), result)
return
}
defer watcher.Stop()
logrus.Debugf("opening watcher for %s", schema.ID)
go func() {
<-apiOp.Request.Context().Done()
watcher.Stop()
}()
for event := range watcher.ResultChan() {
data := event.Object.(*unstructured.Unstructured)
result <- s.toAPIEvent(apiOp, schema, event.Type, data)
}
}
func (s *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, w types.WatchRequest) (chan types.APIEvent, error) {
k8sClient, err := s.clientGetter.Client(apiOp, schema)
if err != nil {
return nil, err
}
result := make(chan types.APIEvent)
go func() {
defer func() {
logrus.Debugf("closing watcher for %s", schema.ID)
close(result)
}()
for i, obj := range list.Items {
result <- s.toAPIEvent(apiOp, schema, i, len(list.Items), false, &obj)
}
timeout := int64(60 * 30)
watcher, err := k8sClient.Watch(metav1.ListOptions{
Watch: true,
TimeoutSeconds: &timeout,
ResourceVersion: list.GetResourceVersion(),
})
if err != nil {
logrus.Debugf("stopping watch for %s: %v", schema.ID, err)
return
}
defer watcher.Stop()
for event := range watcher.ResultChan() {
data := event.Object.(*unstructured.Unstructured)
result <- s.toAPIEvent(apiOp, schema, 0, 0, event.Type == watch.Deleted, data)
}
s.listAndWatch(apiOp, k8sClient, schema, w, result)
logrus.Debugf("closing watcher for %s", schema.ID)
close(result)
}()
return result, nil
}
func (s *Store) toAPIEvent(apiOp *types.APIRequest, schema *types.Schema, index, count int, remove bool, obj *unstructured.Unstructured) types.APIEvent {
func (s *Store) toAPIEvent(apiOp *types.APIRequest, schema *types.Schema, et watch.EventType, obj *unstructured.Unstructured) types.APIEvent {
name := "resource.change"
if remove && obj.Object != nil {
switch et {
case watch.Deleted:
name = "resource.remove"
case watch.Added:
name = "resource.create"
}
s.fromInternal(apiOp, schema, obj.Object)
return types.APIEvent{
Name: name,
Count: count,
Index: index,
Object: types.ToAPI(obj.Object),
}
}

View File

@@ -34,7 +34,7 @@ func (s *Store) ByID(apiOp *types.APIRequest, schema *types.Schema, id string) (
return types.APIObject{}, httperror.NewAPIError(httperror.NotFound, "no such schema")
}
func (s *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, opt *types.QueryOptions) (chan types.APIEvent, error) {
func (s *Store) Watch(apiOp *types.APIRequest, schema *types.Schema, wr types.WatchRequest) (chan types.APIEvent, error) {
return nil, nil
}

View File

@@ -0,0 +1,57 @@
package subscribe
import (
"io"
"github.com/rancher/norman/pkg/api/writer"
"github.com/rancher/norman/pkg/types"
)
type Converter struct {
writer.EncodingResponseWriter
apiOp *types.APIRequest
obj interface{}
}
func MarshallObject(apiOp *types.APIRequest, event types.APIEvent) types.APIEvent {
if event.Error != nil {
return event
}
if event.Object.IsNil() {
return event
}
data, err := newConverter(apiOp).ToAPIObject(event.Object)
if err != nil {
event.Error = err
return event
}
event.Data = data.Raw()
return event
}
func newConverter(apiOp *types.APIRequest) *Converter {
c := &Converter{
apiOp: apiOp,
}
c.EncodingResponseWriter = writer.EncodingResponseWriter{
ContentType: "application/json",
Encoder: c.Encoder,
}
return c
}
func (c *Converter) ToAPIObject(data interface{}) (types.APIObject, error) {
c.obj = nil
if err := c.VersionBody(c.apiOp, nil, data); err != nil {
return types.APIObject{}, err
}
return types.ToAPI(c.obj), nil
}
func (c *Converter) Encoder(w io.Writer, obj interface{}) error {
c.obj = obj
return nil
}

View File

@@ -1,29 +1,23 @@
package subscribe
import (
"context"
"encoding/json"
"errors"
"io"
"time"
"github.com/gorilla/websocket"
"github.com/rancher/norman/pkg/api/writer"
"github.com/rancher/norman/pkg/httperror"
"github.com/rancher/norman/pkg/parse"
"github.com/rancher/norman/pkg/types"
"github.com/rancher/norman/pkg/types/convert"
"github.com/rancher/norman/pkg/types/slice"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)
var upgrader = websocket.Upgrader{}
var upgrader = websocket.Upgrader{
HandshakeTimeout: 60 * time.Second,
EnableCompression: true,
}
type Subscribe struct {
ResourceTypes []string
APIVersions []string
ProjectID string `norman:"type=reference[/v3/schemas/project]"`
Stop bool `json:"stop,omitempty"`
ResourceType string `json:"resourceType,omitempty"`
ResourceVersion string `json:"resourceVersion,omitempty"`
}
func Handler(apiOp *types.APIRequest) (types.APIObject, error) {
@@ -34,102 +28,46 @@ func Handler(apiOp *types.APIRequest) (types.APIObject, error) {
return types.APIObject{}, err
}
func getMatchingSchemas(apiOp *types.APIRequest) []*types.Schema {
resourceTypes := apiOp.Request.URL.Query()["resourceTypes"]
var schemas []*types.Schema
for _, schema := range apiOp.Schemas.Schemas() {
if !matches(resourceTypes, schema.ID) {
continue
}
if schema.Store != nil {
schemas = append(schemas, schema)
}
}
return schemas
}
func handler(apiOp *types.APIRequest) error {
schemas := getMatchingSchemas(apiOp)
if len(schemas) == 0 {
return httperror.NewAPIError(httperror.NotFound, "no resources types matched")
}
c, err := upgrader.Upgrade(apiOp.Response, apiOp.Request, nil)
if err != nil {
return err
}
defer c.Close()
cancelCtx, cancel := context.WithCancel(apiOp.Request.Context())
readerGroup, ctx := errgroup.WithContext(cancelCtx)
apiOp.Request = apiOp.Request.WithContext(ctx)
watches := NewWatchSession(apiOp)
defer watches.Close()
go func() {
for {
if _, _, err := c.NextReader(); err != nil {
cancel()
c.Close()
break
}
}
}()
events := make(chan types.APIEvent)
for _, schema := range schemas {
if apiOp.AccessControl.CanWatch(apiOp, schema) == nil {
streamStore(ctx, readerGroup, apiOp, schema, events)
}
}
go func() {
readerGroup.Wait()
close(events)
}()
capture := &Capture{}
captureWriter := writer.EncodingResponseWriter{
ContentType: "application/json",
Encoder: capture.Encoder,
}
t := time.NewTicker(60 * time.Second)
events := watches.Watch(c)
t := time.NewTicker(30 * time.Second)
defer t.Stop()
done := false
for !done {
for {
select {
case item, ok := <-events:
case event, ok := <-events:
if !ok {
done = true
break
return nil
}
schema := apiOp.Schemas.Schema(convert.ToString(item.Object.Map()["type"]))
if schema != nil {
if err := captureWriter.VersionBody(apiOp, nil, item.Object); err != nil {
cancel()
continue
}
item.Object = types.ToAPI(capture.Object)
if err := writeData(c, item); err != nil {
cancel()
}
if err := writeData(apiOp, c, event); err != nil {
return err
}
case <-t.C:
if err := writeData(c, types.APIEvent{Name: "ping"}); err != nil {
cancel()
if err := writeData(apiOp, c, types.APIEvent{Name: "ping"}); err != nil {
return err
}
}
}
// no point in ever returning null because the connection is hijacked and we can't write it
return nil
}
func writeData(c *websocket.Conn, event types.APIEvent) error {
event.Data = event.Object.Raw()
func writeData(apiOp *types.APIRequest, c *websocket.Conn, event types.APIEvent) error {
event = MarshallObject(apiOp, event)
if event.Error != nil {
event.Name = "resource.error"
event.Data = map[string]interface{}{
"error": event.Error.Error(),
}
}
messageWriter, err := c.NextWriter(websocket.TextMessage)
if err != nil {
return err
@@ -138,51 +76,3 @@ func writeData(c *websocket.Conn, event types.APIEvent) error {
return json.NewEncoder(messageWriter).Encode(event)
}
func watch(apiOp *types.APIRequest, schema *types.Schema, opts *types.QueryOptions) (chan types.APIEvent, error) {
c, err := schema.Store.Watch(apiOp, schema, opts)
if err != nil {
return nil, err
}
return types.APIChan(c, func(data types.APIEvent) types.APIEvent {
data.Object = apiOp.FilterObject(nil, schema, data.Object)
return data
}), nil
}
func streamStore(ctx context.Context, eg *errgroup.Group, apiOp *types.APIRequest, schema *types.Schema, result chan types.APIEvent) {
eg.Go(func() error {
opts := parse.QueryOptions(apiOp, schema)
events, err := watch(apiOp, schema, &opts)
if err != nil || events == nil {
if err != nil {
logrus.Errorf("failed on subscribe %s: %v", schema.ID, err)
}
return err
}
logrus.Debugf("watching %s", schema.ID)
for e := range events {
result <- e
}
return errors.New("disconnect")
})
}
func matches(items []string, item string) bool {
if len(items) == 0 {
return true
}
return slice.ContainsString(items, item)
}
type Capture struct {
Object interface{}
}
func (c *Capture) Encoder(w io.Writer, obj interface{}) error {
c.Object = obj
return nil
}

View File

@@ -0,0 +1,140 @@
package subscribe
import (
"context"
"encoding/json"
"fmt"
"sync"
"github.com/gorilla/websocket"
"github.com/rancher/norman/pkg/types"
)
type WatchSession struct {
sync.Mutex
apiOp *types.APIRequest
watchers map[string]func()
wg sync.WaitGroup
ctx context.Context
cancel func()
}
func (s *WatchSession) stop(id string, resp chan<- types.APIEvent) {
s.Lock()
defer s.Unlock()
if cancel, ok := s.watchers[id]; ok {
cancel()
resp <- types.APIEvent{
Name: "resource.stop",
ResourceType: id,
}
}
delete(s.watchers, id)
}
func (s *WatchSession) add(resourceType, revision string, resp chan<- types.APIEvent) {
s.Lock()
defer s.Unlock()
ctx, cancel := context.WithCancel(s.ctx)
s.watchers[resourceType] = cancel
s.wg.Add(1)
go func() {
defer s.wg.Done()
defer s.stop(resourceType, resp)
if err := s.stream(ctx, resourceType, revision, resp); err != nil {
sendErr(resp, err, resourceType)
}
}()
}
func (s *WatchSession) stream(ctx context.Context, resourceType, revision string, result chan<- types.APIEvent) error {
schema := s.apiOp.Schemas.Schema(resourceType)
if schema == nil {
return fmt.Errorf("failed to find schema %s", resourceType)
} else if schema.Store == nil {
return fmt.Errorf("schema %s does not support watching", resourceType)
}
if err := s.apiOp.AccessControl.CanWatch(s.apiOp, schema); err != nil {
return err
}
c, err := schema.Store.Watch(s.apiOp.WithContext(ctx), schema, types.WatchRequest{Revision: revision})
if err != nil {
return err
}
result <- types.APIEvent{
Name: "resource.start",
ResourceType: resourceType,
}
for event := range c {
result <- event
}
return nil
}
func NewWatchSession(apiOp *types.APIRequest) *WatchSession {
ws := &WatchSession{
apiOp: apiOp,
watchers: map[string]func(){},
}
ws.ctx, ws.cancel = context.WithCancel(apiOp.Request.Context())
return ws
}
func (s *WatchSession) Watch(conn *websocket.Conn) <-chan types.APIEvent {
result := make(chan types.APIEvent, 100)
go func() {
defer close(result)
if err := s.watch(conn, result); err != nil {
sendErr(result, err, "")
}
}()
return result
}
func (s *WatchSession) Close() {
s.cancel()
s.wg.Wait()
}
func (s *WatchSession) watch(conn *websocket.Conn, resp chan types.APIEvent) error {
defer s.wg.Wait()
defer s.cancel()
for {
_, r, err := conn.NextReader()
if err != nil {
return err
}
var sub Subscribe
if err := json.NewDecoder(r).Decode(&sub); err != nil {
sendErr(resp, err, "")
continue
}
if sub.Stop {
s.stop(sub.ResourceType, resp)
} else if _, ok := s.watchers[sub.ResourceType]; !ok {
s.add(sub.ResourceType, sub.ResourceVersion, resp)
}
}
}
func sendErr(resp chan<- types.APIEvent, err error, resourceType string) {
resp <- types.APIEvent{
ResourceType: resourceType,
Error: err,
}
}

View File

@@ -3,7 +3,7 @@ package definition
import (
"strings"
convert2 "github.com/rancher/norman/pkg/types/convert"
"github.com/rancher/norman/pkg/types/convert"
)
func IsMapType(fieldType string) bool {
@@ -41,5 +41,5 @@ func GetShortTypeFromFull(fullType string) string {
}
func GetFullType(data map[string]interface{}) string {
return convert2.ToString(data["type"])
return convert.ToString(data["type"])
}

View File

@@ -1,6 +1,7 @@
package types
import (
"context"
"encoding/json"
"net/http"
"net/url"
@@ -112,6 +113,12 @@ type APIRequest struct {
Response http.ResponseWriter
}
func (r *APIRequest) WithContext(ctx context.Context) *APIRequest {
result := *r
result.Request = result.Request.WithContext(ctx)
return &result
}
func (r *APIRequest) GetUser() string {
user, ok := request.UserFrom(r.Request.Context())
if ok {
@@ -160,6 +167,7 @@ type QueryOptions struct {
Sort Sort
Pagination *Pagination
Conditions []*QueryCondition
Options map[string]string
}
type ReferenceValidator interface {
@@ -189,14 +197,18 @@ type Store interface {
Create(apiOp *APIRequest, schema *Schema, data APIObject) (APIObject, error)
Update(apiOp *APIRequest, schema *Schema, data APIObject, id string) (APIObject, error)
Delete(apiOp *APIRequest, schema *Schema, id string) (APIObject, error)
Watch(apiOp *APIRequest, schema *Schema, opt *QueryOptions) (chan APIEvent, error)
Watch(apiOp *APIRequest, schema *Schema, w WatchRequest) (chan APIEvent, error)
}
type WatchRequest struct {
Revision string
}
type APIEvent struct {
Name string `json:"name,omitempty"`
Count int `json:"count,omitempty"`
Index int `json:"index,omitempty"`
Object APIObject `json:"-"`
Name string `json:"name,omitempty"`
ResourceType string `json:"resourceType,omitempty"`
Object APIObject `json:"-"`
Error error `json:"-"`
// Data should be used
Data interface{} `json:"data,omitempty"`
}

2
vendor/modules.txt vendored
View File

@@ -36,6 +36,8 @@ github.com/modern-go/concurrent
github.com/modern-go/reflect2
# github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f
github.com/mxk/go-flowrate/flowrate
# github.com/pkg/errors v0.8.1
github.com/pkg/errors
# github.com/rancher/norman v0.0.0-20190704000224-043a1c919df3 => ../norman
github.com/rancher/norman/pkg/authorization
github.com/rancher/norman/pkg/types