Skip to content

Commit

Permalink
Use k8s go-client as kubernetes client
Browse files Browse the repository at this point in the history
  • Loading branch information
jtblin committed Apr 25, 2017
1 parent 2336325 commit 68ab0a8
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 233 deletions.
38 changes: 22 additions & 16 deletions cmd/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package cmd
import (
"time"

"k8s.io/kubernetes/pkg/api"
kcache "k8s.io/kubernetes/pkg/client/cache"
"k8s.io/kubernetes/pkg/client/restclient"
client "k8s.io/kubernetes/pkg/client/unversioned"
selector "k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/util/wait"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api/v1"
selector "k8s.io/client-go/pkg/fields"
"k8s.io/client-go/rest"
kcache "k8s.io/client-go/tools/cache"
)

const (
Expand All @@ -17,18 +17,18 @@ const (
)

type k8s struct {
*client.Client
*kubernetes.Clientset
}

// Returns a cache.ListWatch that gets all changes to pods.
func (k8s *k8s) createPodLW() *kcache.ListWatch {
return kcache.NewListWatchFromClient(k8s, "pods", api.NamespaceAll, selector.Everything())
return kcache.NewListWatchFromClient(k8s.CoreV1().RESTClient(), "pods", v1.NamespaceAll, selector.Everything())
}

func (k8s *k8s) watchForPods(podManager kcache.ResourceEventHandler) kcache.Store {
podStore, podController := kcache.NewInformer(
k8s.createPodLW(),
&api.Pod{},
&v1.Pod{},
resyncPeriod,
podManager,
)
Expand All @@ -38,13 +38,13 @@ func (k8s *k8s) watchForPods(podManager kcache.ResourceEventHandler) kcache.Stor

// returns a listwatcher of namespaces
func (k8s *k8s) createNamespaceLW() *kcache.ListWatch {
return kcache.NewListWatchFromClient(k8s, "namespaces", api.NamespaceAll, selector.Everything())
return kcache.NewListWatchFromClient(k8s.CoreV1().RESTClient(), "namespaces", v1.NamespaceAll, selector.Everything())
}

func (k8s *k8s) watchForNamespaces(nsManager kcache.ResourceEventHandler) kcache.Store {
nsStore, nsController := kcache.NewInformer(
k8s.createNamespaceLW(),
&api.Namespace{},
&v1.Namespace{},
resyncPeriod,
nsManager,
)
Expand All @@ -53,20 +53,26 @@ func (k8s *k8s) watchForNamespaces(nsManager kcache.ResourceEventHandler) kcache
}

func newK8s(host, token string, insecure bool) (*k8s, error) {
var c *client.Client
var config *rest.Config
var err error
if host != "" && token != "" {
config := restclient.Config{
config = &rest.Config{
Host: host,
BearerToken: token,
Insecure: insecure,
}
c, err = client.New(&config)
} else {
c, err = client.NewInCluster()
config, err = rest.InClusterConfig()
if err != nil {
return nil, err
}
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
return &k8s{c}, nil
return &k8s{client}, nil
}
12 changes: 6 additions & 6 deletions cmd/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"

log "github.com/Sirupsen/logrus"
"k8s.io/kubernetes/pkg/api"
"k8s.io/client-go/pkg/api/v1"
)

type namespaceHandler struct {
Expand All @@ -13,7 +13,7 @@ type namespaceHandler struct {

// OnAdd called with a namespace is added to k8s
func (h *namespaceHandler) OnAdd(obj interface{}) {
ns, ok := obj.(*api.Namespace)
ns, ok := obj.(*v1.Namespace)
if !ok {
log.Errorf("Expected Namespace but OnAdd handler received %+v", obj)
return
Expand All @@ -31,8 +31,8 @@ func (h *namespaceHandler) OnAdd(obj interface{}) {

// OnUpdate called with a namespace is updated inside k8s
func (h *namespaceHandler) OnUpdate(oldObj, newObj interface{}) {
//ons, ok := oldObj.(*api.Namespace)
nns, ok := newObj.(*api.Namespace)
//ons, ok := oldObj.(*v1.Namespace)
nns, ok := newObj.(*v1.Namespace)
if !ok {
log.Errorf("Expected Namespace but OnUpdate handler received %+v", newObj)
return
Expand All @@ -50,7 +50,7 @@ func (h *namespaceHandler) OnUpdate(oldObj, newObj interface{}) {

// OnDelete called with a namespace is removed from k8s
func (h *namespaceHandler) OnDelete(obj interface{}) {
ns, ok := obj.(*api.Namespace)
ns, ok := obj.(*v1.Namespace)
if !ok {
log.Errorf("Expected Namespace but OnDelete handler received %+v", obj)
return
Expand All @@ -61,7 +61,7 @@ func (h *namespaceHandler) OnDelete(obj interface{}) {

// getRoleAnnotations reads the "iam.amazonaws.com/allowed-roles" annotation off a namespace
// and splits them as a JSON list (["role1", "role2", "role3"])
func (h *namespaceHandler) getRoleAnnotation(ns *api.Namespace) []string {
func (h *namespaceHandler) getRoleAnnotation(ns *v1.Namespace) []string {
rolesString := ns.Annotations[h.storage.namespaceKey]
if rolesString != "" {
var decoded []string
Expand Down
14 changes: 7 additions & 7 deletions cmd/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cmd

import (
log "github.com/Sirupsen/logrus"
"k8s.io/kubernetes/pkg/api"
kcache "k8s.io/kubernetes/pkg/client/cache"
"k8s.io/client-go/pkg/api/v1"
kcache "k8s.io/client-go/tools/cache"
)

type podHandler struct {
Expand All @@ -12,7 +12,7 @@ type podHandler struct {

// OnAdd is called when a pod is added.
func (p *podHandler) OnAdd(obj interface{}) {
pod, ok := obj.(*api.Pod)
pod, ok := obj.(*v1.Pod)
if !ok {
log.Errorf("Expected Pod but OnAdd handler received %+v", obj)
return
Expand All @@ -31,8 +31,8 @@ func (p *podHandler) OnAdd(obj interface{}) {

// OnUpdate is called when a pod is modified.
func (p *podHandler) OnUpdate(oldObj, newObj interface{}) {
oldPod, ok1 := oldObj.(*api.Pod)
newPod, ok2 := newObj.(*api.Pod)
oldPod, ok1 := oldObj.(*v1.Pod)
newPod, ok2 := newObj.(*v1.Pod)
if !ok1 || !ok2 {
log.Errorf("Expected Pod but OnUpdate handler received %+v %+v", oldObj, newObj)
return
Expand All @@ -55,11 +55,11 @@ func (p *podHandler) OnUpdate(oldObj, newObj interface{}) {

// OnDelete is called when a pod is deleted.
func (p *podHandler) OnDelete(obj interface{}) {
pod, ok := obj.(*api.Pod)
pod, ok := obj.(*v1.Pod)
if !ok {
deletedObj, dok := obj.(kcache.DeletedFinalStateUnknown)
if dok {
pod, ok = deletedObj.Obj.(*api.Pod)
pod, ok = deletedObj.Obj.(*v1.Pod)
}
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"sync"

log "github.com/Sirupsen/logrus"
"k8s.io/kubernetes/pkg/api"
"k8s.io/client-go/pkg/api/v1"
)

// store implements the k8s framework ResourceEventHandler interface.
Expand Down Expand Up @@ -35,13 +35,13 @@ func (s *store) Get(IP string) (string, error) {
return "", fmt.Errorf("Unable to find role for IP %s", IP)
}

func (s *store) AddRoleToIP(pod *api.Pod, role string) {
func (s *store) AddRoleToIP(pod *v1.Pod, role string) {
s.mutex.Lock()
s.rolesByIP[pod.Status.PodIP] = role
s.mutex.Unlock()
}

func (s *store) AddNamespaceToIP(pod *api.Pod) {
func (s *store) AddNamespaceToIP(pod *v1.Pod) {
namespace := pod.GetNamespace()
s.mutex.Lock()
s.namespaceByIP[pod.Status.PodIP] = namespace
Expand Down
Loading

0 comments on commit 68ab0a8

Please sign in to comment.