背景:
紧接 client-go gin 的简略整合二(list 列表相干进一步操作),namespace deployment service 都 list 列表展示了,总感觉还少点什么?比方显示集群中所有运行的 pod 列表?依据 namespace 显示 pod 列表?依照 deployment 名称查问所蕴含的 pod?总而言之这一部分就围绕着 pod 列表的展示开展了!
client-go gin 的简略整合二(list 列表相干再进阶)
1. 展示命名空间的 pod 相干信息
先确认一下须要获取的信息:
kubectl get pods -o wide
[root@zhangpeng ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-7b5d9df6b8-dsx8j 1/1 Running 0 5d19h 10.31.0.4 cn-beijing.172.25.84.228 <none> <none>
name status restarts ip node这几个必定三要搞上的 输入一下 pod 的 yaml 看还有什么要输入的
[root@zhangpeng ~]# kubectl get pods nginx-7b5d9df6b8-dsx8j -o yaml
createtime lables image也增加一下!
根本 copy 了一下 Namespace.go 外面 func ListNamespace 过去:
src/service/Pod.go
package service
import (
"context"
"github.com/gin-gonic/gin"
. "k8s-demo1/src/lib"
package service
import (
"context"
"github.com/gin-gonic/gin"
. "k8s-demo1/src/lib"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Pod struct {
Namespace string
Status string
Images string
NodeName string
CreateTime string
Labels map[string]string
}
func ListallPod(g *gin.Context) {ns := g.Query("ns")
pods, err := K8sClient.CoreV1().Pods(ns).List(context.Background(), metav1.ListOptions{})
if err != nil {g.Error(err)
}
ret := make([]*Pod, 0)
for _, item := range pods.Items {
ret = append(ret, &Pod{
Namespace: item.Namespace,
Name: item.Name,
Status: string(item.Status.Phase),
Labels: item.Labels,
NodeName: item.Spec.NodeName,
Images: item.Spec.Containers[0].Image,
CreateTime: item.CreationTimestamp.Format("2006-01-02 15:04:05"),
})
}
g.JSON(200, ret)
return
}
Status取了 Phase 的值应该是没有问题的吧?Images 跟 deployment 取值一样。原本开始筹备搞上 restart 的次数 …… 然而 kube-system 下 pod 有异样输入就先疏忽了!
main.go
package main
import (
"github.com/gin-gonic/gin"
"k8s-demo1/src/service"
)
func main() {r := gin.Default()
r.GET("/", func(context *gin.Context) {context.JSON(200, "hello")
})
r.GET("/namespaces", service.ListNamespace)
r.GET("/deployments", service.ListDeployment)
r.GET("/service", service.ListService)
r.GET("pods", service.ListallPod)
r.Run()}
减少 r.GET(“pods”, service.ListallPod)路由!运行 main.go
http://127.0.0.1:8080/pods
http://127.0.0.1:8080/pods?ns…
根本实现!
2. 依照 deployment 为条件显示对应 pod 列表
依照 namespace 命名空间辨别排序 pod 列表还是很简略的, 如何依照 deployment 名字去显示 pod 列表呢?
这边是照抄的沈老师的课程,本人还没有转过弯来 ……
deployment 是通过 selector 标签去匹配的 pod labels,我就先那么了解了 ……
写一个办法 GetPodsByDep 依据 namespace depoyment 名称获取 pod 相干信息:
src/service/Deployment.go 文件中:
func GetPodsByDep(ns string, dep *v1.Deployment) []*Pod {ctx := context.Background()
listopt := metav1.ListOptions{LabelSelector: GetLabels(dep.Spec.Selector.MatchLabels),
}
list, err := K8sClient.CoreV1().Pods(ns).List(ctx, listopt)
if err != nil {panic(err.Error())
}
pods := make([]*Pod, len(list.Items))
for i, pod := range list.Items {pods[i] = &Pod{
Namespace: pod.Namespace,
Name: pod.Name, // 获取 pod 名称
Status: string(pod.Status.Phase),
Images: pod.Spec.Containers[0].Image,
NodeName: pod.Spec.NodeName, // 所属节点
Labels: pod.Labels,
CreateTime: pod.CreationTimestamp.Format("2006-01-02 15:04:05"), // 创立工夫
}
}
return pods
}
src/service/Deployment.go 最终如下:
package service
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
. "k8s-demo1/src/lib"
v1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type Deployment struct {
Namespace string
Name string
Replicas int32
AvailableReplicas int32
UnavailableReplicas int32
Images string
CreateTime string
Labels map[string]string
Pods []*Pod}
func ListDeployment(g *gin.Context) {ns := g.Query("ns")
dps, err := K8sClient.AppsV1().Deployments(ns).List(context.Background(), metav1.ListOptions{})
if err != nil {g.Error(err)
}
ret := make([]*Deployment, 0)
for _, item := range dps.Items {
ret = append(ret, &Deployment{
Name: item.Name,
Replicas: item.Status.Replicas,
AvailableReplicas: item.Status.AvailableReplicas,
UnavailableReplicas: item.Status.UnavailableReplicas,
Images: item.Spec.Template.Spec.Containers[0].Image,
Labels: item.GetLabels(),})
}
g.JSON(200, ret)
return
}
func GetPodsByDep(ns string, dep *v1.Deployment) []*Pod {ctx := context.Background()
listopt := metav1.ListOptions{LabelSelector: GetLabels(dep.Spec.Selector.MatchLabels),
}
list, err := K8sClient.CoreV1().Pods(ns).List(ctx, listopt)
if err != nil {panic(err.Error())
}
pods := make([]*Pod, len(list.Items))
for i, pod := range list.Items {pods[i] = &Pod{
Namespace: pod.Namespace,
Name: pod.Name, // 获取 pod 名称
Status: string(pod.Status.Phase),
Images: pod.Spec.Containers[0].Image,
NodeName: pod.Spec.NodeName, // 所属节点
Labels: pod.Labels,
CreateTime: pod.CreationTimestamp.Format("2006-01-02 15:04:05"), // 创立工夫
}
}
return pods
}
func GetDeployment(g *gin.Context) {ns := g.Query("ns")
name := g.Query("name")
ctx := context.Background()
getopt := metav1.GetOptions{}
dps, err := K8sClient.AppsV1().Deployments(ns).Get(ctx, name, getopt)
if err != nil {g.Error(err)
}
ret := make([]*Deployment, 0)
ret = append(ret, &Deployment{
Namespace: dps.Namespace,
Name: dps.Name,
Replicas: dps.Status.Replicas,
AvailableReplicas: dps.Status.AvailableReplicas,
UnavailableReplicas: dps.Status.UnavailableReplicas,
Images: dps.Spec.Template.Spec.Containers[0].Image,
CreateTime: dps.CreationTimestamp.Format("2006-01-02 15:03:04"),
Labels: dps.Labels,
Pods: GetPodsByDep(ns, dps),
})
g.JSON(200, ret)
return
}
main.go 如下:
package main
import (
"github.com/gin-gonic/gin"
"k8s-demo1/src/service"
)
func main() {r := gin.Default()
r.GET("/", func(context *gin.Context) {context.JSON(200, "hello")
})
r.GET("/namespaces", service.ListNamespace)
r.GET("/deployments", service.ListDeployment)
r.GET("/service", service.ListService)
r.GET("/deployment", service.GetDeployment)
r.GET("pods", service.ListallPod)
r.Run()}
运行 main.go
http://127.0.0.1:8080/deploym…
后记:
- GetPodsByDep还是有点懵
- 指针还是蛊惑
- map 啥的数据格式啥的还是蒙懵阿 ……
- 这一周好好消化一下 list……
- 前面貌似还有 informer? list watch 机制啥的 …… 一步一步来吧!
- 最近工作有点杂,更新会慢一些。然而会继续更新 ing……