Skip to content

Instantly share code, notes, and snippets.

@creaktive
Last active April 11, 2018 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creaktive/a4e68caf084992c2add6ad77e03daa66 to your computer and use it in GitHub Desktop.
Save creaktive/a4e68caf084992c2add6ad77e03daa66 to your computer and use it in GitHub Desktop.
k8s-sniffer
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"time"
yaml "gopkg.in/yaml.v2"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
var kubeconfig string
var master string
kubeconfig = "/Users/spoussep/.kube/config"
master = ""
// creates the connection
config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil {
panic(err.Error())
}
// creates the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
stop := make(chan struct{})
// create the pod watcher & setup an informer to call functions when the watchlist changes
_, pods_controller := cache.NewInformer(
cache.NewListWatchFromClient(
clientset.Core().RESTClient(),
"pods",
v1.NamespaceAll,
fields.Everything(),
),
&v1.Pod{},
0*time.Second,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
yml, _ := yaml.Marshal(obj)
fmt.Printf("##### POD CREATED:\n%s\n", yml)
},
UpdateFunc: func(old interface{}, new interface{}) {
yml, _ := yaml.Marshal(new)
fmt.Printf("##### POD UPDATED:\n%s\n", yml)
},
DeleteFunc: func(obj interface{}) {
yml, _ := yaml.Marshal(obj)
fmt.Printf("##### POD DELETED:\n%s\n", yml)
},
},
)
go pods_controller.Run(stop)
// samewise, for services
_, services_controller := cache.NewInformer(
cache.NewListWatchFromClient(
clientset.Core().RESTClient(),
"services",
v1.NamespaceAll,
fields.Everything(),
),
&v1.Service{},
0*time.Second,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
yml, _ := yaml.Marshal(obj)
fmt.Printf("##### SERVICE CREATED:\n%s\n", yml)
},
UpdateFunc: func(old interface{}, new interface{}) {
yml, _ := yaml.Marshal(new)
fmt.Printf("##### SERVICE UPDATED:\n%s\n", yml)
},
DeleteFunc: func(obj interface{}) {
yml, _ := yaml.Marshal(obj)
fmt.Printf("##### SERVICE DELETED:\n%s\n", yml)
},
},
)
go services_controller.Run(stop)
// samewise, for deployments
/*
_, deployment_controller := cache.NewInformer(
cache.NewListWatchFromClient(
clientset.Core().RESTClient(),
"deployments",
v1.NamespaceAll,
fields.Everything(),
),
&dpl.Deployment{},
0*time.Second,
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
yml, _ := yaml.Marshal(obj)
fmt.Printf("##### DEPLOYMENT CREATED:\n%s\n", yml)
},
UpdateFunc: func(old interface{}, new interface{}) {
yml, _ := yaml.Marshal(new)
fmt.Printf("##### DEPLOYMENT UPDATED:\n%s\n", yml)
},
DeleteFunc: func(obj interface{}) {
yml, _ := yaml.Marshal(obj)
fmt.Printf("##### DEPLOYMENT DELETED:\n%s\n", yml)
},
},
)
go deployment_controller.Run(stop)
*/
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
fmt.Println("Shutdown signal received, exiting...")
close(stop)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment