MCP HubMCP Hub
weibaohui

kom

by: weibaohui

kom 是一个用于 Kubernetes 操作的工具,SDK级的kubectl、client-go的使用封装。并且支持作为管理k8s 的 MCP server。 它提供了一系列功能来管理 Kubernetes 资源,包括创建、更新、删除和获取资源,甚至使用SQL查询k8s资源。这个项目支持多种 Kubernetes 资源类型的操作,并能够处理自定义资源定义(CRD)。 通过使用 kom,你可以轻松地进行资源的增删改查和日志获取以及操作POD内文件等动作。

82created 21/10/2024
Visit
Kubernetes
SDK

📌Overview

Purpose: kom is designed to facilitate Kubernetes operations, providing a simplified interface for managing various Kubernetes resources.

Overview: kom is a tool that encapsulates the functionalities of SDK-level kubectl and client-go, allowing users to easily create, update, delete, and retrieve Kubernetes resources, including support for Custom Resource Definitions (CRD). It streamlines resource management within Kubernetes clusters and enables various operational tasks with intuitive methods, including the ability to query resources using SQL statements.

Key Features:

  • Ease of Use: Offers comprehensive functions for managing built-in and CRD resources with straightforward operations for creation, update, deletion, and listing.

  • Multi-Cluster Support: Simplifies management across multiple Kubernetes clusters through the RegisterCluster functionality, allowing seamless cluster operations.

  • Custom Resource Definition (CRD) Support: Enables users to define and interact with custom resources effortlessly.

  • Callback Mechanism: Facilitates business logic extensions by allowing custom callback registration for various operations, enhancing integration without tight coupling to Kubernetes.

  • File Operations in Pod: Provides functions for seamless file management within Pods, including uploading, downloading, and deleting files.

  • SQL Query Support: Streamlines resource queries using SQL, allowing for advanced filtering and retrieval of Kubernetes resources.


Kom - Kubernetes Operations Manager

English | 中文

简介

kom 是一个用于 Kubernetes 操作的工具,它提供了一系列功能来管理 Kubernetes 资源,包括创建、更新、删除和获取资源,支持多种 Kubernetes 资源类型的操作以及自定义资源定义(CRD)。

特点

  • 简单易用:支持创建、更新、删除、获取、列表等功能。
  • 多集群支持:通过RegisterCluster管理多个 Kubernetes 集群。
  • MCP支持:支持多集群的MCP管理和SSE模式。
  • 跨命名空间支持:能够跨命名空间查询资源。
  • 链式调用:提供链式调用,使得操作资源更加简单和直观。
  • 自定义资源定义支持:支持自定义资源的定义和操作。
  • 回调机制支持:轻松拓展业务逻辑。
  • POD内文件操作:支持上传、下载、删除文件。
  • 高频操作封装:如deployment的重启、扩缩容等功能。
  • SQL查询:允许使用SQL查询Kubernetes资源。
  • 查询缓存:提升高频、批量查询性能。

安装

import (
    "github.com/weibaohui/kom"
    "github.com/weibaohui/kom/callbacks"
)

func main() {
    callbacks.RegisterInit()
    defaultKubeConfig := os.Getenv("KUBECONFIG")
    if defaultKubeConfig == "" {
        defaultKubeConfig = filepath.Join(homedir.HomeDir(), ".kube", "config")
    }
    _, _ = kom.Clusters().RegisterInCluster()
    _, _ = kom.Clusters().RegisterByPathWithID(defaultKubeConfig, "default")
    kom.Clusters().Show()
}

使用示例

多集群 k8s MCP 支持

mcp.RunMCPServer("kom mcp server", "0.0.1", 9096)

多集群管理

注册多集群

kom.Clusters().RegisterInCluster()
kom.Clusters().RegisterByPathWithID("/Users/kom/.kube/orb", "orb")
kom.Clusters().RegisterByPathWithID("/Users/kom/.kube/config", "docker-desktop")
kom.Clusters().RegisterByPathWithID("/Users/kom/.kube/config", "default")

显示已注册集群

kom.Clusters().Show()

选择集群

// 使用默认集群查询资源
var pods []corev1.Pod
err = kom.DefaultCluster().Resource(&corev1.Pod{}).Namespace("kube-system").List(&pods).Error

内置资源对象的操作

var item v1.Deployment

// 创建资源
item = v1.Deployment{
    ObjectMeta: metav1.ObjectMeta{
        Name:      "nginx",
        Namespace: "default",
    },
    Spec: v1.DeploymentSpec{
        Template: corev1.PodTemplateSpec{
            Spec: corev1.PodSpec{
                Containers: []corev1.Container{{Name: "test", Image: "nginx:1.14.2"}},
            },
        },
    },
}
err := kom.DefaultCluster().Resource(&item).Create(&item).Error

YAML 操作

yaml := `apiVersion: v1
kind: ConfigMap
metadata:
  name: example-config
  namespace: default
data:
  key: value
`
results := kom.DefaultCluster().Applier().Apply(yaml)
results = kom.DefaultCluster().Applier().Delete(yaml)

Pod 操作

获取日志

var stream io.ReadCloser
err := kom.DefaultCluster().Namespace("default").Name("random-char-pod").Ctl().Pod().ContainerName("container").GetLogs(&stream, &corev1.PodLogOptions{}).Error

自定义资源定义(CRD)操作

var item unstructured.Unstructured

// 创建CRD的CR对象
item = unstructured.Unstructured{
    Object: map[string]interface{}{
        "apiVersion": "stable.example.com/v1",
        "kind":       "CronTab",
        "metadata": map[string]interface{}{
            "name":      "test-crontab",
            "namespace": "default",
        },
        "spec": map[string]interface{}{
            "cronSpec": "* * * * */8",
            "image":    "test-crontab-image",
        },
    },
}
err := kom.DefaultCluster().CRD("stable.example.com", "v1", "CronTab").Namespace(item.GetNamespace()).Name(item.GetName()).Create(&item).Error

k8s版本兼容性测试

k8s版本测试结果
V1.31.3
V1.30.7
V1.25.7
V1.21.0

联系我

微信:大罗马的太阳,搜索ID:daluomadetaiyang,备注kom。