Skip to content
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions client/injection/kube/reconciler/apps/v1/deployment/reconciler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions client/injection/kube/reconciler/core/v1/namespace/reconciler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions client/injection/kube/reconciler/core/v1/secret/controller.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions client/injection/kube/reconciler/core/v1/secret/reconciler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions codegen/cmd/injection-gen/generators/reconciler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ func NewImpl(ctx {{.contextContext|raw}}, r Interface{{if .hasClass}}, classValu
if opts.DemoteFunc != nil {
rec.DemoteFunc = opts.DemoteFunc
}
if len(opts.ContextWrappers) != 0 {
rec.ContextWrappers = append(rec.ContextWrappers, opts.ContextWrappers...)
}
}

rec.Recorder = createRecorder(ctx, agentName)
Expand Down
11 changes: 11 additions & 0 deletions codegen/cmd/injection-gen/generators/reconciler_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ type reconcilerImpl struct {
// Kubernetes API.
Recorder {{.recordEventRecorder|raw}}

// ContextWrappers allows to decorate the context passed to the reconcile methods
ContextWrappers []func(context.Context)context.Context

// configStore allows for decorating a context with config maps.
// +optional
configStore {{.reconcilerConfigStore|raw}}
Expand Down Expand Up @@ -366,6 +369,9 @@ func NewReconciler(ctx {{.contextContext|raw}}, logger *{{.zapSugaredLogger|raw}
if opts.DemoteFunc != nil {
rec.DemoteFunc = opts.DemoteFunc
}
if len(opts.ContextWrappers) != 0 {
rec.ContextWrappers = append(rec.ContextWrappers, opts.ContextWrappers...)
}
}

return rec
Expand Down Expand Up @@ -401,6 +407,11 @@ func (r *reconcilerImpl) Reconcile(ctx {{.contextContext|raw}}, key string) erro
// Add the recorder to context.
ctx = {{.controllerWithEventRecorder|raw}}(ctx, r.Recorder)

// Decorate the context with additional wrapper functions
for _, fn := range r.ContextWrappers {
ctx = fn(ctx)
}

// Get the resource with this namespace/name.
{{if .nonNamespaced}}
getter := r.Lister
Expand Down
17 changes: 16 additions & 1 deletion controller/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ limitations under the License.

package controller

import "knative.dev/pkg/reconciler"
import (
"context"

"knative.dev/pkg/reconciler"
)

// Options is additional resources a Controller might want to use depending
// on implementation.
Expand All @@ -38,9 +42,20 @@ type Options struct {

// DemoteFunc configures the demote function this reconciler uses
DemoteFunc func(b reconciler.Bucket)

// ContextWrappers configures the functions to invoke to wrap the context provided to the reconciliation functions
ContextWrappers []func(context.Context) context.Context
}

// OptionsFn is a callback method signature that accepts an Impl and returns
// Options. Used for controllers that need access to the members of Options but
// to build Options, integrators need an Impl.
type OptionsFn func(impl *Impl) Options

// WithContextWrapper adds a context wrapper.
// You can add several context wrappers to the same reconciler impl.
func WithContextWrapper(fn func(context.Context) context.Context) OptionsFn {
return func(impl *Impl) Options {
return Options{ContextWrappers: []func(context.Context) context.Context{fn}}
}
}
31 changes: 31 additions & 0 deletions controller/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright 2021 The Knative Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controller

import (
"context"
"testing"
)

func TestWithContextWrapper(t *testing.T) {
options := WithContextWrapper(func(ctx context.Context) context.Context {
return context.Background()
})(nil)
if len(options.ContextWrappers) != 1 {
t.Error("Length of ContextWrappers should be 1")
}
}