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

Allow the ability to match unset class for genreconciler. #1163

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions codegen/cmd/injection-gen/generators/reconciler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ const (
// the queue through an implementation of {{.controllerReconciler|raw}}, delegating to
// the provided Interface and optional Finalizer methods. OptionsFn is used to return
// {{.controllerOptions|raw}} to be used but the internal reconciler.
func NewImpl(ctx {{.contextContext|raw}}, r Interface{{if .hasClass}}, classValue string{{end}}, optionsFns ...{{.controllerOptionsFn|raw}}) *{{.controllerImpl|raw}} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had looked at this before and it is too easy for multiple controllers to do this wrong and both race to reconcile a resource.

/hold

func NewImpl(ctx {{.contextContext|raw}}, r Interface{{if .hasClass}}, classFilter func(interface{}) bool{{end}}, optionsFns ...{{.controllerOptionsFn|raw}}) *{{.controllerImpl|raw}} {
logger := {{.loggingFromContext|raw}}(ctx)
// Check the options function input. It should be 0 or 1.
Expand Down Expand Up @@ -198,7 +198,7 @@ func NewImpl(ctx {{.contextContext|raw}}, r Interface{{if .hasClass}}, classValu
Lister: {{.type|lowercaseSingular}}Informer.Lister(),
Recorder: recorder,
reconciler: r,
{{if .hasClass}}classValue: classValue,{{end}}
{{if .hasClass}}classFilter: classFilter,{{end}}
}
impl := {{.controllerNewImpl|raw}}(rec, logger, defaultQueueName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,15 @@ func NewController(

{{if .hasClass}}
classValue := "default" // TODO: update this to the appropriate value.
classFilter := {{.annotationFilterFunc|raw}}({{.classAnnotationKey|raw}}, classValue, false /*allowUnset*/)
allowUnset := false // TODO: update this to specific matching unset ClassAnnotationKey or not.
classFilter := {{.annotationFilterFunc|raw}}({{.classAnnotationKey|raw}}, classValue, allowUnset /*allowUnset*/)
{{end}}

// TODO: setup additional informers here.
{{if .hasClass}}// TODO: remember to use the classFilter from above to filter appropriately.{{end}}

r := &Reconciler{}
impl := {{.reconcilerNewImpl|raw}}(ctx, r{{if .hasClass}}, classValue{{end}})
impl := {{.reconcilerNewImpl|raw}}(ctx, r{{if .hasClass}}, classFilter{{end}})

logger.Info("Setting up event handlers.")

Expand Down
12 changes: 6 additions & 6 deletions codegen/cmd/injection-gen/generators/reconciler_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ type reconcilerImpl struct {
reconciler Interface

{{if .hasClass}}
// classValue is the resource annotation[{{ .class }}] instance value this reconciler instance filters on.
classValue string
// classFilter is the filter function for annotation[eventing.knative.dev/broker.class] instance value this reconciler instance filters on.
classFilter func(interface{}) bool
{{end}}
}

Expand All @@ -209,7 +209,7 @@ var _ controller.Reconciler = (*reconcilerImpl)(nil)
`

var reconcilerNewReconciler = `
func NewReconciler(ctx {{.contextContext|raw}}, logger *{{.zapSugaredLogger|raw}}, client {{.clientsetInterface|raw}}, lister {{.resourceLister|raw}}, recorder {{.recordEventRecorder|raw}}, r Interface{{if .hasClass}}, classValue string{{end}}, options ...{{.controllerOptions|raw}} ) {{.controllerReconciler|raw}} {
func NewReconciler(ctx {{.contextContext|raw}}, logger *{{.zapSugaredLogger|raw}}, client {{.clientsetInterface|raw}}, lister {{.resourceLister|raw}}, recorder {{.recordEventRecorder|raw}}, r Interface{{if .hasClass}}, classFilter func(interface{}) bool{{end}}, options ...{{.controllerOptions|raw}} ) {{.controllerReconciler|raw}} {
// Check the options function input. It should be 0 or 1.
if len(options) > 1 {
logger.Fatalf("up to one options struct is supported, found %d", len(options))
Expand All @@ -220,7 +220,7 @@ func NewReconciler(ctx {{.contextContext|raw}}, logger *{{.zapSugaredLogger|raw}
Lister: lister,
Recorder: recorder,
reconciler: r,
{{if .hasClass}}classValue: classValue,{{end}}
{{if .hasClass}}classFilter: classFilter,{{end}}
}

for _, opts := range options {
Expand Down Expand Up @@ -263,10 +263,10 @@ func (r *reconcilerImpl) Reconcile(ctx {{.contextContext|raw}}, key string) erro
return err
}
{{if .hasClass}}
if classValue, found := original.GetAnnotations()[ClassAnnotationKey]; !found || classValue != r.classValue {
if !r.classFilter(original) {
logger.Debugw("Skip reconciling resource, class annotation value does not match reconciler instance value.",
zap.String("classKey", ClassAnnotationKey),
zap.String("issue", classValue+"!="+r.classValue))
zap.String("classValue", original.GetAnnotations()[ClassAnnotationKey]))
return nil
}
{{end}}
Expand Down