More efficient field selectors

pull/6/head
Wojciech Tyczynski 2016-08-23 16:34:02 +02:00
parent ea7d417377
commit d5a596bca1
29 changed files with 45 additions and 54 deletions

View File

@ -41,7 +41,7 @@ func (clusterStrategy) NamespaceScoped() bool {
} }
func ClusterToSelectableFields(cluster *federation.Cluster) fields.Set { func ClusterToSelectableFields(cluster *federation.Cluster) fields.Set {
return generic.ObjectMetaFieldsSet(cluster.ObjectMeta, false) return generic.ObjectMetaFieldsSet(&cluster.ObjectMeta, false)
} }
func MatchCluster(label labels.Selector, field fields.Selector) *generic.SelectionPredicate { func MatchCluster(label labels.Selector, field fields.Selector) *generic.SelectionPredicate {

View File

@ -184,5 +184,5 @@ func Matcher(label labels.Selector, field fields.Selector) *generic.SelectionPre
// SelectableFields returns a field set that can be used for filter selection // SelectableFields returns a field set that can be used for filter selection
func SelectableFields(obj *certificates.CertificateSigningRequest) fields.Set { func SelectableFields(obj *certificates.CertificateSigningRequest) fields.Set {
return generic.ObjectMetaFieldsSet(obj.ObjectMeta, false) return generic.ObjectMetaFieldsSet(&obj.ObjectMeta, false)
} }

View File

@ -84,7 +84,7 @@ func (strategy) ValidateUpdate(ctx api.Context, newObj, oldObj runtime.Object) f
// ConfigMapToSelectableFields returns a field set that represents the object for matching purposes. // ConfigMapToSelectableFields returns a field set that represents the object for matching purposes.
func ConfigMapToSelectableFields(cfg *api.ConfigMap) fields.Set { func ConfigMapToSelectableFields(cfg *api.ConfigMap) fields.Set {
return generic.ObjectMetaFieldsSet(cfg.ObjectMeta, true) return generic.ObjectMetaFieldsSet(&cfg.ObjectMeta, true)
} }
// MatchConfigMap returns a generic matcher for a given label and field selector. // MatchConfigMap returns a generic matcher for a given label and field selector.

View File

@ -103,7 +103,7 @@ func (rcStrategy) AllowUnconditionalUpdate() bool {
// ControllerToSelectableFields returns a field set that represents the object. // ControllerToSelectableFields returns a field set that represents the object.
func ControllerToSelectableFields(controller *api.ReplicationController) fields.Set { func ControllerToSelectableFields(controller *api.ReplicationController) fields.Set {
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(controller.ObjectMeta, true) objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&controller.ObjectMeta, true)
controllerSpecificFieldsSet := fields.Set{ controllerSpecificFieldsSet := fields.Set{
"status.replicas": strconv.Itoa(int(controller.Status.Replicas)), "status.replicas": strconv.Itoa(int(controller.Status.Replicas)),
} }

View File

@ -106,7 +106,7 @@ func (daemonSetStrategy) AllowUnconditionalUpdate() bool {
// DaemonSetToSelectableFields returns a field set that represents the object. // DaemonSetToSelectableFields returns a field set that represents the object.
func DaemonSetToSelectableFields(daemon *extensions.DaemonSet) fields.Set { func DaemonSetToSelectableFields(daemon *extensions.DaemonSet) fields.Set {
return generic.ObjectMetaFieldsSet(daemon.ObjectMeta, true) return generic.ObjectMetaFieldsSet(&daemon.ObjectMeta, true)
} }
// MatchSetDaemon is the filter used by the generic etcd backend to route // MatchSetDaemon is the filter used by the generic etcd backend to route

View File

@ -112,7 +112,7 @@ func (deploymentStatusStrategy) ValidateUpdate(ctx api.Context, obj, old runtime
// DeploymentToSelectableFields returns a field set that represents the object. // DeploymentToSelectableFields returns a field set that represents the object.
func DeploymentToSelectableFields(deployment *extensions.Deployment) fields.Set { func DeploymentToSelectableFields(deployment *extensions.Deployment) fields.Set {
return generic.ObjectMetaFieldsSet(deployment.ObjectMeta, true) return generic.ObjectMetaFieldsSet(&deployment.ObjectMeta, true)
} }
// MatchDeployment is the filter used by the generic etcd backend to route // MatchDeployment is the filter used by the generic etcd backend to route

View File

@ -90,5 +90,5 @@ func EndpointsAttributes(obj runtime.Object) (objLabels labels.Set, objFields fi
if !ok { if !ok {
return nil, nil, fmt.Errorf("invalid object type %#v", obj) return nil, nil, fmt.Errorf("invalid object type %#v", obj)
} }
return endpoints.Labels, generic.ObjectMetaFieldsSet(endpoints.ObjectMeta, true), nil return endpoints.Labels, generic.ObjectMetaFieldsSet(&endpoints.ObjectMeta, true), nil
} }

View File

@ -20,7 +20,6 @@ import (
"fmt" "fmt"
"k8s.io/kubernetes/pkg/api" "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/errors"
"k8s.io/kubernetes/pkg/api/validation" "k8s.io/kubernetes/pkg/api/validation"
"k8s.io/kubernetes/pkg/fields" "k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels" "k8s.io/kubernetes/pkg/labels"
@ -71,20 +70,22 @@ func (eventStrategy) AllowUnconditionalUpdate() bool {
} }
func MatchEvent(label labels.Selector, field fields.Selector) *generic.SelectionPredicate { func MatchEvent(label labels.Selector, field fields.Selector) *generic.SelectionPredicate {
return &generic.SelectionPredicate{Label: label, Field: field, GetAttrs: getAttrs} return &generic.SelectionPredicate{
Label: label,
Field: field,
GetAttrs: func(obj runtime.Object) (labels.Set, fields.Set, error) {
event, ok := obj.(*api.Event)
if !ok {
return nil, nil, fmt.Errorf("not an event")
}
return labels.Set(event.Labels), EventToSelectableFields(event), nil
},
}
} }
func getAttrs(obj runtime.Object) (objLabels labels.Set, objFields fields.Set, err error) { // EventToSelectableFields returns a field set that represents the object
event, ok := obj.(*api.Event) func EventToSelectableFields(event *api.Event) fields.Set {
if !ok { objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&event.ObjectMeta, true)
return nil, nil, errors.NewInternalError(fmt.Errorf("object is not of type event: %#v", obj))
}
l := event.Labels
if l == nil {
l = labels.Set{}
}
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(event.ObjectMeta, true)
specificFieldsSet := fields.Set{ specificFieldsSet := fields.Set{
"involvedObject.kind": event.InvolvedObject.Kind, "involvedObject.kind": event.InvolvedObject.Kind,
"involvedObject.namespace": event.InvolvedObject.Namespace, "involvedObject.namespace": event.InvolvedObject.Namespace,
@ -97,5 +98,5 @@ func getAttrs(obj runtime.Object) (objLabels labels.Set, objFields fields.Set, e
"source": event.Source.Component, "source": event.Source.Component,
"type": event.Type, "type": event.Type,
} }
return l, generic.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet), nil return generic.MergeFieldsSets(objectMetaFieldsSet, specificFieldsSet)
} }

View File

@ -24,7 +24,6 @@ import (
"k8s.io/kubernetes/pkg/api/testapi" "k8s.io/kubernetes/pkg/api/testapi"
apitesting "k8s.io/kubernetes/pkg/api/testing" apitesting "k8s.io/kubernetes/pkg/api/testing"
"k8s.io/kubernetes/pkg/fields" "k8s.io/kubernetes/pkg/fields"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/util/diff" "k8s.io/kubernetes/pkg/util/diff"
) )
@ -60,13 +59,7 @@ func TestGetAttrs(t *testing.T) {
Source: api.EventSource{Component: "test"}, Source: api.EventSource{Component: "test"},
Type: api.EventTypeNormal, Type: api.EventTypeNormal,
} }
label, field, err := getAttrs(eventA) field := EventToSelectableFields(eventA)
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
if e, a := label, (labels.Set{}); !reflect.DeepEqual(e, a) {
t.Errorf("diff: %s", diff.ObjectDiff(e, a))
}
expect := fields.Set{ expect := fields.Set{
"metadata.name": "f0118", "metadata.name": "f0118",
"metadata.namespace": "default", "metadata.namespace": "default",
@ -87,10 +80,7 @@ func TestGetAttrs(t *testing.T) {
} }
func TestSelectableFieldLabelConversions(t *testing.T) { func TestSelectableFieldLabelConversions(t *testing.T) {
_, fset, err := getAttrs(&api.Event{}) fset := EventToSelectableFields(&api.Event{})
if err != nil {
t.Fatalf("Unexpected error %v", err)
}
apitesting.TestSelectableFieldLabelConversionsOfKind(t, apitesting.TestSelectableFieldLabelConversionsOfKind(t,
testapi.Default.GroupVersion().String(), testapi.Default.GroupVersion().String(),
"Event", "Event",

View File

@ -27,8 +27,8 @@ import (
// AttrFunc returns label and field sets for List or Watch to compare against, or an error. // AttrFunc returns label and field sets for List or Watch to compare against, or an error.
type AttrFunc func(obj runtime.Object) (label labels.Set, field fields.Set, err error) type AttrFunc func(obj runtime.Object) (label labels.Set, field fields.Set, err error)
// ObjectMetaFieldsSet returns a fields set that represents the ObjectMeta. // ObjectMetaFieldsSet returns a fields that represents the ObjectMeta.
func ObjectMetaFieldsSet(objectMeta api.ObjectMeta, hasNamespaceField bool) fields.Set { func ObjectMetaFieldsSet(objectMeta *api.ObjectMeta, hasNamespaceField bool) fields.Set {
if !hasNamespaceField { if !hasNamespaceField {
return fields.Set{ return fields.Set{
"metadata.name": objectMeta.Name, "metadata.name": objectMeta.Name,

View File

@ -1046,7 +1046,7 @@ func newTestGenericStoreRegistry(t *testing.T, hasCacheEnabled bool) (*etcdtesti
if !ok { if !ok {
return nil, nil, fmt.Errorf("not a pod") return nil, nil, fmt.Errorf("not a pod")
} }
return labels.Set(pod.ObjectMeta.Labels), generic.ObjectMetaFieldsSet(pod.ObjectMeta, true), nil return labels.Set(pod.ObjectMeta.Labels), generic.ObjectMetaFieldsSet(&pod.ObjectMeta, true), nil
}, },
} }
}, },

View File

@ -99,7 +99,7 @@ func (ingressStrategy) AllowUnconditionalUpdate() bool {
// IngressToSelectableFields returns a field set that represents the object. // IngressToSelectableFields returns a field set that represents the object.
func IngressToSelectableFields(ingress *extensions.Ingress) fields.Set { func IngressToSelectableFields(ingress *extensions.Ingress) fields.Set {
return generic.ObjectMetaFieldsSet(ingress.ObjectMeta, true) return generic.ObjectMetaFieldsSet(&ingress.ObjectMeta, true)
} }
// MatchIngress is the filter used by the generic etcd backend to ingress // MatchIngress is the filter used by the generic etcd backend to ingress

View File

@ -156,7 +156,7 @@ func (jobStatusStrategy) ValidateUpdate(ctx api.Context, obj, old runtime.Object
// JobSelectableFields returns a field set that represents the object for matching purposes. // JobSelectableFields returns a field set that represents the object for matching purposes.
func JobToSelectableFields(job *batch.Job) fields.Set { func JobToSelectableFields(job *batch.Job) fields.Set {
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(job.ObjectMeta, true) objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&job.ObjectMeta, true)
specificFieldsSet := fields.Set{ specificFieldsSet := fields.Set{
"status.successful": strconv.Itoa(int(job.Status.Succeeded)), "status.successful": strconv.Itoa(int(job.Status.Succeeded)),
} }

View File

@ -151,7 +151,7 @@ func MatchNamespace(label labels.Selector, field fields.Selector) *generic.Selec
// NamespaceToSelectableFields returns a field set that represents the object // NamespaceToSelectableFields returns a field set that represents the object
func NamespaceToSelectableFields(namespace *api.Namespace) fields.Set { func NamespaceToSelectableFields(namespace *api.Namespace) fields.Set {
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(namespace.ObjectMeta, false) objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&namespace.ObjectMeta, false)
specificFieldsSet := fields.Set{ specificFieldsSet := fields.Set{
"status.phase": string(namespace.Status.Phase), "status.phase": string(namespace.Status.Phase),
// This is a bug, but we need to support it for backward compatibility. // This is a bug, but we need to support it for backward compatibility.

View File

@ -92,7 +92,7 @@ func (networkPolicyStrategy) AllowUnconditionalUpdate() bool {
// NetworkPolicyToSelectableFields returns a field set that represents the object. // NetworkPolicyToSelectableFields returns a field set that represents the object.
func NetworkPolicyToSelectableFields(networkPolicy *extensions.NetworkPolicy) fields.Set { func NetworkPolicyToSelectableFields(networkPolicy *extensions.NetworkPolicy) fields.Set {
return generic.ObjectMetaFieldsSet(networkPolicy.ObjectMeta, true) return generic.ObjectMetaFieldsSet(&networkPolicy.ObjectMeta, true)
} }
// MatchNetworkPolicy is the filter used by the generic etcd backend to watch events // MatchNetworkPolicy is the filter used by the generic etcd backend to watch events

View File

@ -139,7 +139,7 @@ type ResourceGetter interface {
// NodeToSelectableFields returns a field set that represents the object. // NodeToSelectableFields returns a field set that represents the object.
func NodeToSelectableFields(node *api.Node) fields.Set { func NodeToSelectableFields(node *api.Node) fields.Set {
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(node.ObjectMeta, false) objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&node.ObjectMeta, false)
specificFieldsSet := fields.Set{ specificFieldsSet := fields.Set{
"spec.unschedulable": fmt.Sprint(node.Spec.Unschedulable), "spec.unschedulable": fmt.Sprint(node.Spec.Unschedulable),
} }

View File

@ -111,7 +111,7 @@ func MatchPersistentVolumes(label labels.Selector, field fields.Selector) *gener
// PersistentVolumeToSelectableFields returns a field set that represents the object // PersistentVolumeToSelectableFields returns a field set that represents the object
func PersistentVolumeToSelectableFields(persistentvolume *api.PersistentVolume) fields.Set { func PersistentVolumeToSelectableFields(persistentvolume *api.PersistentVolume) fields.Set {
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(persistentvolume.ObjectMeta, false) objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&persistentvolume.ObjectMeta, false)
specificFieldsSet := fields.Set{ specificFieldsSet := fields.Set{
// This is a bug, but we need to support it for backward compatibility. // This is a bug, but we need to support it for backward compatibility.
"name": persistentvolume.Name, "name": persistentvolume.Name,

View File

@ -111,7 +111,7 @@ func MatchPersistentVolumeClaim(label labels.Selector, field fields.Selector) *g
// PersistentVolumeClaimToSelectableFields returns a field set that represents the object // PersistentVolumeClaimToSelectableFields returns a field set that represents the object
func PersistentVolumeClaimToSelectableFields(persistentvolumeclaim *api.PersistentVolumeClaim) fields.Set { func PersistentVolumeClaimToSelectableFields(persistentvolumeclaim *api.PersistentVolumeClaim) fields.Set {
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(persistentvolumeclaim.ObjectMeta, true) objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&persistentvolumeclaim.ObjectMeta, true)
specificFieldsSet := fields.Set{ specificFieldsSet := fields.Set{
// This is a bug, but we need to support it for backward compatibility. // This is a bug, but we need to support it for backward compatibility.
"name": persistentvolumeclaim.Name, "name": persistentvolumeclaim.Name,

View File

@ -98,7 +98,7 @@ func (petSetStrategy) AllowUnconditionalUpdate() bool {
// PetSetToSelectableFields returns a field set that represents the object. // PetSetToSelectableFields returns a field set that represents the object.
func PetSetToSelectableFields(petSet *apps.PetSet) fields.Set { func PetSetToSelectableFields(petSet *apps.PetSet) fields.Set {
return generic.ObjectMetaFieldsSet(petSet.ObjectMeta, true) return generic.ObjectMetaFieldsSet(&petSet.ObjectMeta, true)
} }
// MatchPetSet is the filter used by the generic etcd backend to watch events // MatchPetSet is the filter used by the generic etcd backend to watch events

View File

@ -191,7 +191,7 @@ func NodeNameTriggerFunc(obj runtime.Object) []storage.MatchValue {
// PodToSelectableFields returns a field set that represents the object // PodToSelectableFields returns a field set that represents the object
// TODO: fields are not labels, and the validation rules for them do not apply. // TODO: fields are not labels, and the validation rules for them do not apply.
func PodToSelectableFields(pod *api.Pod) fields.Set { func PodToSelectableFields(pod *api.Pod) fields.Set {
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(pod.ObjectMeta, true) objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&pod.ObjectMeta, true)
podSpecificFieldsSet := fields.Set{ podSpecificFieldsSet := fields.Set{
"spec.nodeName": pod.Spec.NodeName, "spec.nodeName": pod.Spec.NodeName,
"spec.restartPolicy": string(pod.Spec.RestartPolicy), "spec.restartPolicy": string(pod.Spec.RestartPolicy),

View File

@ -97,7 +97,7 @@ func (podDisruptionBudgetStrategy) AllowUnconditionalUpdate() bool {
// PodDisruptionBudgetToSelectableFields returns a field set that represents the object. // PodDisruptionBudgetToSelectableFields returns a field set that represents the object.
func PodDisruptionBudgetToSelectableFields(podDisruptionBudget *policy.PodDisruptionBudget) fields.Set { func PodDisruptionBudgetToSelectableFields(podDisruptionBudget *policy.PodDisruptionBudget) fields.Set {
return generic.ObjectMetaFieldsSet(podDisruptionBudget.ObjectMeta, true) return generic.ObjectMetaFieldsSet(&podDisruptionBudget.ObjectMeta, true)
} }
// MatchPodDisruptionBudget is the filter used by the generic etcd backend to watch events // MatchPodDisruptionBudget is the filter used by the generic etcd backend to watch events

View File

@ -90,5 +90,5 @@ func MatchPodSecurityPolicy(label labels.Selector, field fields.Selector) *gener
// PodSecurityPolicyToSelectableFields returns a label set that represents the object // PodSecurityPolicyToSelectableFields returns a label set that represents the object
func PodSecurityPolicyToSelectableFields(obj *extensions.PodSecurityPolicy) fields.Set { func PodSecurityPolicyToSelectableFields(obj *extensions.PodSecurityPolicy) fields.Set {
return generic.ObjectMetaFieldsSet(obj.ObjectMeta, false) return generic.ObjectMetaFieldsSet(&obj.ObjectMeta, false)
} }

View File

@ -104,7 +104,7 @@ func (rsStrategy) AllowUnconditionalUpdate() bool {
// ReplicaSetToSelectableFields returns a field set that represents the object. // ReplicaSetToSelectableFields returns a field set that represents the object.
func ReplicaSetToSelectableFields(rs *extensions.ReplicaSet) fields.Set { func ReplicaSetToSelectableFields(rs *extensions.ReplicaSet) fields.Set {
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(rs.ObjectMeta, true) objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&rs.ObjectMeta, true)
rsSpecificFieldsSet := fields.Set{ rsSpecificFieldsSet := fields.Set{
"status.replicas": strconv.Itoa(int(rs.Status.Replicas)), "status.replicas": strconv.Itoa(int(rs.Status.Replicas)),
} }

View File

@ -114,5 +114,5 @@ func MatchResourceQuota(label labels.Selector, field fields.Selector) *generic.S
// ResourceQuotaToSelectableFields returns a field set that represents the object // ResourceQuotaToSelectableFields returns a field set that represents the object
func ResourceQuotaToSelectableFields(resourcequota *api.ResourceQuota) fields.Set { func ResourceQuotaToSelectableFields(resourcequota *api.ResourceQuota) fields.Set {
return generic.ObjectMetaFieldsSet(resourcequota.ObjectMeta, true) return generic.ObjectMetaFieldsSet(&resourcequota.ObjectMeta, true)
} }

View File

@ -98,7 +98,7 @@ func (scheduledJobStatusStrategy) ValidateUpdate(ctx api.Context, obj, old runti
// ScheduledJobToSelectableFields returns a field set that represents the object for matching purposes. // ScheduledJobToSelectableFields returns a field set that represents the object for matching purposes.
func ScheduledJobToSelectableFields(scheduledJob *batch.ScheduledJob) fields.Set { func ScheduledJobToSelectableFields(scheduledJob *batch.ScheduledJob) fields.Set {
return generic.ObjectMetaFieldsSet(scheduledJob.ObjectMeta, true) return generic.ObjectMetaFieldsSet(&scheduledJob.ObjectMeta, true)
} }
// MatchScheduledJob is the filter used by the generic etcd backend to route // MatchScheduledJob is the filter used by the generic etcd backend to route

View File

@ -110,7 +110,7 @@ func Matcher(label labels.Selector, field fields.Selector) *generic.SelectionPre
// SelectableFields returns a field set that can be used for filter selection // SelectableFields returns a field set that can be used for filter selection
func SelectableFields(obj *api.Secret) fields.Set { func SelectableFields(obj *api.Secret) fields.Set {
objectMetaFieldsSet := generic.ObjectMetaFieldsSet(obj.ObjectMeta, true) objectMetaFieldsSet := generic.ObjectMetaFieldsSet(&obj.ObjectMeta, true)
secretSpecificFieldsSet := fields.Set{ secretSpecificFieldsSet := fields.Set{
"type": string(obj.Type), "type": string(obj.Type),
} }

View File

@ -115,7 +115,7 @@ func MatchServices(label labels.Selector, field fields.Selector) *generic.Select
} }
func ServiceToSelectableFields(service *api.Service) fields.Set { func ServiceToSelectableFields(service *api.Service) fields.Set {
return generic.ObjectMetaFieldsSet(service.ObjectMeta, true) return generic.ObjectMetaFieldsSet(&service.ObjectMeta, true)
} }
type serviceStatusStrategy struct { type serviceStatusStrategy struct {

View File

@ -93,5 +93,5 @@ func Matcher(label labels.Selector, field fields.Selector) *generic.SelectionPre
// SelectableFields returns a field set that represents the object // SelectableFields returns a field set that represents the object
func SelectableFields(obj *api.ServiceAccount) fields.Set { func SelectableFields(obj *api.ServiceAccount) fields.Set {
return generic.ObjectMetaFieldsSet(obj.ObjectMeta, true) return generic.ObjectMetaFieldsSet(&obj.ObjectMeta, true)
} }

View File

@ -94,5 +94,5 @@ func MatchStorageClasses(label labels.Selector, field fields.Selector) *generic.
// StorageClassToSelectableFields returns a label set that represents the object // StorageClassToSelectableFields returns a label set that represents the object
func StorageClassToSelectableFields(storageClass *extensions.StorageClass) fields.Set { func StorageClassToSelectableFields(storageClass *extensions.StorageClass) fields.Set {
return generic.ObjectMetaFieldsSet(storageClass.ObjectMeta, false) return generic.ObjectMetaFieldsSet(&storageClass.ObjectMeta, false)
} }