mirror of
https://github.com/openshift/openshift-mcp-server.git
synced 2025-10-17 14:27:48 +03:00
* feat: add cluster provider for kubeconfig Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: move server to use ClusterProvider interface Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: authentication middleware works with cluster provider Signed-off-by: Calum Murray <cmurray@redhat.com> * fix: unit tests work after cluster provider changes Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: add tool mutator to add cluster parameter Signed-off-by: Calum Murray <cmurray@redhat.com> * test: handle cluster parameter Signed-off-by: Calum Murray <cmurray@redhat.com> * fix: handle lazy init correctly Signed-off-by: Calum Murray <cmurray@redhat.com> * refactor: move to using multi-strategy ManagerProvider Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: add contexts_list tool Signed-off-by: Calum Murray <cmurray@redhat.com> * refactor: make tool mutator generic between cluster/context naming Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: introduce tool filter Signed-off-by: Calum Murray <cmurray@redhat.com> * refactor: use new ManagerProvider/mutator/filter within mcp server Signed-off-by: Calum Murray <cmurray@redhat.com> * fix(test): tests expect context parameter in tool defs Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: auth handles multi-cluster case correctly Signed-off-by: Calum Murray <cmurray@redhat.com> * fix: small changes from local testing Signed-off-by: Calum Murray <cmurray@redhat.com> * chore: fix enum test Signed-off-by: Calum Murray <cmurray@redhat.com> * review: Multi Cluster support (#1) * nit: rename contexts_list to configuration_contexts_list Besides the conventional naming, it helps LLMs understand the context of the tool by providing a certain level of hierarchy. Signed-off-by: Marc Nuri <marc@marcnuri.com> * fix(mcp): ToolMutator doesn't rely on magic strings Signed-off-by: Marc Nuri <marc@marcnuri.com> * refactor(api): don't expose ManagerProvider to toolsets Signed-off-by: Marc Nuri <marc@marcnuri.com> * test(mcp): configuration_contexts_list basic tests Signed-off-by: Marc Nuri <marc@marcnuri.com> * test(toolsets): revert edge-case test This test should not be touched. Signed-off-by: Marc Nuri <marc@marcnuri.com> * test(toolsets): add specific metadata tests for multi-cluster Signed-off-by: Marc Nuri <marc@marcnuri.com> * fix(mcp): ToolFilter doesn't rely on magic strings (partially) Signed-off-by: Marc Nuri <marc@marcnuri.com> * test(api): IsClusterAware and IsTargetListProvider default values Signed-off-by: Marc Nuri <marc@marcnuri.com> * test(mcp): revert unneeded changes in mcp_tools_test.go Signed-off-by: Marc Nuri <marc@marcnuri.com> --------- Signed-off-by: Marc Nuri <marc@marcnuri.com> * fix: always include configuration_contexts_list if contexts > 1 Signed-off-by: Calum Murray <cmurray@redhat.com> * feat: include server urls in configuration_contexts_list Signed-off-by: Calum Murray <cmurray@redhat.com> --------- Signed-off-by: Calum Murray <cmurray@redhat.com> Signed-off-by: Marc Nuri <marc@marcnuri.com> Co-authored-by: Marc Nuri <marc@marcnuri.com>
157 lines
4.9 KiB
Go
157 lines
4.9 KiB
Go
package kubernetes
|
|
|
|
import (
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/client-go/rest"
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
|
|
"k8s.io/client-go/tools/clientcmd/api/latest"
|
|
)
|
|
|
|
const inClusterKubeConfigDefaultContext = "in-cluster"
|
|
|
|
// InClusterConfig is a variable that holds the function to get the in-cluster config
|
|
// Exposed for testing
|
|
var InClusterConfig = func() (*rest.Config, error) {
|
|
// TODO use kubernetes.default.svc instead of resolved server
|
|
// Currently running into: `http: server gave HTTP response to HTTPS client`
|
|
inClusterConfig, err := rest.InClusterConfig()
|
|
if inClusterConfig != nil {
|
|
inClusterConfig.Host = "https://kubernetes.default.svc"
|
|
}
|
|
return inClusterConfig, err
|
|
}
|
|
|
|
// resolveKubernetesConfigurations resolves the required kubernetes configurations and sets them in the Kubernetes struct
|
|
func resolveKubernetesConfigurations(kubernetes *Manager) error {
|
|
// Always set clientCmdConfig
|
|
pathOptions := clientcmd.NewDefaultPathOptions()
|
|
if kubernetes.staticConfig.KubeConfig != "" {
|
|
pathOptions.LoadingRules.ExplicitPath = kubernetes.staticConfig.KubeConfig
|
|
}
|
|
kubernetes.clientCmdConfig = clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
|
|
pathOptions.LoadingRules,
|
|
&clientcmd.ConfigOverrides{ClusterInfo: clientcmdapi.Cluster{Server: ""}})
|
|
var err error
|
|
if kubernetes.IsInCluster() {
|
|
kubernetes.cfg, err = InClusterConfig()
|
|
if err == nil && kubernetes.cfg != nil {
|
|
return nil
|
|
}
|
|
}
|
|
// Out of cluster
|
|
kubernetes.cfg, err = kubernetes.clientCmdConfig.ClientConfig()
|
|
if kubernetes.cfg != nil && kubernetes.cfg.UserAgent == "" {
|
|
kubernetes.cfg.UserAgent = rest.DefaultKubernetesUserAgent()
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (m *Manager) IsInCluster() bool {
|
|
if m.staticConfig.KubeConfig != "" {
|
|
return false
|
|
}
|
|
cfg, err := InClusterConfig()
|
|
return err == nil && cfg != nil
|
|
}
|
|
|
|
func (m *Manager) configuredNamespace() string {
|
|
if ns, _, nsErr := m.clientCmdConfig.Namespace(); nsErr == nil {
|
|
return ns
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func (m *Manager) NamespaceOrDefault(namespace string) string {
|
|
if namespace == "" {
|
|
return m.configuredNamespace()
|
|
}
|
|
return namespace
|
|
}
|
|
|
|
func (k *Kubernetes) NamespaceOrDefault(namespace string) string {
|
|
return k.manager.NamespaceOrDefault(namespace)
|
|
}
|
|
|
|
// ToRESTConfig returns the rest.Config object (genericclioptions.RESTClientGetter)
|
|
func (m *Manager) ToRESTConfig() (*rest.Config, error) {
|
|
return m.cfg, nil
|
|
}
|
|
|
|
// ToRawKubeConfigLoader returns the clientcmd.ClientConfig object (genericclioptions.RESTClientGetter)
|
|
func (m *Manager) ToRawKubeConfigLoader() clientcmd.ClientConfig {
|
|
return m.clientCmdConfig
|
|
}
|
|
|
|
// ConfigurationContextsDefault returns the current context name
|
|
// TODO: Should be moved to the Provider level ?
|
|
func (k *Kubernetes) ConfigurationContextsDefault() (string, error) {
|
|
if k.manager.IsInCluster() {
|
|
return inClusterKubeConfigDefaultContext, nil
|
|
}
|
|
cfg, err := k.manager.clientCmdConfig.RawConfig()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return cfg.CurrentContext, nil
|
|
}
|
|
|
|
// ConfigurationContextsList returns the list of available context names
|
|
// TODO: Should be moved to the Provider level ?
|
|
func (k *Kubernetes) ConfigurationContextsList() (map[string]string, error) {
|
|
if k.manager.IsInCluster() {
|
|
return map[string]string{inClusterKubeConfigDefaultContext: ""}, nil
|
|
}
|
|
cfg, err := k.manager.clientCmdConfig.RawConfig()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
contexts := make(map[string]string, len(cfg.Contexts))
|
|
for name, context := range cfg.Contexts {
|
|
cluster, ok := cfg.Clusters[context.Cluster]
|
|
if !ok || cluster.Server == "" {
|
|
contexts[name] = "unknown"
|
|
} else {
|
|
contexts[name] = cluster.Server
|
|
}
|
|
}
|
|
return contexts, nil
|
|
}
|
|
|
|
// ConfigurationView returns the current kubeconfig content as a kubeconfig YAML
|
|
// If minify is true, keeps only the current-context and the relevant pieces of the configuration for that context.
|
|
// If minify is false, all contexts, clusters, auth-infos, and users are returned in the configuration.
|
|
// TODO: Should be moved to the Provider level ?
|
|
func (k *Kubernetes) ConfigurationView(minify bool) (runtime.Object, error) {
|
|
var cfg clientcmdapi.Config
|
|
var err error
|
|
if k.manager.IsInCluster() {
|
|
cfg = *clientcmdapi.NewConfig()
|
|
cfg.Clusters["cluster"] = &clientcmdapi.Cluster{
|
|
Server: k.manager.cfg.Host,
|
|
InsecureSkipTLSVerify: k.manager.cfg.Insecure,
|
|
}
|
|
cfg.AuthInfos["user"] = &clientcmdapi.AuthInfo{
|
|
Token: k.manager.cfg.BearerToken,
|
|
}
|
|
cfg.Contexts[inClusterKubeConfigDefaultContext] = &clientcmdapi.Context{
|
|
Cluster: "cluster",
|
|
AuthInfo: "user",
|
|
}
|
|
cfg.CurrentContext = inClusterKubeConfigDefaultContext
|
|
} else if cfg, err = k.manager.clientCmdConfig.RawConfig(); err != nil {
|
|
return nil, err
|
|
}
|
|
if minify {
|
|
if err = clientcmdapi.MinifyConfig(&cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
//nolint:staticcheck
|
|
if err = clientcmdapi.FlattenConfig(&cfg); err != nil {
|
|
// ignore error
|
|
//return "", err
|
|
}
|
|
return latest.Scheme.ConvertToVersion(&cfg, latest.ExternalVersion)
|
|
}
|