-
Notifications
You must be signed in to change notification settings - Fork 166
refactor: move provider initialization into factory #365
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
Merged
manusa
merged 1 commit into
containers:main
from
Cali0707:refactor-provider-into-factory
Oct 9, 2025
+224
−145
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package kubernetes | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/containers/kubernetes-mcp-server/pkg/config" | ||
| ) | ||
|
|
||
| // KubeConfigTargetParameterName is the parameter name used to specify | ||
| // the kubeconfig context when using the kubeconfig cluster provider strategy. | ||
| const KubeConfigTargetParameterName = "context" | ||
|
|
||
| // kubeConfigClusterProvider implements ManagerProvider for managing multiple | ||
| // Kubernetes clusters using different contexts from a kubeconfig file. | ||
| // It lazily initializes managers for each context as they are requested. | ||
| type kubeConfigClusterProvider struct { | ||
| defaultContext string | ||
| managers map[string]*Manager | ||
| } | ||
|
|
||
| var _ ManagerProvider = &kubeConfigClusterProvider{} | ||
|
|
||
| func init() { | ||
| RegisterProvider(config.ClusterProviderKubeConfig, newKubeConfigClusterProvider) | ||
| } | ||
|
|
||
| // newKubeConfigClusterProvider creates a provider that manages multiple clusters | ||
| // via kubeconfig contexts. Returns an error if the manager is in-cluster mode. | ||
| func newKubeConfigClusterProvider(m *Manager, cfg *config.StaticConfig) (ManagerProvider, error) { | ||
| // Handle in-cluster mode | ||
| if m.IsInCluster() { | ||
| return nil, fmt.Errorf("kubeconfig ClusterProviderStrategy is invalid for in-cluster deployments") | ||
| } | ||
|
|
||
| rawConfig, err := m.clientCmdConfig.RawConfig() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| allClusterManagers := map[string]*Manager{ | ||
| rawConfig.CurrentContext: m, // we already initialized a manager for the default context, let's use it | ||
| } | ||
|
|
||
| for name := range rawConfig.Contexts { | ||
| if name == rawConfig.CurrentContext { | ||
| continue // already initialized this, don't want to set it to nil | ||
| } | ||
|
|
||
| allClusterManagers[name] = nil | ||
| } | ||
|
|
||
| return &kubeConfigClusterProvider{ | ||
| defaultContext: rawConfig.CurrentContext, | ||
| managers: allClusterManagers, | ||
| }, nil | ||
| } | ||
|
|
||
| func (k *kubeConfigClusterProvider) GetTargets(ctx context.Context) ([]string, error) { | ||
| contextNames := make([]string, 0, len(k.managers)) | ||
| for cluster := range k.managers { | ||
| contextNames = append(contextNames, cluster) | ||
| } | ||
|
|
||
| return contextNames, nil | ||
| } | ||
|
|
||
| func (k *kubeConfigClusterProvider) GetTargetParameterName() string { | ||
| return KubeConfigTargetParameterName | ||
| } | ||
|
|
||
| func (k *kubeConfigClusterProvider) GetManagerFor(ctx context.Context, context string) (*Manager, error) { | ||
| m, ok := k.managers[context] | ||
| if ok && m != nil { | ||
| return m, nil | ||
| } | ||
|
|
||
| baseManager := k.managers[k.defaultContext] | ||
|
|
||
| if baseManager.IsInCluster() { | ||
| // In cluster mode, so context switching is not applicable | ||
| return baseManager, nil | ||
| } | ||
|
|
||
| m, err := baseManager.newForContext(context) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| k.managers[context] = m | ||
|
|
||
| return m, nil | ||
| } | ||
|
|
||
| func (k *kubeConfigClusterProvider) GetDefaultTarget() string { | ||
| return k.defaultContext | ||
| } | ||
|
|
||
| func (k *kubeConfigClusterProvider) WatchTargets(onKubeConfigChanged func() error) { | ||
| m := k.managers[k.defaultContext] | ||
|
|
||
| m.WatchKubeConfig(onKubeConfigChanged) | ||
| } | ||
|
|
||
| func (k *kubeConfigClusterProvider) Close() { | ||
| m := k.managers[k.defaultContext] | ||
|
|
||
| m.Close() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package kubernetes | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "sort" | ||
|
|
||
| "github.com/containers/kubernetes-mcp-server/pkg/config" | ||
| ) | ||
|
|
||
| // ProviderFactory creates a new ManagerProvider instance for a given strategy. | ||
| // Implementations should validate that the Manager is compatible with their strategy | ||
| // (e.g., kubeconfig provider should reject in-cluster managers). | ||
| type ProviderFactory func(m *Manager, cfg *config.StaticConfig) (ManagerProvider, error) | ||
|
|
||
| var providerFactories = make(map[string]ProviderFactory) | ||
|
|
||
| // RegisterProvider registers a provider factory for a given strategy name. | ||
| // This should be called from init() functions in provider implementation files. | ||
| // Panics if a provider is already registered for the given strategy. | ||
| func RegisterProvider(strategy string, factory ProviderFactory) { | ||
| if _, exists := providerFactories[strategy]; exists { | ||
| panic(fmt.Sprintf("provider already registered for strategy '%s'", strategy)) | ||
| } | ||
| providerFactories[strategy] = factory | ||
| } | ||
|
|
||
| // getProviderFactory retrieves a registered provider factory by strategy name. | ||
| // Returns an error if no provider is registered for the given strategy. | ||
| func getProviderFactory(strategy string) (ProviderFactory, error) { | ||
| factory, ok := providerFactories[strategy] | ||
| if !ok { | ||
| available := GetRegisteredStrategies() | ||
| return nil, fmt.Errorf("no provider registered for strategy '%s', available strategies: %v", strategy, available) | ||
| } | ||
| return factory, nil | ||
| } | ||
|
|
||
| // GetRegisteredStrategies returns a sorted list of all registered strategy names. | ||
| // This is useful for error messages and debugging. | ||
| func GetRegisteredStrategies() []string { | ||
| strategies := make([]string, 0, len(providerFactories)) | ||
| for strategy := range providerFactories { | ||
| strategies = append(strategies, strategy) | ||
| } | ||
| sort.Strings(strategies) | ||
| return strategies | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️ This is exactly what I was thinking of