You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
935 B

1 year ago
package node
import (
"context"
1 year ago
"k8s-manager/handler/common"
1 year ago
"log"
"net/http"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/gin-gonic/gin"
)
// List 获取NodeList
func List(ctx *gin.Context) {
1 year ago
// 1. 得到clientClientSet对象
clientSet, err := common.GetClientSet()
1 year ago
if err != nil {
log.Println(err)
ctx.JSON(http.StatusOK, gin.H{
1 year ago
// code == 2, 意味着配置文件有问题
"code": 2,
1 year ago
"message": err.Error(),
})
return
}
// 2. 发出获取节点列表的请求
nodeList, err := clientSet.CoreV1().Nodes().List(
context.Background(),
metav1.ListOptions{},
)
if err != nil {
log.Println(err)
ctx.JSON(http.StatusOK, gin.H{
"code": 1,
"message": err.Error(),
})
return
}
// 3. 接收 k8s cluster 的 apiServer 的响应,处理
// 整理 NodeList 对象
// 4. 给前端做响应
ctx.JSON(http.StatusOK, gin.H{
"code": 0,
"data": nodeList,
})
}