跳到主要内容

API 概述

KubePolaris 提供 RESTful API,支持通过 API 进行自动化操作。

基本信息

项目
基础路径/api
版本v1
协议HTTP/HTTPS
格式JSON

认证

JWT Token

所有 API 请求需要携带 JWT Token:

curl -H "Authorization: Bearer <your-token>" \
https://kubepolaris.example.com/api/clusters

获取 Token

# 登录获取 Token
curl -X POST https://kubepolaris.example.com/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": "admin123"}'

# 响应
{
"code": 200,
"data": {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"expire": "2026-01-08T10:00:00Z"
}
}

刷新 Token

curl -X POST https://kubepolaris.example.com/api/auth/refresh \
-H "Authorization: Bearer <your-token>"

响应格式

成功响应

{
"code": 200,
"message": "success",
"data": {
// 实际数据
}
}

分页响应

{
"code": 200,
"message": "success",
"data": {
"items": [],
"total": 100,
"page": 1,
"pageSize": 20
}
}

错误响应

{
"code": 400,
"message": "Invalid request",
"data": null
}

状态码

状态码说明
200成功
201创建成功
400请求错误
401未认证
403无权限
404资源不存在
500服务器错误

API 端点

认证

方法路径说明
POST/api/auth/login登录
POST/api/auth/logout登出
POST/api/auth/refresh刷新 Token
GET/api/auth/me当前用户信息

集群

方法路径说明
GET/api/clusters集群列表
POST/api/clusters添加集群
GET/api/clusters/:id集群详情
PUT/api/clusters/:id更新集群
DELETE/api/clusters/:id删除集群
POST/api/clusters/:id/test测试连接

节点

方法路径说明
GET/api/clusters/:id/nodes节点列表
GET/api/clusters/:id/nodes/:name节点详情
POST/api/clusters/:id/nodes/:name/cordon禁止调度
POST/api/clusters/:id/nodes/:name/uncordon恢复调度
POST/api/clusters/:id/nodes/:name/drain排空节点

工作负载

方法路径说明
GET/api/clusters/:id/workloads工作负载列表
GET/api/clusters/:id/namespaces/:ns/deployments/:nameDeployment 详情
PUT/api/clusters/:id/namespaces/:ns/deployments/:name更新 Deployment
DELETE/api/clusters/:id/namespaces/:ns/deployments/:name删除 Deployment
POST/api/clusters/:id/namespaces/:ns/deployments/:name/scale扩缩容
POST/api/clusters/:id/namespaces/:ns/deployments/:name/restart重启

Pod

方法路径说明
GET/api/clusters/:id/podsPod 列表
GET/api/clusters/:id/namespaces/:ns/pods/:namePod 详情
DELETE/api/clusters/:id/namespaces/:ns/pods/:name删除 Pod
GET/api/clusters/:id/namespaces/:ns/pods/:name/logsPod 日志

用户

方法路径说明
GET/api/users用户列表
POST/api/users创建用户
GET/api/users/:id用户详情
PUT/api/users/:id更新用户
DELETE/api/users/:id删除用户

示例

获取集群列表

curl -X GET https://kubepolaris.example.com/api/clusters \
-H "Authorization: Bearer <token>"

创建集群

curl -X POST https://kubepolaris.example.com/api/clusters \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{
"name": "production",
"apiServer": "https://k8s.example.com:6443",
"kubeConfig": "base64-encoded-kubeconfig"
}'

扩缩容

curl -X POST https://kubepolaris.example.com/api/clusters/1/namespaces/default/deployments/nginx/scale \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-d '{"replicas": 5}'

SDK

Go SDK

import "github.com/clay-wangzhi/KubePolaris-go-sdk"

client := kubepolaris.NewClient(
kubepolaris.WithBaseURL("https://kubepolaris.example.com"),
kubepolaris.WithToken("your-token"),
)

clusters, err := client.Clusters.List(context.Background())

Python SDK

from kubepolaris import Client

client = Client(
base_url="https://kubepolaris.example.com",
token="your-token"
)

clusters = client.clusters.list()

更多