Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add name to handlers #51

Merged
merged 3 commits into from
Jan 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions condition/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (c Cond) Once(obj runtime.Object, f func() (runtime.Object, error)) (runtim

c.Unknown(obj)
newObj, err := f()
if newObj != nil {
if newObj != nil && !reflect.ValueOf(newObj).IsNil() {
obj = newObj
}

Expand All @@ -103,7 +103,7 @@ func (c Cond) DoUntilTrue(obj runtime.Object, f func() (runtime.Object, error))

c.Unknown(obj)
newObj, err := f()
if newObj != nil {
if newObj != nil && !reflect.ValueOf(newObj).IsNil() {
obj = newObj
}

Expand All @@ -124,7 +124,7 @@ func (c Cond) Do(obj runtime.Object, f func() (runtime.Object, error)) (runtime.
func (c Cond) do(obj runtime.Object, f func() (runtime.Object, error)) (runtime.Object, error) {
c.Unknown(obj)
newObj, err := f()
if newObj != nil {
if newObj != nil && !reflect.ValueOf(newObj).IsNil() {
obj = newObj
}

Expand Down
50 changes: 40 additions & 10 deletions controller/generic_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,22 @@ type HandlerFunc func(key string) error

type GenericController interface {
Informer() cache.SharedIndexInformer
AddHandler(handler HandlerFunc)
AddHandler(name string, handler HandlerFunc)
HandlerCount() int
Enqueue(namespace, name string)
Sync(ctx context.Context) error
Start(ctx context.Context, threadiness int) error
}

type handlerDef struct {
name string
handler HandlerFunc
}

type genericController struct {
sync.Mutex
informer cache.SharedIndexInformer
handlers []HandlerFunc
handlers []handlerDef
queue workqueue.RateLimitingInterface
name string
running bool
Expand Down Expand Up @@ -72,8 +77,11 @@ func (g *genericController) Enqueue(namespace, name string) {
}
}

func (g *genericController) AddHandler(handler HandlerFunc) {
g.handlers = append(g.handlers, handler)
func (g *genericController) AddHandler(name string, handler HandlerFunc) {
g.handlers = append(g.handlers, handlerDef{
name: name,
handler: handler,
})
}

func (g *genericController) Sync(ctx context.Context) error {
Expand Down Expand Up @@ -162,23 +170,45 @@ func (g *genericController) processNextWorkItem() bool {

// do your work on the key. This method will contains your "do stuff" logic
err := g.syncHandler(key.(string))
if _, ok := err.(*ForgetError); err == nil || ok {
checkErr := err
if handlerErr, ok := checkErr.(*handlerError); ok {
checkErr = handlerErr.err
}
if _, ok := checkErr.(*ForgetError); err == nil || ok {
if ok {
logrus.Infof("%v %v completed with dropped err: %v", g.name, key, err)
}
g.queue.Forget(key)
return true
}

utilruntime.HandleError(fmt.Errorf("%v %v failed with : %v", g.name, key, err))
utilruntime.HandleError(fmt.Errorf("%v %v %v", g.name, key, err))
g.queue.AddRateLimited(key)

return true
}

func (g *genericController) syncHandler(s string) error {
func (g *genericController) syncHandler(s string) (err error) {
defer utilruntime.RecoverFromPanic(&err)

var errs []error
for _, handler := range g.handlers {
if err := handler(s); err != nil {
errs = append(errs, err)
if err := handler.handler(s); err != nil {
errs = append(errs, &handlerError{
name: handler.name,
err: err,
})
}
}
return types.NewErrors(errs)
err = types.NewErrors(errs)
return
}

type handlerError struct {
name string
err error
}

func (h *handlerError) Error() string {
return fmt.Sprintf("[%s] failed with : %v", h.name, h.err)
}
14 changes: 7 additions & 7 deletions generator/controller_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type {{.schema.CodeName}}Lister interface {
type {{.schema.CodeName}}Controller interface {
Informer() cache.SharedIndexInformer
Lister() {{.schema.CodeName}}Lister
AddHandler(handler {{.schema.CodeName}}HandlerFunc)
AddHandler(name string, handler {{.schema.CodeName}}HandlerFunc)
Enqueue(namespace, name string)
Sync(ctx context.Context) error
Start(ctx context.Context, threadiness int) error
Expand All @@ -69,7 +69,7 @@ type {{.schema.CodeName}}Interface interface {
Watch(opts metav1.ListOptions) (watch.Interface, error)
DeleteCollection(deleteOpts *metav1.DeleteOptions, listOpts metav1.ListOptions) error
Controller() {{.schema.CodeName}}Controller
AddSyncHandler(sync {{.schema.CodeName}}HandlerFunc)
AddHandler(name string, sync {{.schema.CodeName}}HandlerFunc)
AddLifecycle(name string, lifecycle {{.schema.CodeName}}Lifecycle)
}

Expand Down Expand Up @@ -115,8 +115,8 @@ func (c *{{.schema.ID}}Controller) Lister() {{.schema.CodeName}}Lister {
}


func (c *{{.schema.ID}}Controller) AddHandler(handler {{.schema.CodeName}}HandlerFunc) {
c.GenericController.AddHandler(func(key string) error {
func (c *{{.schema.ID}}Controller) AddHandler(name string, handler {{.schema.CodeName}}HandlerFunc) {
c.GenericController.AddHandler(name, func(key string) error {
obj, exists, err := c.Informer().GetStore().GetByKey(key)
if err != nil {
return err
Expand Down Expand Up @@ -219,12 +219,12 @@ func (s *{{.schema.ID}}Client) DeleteCollection(deleteOpts *metav1.DeleteOptions
return s.objectClient.DeleteCollection(deleteOpts, listOpts)
}

func (s *{{.schema.ID}}Client) AddSyncHandler(sync {{.schema.CodeName}}HandlerFunc) {
s.Controller().AddHandler(sync)
func (s *{{.schema.ID}}Client) AddHandler(name string, sync {{.schema.CodeName}}HandlerFunc) {
s.Controller().AddHandler(name, sync)
}

func (s *{{.schema.ID}}Client) AddLifecycle(name string, lifecycle {{.schema.CodeName}}Lifecycle) {
sync := New{{.schema.CodeName}}LifecycleAdapter(name, s, lifecycle)
s.AddSyncHandler(sync)
s.AddHandler(name, sync)
}
`
3 changes: 3 additions & 0 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func getTypeString(nullable bool, typeName string, schema *types.Schema, schemas
func getTypeMap(schema *types.Schema, schemas *types.Schemas) map[string]fieldInfo {
result := map[string]fieldInfo{}
for name, field := range schema.ResourceFields {
if strings.EqualFold(name, "id") {
continue
}
result[field.CodeName] = fieldInfo{
Name: name,
Type: getGoType(field, schema, schemas),
Expand Down
4 changes: 4 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package types

const (
ResourceFieldID = "id"
)

type Collection struct {
Type string `json:"type,omitempty"`
Links map[string]string `json:"links"`
Expand Down