Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use kubernetes informer framework #30

Merged
merged 2 commits into from
Oct 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
601 changes: 409 additions & 192 deletions Godeps/Godeps.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package main
import (
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/1.4/pkg/apis/extensions/v1beta1"
"k8s.io/client-go/1.5/pkg/apis/extensions/v1beta1"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"

"k8s.io/client-go/1.4/pkg/api/v1"
"k8s.io/client-go/1.4/pkg/apis/extensions/v1beta1"
"k8s.io/client-go/1.5/pkg/api/v1"
"k8s.io/client-go/1.5/pkg/apis/extensions/v1beta1"
)

var (
Expand Down
73 changes: 29 additions & 44 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ import (
"github.com/golang/glog"
"github.com/openshift/origin/pkg/util/proc"
flag "github.com/spf13/pflag"
clientset "k8s.io/client-go/1.4/kubernetes"
"k8s.io/client-go/1.4/pkg/api"
"k8s.io/client-go/1.4/pkg/api/v1"
"k8s.io/client-go/1.4/pkg/apis/extensions/v1beta1"
"k8s.io/client-go/1.4/pkg/runtime"
"k8s.io/client-go/1.4/pkg/watch"
restclient "k8s.io/client-go/1.4/rest"
"k8s.io/client-go/1.4/tools/cache"
"k8s.io/client-go/1.4/tools/clientcmd"
"golang.org/x/net/context"
clientset "k8s.io/client-go/1.5/kubernetes"
"k8s.io/client-go/1.5/pkg/api"
"k8s.io/client-go/1.5/pkg/api/v1"
"k8s.io/client-go/1.5/pkg/apis/extensions/v1beta1"
restclient "k8s.io/client-go/1.5/rest"
"k8s.io/client-go/1.5/tools/cache"
"k8s.io/client-go/1.5/tools/clientcmd"

"github.com/prometheus/client_golang/prometheus"
)
Expand Down Expand Up @@ -73,7 +72,7 @@ func main() {
glog.Fatalf("Failed to create client: %v", err)
}

initializeMetrics(kubeClient)
initializeMetricCollection(kubeClient)
metricsServer()
}

Expand Down Expand Up @@ -174,54 +173,36 @@ func (l NodeLister) List() (v1.NodeList, error) {
return l()
}

// initializeMetrics creates a new controller from the given config.
func initializeMetrics(kubeClient clientset.Interface) {
dplStore, dplController := cache.NewNamespaceKeyedIndexerAndReflector(
&cache.ListWatch{
ListFunc: func(options api.ListOptions) (runtime.Object, error) {
return kubeClient.Extensions().Deployments(api.NamespaceAll).List(options)
},
WatchFunc: func(options api.ListOptions) (watch.Interface, error) {
return kubeClient.Extensions().Deployments(api.NamespaceAll).Watch(options)
},
}, &v1beta1.Deployment{}, resyncPeriod)

podStore, podController := cache.NewNamespaceKeyedIndexerAndReflector(
cache.NewListWatchFromClient(
kubeClient.Core().GetRESTClient(),
"pods",
api.NamespaceAll,
nil,
), &v1.Pod{}, resyncPeriod)

nodeStore, nodeController := cache.NewNamespaceKeyedIndexerAndReflector(
cache.NewListWatchFromClient(
kubeClient.Core().GetRESTClient(),
"nodes",
api.NamespaceAll,
nil,
), &v1.Node{}, resyncPeriod)

go dplController.Run()
go podController.Run()
go nodeController.Run()
// initializeMetricCollection creates and starts informers and initializes and
// registers metrics for collection.
func initializeMetricCollection(kubeClient clientset.Interface) {
cclient := kubeClient.Core().GetRESTClient()
eclient := kubeClient.Extensions().GetRESTClient()

dlw := cache.NewListWatchFromClient(eclient, "deployments", api.NamespaceAll, nil)
plw := cache.NewListWatchFromClient(cclient, "pods", api.NamespaceAll, nil)
nlw := cache.NewListWatchFromClient(cclient, "nodes", api.NamespaceAll, nil)

dinf := cache.NewSharedInformer(dlw, &v1beta1.Deployment{}, resyncPeriod)
pinf := cache.NewSharedInformer(plw, &v1.Pod{}, resyncPeriod)
ninf := cache.NewSharedInformer(nlw, &v1.Node{}, resyncPeriod)

dplLister := DeploymentLister(func() (deployments []v1beta1.Deployment, err error) {
for _, c := range dplStore.List() {
for _, c := range dinf.GetStore().List() {
deployments = append(deployments, *(c.(*v1beta1.Deployment)))
}
return deployments, nil
})

podLister := PodLister(func() (pods []v1.Pod, err error) {
for _, m := range podStore.List() {
for _, m := range pinf.GetStore().List() {
pods = append(pods, *m.(*v1.Pod))
}
return pods, nil
})

nodeLister := NodeLister(func() (machines v1.NodeList, err error) {
for _, m := range nodeStore.List() {
for _, m := range ninf.GetStore().List() {
machines.Items = append(machines.Items, *(m.(*v1.Node)))
}
return machines, nil
Expand All @@ -230,4 +211,8 @@ func initializeMetrics(kubeClient clientset.Interface) {
prometheus.MustRegister(&deploymentCollector{store: dplLister})
prometheus.MustRegister(&podCollector{store: podLister})
prometheus.MustRegister(&nodeCollector{store: nodeLister})

go dinf.Run(context.Background().Done())
go pinf.Run(context.Background().Done())
go ninf.Run(context.Background().Done())
}
2 changes: 1 addition & 1 deletion node.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package main
import (
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/1.4/pkg/api/v1"
"k8s.io/client-go/1.5/pkg/api/v1"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package main
import (
"testing"

"k8s.io/client-go/1.4/pkg/api/resource"
"k8s.io/client-go/1.4/pkg/api/v1"
"k8s.io/client-go/1.5/pkg/api/resource"
"k8s.io/client-go/1.5/pkg/api/v1"
)

type mockNodeStore struct {
Expand Down
2 changes: 1 addition & 1 deletion pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package main
import (
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/1.4/pkg/api/v1"
"k8s.io/client-go/1.5/pkg/api/v1"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package main
import (
"testing"

"k8s.io/client-go/1.4/pkg/api/resource"
"k8s.io/client-go/1.4/pkg/api/v1"
"k8s.io/client-go/1.5/pkg/api/resource"
"k8s.io/client-go/1.5/pkg/api/v1"
)

type mockPodStore struct {
Expand Down
202 changes: 202 additions & 0 deletions vendor/cloud.google.com/go/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/PuerkitoBio/purell/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vendor/github.com/PuerkitoBio/purell/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading