Don't validate selector that is already validated

This commit is contained in:
Wojciech Tyczynski 2016-08-19 09:36:55 +02:00
parent 42aee3ac5e
commit e9d5be628a
8 changed files with 18 additions and 20 deletions

View File

@ -269,11 +269,10 @@ func (s *StoreToReplicationControllerLister) GetPodControllers(pod *api.Pod) (co
for _, m := range items { for _, m := range items {
rc = *m.(*api.ReplicationController) rc = *m.(*api.ReplicationController)
labelSet := labels.Set(rc.Spec.Selector) selector = labels.Set(rc.Spec.Selector).AsSelectorPreValidated()
selector = labels.Set(rc.Spec.Selector).AsSelector()
// If an rc with a nil or empty selector creeps in, it should match nothing, not everything. // If an rc with a nil or empty selector creeps in, it should match nothing, not everything.
if labelSet.AsSelector().Empty() || !selector.Matches(labels.Set(pod.Labels)) { if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) {
continue continue
} }
controllers = append(controllers, rc) controllers = append(controllers, rc)
@ -513,7 +512,7 @@ func (s *StoreToServiceLister) GetPodServices(pod *api.Pod) (services []api.Serv
// services with nil selectors match nothing, not everything. // services with nil selectors match nothing, not everything.
continue continue
} }
selector = labels.Set(service.Spec.Selector).AsSelector() selector = labels.Set(service.Spec.Selector).AsSelectorPreValidated()
if selector.Matches(labels.Set(pod.Labels)) { if selector.Matches(labels.Set(pod.Labels)) {
services = append(services, service) services = append(services, service)
} }

View File

@ -143,7 +143,7 @@ func TestStoreToReplicationControllerLister(t *testing.T) {
}, },
}, },
list: func(lister StoreToReplicationControllerLister) ([]api.ReplicationController, error) { list: func(lister StoreToReplicationControllerLister) ([]api.ReplicationController, error) {
return lister.ReplicationControllers(api.NamespaceAll).List(labels.Set{}.AsSelector()) return lister.ReplicationControllers(api.NamespaceAll).List(labels.Set{}.AsSelectorPreValidated())
}, },
outRCNames: sets.NewString("hmm", "foo"), outRCNames: sets.NewString("hmm", "foo"),
}, },
@ -158,7 +158,7 @@ func TestStoreToReplicationControllerLister(t *testing.T) {
}, },
}, },
list: func(lister StoreToReplicationControllerLister) ([]api.ReplicationController, error) { list: func(lister StoreToReplicationControllerLister) ([]api.ReplicationController, error) {
return lister.ReplicationControllers("hmm").List(labels.Set{}.AsSelector()) return lister.ReplicationControllers("hmm").List(labels.Set{}.AsSelectorPreValidated())
}, },
outRCNames: sets.NewString("hmm"), outRCNames: sets.NewString("hmm"),
}, },
@ -715,7 +715,7 @@ func TestStoreToPodLister(t *testing.T) {
spl := StoreToPodLister{store} spl := StoreToPodLister{store}
// Verify that we can always look up by Namespace. // Verify that we can always look up by Namespace.
defaultPods, err := spl.Pods(api.NamespaceDefault).List(labels.Set{}.AsSelector()) defaultPods, err := spl.Pods(api.NamespaceDefault).List(labels.Set{}.AsSelectorPreValidated())
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
} else if e, a := 1, len(defaultPods); e != a { } else if e, a := 1, len(defaultPods); e != a {
@ -725,7 +725,7 @@ func TestStoreToPodLister(t *testing.T) {
} }
for _, id := range ids { for _, id := range ids {
got, err := spl.List(labels.Set{"name": id}.AsSelector()) got, err := spl.List(labels.Set{"name": id}.AsSelectorPreValidated())
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v", err) t.Errorf("Unexpected error: %v", err)
continue continue

View File

@ -472,7 +472,7 @@ func (r RealPodControl) createPods(nodeName, namespace string, template *api.Pod
if len(nodeName) != 0 { if len(nodeName) != 0 {
pod.Spec.NodeName = nodeName pod.Spec.NodeName = nodeName
} }
if labels.Set(pod.Labels).AsSelector().Empty() { if labels.Set(pod.Labels).AsSelectorPreValidated().Empty() {
return fmt.Errorf("unable to create pods, no labels") return fmt.Errorf("unable to create pods, no labels")
} }
if newPod, err := r.KubeClient.Core().Pods(namespace).Create(pod); err != nil { if newPod, err := r.KubeClient.Core().Pods(namespace).Create(pod); err != nil {

View File

@ -358,7 +358,7 @@ func (e *EndpointController) syncService(key string) {
} }
glog.V(5).Infof("About to update endpoints for service %q", key) glog.V(5).Infof("About to update endpoints for service %q", key)
pods, err := e.podStore.Pods(service.Namespace).List(labels.Set(service.Spec.Selector).AsSelector()) pods, err := e.podStore.Pods(service.Namespace).List(labels.Set(service.Spec.Selector).AsSelectorPreValidated())
if err != nil { if err != nil {
// Since we're getting stuff from a local cache, it is // Since we're getting stuff from a local cache, it is
// basically impossible to get this error. // basically impossible to get this error.

View File

@ -659,7 +659,7 @@ func (rsc *ReplicaSetController) syncReplicaSet(key string) error {
// part of the filteredPods. // part of the filteredPods.
fullyLabeledReplicasCount := 0 fullyLabeledReplicasCount := 0
readyReplicasCount := 0 readyReplicasCount := 0
templateLabel := labels.Set(rs.Spec.Template.Labels).AsSelector() templateLabel := labels.Set(rs.Spec.Template.Labels).AsSelectorPreValidated()
for _, pod := range filteredPods { for _, pod := range filteredPods {
if templateLabel.Matches(labels.Set(pod.Labels)) { if templateLabel.Matches(labels.Set(pod.Labels)) {
fullyLabeledReplicasCount++ fullyLabeledReplicasCount++

View File

@ -291,11 +291,10 @@ func isControllerMatch(pod *api.Pod, rc *api.ReplicationController) bool {
if rc.Namespace != pod.Namespace { if rc.Namespace != pod.Namespace {
return false return false
} }
labelSet := labels.Set(rc.Spec.Selector) selector := labels.Set(rc.Spec.Selector).AsSelectorPreValidated()
selector := labels.Set(rc.Spec.Selector).AsSelector()
// If an rc with a nil or empty selector creeps in, it should match nothing, not everything. // If an rc with a nil or empty selector creeps in, it should match nothing, not everything.
if labelSet.AsSelector().Empty() || !selector.Matches(labels.Set(pod.Labels)) { if selector.Empty() || !selector.Matches(labels.Set(pod.Labels)) {
return false return false
} }
return true return true
@ -662,7 +661,7 @@ func (rm *ReplicationManager) syncReplicationController(key string) error {
rm.queue.Add(key) rm.queue.Add(key)
return err return err
} }
cm := controller.NewPodControllerRefManager(rm.podControl, rc.ObjectMeta, labels.Set(rc.Spec.Selector).AsSelector(), getRCKind()) cm := controller.NewPodControllerRefManager(rm.podControl, rc.ObjectMeta, labels.Set(rc.Spec.Selector).AsSelectorPreValidated(), getRCKind())
matchesAndControlled, matchesNeedsController, controlledDoesNotMatch := cm.Classify(pods) matchesAndControlled, matchesNeedsController, controlledDoesNotMatch := cm.Classify(pods)
for _, pod := range matchesNeedsController { for _, pod := range matchesNeedsController {
err := cm.AdoptPod(pod) err := cm.AdoptPod(pod)
@ -694,7 +693,7 @@ func (rm *ReplicationManager) syncReplicationController(key string) error {
return aggregate return aggregate
} }
} else { } else {
pods, err := rm.podStore.Pods(rc.Namespace).List(labels.Set(rc.Spec.Selector).AsSelector()) pods, err := rm.podStore.Pods(rc.Namespace).List(labels.Set(rc.Spec.Selector).AsSelectorPreValidated())
if err != nil { if err != nil {
glog.Errorf("Error getting pods for rc %q: %v", key, err) glog.Errorf("Error getting pods for rc %q: %v", key, err)
rm.queue.Add(key) rm.queue.Add(key)
@ -716,7 +715,7 @@ func (rm *ReplicationManager) syncReplicationController(key string) error {
// matching pods must be part of the filteredPods. // matching pods must be part of the filteredPods.
fullyLabeledReplicasCount := 0 fullyLabeledReplicasCount := 0
readyReplicasCount := 0 readyReplicasCount := 0
templateLabel := labels.Set(rc.Spec.Template.Labels).AsSelector() templateLabel := labels.Set(rc.Spec.Template.Labels).AsSelectorPreValidated()
for _, pod := range filteredPods { for _, pod := range filteredPods {
if templateLabel.Matches(labels.Set(pod.Labels)) { if templateLabel.Matches(labels.Set(pod.Labels)) {
fullyLabeledReplicasCount++ fullyLabeledReplicasCount++

View File

@ -61,7 +61,7 @@ func (ls Set) AsSelector() Selector {
return SelectorFromSet(ls) return SelectorFromSet(ls)
} }
// ValidatedAsSelector converts labels into a selector, but // AsSelectorPreValidated converts labels into a selector, but
// assumes that labels are already validated and thus don't // assumes that labels are already validated and thus don't
// preform any validation. // preform any validation.
// According to our measurements this is significantly faster // According to our measurements this is significantly faster

View File

@ -85,7 +85,7 @@ func (f FakeServiceLister) GetPodServices(pod *api.Pod) (services []api.Service,
if service.Namespace != pod.Namespace { if service.Namespace != pod.Namespace {
continue continue
} }
selector = labels.Set(service.Spec.Selector).AsSelector() selector = labels.Set(service.Spec.Selector).AsSelectorPreValidated()
if selector.Matches(labels.Set(pod.Labels)) { if selector.Matches(labels.Set(pod.Labels)) {
services = append(services, service) services = append(services, service)
} }
@ -134,7 +134,7 @@ func (f FakeControllerLister) GetPodControllers(pod *api.Pod) (controllers []api
if controller.Namespace != pod.Namespace { if controller.Namespace != pod.Namespace {
continue continue
} }
selector = labels.Set(controller.Spec.Selector).AsSelector() selector = labels.Set(controller.Spec.Selector).AsSelectorPreValidated()
if selector.Matches(labels.Set(pod.Labels)) { if selector.Matches(labels.Set(pod.Labels)) {
controllers = append(controllers, controller) controllers = append(controllers, controller)
} }