From f697732a5d653d7b80e06c364dd777a8cd66b955 Mon Sep 17 00:00:00 2001 From: combk8s Date: Thu, 17 Dec 2015 19:57:07 +0800 Subject: [PATCH] move parse or die logic to selector.go --- pkg/controller/gc/gc_controller.go | 10 +--------- pkg/controller/gc/gc_controller_test.go | 4 ---- pkg/fields/selector.go | 10 ++++++++++ plugin/pkg/scheduler/factory/factory.go | 14 +++----------- 4 files changed, 14 insertions(+), 24 deletions(-) diff --git a/pkg/controller/gc/gc_controller.go b/pkg/controller/gc/gc_controller.go index 633323fd877c4..9267515448418 100644 --- a/pkg/controller/gc/gc_controller.go +++ b/pkg/controller/gc/gc_controller.go @@ -62,7 +62,7 @@ func New(kubeClient client.Interface, resyncPeriod controller.ResyncPeriodFunc, }, } - terminatedSelector := compileTerminatedPodSelector() + terminatedSelector := fields.ParseSelectorOrDie("status.phase!=" + string(api.PodPending) + ",status.phase!=" + string(api.PodRunning) + ",status.phase!=" + string(api.PodUnknown)) gcc.podStore.Store, gcc.podStoreSyncer = framework.NewInformer( &cache.ListWatch{ @@ -114,14 +114,6 @@ func (gcc *GCController) gc() { wait.Wait() } -func compileTerminatedPodSelector() fields.Selector { - selector, err := fields.ParseSelector("status.phase!=" + string(api.PodPending) + ",status.phase!=" + string(api.PodRunning) + ",status.phase!=" + string(api.PodUnknown)) - if err != nil { - panic("terminatedSelector must compile: " + err.Error()) - } - return selector -} - // byCreationTimestamp sorts a list by creation timestamp, using their names as a tie breaker. type byCreationTimestamp []*api.Pod diff --git a/pkg/controller/gc/gc_controller_test.go b/pkg/controller/gc/gc_controller_test.go index a935881910afc..037a6c892b252 100644 --- a/pkg/controller/gc/gc_controller_test.go +++ b/pkg/controller/gc/gc_controller_test.go @@ -102,7 +102,3 @@ func TestGC(t *testing.T) { } } } - -func TestTerminatedPodSelectorCompiles(t *testing.T) { - compileTerminatedPodSelector() -} diff --git a/pkg/fields/selector.go b/pkg/fields/selector.go index 3d0f8711b644c..c0a6385818aaf 100644 --- a/pkg/fields/selector.go +++ b/pkg/fields/selector.go @@ -181,6 +181,16 @@ func SelectorFromSet(ls Set) Selector { return andTerm(items) } +// ParseSelectorOrDie takes a string representing a selector and returns an +// object suitable for matching, or panic when an error occur. +func ParseSelectorOrDie(s string) Selector { + selector, err := ParseSelector(s) + if err != nil { + panic(err) + } + return selector +} + // ParseSelector takes a string representing a selector and returns an // object suitable for matching, or an error. func ParseSelector(selector string) (Selector, error) { diff --git a/plugin/pkg/scheduler/factory/factory.go b/plugin/pkg/scheduler/factory/factory.go index d34527268481f..2d1d1922aeebe 100644 --- a/plugin/pkg/scheduler/factory/factory.go +++ b/plugin/pkg/scheduler/factory/factory.go @@ -260,20 +260,12 @@ func (factory *ConfigFactory) createUnassignedPodLW() *cache.ListWatch { return cache.NewListWatchFromClient(factory.Client, "pods", api.NamespaceAll, fields.Set{client.PodHost: ""}.AsSelector()) } -func parseSelectorOrDie(s string) fields.Selector { - selector, err := fields.ParseSelector(s) - if err != nil { - panic(err) - } - return selector -} - // Returns a cache.ListWatch that finds all pods that are // already scheduled. // TODO: return a ListerWatcher interface instead? func (factory *ConfigFactory) createAssignedPodLW() *cache.ListWatch { return cache.NewListWatchFromClient(factory.Client, "pods", api.NamespaceAll, - parseSelectorOrDie(client.PodHost+"!=")) + fields.ParseSelectorOrDie(client.PodHost+"!=")) } // createNodeLW returns a cache.ListWatch that gets all changes to nodes. @@ -285,12 +277,12 @@ func (factory *ConfigFactory) createNodeLW() *cache.ListWatch { // Returns a cache.ListWatch that gets all changes to services. func (factory *ConfigFactory) createServiceLW() *cache.ListWatch { - return cache.NewListWatchFromClient(factory.Client, "services", api.NamespaceAll, parseSelectorOrDie("")) + return cache.NewListWatchFromClient(factory.Client, "services", api.NamespaceAll, fields.ParseSelectorOrDie("")) } // Returns a cache.ListWatch that gets all changes to controllers. func (factory *ConfigFactory) createControllerLW() *cache.ListWatch { - return cache.NewListWatchFromClient(factory.Client, "replicationControllers", api.NamespaceAll, parseSelectorOrDie("")) + return cache.NewListWatchFromClient(factory.Client, "replicationControllers", api.NamespaceAll, fields.ParseSelectorOrDie("")) } func (factory *ConfigFactory) makeDefaultErrorFunc(backoff *podBackoff, podQueue *cache.FIFO) func(pod *api.Pod, err error) {