其他分享
首页 > 其他分享> > kubernetes-handbook informer

kubernetes-handbook informer

作者:互联网

 

cat<<EOF >informer.go
package main

import (
    "flag"
    "fmt"
    "time"

    "k8s.io/client-go/informers"
    coreinformers "k8s.io/client-go/informers/core/v1"
    "k8s.io/client-go/kubernetes"
    klog "k8s.io/klog/v2"
    //"k8s.io/client-go/pkg/api/v1"
    "k8s.io/api/core/v1"
    "k8s.io/client-go/tools/cache"
    "k8s.io/client-go/tools/clientcmd"
    "k8s.io/kubectl/pkg/util/logs"
)

// PodLoggingController logs the name and namespace of pods that are added,
// deleted, or updated
type PodLoggingController struct {
    informerFactory informers.SharedInformerFactory
    podInformer     coreinformers.PodInformer
}

// Run starts shared informers and waits for the shared informer cache to
// synchronize.
func (c *PodLoggingController) Run(stopCh chan struct{}) error {
    // Starts all the shared informers that have been created by the factory so
    // far.
    c.informerFactory.Start(stopCh)
    // wait for the initial synchronization of the local cache.
    if !cache.WaitForCacheSync(stopCh, c.podInformer.Informer().HasSynced) {
        return fmt.Errorf("Failed to sync")
    }
    return nil
}

func (c *PodLoggingController) podAdd(obj interface{}) {
    pod := obj.(*v1.Pod)
    klog.Infof("POD CREATED: %s/%s", pod.Namespace, pod.Name)
}

func (c *PodLoggingController) podUpdate(old, new interface{}) {
    oldPod := old.(*v1.Pod)
    newPod := new.(*v1.Pod)
    klog.Infof(
        "POD UPDATED. %s/%s %s",
        oldPod.Namespace, oldPod.Name, newPod.Status.Phase,
    )
}

func (c *PodLoggingController) podDelete(obj interface{}) {
    pod := obj.(*v1.Pod)
    klog.Infof("POD DELETED: %s/%s", pod.Namespace, pod.Name)
}

// NewPodLoggingController creates a PodLoggingController
func NewPodLoggingController(informerFactory informers.SharedInformerFactory) *PodLoggingController {
    podInformer := informerFactory.Core().V1().Pods()

    c := &PodLoggingController{
        informerFactory: informerFactory,
        podInformer:     podInformer,
    }
    podInformer.Informer().AddEventHandler(
        // Your custom resource event handlers.
        cache.ResourceEventHandlerFuncs{
            // Called on creation
            AddFunc: c.podAdd,
            // Called on resource update and every resyncPeriod on existing resources.
            UpdateFunc: c.podUpdate,
            // Called on resource deletion.
            DeleteFunc: c.podDelete,
        },
    )
    return c
}

var kubeconfig string

func init() {
    flag.StringVar(&kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file")
}

func main() {
    flag.Parse()
    logs.InitLogs()
    defer logs.FlushLogs()

    config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
    if err != nil {
        panic(err.Error())
    }

    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        klog.Fatal(err)
    }

    factory := informers.NewSharedInformerFactory(clientset, time.Hour*24)
    controller := NewPodLoggingController(factory)
    stop := make(chan struct{})
    defer close(stop)
    err = controller.Run(stop)
    if err != nil {
        klog.Fatal(err)
    }
    select {}
}
EOF

 

 

 

 

root@ubuntu:~/go_learn/informer# go mod init informer
go: creating new go.mod: module informer
root@ubuntu:~/go_learn/informer# ls
go.mod  informer.go
root@ubuntu:~/go_learn/informer# go mod vendor

 

root@ubuntu:~/go_learn/informer# go mod vendor
go: finding module for package k8s.io/client-go/kubernetes
go: finding module for package k8s.io/client-go/tools/clientcmd
go: finding module for package k8s.io/klog/v2
go: finding module for package k8s.io/client-go/informers/core/v1
go: finding module for package k8s.io/api/core/v1
go: finding module for package k8s.io/client-go/tools/cache
go: finding module for package k8s.io/kubectl/pkg/util/logs
go: finding module for package k8s.io/client-go/informers
go: downloading k8s.io/client-go v1.5.2
go: downloading k8s.io/api v0.21.2
go: downloading k8s.io/kubectl v0.21.2
go: found k8s.io/api/core/v1 in k8s.io/api v0.21.2
go: found k8s.io/klog/v2 in k8s.io/klog/v2 v2.9.0
go: found k8s.io/kubectl/pkg/util/logs in k8s.io/kubectl v0.21.2
go: downloading k8s.io/client-go v0.21.2
go: downloading github.com/googleapis/gnostic v0.4.1
go: downloading k8s.io/utils v0.0.0-20201110183641-67b214c5f920
go: downloading github.com/golang/protobuf v1.4.3
go: downloading google.golang.org/appengine v1.6.5
go: downloading google.golang.org/protobuf v1.25.0

 

 

root@ubuntu:~/go_learn/informer# go build ./ -o informer
can't load package: package -o: cannot find package "." in:
        /root/go_learn/informer/vendor/-o
root@ubuntu:~/go_learn/informer# go build . -o informer
can't load package: package -o: cannot find package "." in:
        /root/go_learn/informer/vendor/-o
root@ubuntu:~/go_learn/informer# go build  -o informer .
root@ubuntu:~/go_learn/informer#

 

 

root@ubuntu:~/go_learn/informer# ./informer    -kubeconfig=$HOME/.kube/config
I0708 10:46:09.094719   28135 informer.go:41] POD CREATED: kube-system/kube-proxy-xbdlt
I0708 10:46:09.094890   28135 informer.go:41] POD CREATED: kube-system/kube-proxy-dpwh6
I0708 10:46:09.094907   28135 informer.go:41] POD CREATED: kube-system/kube-proxy-wvdkr
I0708 10:46:09.094919   28135 informer.go:41] POD CREATED: kube-system/kube-controller-manager-cloud
I0708 10:46:09.094934   28135 informer.go:41] POD CREATED: kube-system/calico-kube-controllers-5978c5f6b5-xk6cq
I0708 10:46:09.094949   28135 informer.go:41] POD CREATED: kube-system/kube-controller-manager-ubuntu
I0708 10:46:09.094961   28135 informer.go:41] POD CREATED: default/test-job-default-nginx-0
I0708 10:46:09.094975   28135 informer.go:41] POD CREATED: default/test-job-default-nginx-1
I0708 10:46:09.094987   28135 informer.go:41] POD CREATED: kube-system/kube-scheduler-ubuntu
I0708 10:46:09.095000   28135 informer.go:41] POD CREATED: kube-system/kube-controller-manager-centos7
I0708 10:46:09.095012   28135 informer.go:41] POD CREATED: kube-system/kube-scheduler-centos7
I0708 10:46:09.095027   28135 informer.go:41] POD CREATED: kube-system/kube-batch-7bfddfb67c-9l8pw
I0708 10:46:09.095039   28135 informer.go:41] POD CREATED: volcano-system/volcano-scheduler-7f48dddb8f-8g6b5
I0708 10:46:09.095052   28135 informer.go:41] POD CREATED: volcano-system/volcano-admission-6cc49fdc5-5zgzs
I0708 10:46:09.095064   28135 informer.go:41] POD CREATED: default/job-1-nginx-0
I0708 10:46:09.095077   28135 informer.go:41] POD CREATED: kube-system/kube-scheduler-cloud
I0708 10:46:09.095089   28135 informer.go:41] POD CREATED: kube-system/kube-apiserver-cloud
I0708 10:46:09.095112   28135 informer.go:41] POD CREATED: kube-system/calico-node-nwpfl
I0708 10:46:09.095134   28135 informer.go:41] POD CREATED: kube-system/calico-node-blvrv
I0708 10:46:09.095157   28135 informer.go:41] POD CREATED: default/test-job-default-nginx-3
I0708 10:46:09.095179   28135 informer.go:41] POD CREATED: default/test-job-default-nginx-4
I0708 10:46:09.095197   28135 informer.go:41] POD CREATED: volcano-system/volcano-admission-init-qgh9b
I0708 10:46:09.095210   28135 informer.go:41] POD CREATED: default/example-foo-54dc4db9fc-88nd7
I0708 10:46:09.095230   28135 informer.go:41] POD CREATED: kube-system/coredns-546565776c-82jfw
I0708 10:46:09.095246   28135 informer.go:41] POD CREATED: kube-system/kube-apiserver-centos7
I0708 10:46:09.095266   28135 informer.go:41] POD CREATED: kube-system/calico-node-6tc54
I0708 10:46:09.095283   28135 informer.go:41] POD CREATED: kube-system/calico-node-rswtj
I0708 10:46:09.095299   28135 informer.go:41] POD CREATED: volcano-system/volcano-controllers-5f5c4f4785-8dbgl
I0708 10:46:09.095311   28135 informer.go:41] POD CREATED: default/test-job-default-nginx-2
I0708 10:46:09.095326   28135 informer.go:41] POD CREATED: default/test-job-default-nginx-5
I0708 10:46:09.095338   28135 informer.go:41] POD CREATED: kube-system/kube-apiserver-ubuntu
I0708 10:46:09.095354   28135 informer.go:41] POD CREATED: kube-system/coredns-546565776c-px8bd
I0708 10:46:09.095368   28135 informer.go:41] POD CREATED: kube-system/kube-proxy-6jk97

 

删掉一个deploy

 

root@ubuntu:~# kubectl get deploy
NAME          READY   UP-TO-DATE   AVAILABLE   AGE
example-foo   1/1     1            1           46h
root@ubuntu:~# kubectl delete deploy example-foo
deployment.apps "example-foo" deleted
root@ubuntu:~# kubectl delete deploy example-foo

 

root@ubuntu:~/go_learn/informer# ./informer    -kubeconfig=$HOME/.kube/config
I0708 10:46:09.094719   28135 informer.go:41] POD CREATED: kube-system/kube-proxy-xbdlt
I0708 10:46:09.094890   28135 informer.go:41] POD CREATED: kube-system/kube-proxy-dpwh6
I0708 10:46:09.094907   28135 informer.go:41] POD CREATED: kube-system/kube-proxy-wvdkr
I0708 10:46:09.094919   28135 informer.go:41] POD CREATED: kube-system/kube-controller-manager-cloud
I0708 10:46:09.094934   28135 informer.go:41] POD CREATED: kube-system/calico-kube-controllers-5978c5f6b5-xk6cq
I0708 10:46:09.094949   28135 informer.go:41] POD CREATED: kube-system/kube-controller-manager-ubuntu
I0708 10:46:09.094961   28135 informer.go:41] POD CREATED: default/test-job-default-nginx-0
I0708 10:46:09.094975   28135 informer.go:41] POD CREATED: default/test-job-default-nginx-1
I0708 10:46:09.094987   28135 informer.go:41] POD CREATED: kube-system/kube-scheduler-ubuntu
I0708 10:46:09.095000   28135 informer.go:41] POD CREATED: kube-system/kube-controller-manager-centos7
I0708 10:46:09.095012   28135 informer.go:41] POD CREATED: kube-system/kube-scheduler-centos7
I0708 10:46:09.095027   28135 informer.go:41] POD CREATED: kube-system/kube-batch-7bfddfb67c-9l8pw
I0708 10:46:09.095039   28135 informer.go:41] POD CREATED: volcano-system/volcano-scheduler-7f48dddb8f-8g6b5
I0708 10:46:09.095052   28135 informer.go:41] POD CREATED: volcano-system/volcano-admission-6cc49fdc5-5zgzs
I0708 10:46:09.095064   28135 informer.go:41] POD CREATED: default/job-1-nginx-0
I0708 10:46:09.095077   28135 informer.go:41] POD CREATED: kube-system/kube-scheduler-cloud
I0708 10:46:09.095089   28135 informer.go:41] POD CREATED: kube-system/kube-apiserver-cloud
I0708 10:46:09.095112   28135 informer.go:41] POD CREATED: kube-system/calico-node-nwpfl
I0708 10:46:09.095134   28135 informer.go:41] POD CREATED: kube-system/calico-node-blvrv
I0708 10:46:09.095157   28135 informer.go:41] POD CREATED: default/test-job-default-nginx-3
I0708 10:46:09.095179   28135 informer.go:41] POD CREATED: default/test-job-default-nginx-4
I0708 10:46:09.095197   28135 informer.go:41] POD CREATED: volcano-system/volcano-admission-init-qgh9b
I0708 10:46:09.095210   28135 informer.go:41] POD CREATED: default/example-foo-54dc4db9fc-88nd7
I0708 10:46:09.095230   28135 informer.go:41] POD CREATED: kube-system/coredns-546565776c-82jfw
I0708 10:46:09.095246   28135 informer.go:41] POD CREATED: kube-system/kube-apiserver-centos7
I0708 10:46:09.095266   28135 informer.go:41] POD CREATED: kube-system/calico-node-6tc54
I0708 10:46:09.095283   28135 informer.go:41] POD CREATED: kube-system/calico-node-rswtj
I0708 10:46:09.095299   28135 informer.go:41] POD CREATED: volcano-system/volcano-controllers-5f5c4f4785-8dbgl
I0708 10:46:09.095311   28135 informer.go:41] POD CREATED: default/test-job-default-nginx-2
I0708 10:46:09.095326   28135 informer.go:41] POD CREATED: default/test-job-default-nginx-5
I0708 10:46:09.095338   28135 informer.go:41] POD CREATED: kube-system/kube-apiserver-ubuntu
I0708 10:46:09.095354   28135 informer.go:41] POD CREATED: kube-system/coredns-546565776c-px8bd
I0708 10:46:09.095368   28135 informer.go:41] POD CREATED: kube-system/kube-proxy-6jk97
I0708 10:49:26.530314   28135 informer.go:47] POD UPDATED. default/example-foo-54dc4db9fc-88nd7 Running
I0708 10:49:27.076371   28135 informer.go:47] POD UPDATED. default/example-foo-54dc4db9fc-88nd7 Running
I0708 10:49:28.845567   28135 informer.go:47] POD UPDATED. default/example-foo-54dc4db9fc-88nd7 Pending
I0708 10:49:29.856741   28135 informer.go:47] POD UPDATED. default/example-foo-54dc4db9fc-88nd7 Pending
I0708 10:49:29.858950   28135 informer.go:55] POD DELETED: default/example-foo-54dc4db9fc-88nd7
I0708 10:49:47.069494   28135 informer.go:47] POD UPDATED. kube-system/kube-batch-7bfddfb67c-9l8pw Running
I0708 10:50:00.168341   28135 informer.go:47] POD UPDATED. kube-system/kube-batch-7bfddfb67c-9l8pw Running

 

 

kubernetes-handbook 

标签:kubernetes,I0708,handbook,28135,go,informer,kube,POD
来源: https://www.cnblogs.com/dream397/p/14985097.html