mirror of
https://github.com/containers/kubernetes-mcp-server.git
synced 2025-10-23 01:22:57 +03:00
* refactor(kubernetes): streamline provider configuration and in-cluster detection - Removed IsInCluster method from Manager and created function scoped to the runtime environment. As a method, the implementation was not correct. Removed GetAPIServerHost method from Manager which is no used. - **Temporarily** added an `inCluster` field to the Manager struct but should be eventually removed since it doesn't really make sense to hava a Manager in-cluster or out-of-cluster in the multi-cluster scenario. - Provider resolution (resolveStrategy) is now clearer, added complete coverage for all scenarios. - Added additional coverage for provider and manager. Signed-off-by: Marc Nuri <marc@marcnuri.com> * refactor(kubernetes): update NewManager to accept kubeconfig context and simplify manager creation - Removes Provider.newForContext(context string) method. Signed-off-by: Marc Nuri <marc@marcnuri.com> --------- Signed-off-by: Marc Nuri <marc@marcnuri.com>
82 lines
2.6 KiB
Go
82 lines
2.6 KiB
Go
package kubernetes
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/containers/kubernetes-mcp-server/pkg/config"
|
|
authenticationv1api "k8s.io/api/authentication/v1"
|
|
)
|
|
|
|
// singleClusterProvider implements Provider for managing a single
|
|
// Kubernetes cluster. Used for in-cluster deployments or when multi-cluster
|
|
// support is disabled.
|
|
type singleClusterProvider struct {
|
|
strategy string
|
|
manager *Manager
|
|
}
|
|
|
|
var _ Provider = &singleClusterProvider{}
|
|
|
|
func init() {
|
|
RegisterProvider(config.ClusterProviderInCluster, newSingleClusterProvider(config.ClusterProviderInCluster))
|
|
RegisterProvider(config.ClusterProviderDisabled, newSingleClusterProvider(config.ClusterProviderDisabled))
|
|
}
|
|
|
|
// newSingleClusterProvider creates a provider that manages a single cluster.
|
|
// Validates that the manager is in-cluster when the in-cluster strategy is used.
|
|
func newSingleClusterProvider(strategy string) ProviderFactory {
|
|
return func(m *Manager, cfg *config.StaticConfig) (Provider, error) {
|
|
if cfg != nil && cfg.KubeConfig != "" && strategy == config.ClusterProviderInCluster {
|
|
return nil, fmt.Errorf("kubeconfig file %s cannot be used with the in-cluster ClusterProviderStrategy", cfg.KubeConfig)
|
|
}
|
|
if strategy == config.ClusterProviderInCluster && !IsInCluster(cfg) {
|
|
return nil, fmt.Errorf("server must be deployed in cluster for the in-cluster ClusterProviderStrategy")
|
|
}
|
|
|
|
return &singleClusterProvider{
|
|
manager: m,
|
|
strategy: strategy,
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
func (p *singleClusterProvider) IsOpenShift(ctx context.Context) bool {
|
|
return p.manager.IsOpenShift(ctx)
|
|
}
|
|
|
|
func (p *singleClusterProvider) VerifyToken(ctx context.Context, target, token, audience string) (*authenticationv1api.UserInfo, []string, error) {
|
|
if target != "" {
|
|
return nil, nil, fmt.Errorf("unable to get manager for other context/cluster with %s strategy", p.strategy)
|
|
}
|
|
return p.manager.VerifyToken(ctx, token, audience)
|
|
}
|
|
|
|
func (p *singleClusterProvider) GetTargets(_ context.Context) ([]string, error) {
|
|
return []string{""}, nil
|
|
}
|
|
|
|
func (p *singleClusterProvider) GetDerivedKubernetes(ctx context.Context, target string) (*Kubernetes, error) {
|
|
if target != "" {
|
|
return nil, fmt.Errorf("unable to get manager for other context/cluster with %s strategy", p.strategy)
|
|
}
|
|
|
|
return p.manager.Derived(ctx)
|
|
}
|
|
|
|
func (p *singleClusterProvider) GetDefaultTarget() string {
|
|
return ""
|
|
}
|
|
|
|
func (p *singleClusterProvider) GetTargetParameterName() string {
|
|
return ""
|
|
}
|
|
|
|
func (p *singleClusterProvider) WatchTargets(watch func() error) {
|
|
p.manager.WatchKubeConfig(watch)
|
|
}
|
|
|
|
func (p *singleClusterProvider) Close() {
|
|
p.manager.Close()
|
|
}
|