From 8ef3c8b389919f4bab720eb683667221fe3cb44d Mon Sep 17 00:00:00 2001 From: Ke Zhang Date: Wed, 28 Sep 2016 14:07:25 +0800 Subject: [PATCH] Use abstract cache.NewListWatchFromClient to make the code clean --- pkg/kubelet/kubelet.go | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) diff --git a/pkg/kubelet/kubelet.go b/pkg/kubelet/kubelet.go index 3f41e544571..386c4daabb9 100644 --- a/pkg/kubelet/kubelet.go +++ b/pkg/kubelet/kubelet.go @@ -68,7 +68,6 @@ import ( "k8s.io/kubernetes/pkg/kubelet/util/sliceutils" "k8s.io/kubernetes/pkg/kubelet/volumemanager" "k8s.io/kubernetes/pkg/labels" - "k8s.io/kubernetes/pkg/runtime" "k8s.io/kubernetes/pkg/security/apparmor" "k8s.io/kubernetes/pkg/types" "k8s.io/kubernetes/pkg/util/bandwidth" @@ -88,7 +87,6 @@ import ( "k8s.io/kubernetes/pkg/util/sets" "k8s.io/kubernetes/pkg/util/wait" "k8s.io/kubernetes/pkg/volume" - "k8s.io/kubernetes/pkg/watch" "k8s.io/kubernetes/plugin/pkg/scheduler/algorithm/predicates" ) @@ -359,36 +357,16 @@ func NewMainKubelet(kubeCfg *componentconfig.KubeletConfiguration, kubeDeps *Kub serviceStore := cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}) if kubeClient != nil { - // TODO: cache.NewListWatchFromClient is limited as it takes a client implementation rather - // than an interface. There is no way to construct a list+watcher using resource name. - listWatch := &cache.ListWatch{ - ListFunc: func(options api.ListOptions) (runtime.Object, error) { - return kubeClient.Core().Services(api.NamespaceAll).List(options) - }, - WatchFunc: func(options api.ListOptions) (watch.Interface, error) { - return kubeClient.Core().Services(api.NamespaceAll).Watch(options) - }, - } - cache.NewReflector(listWatch, &api.Service{}, serviceStore, 0).Run() + serviceLW := cache.NewListWatchFromClient(kubeClient.(*clientset.Clientset).CoreClient, "services", api.NamespaceAll, fields.Everything()) + cache.NewReflector(serviceLW, &api.Service{}, serviceStore, 0).Run() } serviceLister := &cache.StoreToServiceLister{Indexer: serviceStore} nodeStore := cache.NewStore(cache.MetaNamespaceKeyFunc) if kubeClient != nil { - // TODO: cache.NewListWatchFromClient is limited as it takes a client implementation rather - // than an interface. There is no way to construct a list+watcher using resource name. fieldSelector := fields.Set{api.ObjectNameField: string(nodeName)}.AsSelector() - listWatch := &cache.ListWatch{ - ListFunc: func(options api.ListOptions) (runtime.Object, error) { - options.FieldSelector = fieldSelector - return kubeClient.Core().Nodes().List(options) - }, - WatchFunc: func(options api.ListOptions) (watch.Interface, error) { - options.FieldSelector = fieldSelector - return kubeClient.Core().Nodes().Watch(options) - }, - } - cache.NewReflector(listWatch, &api.Node{}, nodeStore, 0).Run() + nodeLW := cache.NewListWatchFromClient(kubeClient.(*clientset.Clientset).CoreClient, "nodes", api.NamespaceAll, fieldSelector) + cache.NewReflector(nodeLW, &api.Node{}, nodeStore, 0).Run() } nodeLister := &cache.StoreToNodeLister{Store: nodeStore} nodeInfo := &predicates.CachedNodeInfo{StoreToNodeLister: nodeLister}