Skip to content

Commit 71ba148

Browse files
committed
refactor: use factory pattern for plugin creation to use created scheme and client
Signed-off-by: JM Huibonhoa <jm.huibonhoa@solo.io>
1 parent 7d73c37 commit 71ba148

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
func main() {
2929
app.Start(func() (*app.ExtensionConfig, error) {
3030
return &app.ExtensionConfig{
31-
Plugins: nil,
31+
PluginFactories: nil,
3232
RegisterSchemes: nil,
3333
}, nil
3434
})

pkg/app/app.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3232
ctrl "sigs.k8s.io/controller-runtime"
3333
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
34+
"sigs.k8s.io/controller-runtime/pkg/client"
3435
"sigs.k8s.io/controller-runtime/pkg/healthz"
3536
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3637
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
@@ -110,9 +111,15 @@ func (cfg *Config) SetFlags(commandLine *flag.FlagSet) {
110111
"If set, HTTP/2 will be enabled for the metrics and webhook servers")
111112
}
112113

114+
// PluginFactory creates a TranslatorPlugin when provided with the client and scheme.
115+
// This allows plugins to be initialized after the manager is created, giving them access to
116+
// the Kubernetes client and scheme. Plugins should create their own logger.
117+
type PluginFactory func(client.Client, *runtime.Scheme) transportadapter.TranslatorPlugin
118+
113119
type ExtensionConfig struct {
114-
// Plugins are translator plugins for extending MCPServer translation behavior
115-
Plugins []transportadapter.TranslatorPlugin
120+
// PluginFactories are factories that create translator plugins for extending MCPServer translation behavior.
121+
// These factories are called after the manager is created, allowing plugins to access the client and scheme.
122+
PluginFactories []PluginFactory
116123
// RegisterSchemes is an optional function to register additional API types to the runtime scheme.
117124
// This is called before the manager is created, allowing extensions to add their own CRDs.
118125
RegisterSchemes func(*runtime.Scheme) error
@@ -265,10 +272,16 @@ func Start(getExtensionConfig GetExtensionConfig) {
265272
os.Exit(1)
266273
}
267274

275+
var plugins []transportadapter.TranslatorPlugin
276+
for _, factory := range extensionCfg.PluginFactories {
277+
plugin := factory(mgr.GetClient(), mgr.GetScheme())
278+
plugins = append(plugins, plugin)
279+
}
280+
268281
if err = (&controller.MCPServerReconciler{
269282
Client: mgr.GetClient(),
270283
Scheme: mgr.GetScheme(),
271-
Plugins: extensionCfg.Plugins,
284+
Plugins: plugins,
272285
}).SetupWithManager(mgr); err != nil {
273286
setupLog.Error(err, "unable to create controller", "controller", "MCPServer")
274287
os.Exit(1)

0 commit comments

Comments
 (0)