Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Kubernetes watch pods
// some updates for https://rsmitty.github.io/Kubernetes-Events/
// and http://blog.ctaggart.com/2016/09/accessing-kubernetes-api-on-google.html
import (
"encoding/base64"
"fmt"
"log"
"net/http"
"time"
"golang.org/x/net/context"
"google.golang.org/api/compute/v1"
"google.golang.org/api/container/v1"
"google.golang.org/api/option"
"google.golang.org/api/transport"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/client/cache"
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
"k8s.io/kubernetes/pkg/client/restclient"
"k8s.io/kubernetes/pkg/controller/informers"
"k8s.io/kubernetes/pkg/util/wait"
)
func newKubernetesClient(clstr *container.Cluster) (*clientset.Clientset, error) {
cert, err := base64.StdEncoding.DecodeString(clstr.MasterAuth.ClientCertificate)
if err != nil {
return nil, err
}
key, err := base64.StdEncoding.DecodeString(clstr.MasterAuth.ClientKey)
if err != nil {
return nil, err
}
ca, err := base64.StdEncoding.DecodeString(clstr.MasterAuth.ClusterCaCertificate)
if err != nil {
return nil, err
}
config := &restclient.Config{
Host: clstr.Endpoint,
TLSClientConfig: restclient.TLSClientConfig{CertData: cert, KeyData: key, CAData: ca},
Username: clstr.MasterAuth.Username,
Password: clstr.MasterAuth.Password,
// Insecure: true,
}
kbc, err := clientset.NewForConfig(config)
if err != nil {
return nil, err
}
return kbc, nil
}
func podCreated(obj interface{}) {
pod := obj.(*api.Pod)
fmt.Printf("Pod created: %s\n", pod.ObjectMeta.Name)
}
func podDeleted(obj interface{}) {
pod := obj.(*api.Pod)
fmt.Printf("Pod deleted: %s\n", pod.ObjectMeta.Name)
}
func watchPods(kbc *clientset.Clientset) {
resyncPeriod := 30 * time.Minute
si := informers.NewSharedInformerFactory(kbc, resyncPeriod)
si.Pods().Informer().AddEventHandler(
cache.ResourceEventHandlerFuncs{
AddFunc: podCreated,
DeleteFunc: podDeleted,
},
)
si.Start(wait.NeverStop)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment