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>
109 lines
3.5 KiB
Go
109 lines
3.5 KiB
Go
package kubernetes
|
|
|
|
import (
|
|
"github.com/containers/kubernetes-mcp-server/pkg/config"
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
"k8s.io/client-go/rest"
|
|
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
|
|
}
|
|
|
|
func IsInCluster(cfg *config.StaticConfig) bool {
|
|
// Even if running in-cluster, if a kubeconfig is provided, we consider it as out-of-cluster
|
|
if cfg != nil && cfg.KubeConfig != "" {
|
|
return false
|
|
}
|
|
restConfig, err := InClusterConfig()
|
|
return err == nil && restConfig != nil
|
|
}
|
|
|
|
func (k *Kubernetes) NamespaceOrDefault(namespace string) string {
|
|
return k.manager.NamespaceOrDefault(namespace)
|
|
}
|
|
|
|
// ConfigurationContextsDefault returns the current context name
|
|
// TODO: Should be moved to the Provider level ?
|
|
func (k *Kubernetes) ConfigurationContextsDefault() (string, error) {
|
|
if k.manager.inCluster {
|
|
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.inCluster {
|
|
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.inCluster {
|
|
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)
|
|
}
|