赞
踩
go get github.com/gin-gonic/gin@v1.6.3
go get k8s.io/client-go@v0.20.2
package client import ( "k8s.io/client-go/kubernetes" "k8s.io/client-go/tools/clientcmd" "log" ) func InitK8sClient() *kubernetes.Clientset { config, err := clientcmd. BuildConfigFromFlags("", "config") if err != nil { log.Fatal(err) } // config.Insecure 假如k8s是内网可以走https则不需要设置 config.Insecure = true clientset, err := kubernetes.NewForConfig(config) if err != nil { log.Fatal(err) } return clientset }
package main
import (
"fmt"
"gin_k8s_deploy/client"
)
func main() {
k8sClient := client.InitK8sClient()
fmt.Println(k8sClient)
}
package main import ( "fmt" "gin_k8s_deploy/client" "github.com/gin-gonic/gin" ) func main() { k8sClient := client.InitK8sClient() fmt.Println(k8sClient) // gin实例 r := gin.New() // 加载html模板 r.LoadHTMLGlob("templates/*") // 后端返回页面 r.GET("/", func(c *gin.Context) { c.HTML(200, "index.html", gin.H{"data": "success"}) }) // 运行 r.Run(":8080") }
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
{{ .data }}
</body>
</html>
// k8s 命名空间 接口 r.GET("/ns", func(c *gin.Context) { // namespace 在 k8s 的核心(core)api组 ns, err := k8sClient. CoreV1().Namespaces(). List(context.Background(), metav1.ListOptions{}) if err != nil { log.Fatal(err) } nsRet := make([]string, 0) for _, nsItem := range ns.Items { nsRet = append(nsRet, nsItem.Name) } c.JSON(200, gin.H{"ns_list": nsRet}) })
// k8s deployment 接口 r.GET("/:ns/deployment/list", func(c *gin.Context) { ns := c.Param("ns") // deployment在k8s的 appsv1 api组 deployment, err := k8sClient. AppsV1().Deployments(ns). List(context.Background(), metav1.ListOptions{}) if err != nil { log.Fatal(err) } deploymentRet := make([]string, 0) for _, item := range deployment.Items { deploymentRet = append(deploymentRet, item.Name) } c.JSON(200, gin.H{"deployment_list": deploymentRet}) })
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script> // 调用后端 /ns 接口,返回ns的数组 function getK8sNsList() { return fetch("/ns", { method: 'GET', headers: { 'Content-Type': 'application/json', } }).then(response => { return response.json() }).then(data => { return data.ns_list }) } // 使用dom将ns数组的值保存到select框中的option function displayK8sNsList(ns_list) { ns_list.forEach(ns => { var ele = document.getElementById("selectNs") var option = document.createElement("option") option.name = ns option.value = ns option.text = ns ele.add(option) }) } // 获取后端 /ns 返回的第一个命名空间 function getFirstK8sNsList() { return fetch("/ns", { method: 'GET', headers: { 'Content-Type': 'application/json', } }).then(response => { return response.json() }).then(data => { return data.ns_list[0] }) } // 调用后盾 /deployment/:ns/list 接口,返回deployment数组 function getK8sDeployments(ns) { return fetch("/deployment/" + ns + "/list", { method: 'GET', headers: { 'Content-Type': 'application/json', } }).then(response => { return response.json() }).then(data => { return data.deployment_list }) } // function displayK8sDeploymentList(deployment_list) { var ele = document.getElementById("deployment_list") deployment_list.forEach(deployment => { var li = document.createElement("li") li.textContent= deployment ele.appendChild(li) }) } // select框选中其他值时进行请求 function onNamespaceChange() { var selectedNs = document.getElementById("selectNs").value; var deploymentList = document.getElementById("deployment_list"); while (deploymentList.firstChild) { deploymentList.removeChild(deploymentList.firstChild); } getK8sDeployments(selectedNs).then(deployment_list => { displayK8sDeploymentList(deployment_list) }) } </script> </head> <body> <div class="header"> <h1>K8s面板</h1> <p style="display: inline-block">请选择命名空间: </p> <select name="selectNs" id="selectNs" onchange="onNamespaceChange()"> </select> </div> <div class="content"> <p>deployments</p> <ul id="deployment_list"> </ul> </div> <script> // select框中填充ns数组 getK8sNsList().then(ns_list => { displayK8sNsList(ns_list) }) // 首先打印第一个命名空间的deployments getFirstK8sNsList().then(ns => { getK8sDeployments(ns).then(deployment_list => { displayK8sDeploymentList(deployment_list) }) }) </script> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。