背景:
完成了client-go gin的简单整合七-继续完善,准备整合一下前面的代码(当然了关于上一节的坑还是先留着......没有想好怎么搞:https://www.yuque.com/duiniwukenaihe/hg6ymd/ipd4bf#R0XxW)。这里以service为例也实现一下list watch......
list watch实现service list:
模仿/src/core/deployment_int.go写一个/src/core/service_int.go:
/src/core/service_int.go
package core
import (
"fmt"
corev1 "k8s.io/api/core/v1"
"log"
"sync"
)
type ServiceMap struct {
data sync.Map
}
func (svcmap *ServiceMap) Add(dep *corev1.Service) {
if list, ok := svcmap.data.Load(dep.Namespace); ok {
list = append(list.([]*corev1.Service), dep)
svcmap.data.Store(dep.Namespace, list)
} else {
svcmap.data.Store(dep.Namespace, []*corev1.Service{dep})
}
}
func (svcmap *ServiceMap) Update(dep *corev1.Service) error {
if list, ok := svcmap.data.Load(dep.Namespace); ok {
for i, range_dep := range list.([]*corev1.Service) {
if range_dep.Name == dep.Name {
list.([]*corev1.Service)[i] = dep
svcmap.data.Store(dep.Namespace, list)
}
}
return nil
}
return fmt.Errorf("Service-%s not found", dep.Name)
}
// 删除
func (svcmap *ServiceMap) Delete(dep *corev1.Service) {
if list, ok := svcmap.data.Load(dep.Namespace); ok {
for i, range_dep := range list.([]*corev1.Service) {
if range_dep.Name == dep.Name {
newList := append(list.([]*corev1.Service)[:i], list.([]*corev1.Service)[i+1:]...)
svcmap.data.Store(dep.Namespace, newList)
break
}
}
}
}
func (svcmap *ServiceMap) ListByNS(ns string) ([]*corev1.Service, error) {
if list, ok := svcmap.data.Load(ns); ok {
return list.([]*corev1.Service), nil
}
return nil, fmt.Errorf("record not found")
}
func (svcmap *ServiceMap) GetService(ns string, name string) (*corev1.Service, error) {
depList, err := svcmap.ListByNS(ns)
if err != nil {
return nil, fmt.Errorf("Service not found")
}
for _, item := range depList {
if item.Name == name {
return item, nil
}
}
return nil, fmt.Errorf("Service not found")
}
var svcmap *ServiceMap
func init() {
svcmap = &ServiceMap{}
}
type SvcHandler struct {
}
func (d *SvcHandler) OnAdd(obj interface{}) {
//fmt.Println(obj.(*corev1.Service).Name)
svcmap.Add(obj.(*corev1.Service))
}
func (d *SvcHandler) OnUpdate(oldobj, newObj interface{}) {
err := svcmap.Update(newObj.(*corev1.Service))
if err != nil {
log.Println(err)
}
}
func (d *SvcHandler) OnDelete(obj interface{}) {
if d, ok := obj.(*corev1.Service); ok {
svcmap.Delete(d)
}
}
Int
/src/core/deployment_init.go Init中增加ServiceInformer相关配置:
func InitDeployment() {
factory := informers.NewSharedInformerFactory(lib.K8sClient, 0)
depinformer := factory.Apps().V1().Deployments()
depinformer.Informer().AddEventHandler(&DepHandler{})
Podinformer := factory.Core().V1().Pods()
Podinformer.Informer().AddEventHandler(&PodHandler{})
Rsinformer := factory.Apps().V1().replicasets()
Rsinformer.Informer().AddEventHandler(&RSHandler{})
eventInformer := factory.Core().V1().Events()
eventInformer.Informer().AddEventHandler(&EventHandler{})
ServiceInformer := factory.Core().V1().Services()
ServiceInformer.Informer().AddEventHandler(&SvcHandler{})
factory.Start(wait.NeverStop)
}
修改Service.go为list watch模式
/src/service/Service.go not list-watch原代码:
package service
import (
"context"
"github.com/gin-gonic/gin"
. "k8s-demo1/src/lib"
Metav1 "k8s.io/apimachinery/pkg/apis/Meta/v1"
)
type Service struct {
Name string
Type string
ClusterIp string
ExternalIp []string
Ports []string
Select map[string]string
}
func ListService(g *gin.Context) {
ns := g.Query("ns")
svc, err := K8sClient.CoreV1().Services(ns).List(context.Background(), Metav1.ListOptions{})
if err != nil {
g.Error(err)
return
}
ret := make([]*Service, 0)
for _, item := range svc.Items {
ret = append(ret, &Service{
Name: item.Name,
Type: string(item.Spec.Type),
ClusterIp: item.Spec.ClusterIP,
ExternalIp: item.Spec.ExternalIPs,
Select: item.Spec.Selector,
})
}
g.JSON(200, ret)
return
}
修改成list watch方式:
package service
import (
"github.com/gin-gonic/gin"
"k8s-demo1/src/core"
corev1 "k8s.io/api/core/v1"
)
type Service struct {
Name string
Type string
ClusterIp string
ExternalIp []string
Ports []corev1.ServicePort
Select map[string]string
}
func ListService(g *gin.Context) {
ns := g.Query("ns")
//svc, err := K8sClient.CoreV1().Services(ns).List(context.Background(), Metav1.ListOptions{})
svclist, _ := core.SVCMap.ListByNS(ns)
ret := make([]*Service, 0)
for _, item := range svclist {
ret = append(ret, &Service{
Name: item.Name,
Type: string(item.Spec.Type),
ClusterIp: item.Spec.ClusterIP,
ExternalIp: item.Spec.ExternalIPs,
Ports: item.Spec.Ports,
Select: item.Spec.Selector,
})
}
g.JSON(200, ret)
return
}
运行main.go
package main
import (
"github.com/gin-gonic/gin"
"k8s-demo1/src/core"
"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)
core.InitDeployment()
r.Run()
}
做一个svc
[zhangpeng@zhangpeng k8s]$ cat Nginx1-deployment.yaml
apiVersion: v1
kind: Service
Metadata:
name: Nginx1
labels:
app: Nginx
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: Nginx
[zhangpeng@zhangpeng k8s]$ kubectl apply -f Nginx1-deployment.yaml
service/Nginx1 created
[zhangpeng@zhangpeng k8s]$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 47d
Nginx1 ClusterIP 10.111.134.5 <none> 80/TCP 48s
注:访问 svc就免了,我这里肯定会有问题的。我的app label都是Nginx。所以我不能精确指定到deployment1的(我的初衷),所以尽量是app label去区分deployment!。随便修改一下Nginx中index.html就发现他都会调度的几个deployments。所以我前面整deployment的时候也是一种耍流氓了...不过我还是想区分一下搞出来!
总结:
- list watch 就实现一个service了 statefulset configmap 其他的最后实现吧,curd先实现一下简单的
- deployment 的坑还是先留着......
- 下面继续准备实现一下create delete update的方法......
原文地址:https://cloud.tencent.com/developer/article/2024034
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。