|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/mark3labs/mcp-go/mcp" |
| 8 | + "github.com/weibaohui/kom/mcp/metadata" |
| 9 | +) |
| 10 | + |
| 11 | +var resourceMap = map[string]metadata.ResourceInfo{ |
| 12 | + // 命名空间级别资源 |
| 13 | + "pod": {Group: "", Version: "v1", Kind: "Pod", Namespaced: true}, |
| 14 | + "deployment": {Group: "apps", Version: "v1", Kind: "Deployment", Namespaced: true}, |
| 15 | + "statefulset": {Group: "apps", Version: "v1", Kind: "StatefulSet", Namespaced: true}, |
| 16 | + "daemonset": {Group: "apps", Version: "v1", Kind: "DaemonSet", Namespaced: true}, |
| 17 | + "replicaset": {Group: "apps", Version: "v1", Kind: "ReplicaSet", Namespaced: true}, |
| 18 | + "service": {Group: "", Version: "v1", Kind: "Service", Namespaced: true}, |
| 19 | + "configmap": {Group: "", Version: "v1", Kind: "ConfigMap", Namespaced: true}, |
| 20 | + "secret": {Group: "", Version: "v1", Kind: "Secret", Namespaced: true}, |
| 21 | + "ingress": {Group: "networking.k8s.io", Version: "v1", Kind: "Ingress", Namespaced: true}, |
| 22 | + "networkpolicy": {Group: "networking.k8s.io", Version: "v1", Kind: "NetworkPolicy", Namespaced: true}, |
| 23 | + "role": {Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "Role", Namespaced: true}, |
| 24 | + "rolebinding": {Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "RoleBinding", Namespaced: true}, |
| 25 | + "serviceaccount": {Group: "", Version: "v1", Kind: "ServiceAccount", Namespaced: true}, |
| 26 | + "persistentvolumeclaim": {Group: "", Version: "v1", Kind: "PersistentVolumeClaim", Namespaced: true}, |
| 27 | + "horizontalpodautoscaler": {Group: "autoscaling", Version: "v2", Kind: "HorizontalPodAutoscaler", Namespaced: true}, |
| 28 | + "cronjob": {Group: "batch", Version: "v1", Kind: "CronJob", Namespaced: true}, |
| 29 | + "job": {Group: "batch", Version: "v1", Kind: "Job", Namespaced: true}, |
| 30 | + "node": {Group: "", Version: "v1", Kind: "Node", Namespaced: false}, |
| 31 | + "namespace": {Group: "", Version: "v1", Kind: "Namespace", Namespaced: false}, |
| 32 | + "persistentvolume": {Group: "", Version: "v1", Kind: "PersistentVolume", Namespaced: false}, |
| 33 | + "clusterrole": {Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRole", Namespaced: false}, |
| 34 | + "clusterrolebinding": {Group: "rbac.authorization.k8s.io", Version: "v1", Kind: "ClusterRoleBinding", Namespaced: false}, |
| 35 | + "storageclass": {Group: "storage.k8s.io", Version: "v1", Kind: "StorageClass", Namespaced: false}, |
| 36 | + "customresourcedefinition": {Group: "apiextensions.k8s.io", Version: "v1", Kind: "CustomResourceDefinition", Namespaced: false}, |
| 37 | + "mutatingwebhookconfiguration": {Group: "admissionregistration.k8s.io", Version: "v1", Kind: "MutatingWebhookConfiguration", Namespaced: false}, |
| 38 | + "validatingwebhookconfiguration": {Group: "admissionregistration.k8s.io", Version: "v1", Kind: "ValidatingWebhookConfiguration", Namespaced: false}, |
| 39 | +} |
| 40 | + |
| 41 | +// GetResourceInfo 根据资源类型字符串返回资源信息 |
| 42 | +func GetResourceInfo(resourceType string) (metadata.ResourceInfo, bool) { |
| 43 | + resourceType = strings.ToLower(resourceType) |
| 44 | + if info, exists := resourceMap[resourceType]; exists { |
| 45 | + return info, true |
| 46 | + } |
| 47 | + return metadata.ResourceInfo{}, false |
| 48 | +} |
| 49 | + |
| 50 | +// IsNamespaced 判断资源是否为命名空间级别 |
| 51 | +func IsNamespaced(resourceType string) bool { |
| 52 | + resourceType = strings.ToLower(resourceType) |
| 53 | + if info, exists := resourceMap[resourceType]; exists { |
| 54 | + return info.Namespaced |
| 55 | + } |
| 56 | + return false |
| 57 | +} |
| 58 | + |
| 59 | +// ParseFromRequest 从请求中解析资源元数据 |
| 60 | +func ParseFromRequest(ctx context.Context, request mcp.CallToolRequest, serverConfig *metadata.ServerConfig) (context.Context, *metadata.ResourceMetadata, error) { |
| 61 | + if serverConfig != nil { |
| 62 | + if authVal, ok := ctx.Value(serverConfig.AuthKey).(string); ok { |
| 63 | + ctx = context.WithValue(ctx, serverConfig.AuthKey, authVal) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + // 验证必要参数 |
| 68 | + // 获取cluster参数,如果不存在则使用默认值空字符串 |
| 69 | + cluster := "" |
| 70 | + if clusterVal, ok := request.Params.Arguments["cluster"].(string); ok { |
| 71 | + cluster = clusterVal |
| 72 | + } |
| 73 | + |
| 74 | + // 获取name参数,如果不存在则返回错误 |
| 75 | + name := "" |
| 76 | + if nameVal, ok := request.Params.Arguments["name"].(string); ok { |
| 77 | + name = nameVal |
| 78 | + } |
| 79 | + |
| 80 | + // 获取命名空间参数(可选,支持集群级资源) |
| 81 | + namespace := "" |
| 82 | + if ns, ok := request.Params.Arguments["namespace"].(string); ok { |
| 83 | + namespace = ns |
| 84 | + } |
| 85 | + |
| 86 | + // 获取资源类型信息 |
| 87 | + var group, version, kind string |
| 88 | + if resourceType, ok := request.Params.Arguments["kind"].(string); ok && resourceType != "" { |
| 89 | + // 如果提供了resourceType,从type.go获取资源信息 |
| 90 | + if info, exists := GetResourceInfo(resourceType); exists { |
| 91 | + // 优先使用用户指定的GVK,如果未指定则使用默认值 |
| 92 | + group = getStringParam(request, "group", info.Group) |
| 93 | + version = getStringParam(request, "version", info.Version) |
| 94 | + kind = getStringParam(request, "kind", info.Kind) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + // 如果没有通过resourceType获取到信息,则使用直接指定的GVK |
| 99 | + if group == "" { |
| 100 | + group = getStringParam(request, "group", "") |
| 101 | + } |
| 102 | + if version == "" { |
| 103 | + version = getStringParam(request, "version", "") |
| 104 | + } |
| 105 | + if kind == "" { |
| 106 | + kind = getStringParam(request, "kind", "") |
| 107 | + } |
| 108 | + |
| 109 | + return ctx, &metadata.ResourceMetadata{ |
| 110 | + Cluster: cluster, |
| 111 | + Namespace: namespace, |
| 112 | + Name: name, |
| 113 | + Group: group, |
| 114 | + Version: version, |
| 115 | + Kind: kind, |
| 116 | + }, nil |
| 117 | +} |
| 118 | + |
| 119 | +// getStringParam 从请求参数中获取字符串值,如果不存在或无效则返回默认值 |
| 120 | +func getStringParam(request mcp.CallToolRequest, key, defaultValue string) string { |
| 121 | + if value, ok := request.Params.Arguments[key].(string); ok && value != "" { |
| 122 | + return value |
| 123 | + } |
| 124 | + return defaultValue |
| 125 | +} |
0 commit comments